Java Code Examples for org.apache.catalina.startup.Tomcat#getConnector()

The following examples show how to use org.apache.catalina.startup.Tomcat#getConnector() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestConnector.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Test
public void testPort() throws Exception {
    Tomcat tomcat = getTomcatInstance();

    Connector connector1 = tomcat.getConnector();
    connector1.setPort(0);

    Connector connector2 = new Connector();
    connector2.setPort(0);

    tomcat.getService().addConnector(connector2);

    tomcat.start();

    int localPort1 = connector1.getLocalPort();
    int localPort2 = connector2.getLocalPort();

    Assert.assertTrue(localPort1 > 0);
    Assert.assertTrue(localPort2 > 0);
}
 
Example 2
Source File: AbstractTomcatServer.java    From cxf with Apache License 2.0 6 votes vote down vote up
protected void run() {
    System.setProperty("java.naming.factory.url", "org.eclipse.jetty.jndi");
    System.setProperty("java.naming.factory.initial", "org.eclipse.jetty.jndi.InitialContextFactory");

    server = new Tomcat();
    server.setPort(port);
    server.getConnector();

    try {
        base = Files.createTempDirectory("tmp-");
        server.setBaseDir(base.toString());

        server.getHost().setAppBase(base.toString());
        server.getHost().setAutoDeploy(true);
        server.getHost().setDeployOnStartup(true);

        server.addWebapp(contextPath, getClass().getResource(resourcePath).toURI().getPath().toString());
        server.start();
    } catch (final Exception ex) {
        ex.printStackTrace();
        fail(ex.getMessage());
    }
}
 
Example 3
Source File: AbstractWebServiceTest.java    From openmeetings with Apache License 2.0 6 votes vote down vote up
@BeforeAll
public static void initialize() throws Exception {
	AbstractSpringTest.init();
	tomcat = new Tomcat();
	Connector connector = new Connector("HTTP/1.1");
	connector.setProperty("address", InetAddress.getByName(HOST).getHostAddress());
	connector.setPort(0);
	tomcat.getService().addConnector(connector);
	tomcat.setConnector(connector);
	File wd = Files.createTempDirectory("om" + randomUUID().toString()).toFile();
	tomcat.setBaseDir(wd.getCanonicalPath());
	tomcat.getHost().setAppBase(wd.getCanonicalPath());
	tomcat.getHost().setAutoDeploy(false);
	tomcat.getHost().setDeployOnStartup(false);
	tomcat.addWebapp(CONTEXT, getOmHome().getAbsolutePath());
	tomcat.getConnector(); // to init the connector
	tomcat.start();
	port = tomcat.getConnector().getLocalPort();
}
 
Example 4
Source File: TestConnector.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
@Test
public void testPort() throws Exception {
    Tomcat tomcat = getTomcatInstance();

    Connector connector1 = tomcat.getConnector();
    connector1.setPort(0);

    Connector connector2 = new Connector();
    connector2.setPort(0);

    tomcat.getService().addConnector(connector2);

    tomcat.start();

    int localPort1 = connector1.getLocalPort();
    int localPort2 = connector2.getLocalPort();

    assertTrue(localPort1 > 0);
    assertTrue(localPort2 > 0);
}
 
Example 5
Source File: TestConnector.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
@Test
public void testPort() throws Exception {
    Tomcat tomcat = getTomcatInstance();

    Connector connector1 = tomcat.getConnector();
    connector1.setPort(0);

    Connector connector2 = new Connector();
    connector2.setPort(0);

    tomcat.getService().addConnector(connector2);

    tomcat.start();

    int localPort1 = connector1.getLocalPort();
    int localPort2 = connector2.getLocalPort();

    assertTrue(localPort1 > 0);
    assertTrue(localPort2 > 0);
}
 
Example 6
Source File: TomcatServer.java    From pippo with Apache License 2.0 6 votes vote down vote up
private void enableSSLConnector(Tomcat tomcat) {
    log.info("Using https protocol");
    Connector connector = tomcat.getConnector();
    connector.setPort(getSettings().getPort());
    connector.setSecure(true);
    connector.setScheme("https");
    connector.setAttribute("keyAlias", getSettings().getKeyAlias());
    connector.setAttribute("keystorePass", getSettings().getKeystorePassword());
    connector.setAttribute("keystoreType", getSettings().getKeyType());
    connector.setAttribute("keystoreFile", getSettings().getKeystoreFile());
    connector.setAttribute("clientAuth", getSettings().getClientAuth());
    if (getSettings().getClientAuth()) {
        connector.setAttribute("truststoreFile", getSettings().getTruststoreFile());
        connector.setAttribute("truststorePass", getSettings().getTruststorePassword());
    }
    connector.setAttribute("protocol", "HTTP/1.1");
    connector.setAttribute("sslProtocol", "TLS");
    connector.setAttribute("maxThreads", getSettings().getMaxConnections());
    connector.setAttribute("protocol", "org.apache.coyote.http11.Http11AprProtocol");
    connector.setAttribute("SSLEnabled", true);
}
 
Example 7
Source File: Http2TestBase.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
void handleGoAwayResponse(int lastStream, Http2Error expectedError)
        throws Http2Exception, IOException {
    try {
        parser.readFrame(true);

        Assert.assertTrue(output.getTrace(), output.getTrace().startsWith(
                "0-Goaway-[" + lastStream + "]-[" + expectedError.getCode() + "]-["));
    } catch (SocketException se) {
        // On some platform / Connector combinations (e.g. Windows / NIO2),
        // the TCP connection close will be processed before the client gets
        // a chance to read the connection close frame.
        Tomcat tomcat = getTomcatInstance();
        Connector connector = tomcat.getConnector();

        Assume.assumeTrue("This test is only expected to trigger an exception with NIO2",
                connector.getProtocolHandlerClassName().contains("Nio2"));

        Assume.assumeTrue("This test is only expected to trigger an exception on Windows",
                System.getProperty("os.name").startsWith("Windows"));
    }
}
 
Example 8
Source File: TestSwallowAbortedUploads.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
private synchronized void init(int status, boolean swallow)
        throws Exception {

    Tomcat tomcat = getTomcatInstance();
    context = tomcat.addContext("", TEMP_DIR);
    AbortedPOSTServlet servlet = new AbortedPOSTServlet(status);
    Tomcat.addServlet(context, servletName, servlet);
    context.addServletMappingDecoded(URI, servletName);
    context.setSwallowAbortedUploads(swallow);

    tomcat.start();

    Connector c = tomcat.getConnector();
    c.setMaxPostSize(2 * hugeSize);
    c.setProperty("maxSwallowSize", Integer.toString(hugeSize));

    setPort(c.getLocalPort());
}
 
Example 9
Source File: TestHttp11InputBuffer.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
private Exception doRequest() {

            Tomcat tomcat = getTomcatInstance();

            Context root = tomcat.addContext("", TEMP_DIR);
            Tomcat.addServlet(root, "Bug51557",
                    new Bug51557Servlet(headerName));
            root.addServletMappingDecoded("/test", "Bug51557");

            try {
                Connector connector = tomcat.getConnector();
                connector.setProperty("rejectIllegalHeaderName",
                        Boolean.toString(rejectIllegalHeaderName));
                tomcat.start();
                setPort(connector.getLocalPort());


                // Open connection
                connect();

                String[] request = new String[1];
                request[0] =
                    "GET http://localhost:8080/test HTTP/1.1" + CRLF +
                    "Host: localhost:8080" + CRLF +
                    headerLine + CRLF +
                    "X-Bug51557: abcd" + CRLF +
                    "Connection: close" + CRLF +
                    CRLF;

                setRequest(request);
                processRequest(); // blocks until response has been read

                // Close the connection
                disconnect();
            } catch (Exception e) {
                return e;
            }
            return null;
        }
 
Example 10
Source File: TestHttp11InputBuffer.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
private Exception doRequest() {

            // Ensure body is read correctly
            setUseContentLength(true);

            Tomcat tomcat = getTomcatInstance();

            Context root = tomcat.addContext("", TEMP_DIR);
            Tomcat.addServlet(root, "Bug59089", new TesterServlet());
            root.addServletMappingDecoded("/test", "Bug59089");

            try {
                Connector connector = tomcat.getConnector();
                connector.setProperty("rejectIllegalHeaderName", "false");
                tomcat.start();
                setPort(connector.getLocalPort());

                // Open connection
                connect();

                String[] request = new String[1];
                request[0] = "GET http://localhost:8080/test HTTP/1.1" + CRLF +
                        "Host: localhost:8080" + CRLF +
                        "X-Header: Ignore" + CRLF +
                        "X-Header" + (char) 130 + ": Broken" + CRLF + CRLF;

                setRequest(request);
                processRequest(); // blocks until response has been read

                // Close the connection
                disconnect();
            } catch (Exception e) {
                return e;
            }
            return null;
        }
 
Example 11
Source File: TestCustomSsl.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Test
public void testCustomSslImplementation() throws Exception {

    TesterSupport.configureClientSsl();

    Tomcat tomcat = getTomcatInstance();
    Connector connector = tomcat.getConnector();

    Assume.assumeFalse("This test is only for JSSE based SSL connectors",
            connector.getProtocolHandlerClassName().contains("Apr"));

    connector.setProperty("sslImplementationName",
            "org.apache.tomcat.util.net.jsse.TesterBug50640SslImpl");

    // This setting will break ssl configuration unless the custom
    // implementation is used.
    connector.setProperty(TesterBug50640SslImpl.PROPERTY_NAME,
            TesterBug50640SslImpl.PROPERTY_VALUE);

    connector.setProperty("sslProtocol", "tls");

    File keystoreFile =
        new File(TesterSupport.LOCALHOST_RSA_JKS);
    connector.setAttribute(
            "keystoreFile", keystoreFile.getAbsolutePath());

    connector.setSecure(true);
    connector.setProperty("SSLEnabled", "true");

    File appDir = new File(getBuildDirectory(), "webapps/examples");
    Context ctxt  = tomcat.addWebapp(
            null, "/examples", appDir.getAbsolutePath());
    ctxt.addApplicationListener(WsContextListener.class.getName());

    tomcat.start();
    ByteChunk res = getUrl("https://localhost:" + getPort() +
        "/examples/servlets/servlet/HelloWorldExample");
    Assert.assertTrue(res.toString().indexOf("<a href=\"../helloworld.html\">") > 0);
}
 
Example 12
Source File: TestSSLHostConfigCompat.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Override
public void setUp() throws Exception {
    super.setUp();

    AprLifecycleListener listener = new AprLifecycleListener();
    Assume.assumeTrue(AprLifecycleListener.isAprAvailable());
    Assume.assumeTrue(JreCompat.isJre8Available());

    Tomcat tomcat = getTomcatInstance();
    Connector connector = tomcat.getConnector();

    connector.setPort(0);
    connector.setScheme("https");
    connector.setSecure(true);
    connector.setProperty("SSLEnabled", "true");
    connector.setProperty("sslImplementationName", sslImplementationName);
    sslHostConfig.setProtocols("TLSv1.2");
    connector.addSslHostConfig(sslHostConfig);

    StandardServer server = (StandardServer) tomcat.getServer();
    server.addLifecycleListener(listener);

    // Simple webapp
    Context ctxt = tomcat.addContext("", null);
    Tomcat.addServlet(ctxt, "TesterServlet", new TesterServlet());
    ctxt.addServletMappingDecoded("/*", "TesterServlet");
}
 
Example 13
Source File: TestConnector.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Test
public void testStop() throws Exception {
    Tomcat tomcat = getTomcatInstance();

    // No file system docBase required
    Context root = tomcat.addContext("", null);
    Wrapper w =
        Tomcat.addServlet(root, "tester", new TesterServlet());
    w.setAsyncSupported(true);
    root.addServletMapping("/", "tester");

    Connector connector = tomcat.getConnector();

    tomcat.start();

    ByteChunk bc = new ByteChunk();
    int rc = getUrl("http://localhost:" + getPort() + "/", bc, null, null);

    assertEquals(200, rc);
    assertEquals("OK", bc.toString());

    rc = -1;
    bc.recycle();

    connector.stop();

    try {
        rc = getUrl("http://localhost:" + getPort() + "/", bc, 1000,
                null, null);
    } catch (SocketTimeoutException ste) {
        // May also see this with NIO
        // Make sure the test passes if we do
        rc = 503;
    }
    assertEquals(503, rc);
}
 
Example 14
Source File: TestCustomSsl.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Test
public void testCustomSslImplementation() throws Exception {

    TesterSupport.configureClientSsl();

    Tomcat tomcat = getTomcatInstance();
    Connector connector = tomcat.getConnector();

    Assume.assumeFalse("This test is only for JSSE based SSL connectors",
            connector.getProtocolHandlerClassName().contains("Apr"));

    connector.setProperty("sslImplementationName",
            "org.apache.tomcat.util.net.jsse.TesterBug50640SslImpl");
    connector.setProperty(TesterBug50640SslImpl.PROPERTY_NAME,
            TesterBug50640SslImpl.PROPERTY_VALUE);

    connector.setProperty("sslProtocol", "tls");

    File keystoreFile =
        new File("test/org/apache/tomcat/util/net/localhost.jks");
    connector.setAttribute(
            "keystoreFile", keystoreFile.getAbsolutePath());

    connector.setSecure(true);
    connector.setProperty("SSLEnabled", "true");

    File appDir = new File(getBuildDirectory(), "webapps/examples");
    tomcat.addWebapp(null, "/examples", appDir.getAbsolutePath());

    tomcat.start();
    ByteChunk res = getUrl("https://localhost:" + getPort() +
        "/examples/servlets/servlet/HelloWorldExample");
    assertTrue(res.toString().indexOf("<h1>Hello World!</h1>") > 0);
}
 
Example 15
Source File: TestConnector.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
@Test
public void testStop() throws Exception {
    Tomcat tomcat = getTomcatInstance();

    // No file system docBase required
    Context root = tomcat.addContext("", null);
    Wrapper w =
        Tomcat.addServlet(root, "tester", new TesterServlet());
    w.setAsyncSupported(true);
    root.addServletMapping("/", "tester");

    Connector connector = tomcat.getConnector();

    tomcat.start();

    ByteChunk bc = new ByteChunk();
    int rc = getUrl("http://localhost:" + getPort() + "/", bc, null, null);

    assertEquals(200, rc);
    assertEquals("OK", bc.toString());

    rc = -1;
    bc.recycle();

    connector.stop();

    try {
        rc = getUrl("http://localhost:" + getPort() + "/", bc, 1000,
                null, null);
    } catch (SocketTimeoutException ste) {
        // May also see this with NIO
        // Make sure the test passes if we do
        rc = 503;
    }
    assertEquals(503, rc);
}
 
Example 16
Source File: TestSwallowAbortedUploads.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
private synchronized void init(boolean limited, boolean swallow)
        throws Exception {

    Tomcat tomcat = getTomcatInstance();
    context = tomcat.addContext("", TEMP_DIR);
    Wrapper w;
    w = Tomcat.addServlet(context, servletName,
                          new AbortedUploadServlet());
    // Tomcat.addServlet does not respect annotations, so we have
    // to set our own MultipartConfigElement.
    // Choose upload file size limit.
    if (limited) {
        w.setMultipartConfigElement(new MultipartConfigElement("",
                limitSize, -1, -1));
    } else {
        w.setMultipartConfigElement(new MultipartConfigElement(""));
    }
    context.addServletMappingDecoded(URI, servletName);
    context.setSwallowAbortedUploads(swallow);

    Connector c = tomcat.getConnector();
    c.setMaxPostSize(2 * hugeSize);
    c.setProperty("maxSwallowSize", Integer.toString(hugeSize));

    tomcat.start();
    setPort(c.getLocalPort());
}
 
Example 17
Source File: TestCustomSsl.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
@Test
public void testCustomSslImplementation() throws Exception {

    TesterSupport.configureClientSsl();

    Tomcat tomcat = getTomcatInstance();
    Connector connector = tomcat.getConnector();

    Assume.assumeFalse("This test is only for JSSE based SSL connectors",
            connector.getProtocolHandlerClassName().contains("Apr"));

    connector.setProperty("sslImplementationName",
            "org.apache.tomcat.util.net.jsse.TesterBug50640SslImpl");
    connector.setProperty(TesterBug50640SslImpl.PROPERTY_NAME,
            TesterBug50640SslImpl.PROPERTY_VALUE);

    connector.setProperty("sslProtocol", "tls");

    File keystoreFile =
        new File("test/org/apache/tomcat/util/net/localhost.jks");
    connector.setAttribute(
            "keystoreFile", keystoreFile.getAbsolutePath());

    connector.setSecure(true);
    connector.setProperty("SSLEnabled", "true");

    File appDir = new File(getBuildDirectory(), "webapps/examples");
    tomcat.addWebapp(null, "/examples", appDir.getAbsolutePath());

    tomcat.start();
    ByteChunk res = getUrl("https://localhost:" + getPort() +
        "/examples/servlets/servlet/HelloWorldExample");
    assertTrue(res.toString().indexOf("<a href=\"../helloworld.html\">") > 0);
}
 
Example 18
Source File: TestConnector.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
private void doTestTrace(Servlet servlet, boolean allowTrace) throws Exception {
    Tomcat tomcat = getTomcatInstance();

    File appDir = new File("test/webapp");
    Context root = tomcat.addContext("", appDir.getAbsolutePath());
    Tomcat.addServlet(root, "default", servlet);
    root.addServletMappingDecoded("/", "default");

    Connector connector = tomcat.getConnector();
    connector.setAllowTrace(allowTrace);

    tomcat.start();

    ByteChunk bc = new ByteChunk();
    Map<String,List<String>> respHeaders = new HashMap<>();
    int rc = methodUrl("http://localhost:" + getPort() + "/index.html",
            bc, 30000, null, respHeaders, "OPTIONS");

    Assert.assertEquals(200, rc);

    boolean foundTrace = false;
    for (String header : respHeaders.get("Allow")) {
        if (header.contains("TRACE")) {
            foundTrace = true;
            break;
        }
    }

    if (allowTrace) {
        Assert.assertTrue(foundTrace);
    } else {
        Assert.assertFalse(foundTrace);
    }
}
 
Example 19
Source File: AbstractTomcatServer.java    From cxf with Apache License 2.0 5 votes vote down vote up
protected void run() {
    server = new Tomcat();
    server.setPort(port);
    server.getConnector();

    try {
        base = Files.createTempDirectory("tmp-");
        server.setBaseDir(base.toString());

        if (resourcePath == null) {
            final Context context = server.addContext("", base.toString());
            final Wrapper cxfServlet = Tomcat.addServlet(context, "cxfServlet", new CXFNonSpringJaxrsServlet());
            cxfServlet.addInitParameter("jaxrs.serviceClasses", BookStore.class.getName());
            cxfServlet.addInitParameter("jaxrs.providers", String.join(",",
                JacksonJsonProvider.class.getName(),
                BookStoreResponseFilter.class.getName()
            ));
            cxfServlet.setAsyncSupported(true);
            context.addServletMappingDecoded("/rest/*", "cxfServlet");
        } else {
            server.getHost().setAppBase(base.toString());
            server.getHost().setAutoDeploy(true);
            server.getHost().setDeployOnStartup(true);
            server.addWebapp(contextPath, getClass().getResource(resourcePath).toURI().getPath().toString());
        }

        server.start();
    } catch (final Exception ex) {
        ex.printStackTrace();
        fail(ex.getMessage());
    }
}
 
Example 20
Source File: TestConnector.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Test
public void testStop() throws Exception {
    Tomcat tomcat = getTomcatInstance();

    Context root = tomcat.addContext("", TEMP_DIR);
    Wrapper w =
        Tomcat.addServlet(root, "tester", new TesterServlet());
    w.setAsyncSupported(true);
    root.addServletMappingDecoded("/", "tester");

    Connector connector = tomcat.getConnector();

    tomcat.start();

    ByteChunk bc = new ByteChunk();
    int rc = getUrl("http://localhost:" + getPort() + "/", bc, null, null);

    Assert.assertEquals(200, rc);
    Assert.assertEquals("OK", bc.toString());

    rc = -1;
    bc.recycle();

    connector.stop();

    try {
        rc = getUrl("http://localhost:" + getPort() + "/", bc, 1000,
                null, null);
    } catch (SocketTimeoutException ste) {
        // May also see this with NIO
        // Make sure the test passes if we do
        rc = 503;
    }
    Assert.assertEquals(503, rc);
}