/**
   * @param configuration
   * @param useNIO
   * @param httpSecurePort
   * @param secureConnector
   */
  private void configureSSLConnector(
      Configuration configuration,
      Boolean useNIO,
      Integer httpSecurePort,
      Connector secureConnector) {
    secureConnector.setPort(httpSecurePort);
    secureConnector.setSecure(true);
    secureConnector.setScheme("https");
    secureConnector.setProperty("SSLEnabled", "true");

    secureConnector.setProperty("keystoreFile", configuration.getSslKeystore());
    secureConnector.setProperty("keystorePass", configuration.getSslKeyPassword());
    secureConnector.setProperty("clientAuth", "false");
    secureConnector.setProperty("sslProtocol", "TLS");

    // configuration.getSslKeystoreType();
    // configuration.getSslPassword();

    // keystoreFile="${user.home}/.keystore" keystorePass="******"
    // clientAuth="false" sslProtocol="TLS"

    if (useNIO) {
      secureConnector.setProtocolHandlerClassName(Http11NioProtocol.class.getName());
    } else {
      secureConnector.setProtocolHandlerClassName(Http11Protocol.class.getName());
    }
  }
示例#2
0
 private static void addCompression(Connector c) {
   c.setProperty("compression", "on");
   c.setProperty("noCompressionUserAgents", "gozilla, traviata");
   c.setProperty(
       "compressableMimeType",
       "text/html,text/xml,text/css,text/javascript,application/javascript");
 }
示例#3
0
  // Needs to be protected so it can be used by subclasses
  protected void customizeConnector(Connector connector) {
    int port = (getPort() >= 0 ? getPort() : 0);
    connector.setPort(port);
    if (StringUtils.hasText(this.getServerHeader())) {
      connector.setAttribute("server", this.getServerHeader());
    }
    if (connector.getProtocolHandler() instanceof AbstractProtocol) {
      customizeProtocol((AbstractProtocol<?>) connector.getProtocolHandler());
    }
    if (getUriEncoding() != null) {
      connector.setURIEncoding(getUriEncoding().name());
    }

    // If ApplicationContext is slow to start we want Tomcat not to bind to the socket
    // prematurely...
    connector.setProperty("bindOnInit", "false");

    if (getSsl() != null && getSsl().isEnabled()) {
      customizeSsl(connector);
    }
    if (getCompression() != null && getCompression().getEnabled()) {
      customizeCompression(connector);
    }
    for (TomcatConnectorCustomizer customizer : this.tomcatConnectorCustomizers) {
      customizer.customize(connector);
    }
  }
  protected static void initSsl(
      Tomcat tomcat, String keystore, String keystorePass, String keyPass) {

    String protocol = tomcat.getConnector().getProtocolHandlerClassName();
    if (protocol.indexOf("Apr") == -1) {
      Connector connector = tomcat.getConnector();
      connector.setProperty("sslProtocol", "tls");
      File keystoreFile = new File("test/org/apache/tomcat/util/net/" + keystore);
      connector.setAttribute("keystoreFile", keystoreFile.getAbsolutePath());
      File truststoreFile = new File("test/org/apache/tomcat/util/net/ca.jks");
      connector.setAttribute("truststoreFile", truststoreFile.getAbsolutePath());
      if (keystorePass != null) {
        connector.setAttribute("keystorePass", keystorePass);
      }
      if (keyPass != null) {
        connector.setAttribute("keyPass", keyPass);
      }
    } else {
      File keystoreFile = new File("test/org/apache/tomcat/util/net/localhost-cert.pem");
      tomcat.getConnector().setAttribute("SSLCertificateFile", keystoreFile.getAbsolutePath());
      keystoreFile = new File("test/org/apache/tomcat/util/net/localhost-key.pem");
      tomcat.getConnector().setAttribute("SSLCertificateKeyFile", keystoreFile.getAbsolutePath());
    }
    tomcat.getConnector().setSecure(true);
    tomcat.getConnector().setProperty("SSLEnabled", "true");
  }
示例#5
0
 private static void configureHttpConnector(Tomcat tomcat, TomcatConfig tomcatConfig) {
   Connector connector = tomcat.getConnector();
   connector.setProperty("server", "kicktipp");
   connector.setPort(tomcatConfig.getPort());
   connector.setRedirectPort(tomcatConfig.getSslPort());
   addCompression(connector);
 }
示例#6
0
 private static void configureHttpsConnector(Tomcat tomcat, TomcatConfig tomcatConfig) {
   Connector connector = new Connector();
   connector.setProperty("server", "jlot");
   connector.setPort(tomcatConfig.getSslPort());
   connector.setSecure(true);
   connector.setEnableLookups(false);
   connector.setScheme("https");
   connector.setAttribute("SSLEnabled", true);
   File file = new File("../kicktipp.org.keystore");
   String keystoreFile = file.getAbsolutePath();
   connector.setAttribute("keystoreFile", keystoreFile);
   connector.setAttribute("keyAlias", "tomcat");
   connector.setAttribute("keystorePass", "tomcat");
   connector.setAttribute("clientAuth", "false");
   connector.setAttribute("sslProtocol", "TLS");
   addCompression(connector);
   tomcat.getService().addConnector(connector);
 }
  // Needs to be protected so it can be used by subclasses
  protected void customizeConnector(Connector connector) {
    int port = (getPort() >= 0 ? getPort() : 0);
    connector.setPort(port);
    if (connector.getProtocolHandler() instanceof AbstractProtocol) {
      if (getAddress() != null) {
        ((AbstractProtocol) connector.getProtocolHandler()).setAddress(getAddress());
      }
    }
    if (getUriEncoding() != null) {
      connector.setURIEncoding(getUriEncoding());
    }

    // If ApplicationContext is slow to start we want Tomcat not to bind to the socket
    // prematurely...
    connector.setProperty("bindOnInit", "false");
    for (TomcatConnectorCustomizer customizer : this.tomcatConnectorCustomizers) {
      customizer.customize(connector);
    }
  }
示例#8
0
  private void start(boolean await) {

    // try to shutdown a previous Tomcat
    sendShutdownCommand();

    try {
      final ServerSocket srv = new ServerSocket(this.httpPort);
      srv.close();
    } catch (IOException e) {
      log.error("PORT " + this.httpPort + " ALREADY IN USE");
      return;
    }

    // Read a dummy value. This triggers loading of the catalina.properties
    // file
    CatalinaProperties.getProperty("dummy");

    appendSkipJars("tomcat.util.scan.DefaultJarScanner.jarsToSkip", this.skipJarsDefaultJarScanner);
    appendSkipJars(
        "org.apache.catalina.startup.ContextConfig.jarsToSkip", this.skipJarsContextConfig);
    appendSkipJars("org.apache.catalina.startup.TldConfig.jarsToSkip", this.skipJarsTldConfig);

    this.tomcat = new Tomcat();

    if (this.tempDirectory == null) {
      this.tempDirectory = new File(".", "/target/tomcat." + this.httpPort).getAbsolutePath();
    }

    this.tomcat.setBaseDir(this.tempDirectory);

    if (this.silent) {
      this.tomcat.setSilent(true);
    }

    if (this.addDefaultListeners) {
      this.tomcat.getServer().addLifecycleListener(new AprLifecycleListener());
    }

    if (this.useNio) {
      Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
      connector.setPort(this.httpPort);
      connector.setMaxPostSize(this.maxPostSize);
      connector.setURIEncoding("UTF-8");
      this.tomcat.setConnector(connector);
      this.tomcat.getService().addConnector(connector);
    } else {
      this.tomcat.setPort(this.httpPort);
      this.tomcat.getConnector().setURIEncoding("UTF-8");
      this.tomcat.getConnector().setMaxPostSize(this.maxPostSize);
    }

    if (this.compressionMinSize >= 0) {
      this.tomcat
          .getConnector()
          .setProperty("compression", String.valueOf(this.compressionMinSize));
      this.tomcat.getConnector().setProperty("compressableMimeType", this.compressableMimeType);
    }

    if (this.httpsPort != 0) {
      final Connector httpsConnector;
      if (this.useNio) {
        httpsConnector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
      } else {
        httpsConnector = new Connector("HTTP/1.1");
      }
      httpsConnector.setSecure(true);
      httpsConnector.setPort(this.httpsPort);
      httpsConnector.setMaxPostSize(this.maxPostSize);
      httpsConnector.setScheme("https");
      httpsConnector.setURIEncoding("UTF-8");

      httpsConnector.setProperty("SSLEnabled", "true");
      httpsConnector.setProperty("keyAlias", this.keyAlias);
      httpsConnector.setProperty("keystoreFile", this.keyStoreFile);
      httpsConnector.setProperty("keystorePass", this.keyStorePass);
      httpsConnector.setProperty("sslProtocol", this.sslProtocol);

      if (this.compressionMinSize >= 0) {
        httpsConnector.setProperty("compression", String.valueOf(this.compressionMinSize));
        httpsConnector.setProperty("compressableMimeType", this.compressableMimeType);
      }

      this.tomcat.getEngine().setDefaultHost("localhost");
      this.tomcat.getService().addConnector(httpsConnector);
    }

    if (this.shutdownPort != null) {
      this.tomcat.getServer().setPort(this.shutdownPort);
    }

    String contextDir = this.contextDirectory;
    if (contextDir == null) {
      contextDir = new File(".").getAbsolutePath() + "/src/main/webapp";
    }

    final Context ctx;
    try {

      if (!this.contextPath.equals("")) {
        File rootCtxDir = new File("./target/tcroot");
        if (!rootCtxDir.exists()) {
          rootCtxDir.mkdirs();
        }
        Context rootCtx = this.tomcat.addWebapp("", rootCtxDir.getAbsolutePath());
        rootCtx.setPrivileged(true);
        Tomcat.addServlet(rootCtx, "listContexts", new ListContextsServlet(rootCtx))
            .addMapping("/");
      }

      ctx = this.tomcat.addWebapp(this.contextPath, contextDir);
      ctx.setResources(new TargetClassesContext());
    } catch (ServletException e) {
      throw new RuntimeException(e);
    }

    if (this.privileged) {
      ctx.setPrivileged(true);
    }

    if (this.enableNaming
        || !this.contextEnvironments.isEmpty()
        || !this.contextResources.isEmpty()
        || this.contextFileURL != null) {
      this.tomcat.enableNaming();

      if (this.addDefaultListeners) {
        this.tomcat.getServer().addLifecycleListener(new GlobalResourcesLifecycleListener());
      }
    }

    if (this.addDefaultListeners) {
      Server server = this.tomcat.getServer();
      server.addLifecycleListener(new JasperListener());
      server.addLifecycleListener(new JreMemoryLeakPreventionListener());
      server.addLifecycleListener(new ThreadLocalLeakPreventionListener());
    }

    for (ContextEnvironment env : this.contextEnvironments) {
      ctx.getNamingResources().addEnvironment(env);
    }

    for (ContextResource res : this.contextResources) {
      ctx.getNamingResources().addResource(res);
    }

    for (ApplicationParameter param : this.contextInitializationParameters) {
      ctx.addApplicationParameter(param);
    }

    if (this.contextFileURL != null) {
      ctx.setConfigFile(this.contextFileURL);
    }

    // Shutdown tomcat if a failure occurs during startup
    ctx.addLifecycleListener(
        new LifecycleListener() {
          @Override
          public void lifecycleEvent(LifecycleEvent event) {
            if (event.getLifecycle().getState() == LifecycleState.FAILED) {
              ((StandardServer) EmbeddedTomcat.this.tomcat.getServer()).stopAwait();
            }
          }
        });

    try {
      this.tomcat.start();
    } catch (LifecycleException e) {
      throw new RuntimeException(e);
    }

    ((StandardManager) ctx.getManager()).setPathname(null);

    installSlf4jBridge();

    if (await) {
      this.tomcat.getServer().await();
      stop();
    }
  }
示例#9
0
  @Override
  public void start() {
    embedded = new Tomcat();
    AuthConfigFactory.setFactory(new AuthConfigFactoryImpl());

    Context context = embedded.addContext(getContextPath(), "/");
    context.addParameter("contextConfigLocation", getContextResource());
    context.addApplicationListener(ContextLoaderListener.class.getName());

    embedded.getHost().setAppBase("");

    // Each servlet should get an unique name, otherwise all servers will reuse
    // one and the same servlet instance.  Note that name clashes with servlets
    // created somewhere else are still possible.
    String servletName =
        getServletName() == null
            ? "ipf-servlet-" + SERVLET_COUNTER.getAndIncrement()
            : getServletName();

    wrapper = context.createWrapper();
    wrapper.setName(servletName);
    wrapper.setServletClass(getServlet().getClass().getName());

    for (Map.Entry<String, String> parameters : getInitParameters().entrySet()) {
      wrapper.addInitParameter(parameters.getKey(), parameters.getValue());
    }

    context.addChild(wrapper);
    context.addServletMapping(getServletPath(), servletName);

    /*
    VirtualWebappLoader loader = new VirtualWebappLoader(this.getClass().getClassLoader());
    loader.setVirtualClasspath(System.getProperty("java.class.path"));
    context.setLoader(loader);
    */
    Connector connector = embedded.getConnector();
    connector.setPort(getPort());
    if (isSecure()) {
      connector.setSecure(true);
      connector.setScheme("https");
      connector.setProperty("SSLEnabled", "true");
      connector.setProperty("sslProtocol", "TLS");
      connector.setProperty("keystoreFile", getKeystoreFile());
      connector.setProperty("keystorePass", getKeystorePass());
      connector.setProperty("truststoreFile", getTruststoreFile());
      connector.setProperty("truststorePass", getTruststorePass());
      if (getClientAuthType() == ClientAuthType.MUST) {
        connector.setProperty("clientAuth", "true");
      } else if (getClientAuthType() == ClientAuthType.WANT) {
        connector.setProperty("clientAuth", "want");
      }
    }

    try {
      embedded.start();
      wrapper.allocate();
      log.info("Started embedded Tomcat server");
    } catch (Exception e) {
      throw new AssertionError(e);
    }
  }