Search in sources :

Example 6 with ServletHandler

use of org.eclipse.jetty.servlet.ServletHandler in project nifi by apache.

the class TestGetHTTP method testSecure_oneWaySsl.

@Test
public final void testSecure_oneWaySsl() throws Exception {
    // set up web service
    final ServletHandler handler = new ServletHandler();
    handler.addServletWithMapping(HelloWorldServlet.class, "/*");
    // create the service, disabling the need for client auth
    final Map<String, String> serverSslProperties = getKeystoreProperties();
    serverSslProperties.put(TestServer.NEED_CLIENT_AUTH, Boolean.toString(false));
    final TestServer server = new TestServer(serverSslProperties);
    server.addHandler(handler);
    try {
        server.startServer();
        final String destination = server.getSecureUrl();
        // set up NiFi mock controller
        controller = TestRunners.newTestRunner(GetHTTP.class);
        // Use context service with only a truststore
        useSSLContextService(getTruststoreProperties());
        controller.setProperty(GetHTTP.CONNECTION_TIMEOUT, "5 secs");
        controller.setProperty(GetHTTP.URL, destination);
        controller.setProperty(GetHTTP.FILENAME, "testFile");
        controller.setProperty(GetHTTP.ACCEPT_CONTENT_TYPE, "application/json");
        controller.run();
        controller.assertAllFlowFilesTransferred(GetHTTP.REL_SUCCESS, 1);
        final MockFlowFile mff = controller.getFlowFilesForRelationship(GetHTTP.REL_SUCCESS).get(0);
        mff.assertContentEquals("Hello, World!");
    } finally {
        server.shutdownServer();
    }
}
Also used : MockFlowFile(org.apache.nifi.util.MockFlowFile) ServletHandler(org.eclipse.jetty.servlet.ServletHandler) Test(org.junit.Test)

Example 7 with ServletHandler

use of org.eclipse.jetty.servlet.ServletHandler in project nifi by apache.

the class TestGetHTTP method testExpressionLanguage.

@Test
public final void testExpressionLanguage() throws Exception {
    // set up web service
    ServletHandler handler = new ServletHandler();
    handler.addServletWithMapping(UserAgentTestingServlet.class, "/*");
    // create the service
    TestServer server = new TestServer();
    server.addHandler(handler);
    try {
        server.startServer();
        String destination = server.getUrl();
        // set up NiFi mock controller
        controller = TestRunners.newTestRunner(GetHTTP.class);
        controller.setProperty(GetHTTP.CONNECTION_TIMEOUT, "5 secs");
        controller.setProperty(GetHTTP.URL, destination + "/test_${literal(1)}.pdf");
        controller.setProperty(GetHTTP.FILENAME, "test_${now():format('yyyy/MM/dd_HH:mm:ss')}");
        controller.setProperty(GetHTTP.ACCEPT_CONTENT_TYPE, "application/json");
        controller.setProperty(GetHTTP.USER_AGENT, "testUserAgent");
        controller.run();
        controller.assertTransferCount(GetHTTP.REL_SUCCESS, 1);
        MockFlowFile response = controller.getFlowFilesForRelationship(GetHTTP.REL_SUCCESS).get(0);
        response.assertAttributeEquals("gethttp.remote.source", "localhost");
        String fileName = response.getAttribute(CoreAttributes.FILENAME.key());
        assertTrue(fileName.matches("test_\\d\\d\\d\\d/\\d\\d/\\d\\d_\\d\\d:\\d\\d:\\d\\d"));
    // shutdown web service
    } finally {
        server.shutdownServer();
    }
}
Also used : MockFlowFile(org.apache.nifi.util.MockFlowFile) ServletHandler(org.eclipse.jetty.servlet.ServletHandler) Test(org.junit.Test)

Example 8 with ServletHandler

use of org.eclipse.jetty.servlet.ServletHandler in project nifi by apache.

the class TestGetHTTP method testHttpErrors.

/**
 * Test for HTTP errors
 * @throws Exception exception
 */
@Test
public final void testHttpErrors() throws Exception {
    // set up web service
    ServletHandler handler = new ServletHandler();
    handler.addServletWithMapping(HttpErrorServlet.class, "/*");
    // create the service
    TestServer server = new TestServer();
    server.addHandler(handler);
    try {
        server.startServer();
        HttpErrorServlet servlet = (HttpErrorServlet) handler.getServlets()[0].getServlet();
        String destination = server.getUrl();
        this.controller = TestRunners.newTestRunner(GetHTTP.class);
        this.controller.setProperty(GetHTTP.CONNECTION_TIMEOUT, "5 secs");
        this.controller.setProperty(GetHTTP.URL, destination + "/test_${literal(1)}.pdf");
        this.controller.setProperty(GetHTTP.FILENAME, "test_${now():format('yyyy/MM/dd_HH:mm:ss')}");
        this.controller.setProperty(GetHTTP.ACCEPT_CONTENT_TYPE, "application/json");
        this.controller.setProperty(GetHTTP.USER_AGENT, "testUserAgent");
        // 204 - NO CONTENT
        servlet.setErrorToReturn(HttpServletResponse.SC_NO_CONTENT);
        this.controller.run();
        this.controller.assertTransferCount(GetHTTP.REL_SUCCESS, 0);
        // 404 - NOT FOUND
        servlet.setErrorToReturn(HttpServletResponse.SC_NOT_FOUND);
        this.controller.run();
        this.controller.assertTransferCount(GetHTTP.REL_SUCCESS, 0);
        // 500 - INTERNAL SERVER ERROR
        servlet.setErrorToReturn(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        this.controller.run();
        this.controller.assertTransferCount(GetHTTP.REL_SUCCESS, 0);
    } finally {
        // shutdown web service
        server.shutdownServer();
    }
}
Also used : ServletHandler(org.eclipse.jetty.servlet.ServletHandler) Test(org.junit.Test)

Example 9 with ServletHandler

use of org.eclipse.jetty.servlet.ServletHandler in project nifi by apache.

the class JettyWebSocketServer method startServer.

@OnEnabled
@Override
public void startServer(final ConfigurationContext context) throws Exception {
    if (server != null && server.isRunning()) {
        getLogger().info("A WebSocket server is already running. {}", new Object[] { server });
        return;
    }
    configuredPolicy = WebSocketPolicy.newServerPolicy();
    configurePolicy(context, configuredPolicy);
    server = new Server();
    final ContextHandlerCollection handlerCollection = new ContextHandlerCollection();
    final ServletContextHandler contextHandler = new ServletContextHandler();
    servletHandler = new ServletHandler();
    contextHandler.insertHandler(servletHandler);
    handlerCollection.setHandlers(new Handler[] { contextHandler });
    server.setHandler(handlerCollection);
    listenPort = context.getProperty(LISTEN_PORT).asInteger();
    final SslContextFactory sslContextFactory = createSslFactory(context);
    final ServerConnector serverConnector = createConnector(sslContextFactory, listenPort);
    server.setConnectors(new Connector[] { serverConnector });
    servletHandler.addServletWithMapping(JettyWebSocketServlet.class, "/*");
    getLogger().info("Starting JettyWebSocketServer on port {}.", new Object[] { listenPort });
    server.start();
    portToControllerService.put(listenPort, this);
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) ServletHandler(org.eclipse.jetty.servlet.ServletHandler) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) Server(org.eclipse.jetty.server.Server) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) OnEnabled(org.apache.nifi.annotation.lifecycle.OnEnabled)

Example 10 with ServletHandler

use of org.eclipse.jetty.servlet.ServletHandler in project nifi by apache.

the class WebSocketServerExample method setup.

@BeforeClass
public static void setup() throws Exception {
    server = new Server(0);
    final ContextHandlerCollection handlerCollection = new ContextHandlerCollection();
    final ServletContextHandler contextHandler = new ServletContextHandler();
    servletHandler = new ServletHandler();
    contextHandler.insertHandler(servletHandler);
    handlerCollection.setHandlers(new Handler[] { contextHandler });
    server.setHandler(handlerCollection);
    httpConnector = new ServerConnector(server);
    httpConnector.setPort(50010);
    final SslContextFactory sslContextFactory = new SslContextFactory();
    sslContextFactory.setKeyStorePath("src/test/resources/certs/localhost-ks.jks");
    sslContextFactory.setKeyStorePassword("localtest");
    sslContextFactory.setKeyStoreType("JKS");
    final HttpConfiguration https = new HttpConfiguration();
    https.addCustomizer(new SecureRequestCustomizer());
    sslConnector = new ServerConnector(server, new SslConnectionFactory(sslContextFactory, "http/1.1"), new HttpConnectionFactory(https));
    sslConnector.setPort(50011);
    server.setConnectors(new Connector[] { httpConnector, sslConnector });
    servletHolder = servletHandler.addServletWithMapping(WSServlet.class, "/test");
    servletHolder = servletHandler.addServletWithMapping(ConnectionCheckServlet.class, "/check");
    server.start();
    logger.info("Starting server on port {} for HTTP, and {} for HTTPS", httpConnector.getLocalPort(), sslConnector.getLocalPort());
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) ServletHandler(org.eclipse.jetty.servlet.ServletHandler) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) SecureRequestCustomizer(org.eclipse.jetty.server.SecureRequestCustomizer) Server(org.eclipse.jetty.server.Server) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) SslConnectionFactory(org.eclipse.jetty.server.SslConnectionFactory) BeforeClass(org.junit.BeforeClass)

Aggregations

ServletHandler (org.eclipse.jetty.servlet.ServletHandler)54 Server (org.eclipse.jetty.server.Server)23 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)22 ServerConnector (org.eclipse.jetty.server.ServerConnector)11 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)10 Test (org.junit.Test)10 FilterHolder (org.eclipse.jetty.servlet.FilterHolder)8 MockFlowFile (org.apache.nifi.util.MockFlowFile)5 ContextHandlerCollection (org.eclipse.jetty.server.handler.ContextHandlerCollection)5 HashMap (java.util.HashMap)4 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 HttpServletResponse (javax.servlet.http.HttpServletResponse)4 FilterMapping (org.eclipse.jetty.servlet.FilterMapping)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 InetSocketAddress (java.net.InetSocketAddress)3 HttpServlet (javax.servlet.http.HttpServlet)3 ServletMapping (org.eclipse.jetty.servlet.ServletMapping)3 SslContextFactory (org.eclipse.jetty.util.ssl.SslContextFactory)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2