Sunday, 18 October 2015

Jetty: Configuring many connectors

In previous post, I explained how to configure a connector to handle http requests. In this post, I am going to explain, how to configure multiple connectors. In this post, I am going to configure two connectors, one listens http and other listens https requests.

‘http’ request is straight forward, as I shown in previous post. But for https requests, you need a trusted certificate. Java provides command line tool 'keytool' to generate security certificates.

Following post, explains how to generate keystore.
Following is the complete working application.
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.AbstractHandler;

public class HelloHandler extends AbstractHandler {
  public HelloHandler() {

  }

  public void handle(String target, Request baseRequest,
      HttpServletRequest request, HttpServletResponse response)
      throws IOException, ServletException {
    response.setContentType("text/html; charset=utf-8");
    response.setStatus(HttpServletResponse.SC_OK);

    PrintWriter out = response.getWriter();
    out.println("Welcome to Jetty embeddey programmng");
    baseRequest.setHandled(true);
  }
}

import java.io.File;
import java.io.FileNotFoundException;

import org.eclipse.jetty.http.HttpVersion;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.HttpConfiguration;
import org.eclipse.jetty.server.HttpConnectionFactory;
import org.eclipse.jetty.server.SecureRequestCustomizer;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.SslConnectionFactory;
import org.eclipse.jetty.util.ssl.SslContextFactory;

public class ManyConnectors {
  private static final String KEY_STORE_PATH = "/Users/harikrishna_gurram/keystore.jks";
  private static final String KEY_STORE_PASSWORD = "password123";
  private static final String KEY_MANAGER_PASSWORD = "password123";

  private static final int HTTP_PORT = 8080;
  private static final int OUTPUT_BUFFER_SIZE = 30000;

  private static final String HTTPS = "https";
  private static final int HTTPS_PORT = 8443;
  private static final int HTTPS_IDEL_TIME_OUT = 50000;

  public static void main(String[] args) throws Exception {

    File keystoreFile = new File(KEY_STORE_PATH);
    if (!keystoreFile.exists()) {
      throw new FileNotFoundException(keystoreFile.getAbsolutePath());
    }

    Server server = new Server();

    HttpConfiguration http_config = new HttpConfiguration();
    http_config.setSecureScheme(HTTPS);
    http_config.setSecurePort(HTTPS_PORT);
    http_config.setOutputBufferSize(OUTPUT_BUFFER_SIZE);

    ServerConnector http = new ServerConnector(server,
        new HttpConnectionFactory(http_config));
    http.setPort(HTTP_PORT);
    http.setIdleTimeout(OUTPUT_BUFFER_SIZE);

    SslContextFactory sslContextFactory = new SslContextFactory();
    sslContextFactory.setKeyStorePath(keystoreFile.getAbsolutePath());
    sslContextFactory.setKeyStorePassword(KEY_STORE_PASSWORD);
    sslContextFactory.setKeyManagerPassword(KEY_MANAGER_PASSWORD);

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

    ServerConnector https = new ServerConnector(server,
        new SslConnectionFactory(sslContextFactory,
            HttpVersion.HTTP_1_1.asString()),
        new HttpConnectionFactory(https_config));
    https.setPort(HTTPS_PORT);
    https.setIdleTimeout(HTTPS_IDEL_TIME_OUT);

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

    server.setHandler(new HelloHandler());

    server.start();
    server.join();
  }
}
Run above application and hit the url ‘http://localhost:8080/’, you will get following screen.
hit the url ‘https://localhost:8443/’, you will get other screen.






Prevoius                                                 Next                                                 Home

No comments:

Post a Comment