protected void configureConnectors() {
    if (server != null) {
      if (_useNIO) {
        SelectChannelConnector nioConnector = new SelectChannelConnector();
        nioConnector.setUseDirectBuffers(false);
        nioConnector.setPort(_port);
        server.addConnector(nioConnector);
      } else {
        SocketConnector bioConnector = new SocketConnector();
        bioConnector.setPort(_port);
        bioConnector.setMaxIdleTime(3000);
        server.addConnector(bioConnector);
      }

      if (_useSSL) {
        SslSocketConnector sslConnector = new SslSocketConnector();
        sslConnector.setPort(_sslPort);
        // sslConnector.setKeystore("/sdcard/jetty/etc/keystore");
        sslConnector.setKeystore(_keystoreFile);
        sslConnector.setKeystoreType("bks");
        // sslConnector.setTruststore("/sdcard/jetty/etc/keystore");
        sslConnector.setTruststore(_truststoreFile);
        // sslConnector.setPassword("OBF:1vny1zlo1x8e1vnw1vn61x8g1zlu1vn4");
        sslConnector.setPassword(_keystorePassword);
        // sslConnector.setKeyPassword("OBF:1u2u1wml1z7s1z7a1wnl1u2g");
        sslConnector.setKeyPassword(_keymgrPassword);
        // sslConnector.setTrustPassword("OBF:1vny1zlo1x8e1vnw1vn61x8g1zlu1vn4");
        sslConnector.setTrustPassword(_truststorePassword);
        sslConnector.setTruststoreType("bks");
        server.addConnector(sslConnector);
      }
    }
  }
  private static void initSSL(
      Server server,
      int sslport,
      String keystore,
      String password,
      String keyPassword,
      boolean needClientAuth) {

    if (keystore == null) {
      throw new IllegalStateException(
          "you need to provide argument -Drjrkeystore with -Drjrsslport");
    }
    if (password == null) {
      throw new IllegalStateException(
          "you need to provide argument -Drjrpassword with -Drjrsslport");
    }
    if (keyPassword == null) {
      throw new IllegalStateException(
          "you need to provide argument -Drjrkeypassword with -Drjrsslport");
    }

    SslSocketConnector sslConnector = new SslSocketConnector();
    sslConnector.setKeystore(keystore);
    sslConnector.setPassword(password);
    sslConnector.setKeyPassword(keyPassword);

    if (needClientAuth) {
      System.err.println("Enable NeedClientAuth.");
      sslConnector.setNeedClientAuth(needClientAuth);
    }
    sslConnector.setMaxIdleTime(30000);
    sslConnector.setPort(sslport);

    server.addConnector(sslConnector);
  }
 private static SslSocketConnector getSslConnector(int port) {
   SslSocketConnector sslConnector = new SslSocketConnector();
   sslConnector.setKeystore(resourceBase + "/keystore");
   sslConnector.setKeyPassword("pulkkisenjorma");
   sslConnector.setPort(port);
   sslConnector.setMaxIdleTime(30000);
   return sslConnector;
 }
  private static SslSocketConnector createSslConnector() {
    SslSocketConnector sslConnector = new SslSocketConnector();
    sslConnector.setPort(sslPort);
    sslConnector.setKeyPassword("secret");
    sslConnector.setKeystore("src/test/resources/keystore");
    sslConnector.setTrustPassword("secret");
    sslConnector.setTruststore("src/main/resources/truststore");
    sslConnector.setPassword("secret");
    sslConnector.setWantClientAuth(true);
    // sslConnector.setNeedClientAuth(true);

    return sslConnector;
  }
Exemple #5
0
  public static void main(String[] args) throws Exception {
    ApplicationProperties properties = new ApplicationProperties();

    Server server = new Server();
    SocketConnector connector = new SocketConnector();
    // Set some timeout options to make debugging easier.
    connector.setMaxIdleTime(1000 * 60 * 60);
    connector.setSoLingerTime(-1);

    int port = Integer.getInteger("jetty.port", properties.getHttpPort());
    connector.setPort(port);

    SslSocketConnector sslConnector = new SslSocketConnector();
    sslConnector.setMaxIdleTime(1000 * 60 * 60);
    sslConnector.setSoLingerTime(-1);
    sslConnector.setKeyPassword("password");
    sslConnector.setPassword("password");
    sslConnector.setKeystore("src/main/webapp/WEB-INF/keystore");

    port = Integer.getInteger("jetty.sslport", properties.getHttpsPort());
    sslConnector.setPort(port);

    server.setConnectors(new Connector[] {connector, sslConnector});

    WebAppContext bb = new WebAppContext();
    bb.setServer(server);
    bb.setContextPath("/");
    bb.setWar("src/main/webapp");

    // START JMX SERVER
    // MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
    // MBeanContainer mBeanContainer = new MBeanContainer(mBeanServer);
    // server.getContainer().addEventListener(mBeanContainer);
    // mBeanContainer.start();

    server.addHandler(bb);

    try {
      System.out.println(">>> STARTING EMBEDDED JETTY SERVER, PRESS ANY KEY TO STOP");
      server.start();
      while (System.in.available() == 0) {
        Thread.sleep(5000);
      }
      server.stop();
      server.join();
    } catch (Exception e) {
      e.printStackTrace();
      System.exit(100);
    }
  }
  public void start(SoapMonitor soapMonitor, int localPort) {
    Settings settings = soapMonitor.getProject().getSettings();
    server.setThreadPool(new SoapUIJettyThreadPool());
    Context context = new Context(server, ROOT, 0);

    if (sslEndpoint != null) {
      if (sslEndpoint.startsWith(HTTPS)) {
        sslConnector = new SslSocketConnector();
        sslConnector.setKeystore(
            settings.getString(SoapMonitorAction.SecurityTabForm.SSLTUNNEL_KEYSTORE, "JKS"));
        sslConnector.setPassword(
            settings.getString(SoapMonitorAction.SecurityTabForm.SSLTUNNEL_PASSWORD, ""));
        sslConnector.setKeyPassword(
            settings.getString(SoapMonitorAction.SecurityTabForm.SSLTUNNEL_KEYPASSWORD, ""));
        sslConnector.setTruststore(
            settings.getString(SoapMonitorAction.SecurityTabForm.SSLTUNNEL_TRUSTSTORE, "JKS"));
        sslConnector.setTrustPassword(
            settings.getString(
                SoapMonitorAction.SecurityTabForm.SSLTUNNEL_TRUSTSTORE_PASSWORD, ""));
        sslConnector.setNeedClientAuth(false);
        sslConnector.setMaxIdleTime(30000);
        sslConnector.setPort(localPort);

        server.addConnector(sslConnector);
        context.addServlet(new ServletHolder(new TunnelServlet(soapMonitor, sslEndpoint)), ROOT);
      } else {
        if (sslEndpoint.startsWith(HTTP)) {
          connector.setPort(localPort);
          server.addConnector(connector);
          context.addServlet(new ServletHolder(new TunnelServlet(soapMonitor, sslEndpoint)), ROOT);
        } else {
          UISupport.showErrorMessage("Unsupported/unknown protocol tunnel will not start");
          return;
        }
      }
      proxyOrTunnel = false;
    } else {
      proxyOrTunnel = true;
      connector.setPort(localPort);
      server.addConnector(connector);
      context.addServlet(new ServletHolder(new ProxyServlet(soapMonitor)), ROOT);
    }
    try {
      server.start();
    } catch (Exception e) {
      UISupport.showErrorMessage("Error starting monitor: " + e.getMessage());
    }
  }
 @BeforeClass
 public static void startJetty() throws Exception {
   port = PortFactory.findFreePort();
   server = new Server();
   SslSocketConnector sslSocketConnector = new SslSocketConnector();
   sslSocketConnector.setKeystore("src/test/resources/keystore");
   sslSocketConnector.setKeyPassword("password");
   sslSocketConnector.setPort(port);
   server.setConnectors(new Connector[] {sslSocketConnector});
   server.addHandler(
       new AbstractHandler() {
         public void handle(
             String target, HttpServletRequest request, HttpServletResponse response, int dispatch)
             throws IOException, ServletException {
           response.setStatus(Response.Status.OK.getStatusCode());
           Request base_request =
               request instanceof Request
                   ? (Request) request
                   : HttpConnection.getCurrentConnection().getRequest();
           base_request.setHandled(true);
         }
       });
   server.start();
 }
Exemple #8
0
  /**
   * Method to start the Jetty server
   *
   * @param jobDataMap
   */
  private void startServer(JobDataMap jobDataMap) {
    if (LOG.isDebugEnabled()) {
      LOG.debug("Initialising HTTP server");
    }
    int port = Integer.parseInt(jobDataMap.getString("port"));
    String bindAddress = jobDataMap.getString("bindAddress");
    String authConfigFile = jobDataMap.getString("authConfigFile");
    String keystore = jobDataMap.getString("keystore");
    Server server = new Server();
    if (keystore == null || keystore.equals("")) {
      LOG.info("Starting with HTTP (non-encrypted) protocol");
      SelectChannelConnector connector = new SelectChannelConnector();
      connector.setHost(bindAddress);
      connector.setPort(port);
      server.addConnector(connector);
    } else {
      LOG.info("Starting with HTTPS (encrypted) protocol");
      SslSocketConnector sslConnector = new SslSocketConnector();
      sslConnector.setHost(bindAddress);
      sslConnector.setPort(port);
      sslConnector.setKeystore(jobDataMap.getString("keystore"));
      sslConnector.setKeyPassword(jobDataMap.getString("keyPassword"));
      sslConnector.setTruststore(jobDataMap.getString("trustStore"));
      sslConnector.setTrustPassword(jobDataMap.getString("trustPassword"));
      sslConnector.setPassword(jobDataMap.getString("password"));
      server.addConnector(sslConnector);
    }

    if (authConfigFile != null && !(authConfigFile.equals(""))) {
      if (LOG.isDebugEnabled()) {
        LOG.debug("Requiring basic auth");
      }
      Constraint constraint = new Constraint();
      constraint.setName(Constraint.__BASIC_AUTH);
      ;
      constraint.setRoles(new String[] {"user", "grouper"});
      constraint.setAuthenticate(true);

      ConstraintMapping cm = new ConstraintMapping();
      cm.setConstraint(constraint);
      cm.setPathSpec("/*");

      SecurityHandler sh = new SecurityHandler();
      try {
        sh.setUserRealm(new HashUserRealm("Grouper", authConfigFile));
      } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
      sh.setConstraintMappings(new ConstraintMapping[] {cm});

      Handler[] handlers = new Handler[] {sh, new EsbHttpHandler()};
      server.setHandlers(handlers);

    } else {
      if (LOG.isDebugEnabled()) {
        LOG.debug("Not requiring basic auth");
      }
      server.setHandler(new EsbHttpHandler());
    }
    try {
      server.start();
      LOG.info("HTTP server started on address " + bindAddress + " port " + port);
      server.join();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  public static void main(String[] args) throws Exception {
    SocketConnector socketConnector = new SocketConnector();
    socketConnector.setPort(6080);

    SslSocketConnector sslSocketConnector = new SslSocketConnector();
    sslSocketConnector.setPort(6443);
    //        sslSocketConnector.setNeedClientAuth(true); //why not use mutual authentication when
    // we can

    String serverKeystore =
        MockHttpListenerWithAuthentication.class
            .getClassLoader()
            .getResource("cert/serverkeystore.jks")
            .getPath();
    sslSocketConnector.setKeystore(serverKeystore);
    sslSocketConnector.setKeyPassword("serverpass");
    String serverTruststore =
        MockHttpListenerWithAuthentication.class
            .getClassLoader()
            .getResource("cert/servertruststore.jks")
            .getPath();
    sslSocketConnector.setTruststore(serverTruststore);
    sslSocketConnector.setTrustPassword("serverpass");

    server.addConnector(socketConnector);
    server.addConnector(sslSocketConnector);

    SecurityHandler securityHandler = createBasicAuthenticationSecurityHandler();

    HandlerList handlerList = new HandlerList();
    handlerList.addHandler(securityHandler);
    handlerList.addHandler(
        new AbstractHandler() {
          @Override
          public void handle(
              String s,
              HttpServletRequest httpServletRequest,
              HttpServletResponse httpServletResponse,
              int i)
              throws IOException, ServletException {
            System.out.println("uri: " + httpServletRequest.getRequestURI());
            System.out.println("queryString: " + httpServletRequest.getQueryString());
            System.out.println("method: " + httpServletRequest.getMethod());

            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            IOUtils.copy(httpServletRequest.getInputStream(), baos);

            System.out.println("body: " + baos.toString());

            PrintWriter writer = httpServletResponse.getWriter();
            writer.append("testsvar");
            Random r = new Random();
            for (int j = 0; j < 10; j++) {
              int value = r.nextInt(Integer.MAX_VALUE);
              writer.append(value + "");
            }
            System.out.println();
            writer.close();
            httpServletResponse.setStatus(HttpServletResponse.SC_OK);
          }
        });

    server.addHandler(handlerList);
    server.start();
  }
  @SuppressWarnings("unchecked")
  public static void startup(String args[]) {
    try {
      Server server = new Server();
      System.err.println("mycat init ... ");

      int httpPort = 7066;
      int httpsPort = -1;
      String contextPath = "/";

      String keyStorePath = null;
      String keyStorePassword = null;

      String updateServer = null;

      boolean disableUpdateCenterSwitch = false;
      boolean skipInitSetup = false;

      for (int i = 0; i < args.length; ++i) {
        String portStr;
        if (args[i].startsWith("--httpPort=")) {
          portStr = args[i].substring("--httpPort=".length());
          httpPort = Integer.parseInt(portStr);
        }

        if (args[i].startsWith("--httpsPort=")) {
          portStr = args[i].substring("--httpsPort=".length());
          httpsPort = Integer.parseInt(portStr);
        }

        if (args[i].startsWith("--httpsKeyStore=")) {
          keyStorePath = args[i].substring("--httpsKeyStore=".length());
        }

        if (args[i].startsWith("--httpsKeyStorePassword="******"--httpsKeyStorePassword="******"--prefix=")) {
          String prefix = args[i].substring("--prefix=".length());
          if (prefix.startsWith("/")) contextPath = prefix;
          else {
            contextPath = "/" + prefix;
          }
        }
        if (args[i].startsWith("--updateServer=")) {
          updateServer = args[i].substring("--updateServer=".length());
        }

        if (args[i].startsWith("--disableUpdateCenterSwitch")) {
          disableUpdateCenterSwitch = true;
        }

        if (args[i].startsWith("--skipInitSetup")) {
          skipInitSetup = true;
        }
      }

      List connectors = new ArrayList();
      if (httpPort != -1) {
        SelectChannelConnector httpConnector = new SelectChannelConnector();
        httpConnector.setPort(httpPort);
        connectors.add(httpConnector);
      }

      if (httpsPort != -1) {
        SslSocketConnector httpsConnector = new SslSocketConnector();
        httpsConnector.setPort(httpsPort);
        if (keyStorePath != null) {
          httpsConnector.setKeystore(keyStorePath);
        }
        if (keyStorePassword != null) {
          httpsConnector.setKeyPassword(keyStorePassword);
        }
        connectors.add(httpsConnector);
      }

      server.setConnectors((Connector[]) connectors.toArray(new Connector[connectors.size()]));

      ProtectionDomain protectionDomain = Executable.class.getProtectionDomain();
      URL location = protectionDomain.getCodeSource().getLocation();
      String url = java.net.URLDecoder.decode(location.getPath(), "utf-8");
      System.out.println(url);

      File tempDir = new File(getHomeDir(), "war");
      tempDir.mkdirs();

      WebAppContext webapp = new WebAppContext();
      webapp.setContextPath("/mycat"); //
      webapp.setDescriptor(url + "/WEB-INF/web.xml");
      webapp.setResourceBase(url);
      webapp.setTempDirectory(tempDir);

      server.setHandler(webapp);

      System.err.println("mycat start success,port: " + httpsPort);
      server.start();
      server.join();

    } catch (Exception e) {
      System.exit(-1);
    }
  }
  private void deployWebApp() {
    try {
      Server server = new Server();
      SelectChannelConnector connector = new SelectChannelConnector();
      connector.setMaxIdleTime(MAX_IDLE_TIME_MILLIS);
      connector.setHeaderBufferSize(HEADER_BUFFER_SIZE);
      connector.setHost(getHost());
      connector.setPort(getPort());
      if (isHttpsEnabled()) {
        connector.setConfidentialPort(getHttpsPort());
      }
      server.addConnector(connector);

      if (isHttpsEnabled()) {
        SslSocketConnector sslConnector = new SslSocketConnector();
        sslConnector.setMaxIdleTime(MAX_IDLE_TIME_MILLIS);
        sslConnector.setHeaderBufferSize(HEADER_BUFFER_SIZE);
        sslConnector.setHost(getHost());
        sslConnector.setPort(getHttpsPort());
        sslConnector.setKeystore(
            System.getProperty(
                "subsonic.ssl.keystore",
                getClass().getResource("/subsonic.keystore").toExternalForm()));
        sslConnector.setPassword(System.getProperty("subsonic.ssl.password", "subsonic"));
        server.addConnector(sslConnector);
      }

      WebAppContext context = new WebAppContext();
      context.setTempDirectory(getJettyDirectory());
      context.setContextPath(getContextPath());
      context.setWar(getWar());
      context.setOverrideDescriptor("/web-jetty.xml");

      if (isHttpsEnabled()) {

        // Allow non-https for streaming and cover art (for Chromecast, UPnP, Sonos etc)
        context
            .getSecurityHandler()
            .setConstraintMappings(
                new ConstraintMapping[] {
                  createConstraintMapping("/stream", Constraint.DC_NONE),
                  createConstraintMapping("/coverArt.view", Constraint.DC_NONE),
                  createConstraintMapping("/ws/*", Constraint.DC_NONE),
                  createConstraintMapping("/sonos/*", Constraint.DC_NONE),
                  createConstraintMapping("/", Constraint.DC_CONFIDENTIAL)
                });
      }

      server.addHandler(context);
      server.start();

      System.err.println("Subsonic running on: " + getUrl());
      if (isHttpsEnabled()) {
        System.err.println("                and: " + getHttpsUrl());
      }

    } catch (Throwable x) {
      x.printStackTrace();
      exception = x;
    }
  }