コード例 #1
0
  @BeforeClass(alwaysRun = true)
  public void setUpGlobal() throws Exception {

    port1 = findFreePort();
    embedded = new Embedded();
    String path = new File(".").getAbsolutePath();
    embedded.setCatalinaHome(path);

    Engine engine = embedded.createEngine();
    engine.setDefaultHost("127.0.0.1");

    Host host = embedded.createHost("127.0.0.1", path);
    engine.addChild(host);

    Context c = embedded.createContext("/", path);
    c.setReloadable(false);
    Wrapper w = c.createWrapper();
    w.addMapping("/*");
    w.setServletClass(org.apache.catalina.servlets.WebdavServlet.class.getName());
    w.addInitParameter("readonly", "false");
    w.addInitParameter("listings", "true");

    w.setLoadOnStartup(0);

    c.addChild(w);
    host.addChild(c);

    Connector connector =
        embedded.createConnector("127.0.0.1", port1, Http11NioProtocol.class.getName());
    connector.setContainer(host);
    embedded.addEngine(engine);
    embedded.addConnector(connector);
    embedded.start();
  }
コード例 #2
0
 private void addDefaultServlet(Context context) {
   Wrapper defaultServlet = context.createWrapper();
   defaultServlet.setName("default");
   defaultServlet.setServletClass("org.apache.catalina.servlets.DefaultServlet");
   defaultServlet.addInitParameter("debug", "0");
   defaultServlet.addInitParameter("listings", "false");
   defaultServlet.setLoadOnStartup(1);
   // Otherwise the default location of a Spring DispatcherServlet cannot be set
   defaultServlet.setOverridable(true);
   context.addChild(defaultServlet);
   context.addServletMapping("/", "default");
 }
コード例 #3
0
 private void addJspServlet(Context context) {
   Wrapper jspServlet = context.createWrapper();
   jspServlet.setName("jsp");
   jspServlet.setServletClass(getJspServlet().getClassName());
   jspServlet.addInitParameter("fork", "false");
   for (Entry<String, String> initParameter : getJspServlet().getInitParameters().entrySet()) {
     jspServlet.addInitParameter(initParameter.getKey(), initParameter.getValue());
   }
   jspServlet.setLoadOnStartup(3);
   context.addChild(jspServlet);
   context.addServletMapping("*.jsp", "jsp");
   context.addServletMapping("*.jspx", "jsp");
 }
コード例 #4
0
  private static void addServlets(JBossWebMetaData jbwebMD, StandardContext context) {
    for (JBossServletMetaData smd : jbwebMD.getServlets()) {
      final String sc = smd.getServletClass();
      if (sc.equals(WSFServlet.class.getName())) {
        final String servletName = smd.getServletName();
        List<ParamValueMetaData> params = smd.getInitParam();
        List<String> urlPatterns = null;
        for (ServletMappingMetaData smmd : jbwebMD.getServletMappings()) {
          if (smmd.getServletName().equals(servletName)) {
            urlPatterns = smmd.getUrlPatterns();
            break;
          }
        }

        WSFServlet wsfs = new WSFServlet();
        Wrapper wsfsWrapper = context.createWrapper();
        wsfsWrapper.setName(servletName);
        wsfsWrapper.setServlet(wsfs);
        wsfsWrapper.setServletClass(WSFServlet.class.getName());
        for (ParamValueMetaData param : params) {
          wsfsWrapper.addInitParameter(param.getParamName(), param.getParamValue());
        }
        context.addChild(wsfsWrapper);
        for (String urlPattern : urlPatterns) {
          context.addServletMapping(urlPattern, servletName);
        }
      }
    }
  }
コード例 #5
0
 private void addInitParameters(final Wrapper wrapper, final Map<String, String> initParameters) {
   NullArgumentException.validateNotNull(initParameters, "initParameters");
   NullArgumentException.validateNotNull(wrapper, "wrapper");
   for (final Map.Entry<String, String> initParam : initParameters.entrySet()) {
     wrapper.addInitParameter(initParam.getKey(), initParam.getValue());
   }
 }
コード例 #6
0
ファイル: Tomcat.java プロジェクト: anoopgecbh/Tomcat-4
  /**
   * Static version of {@link #initWebappDefaults(String)}
   *
   * @param ctx The context to set the defaults for
   */
  public static void initWebappDefaults(Context ctx) {
    // Default servlet
    Wrapper servlet = addServlet(ctx, "default", "org.apache.catalina.servlets.DefaultServlet");
    servlet.setLoadOnStartup(1);
    servlet.setOverridable(true);

    // JSP servlet (by class name - to avoid loading all deps)
    servlet = addServlet(ctx, "jsp", "org.apache.jasper.servlet.JspServlet");
    servlet.addInitParameter("fork", "false");
    servlet.setLoadOnStartup(3);
    servlet.setOverridable(true);

    // Servlet mappings
    ctx.addServletMapping("/", "default");
    ctx.addServletMapping("*.jsp", "jsp");
    ctx.addServletMapping("*.jspx", "jsp");

    // Sessions
    ctx.setSessionTimeout(30);

    // MIME mappings
    for (int i = 0; i < DEFAULT_MIME_MAPPINGS.length; ) {
      ctx.addMimeMapping(DEFAULT_MIME_MAPPINGS[i++], DEFAULT_MIME_MAPPINGS[i++]);
    }

    // Welcome files
    ctx.addWelcomeFile("index.html");
    ctx.addWelcomeFile("index.htm");
    ctx.addWelcomeFile("index.jsp");
  }
コード例 #7
0
ファイル: RestApiModule.java プロジェクト: homeki/homekicore
  @Override
  public void construct() {
    tomcat = new Tomcat();

    Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
    connector.setURIEncoding("UTF-8");
    connector.setPort(5000);
    tomcat.getService().addConnector(connector);
    tomcat.setConnector(connector);

    try {
      Context ctx;

      if (new File(Configuration.WEBROOT_PATH).exists()) {
        L.i("Static web client found.");

        ctx = tomcat.addContext("", Configuration.WEBROOT_PATH);
        ctx.addWelcomeFile("index.html");

        Wrapper wrapper = tomcat.addServlet(ctx, "DefaultServlet", new DefaultServlet());
        wrapper.setAsyncSupported(true);
        wrapper.addInitParameter("listings", "false");
        wrapper.addMapping("/");
        wrapper.setLoadOnStartup(1);
      } else {
        L.w("No static web client found, web interface will not be available.");
        ctx = tomcat.addContext("/", "/tmp");
      }

      configureMimeMappings(ctx);

      addSessionFilter(ctx);
      addCharacterEncodingFilter(ctx);

      Wrapper wrapper = tomcat.addServlet(ctx, "JerseyServlet", new ServletContainer());
      wrapper.setAsyncSupported(true);
      wrapper.addInitParameter("javax.ws.rs.Application", JerseyApplication.class.getName());
      wrapper.addMapping("/api/*");
      wrapper.setLoadOnStartup(1);

      tomcat.start();
    } catch (Exception e) {
      L.e("Failed to start RestApiModule.", e);
    }
  }
コード例 #8
0
 private void addJspServlet(Context context) {
   Wrapper jspServlet = context.createWrapper();
   jspServlet.setName("jsp");
   jspServlet.setServletClass(getJspServletClassName());
   jspServlet.addInitParameter("fork", "false");
   jspServlet.setLoadOnStartup(3);
   context.addChild(jspServlet);
   context.addServletMapping("*.jsp", "jsp");
   context.addServletMapping("*.jspx", "jsp");
 }
コード例 #9
0
  /** Verify serving of gzipped resources from context root. */
  @Test
  public void testGzippedFile() throws Exception {

    Tomcat tomcat = getTomcatInstance();

    File appDir = new File("test/webapp");

    File gzipIndex = new File(appDir, "index.html.gz");
    long gzipSize = gzipIndex.length();

    File index = new File(appDir, "index.html");
    long indexSize = index.length();

    // app dir is relative to server home
    Context ctxt = tomcat.addContext("", appDir.getAbsolutePath());
    Wrapper defaultServlet =
        Tomcat.addServlet(ctxt, "default", "org.apache.catalina.servlets.DefaultServlet");
    defaultServlet.addInitParameter("gzip", "true");
    ctxt.addServletMapping("/", "default");

    ctxt.addMimeMapping("html", "text/html");

    tomcat.start();

    TestGzipClient gzipClient = new TestGzipClient(getPort());

    gzipClient.reset();
    gzipClient.setRequest(
        new String[] {
          "GET /index.html HTTP/1.1"
              + CRLF
              + "Host: localhost"
              + CRLF
              + "Connection: Close"
              + CRLF
              + "Accept-Encoding: gzip"
              + CRLF
              + CRLF
        });
    gzipClient.connect();
    gzipClient.processRequest();
    assertTrue(gzipClient.isResponse200());
    List<String> responseHeaders = gzipClient.getResponseHeaders();
    assertTrue(responseHeaders.contains("Content-Length: " + gzipSize));

    gzipClient.reset();
    gzipClient.setRequest(
        new String[] {
          "GET /index.html HTTP/1.1"
              + CRLF
              + "Host: localhost"
              + CRLF
              + "Connection: Close"
              + CRLF
              + CRLF
        });
    gzipClient.connect();
    gzipClient.processRequest();
    assertTrue(gzipClient.isResponse200());
    responseHeaders = gzipClient.getResponseHeaders();
    assertTrue(responseHeaders.contains("Content-Type: text/html"));
    assertFalse(responseHeaders.contains("Content-Encoding: gzip"));
    assertTrue(responseHeaders.contains("Content-Length: " + indexSize));
  }
コード例 #10
0
ファイル: TomcatServer.java プロジェクト: oehf/ipf
  @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);
    }
  }
コード例 #11
0
  private void doSimpleCometTest(String initParam) throws Exception {
    Assume.assumeTrue(
        "This test is skipped, because this connector does not support Comet.", isCometSupported());

    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context root = tomcat.addContext("", null);
    Wrapper w = Tomcat.addServlet(root, "comet", new SimpleCometServlet());
    if (initParam != null) {
      w.addInitParameter(initParam, "true");
    }
    root.addServletMapping("/", "comet");

    TesterAccessLogValve alv = new TesterAccessLogValve();
    root.getPipeline().addValve(alv);

    tomcat.start();

    // Create connection to Comet servlet
    final Socket socket = SocketFactory.getDefault().createSocket("localhost", getPort());
    socket.setSoTimeout(60000);

    final OutputStream os = socket.getOutputStream();
    String requestLine = "POST http://localhost:" + getPort() + "/ HTTP/1.1\r\n";
    os.write(requestLine.getBytes());
    os.write("transfer-encoding: chunked\r\n".getBytes());
    os.write("\r\n".getBytes());

    PingWriterThread writeThread = new PingWriterThread(4, os);
    writeThread.start();

    socket.setSoTimeout(25000);
    InputStream is = socket.getInputStream();
    ResponseReaderThread readThread = new ResponseReaderThread(is);
    readThread.start();
    readThread.join();
    os.close();
    is.close();

    String[] response = readThread.getResponse().split("\r\n");
    if (initParam == null) {
      // Normal response expected
      // Validate response
      assertEquals("HTTP/1.1 200 OK", response[0]);
      assertEquals("Server: Apache-Coyote/1.1", response[1]);
      assertTrue(response[2].startsWith("Set-Cookie: JSESSIONID="));
      assertEquals("Content-Type: text/plain;charset=ISO-8859-1", response[3]);
      assertEquals("Transfer-Encoding: chunked", response[4]);
      assertTrue(response[5].startsWith("Date: "));
      assertEquals("", response[6]);
      assertEquals("7", response[7]);
      assertEquals("BEGIN", response[8]);
      assertEquals("", response[9]);
      assertEquals("17", response[10]);
      assertEquals("Client: READ: 4 bytes", response[11]);
      assertEquals("", response[12]);
      assertEquals("17", response[13]);
      assertEquals("Client: READ: 4 bytes", response[14]);
      assertEquals("", response[15]);
      assertEquals("17", response[16]);
      assertEquals("Client: READ: 4 bytes", response[17]);
      assertEquals("", response[18]);
      assertEquals("17", response[19]);
      assertEquals("Client: READ: 4 bytes", response[20]);
      assertEquals("", response[21]);
      assertEquals("d", response[22]);
      assertEquals("Client: END", response[23]);
      assertEquals("", response[24]);
      assertEquals("0", response[25]);
      // Expect 26 lines
      assertEquals(26, response.length);
    } else {
      // Failure expected only expected for the fail on begin
      // Failure at any later stage and the response headers (including
      // the 200 response code will already have been sent to the client
      if (SimpleCometServlet.FAIL_ON_BEGIN.equals(initParam)) {
        assertEquals("HTTP/1.1 500 Internal Server Error", response[0]);
        alv.validateAccessLog(1, 500, 0, 1000);
      } else {
        assertEquals("HTTP/1.1 200 OK", response[0]);
        alv.validateAccessLog(1, 200, 0, 5000);
      }
    }
  }