0

I have a Jetty WebServlet that can connect with various clients written in C# and on android. This currently works using simple HTTP, but I am interested in upgrading it to HTTPS. To try and do this I am creating the server like this:

public static void main(String[] args){

    SslContextFactory contextFactory = new SslContextFactory();
    contextFactory.setKeyStorePath("keystore.jks");
    contextFactory.setKeyStorePassword("********");
    SslConnectionFactory connectionFactory = new SslConnectionFactory(contextFactory, org.eclipse.jetty.http.HttpVersion.HTTP_2_0.toString());

    Server server = new Server(8080);

    ServerConnector connector = new ServerConnector(server, connectionFactory);
    connector.setPort(8443);
    server.addConnector(connector);

    ServletContextHandler servletCH = new ServletContextHandler();
    servletCH.setContextPath("/");
    servletCH.addServlet(ScheduleWebSocketServlet.class, "/schedule");

    server.setHandler(servletCH);

    server.start();
    server.join();


}

This seems to be wrong. The ScheduleWebSocketServlet class is as follows:

@WebServlet(name = "Schedule WebSocketServlet", urlPatterns = {"/schedule"})
public static class ScheduleWebSocketServlet extends WebSocketServlet{

    private static final long serialVersionUID = 5838283767965540728L;

    public void doGet(HttpServletRequest request, HttpServletResponse response){
        try {
            response.getWriter().println("<h1>Hello World</h1>");
        } catch (IOException e) {
            Main.LogError(e);
        }
    }

    @Override
    public void configure(WebSocketServletFactory arg0) {
        arg0.register(ScheduleWebSocket.class);
    }

}

So my question is what is the correct way to use the WebServlet with HTTPS?

Thanks very much for the help

Stromata
  • 245
  • 4
  • 9
  • This is possible, and documented in [various](http://stackoverflow.com/a/29829926/775715) stackoverflow [answers](http://stackoverflow.com/a/29280145/775715). What error are you seeing? – Joakim Erdfelt Sep 25 '15 at 20:04
  • I am not seeing any errors. I am trying to set up HTTPS with a WebSocketServlet but doing it incorrectly. If someone could provide a simple HTTPS example that is compatible with WebSocketServlets it would help me and others who may have a similar setup to mine in the future. – Stromata Sep 25 '15 at 20:52

1 Answers1

0

Ok so my problem turned out to be that I had to create ServerConnections with configurations for http and https, give them their details and assign them to the server. After all this I just need to start them, which should be done after the server is started

    Server server = new Server();

    HttpConfiguration http_config = new HttpConfiguration();
    http_config.setSecureScheme("https");
    http_config.setSecurePort(8443);
    http_config.setOutputBufferSize(32768);

    HttpConfiguration https_config = new HttpConfiguration();
    https_config.addCustomizer(new SecureRequestCustomizer());

    ServerConnector http = new ServerConnector(server, new HttpConnectionFactory(http_config));
    http.setPort(8080);
    http.setIdleTimeout(30000);

    SslContextFactory contextFactory = new SslContextFactory();
    contextFactory.setKeyStorePath("keystore.jks");
    contextFactory.setKeyStorePassword("changeit");
    SslConnectionFactory connectionFactory = new SslConnectionFactory(contextFactory, org.eclipse.jetty.http.HttpVersion.HTTP_1_1.toString());

    ServerConnector https = new ServerConnector(server, connectionFactory, new HttpConnectionFactory(https_config));
    https.setPort(8443);
    https.setIdleTimeout(50000);

    server.setConnectors(new Connector[]{http, https});

    ServletContextHandler servletCH = new ServletContextHandler();
    servletCH.setContextPath("/");
    servletCH.addServlet(ScheduleWebSocketServlet.class, "/schedule");

    server.setHandler(servletCH);

    server.start();
    http.start();
    https.start();
    server.join();
Stromata
  • 245
  • 4
  • 9