Java Code Examples for org.eclipse.jetty.servlet.ServletHandler#addServletWithMapping()

The following examples show how to use org.eclipse.jetty.servlet.ServletHandler#addServletWithMapping() . 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: LivenessService.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
@Override
public void start() throws Exception {
  if (!livenessEnabled) {
    logger.info("Liveness service disabled");
    return;
  }
  final ServerConnector serverConnector = new ServerConnector(embeddedLivenessJetty, NUM_ACCEPTORS, NUM_SELECTORS);
  serverConnector.setPort(config.getInt(DremioConfig.LIVENESS_PORT));
  serverConnector.setHost(LOOPBACK_INTERFACE);
  serverConnector.setAcceptQueueSize(ACCEPT_QUEUE_BACKLOG);
  embeddedLivenessJetty.addConnector(serverConnector);

  ServletHandler handler = new ServletHandler();
  embeddedLivenessJetty.setHandler(handler);

  handler.addServletWithMapping(new ServletHolder(new LivenessServlet()), "/live");
  handler.addServletWithMapping(new ServletHolder(createMetricsServlet()), "/metrics");

  embeddedLivenessJetty.start();
  livenessPort = serverConnector.getLocalPort();
  logger.info("Started liveness service on port {}", livenessPort);
}
 
Example 2
Source File: JettyServletTest.java    From java-specialagent with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void beforeClass() throws Exception {
  System.setProperty(Configuration.SPAN_DECORATORS, "io.opentracing.contrib.specialagent.rule.servlet.MockSpanDecorator");
  System.setProperty(Configuration.SKIP_PATTERN, "/skipit");

  server = new Server(0);

  final ServletHandler servletHandler = new ServletHandler();
  servletHandler.addServletWithMapping(MockServlet.class, "/hello");
  servletHandler.addServletWithMapping(MockServlet.class, "/skipit");
  servletHandler.addFilterWithMapping(MockFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
  server.setHandler(servletHandler);

  server.start();
  serverPort = ((ServerConnector)server.getConnectors()[0]).getLocalPort();
}
 
Example 3
Source File: MinimalServlets.java    From cs601 with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    // Create a basic jetty server object that will listen on port 8080.  Note that if you set this to port 0
    // then a randomly available port will be assigned that you can either look in the logs for the port,
    // or programmatically obtain it for use in test cases.
    Server server = new Server(8080);

    // The ServletHandler is a dead simple way to create a context handler that is backed by an instance of a
    // Servlet.  This handler then needs to be registered with the Server object.
    ServletHandler handler = new ServletHandler();
    server.setHandler(handler);

    // Passing in the class for the servlet allows jetty to instantite an instance of that servlet and mount it
    // on a given context path.

    // !! This is a raw Servlet, not a servlet that has been configured through a web.xml or anything like that !!
    handler.addServletWithMapping(HelloServlet.class, "/*");
    handler.addServletWithMapping(SimpleResponseServlet.class, "/servlet/SimpleResponse");

    // Start things up! By using the server.join() the server thread will join with the current thread.
    // See "http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Thread.html#join()" for more details.
    server.start();
    server.join();
}
 
Example 4
Source File: ExampleServerTest.java    From rack-servlet with Apache License 2.0 5 votes vote down vote up
public ExampleServer(Servlet servlet, String urlPattern) {
  ServletHolder holder = new ServletHolder(servlet);
  ServletHandler handler = new ServletHandler();
  handler.addServletWithMapping(holder, urlPattern);
  server = new Server(0);
  server.setHandler(handler);
}
 
Example 5
Source File: WebhookMockServer.java    From java-slack-sdk with MIT License 5 votes vote down vote up
public WebhookMockServer(int port) {
    this.port = port;
    server = new Server(this.port);
    ServletHandler handler = new ServletHandler();
    server.setHandler(handler);
    handler.addServletWithMapping(WebhookMockApi.class, "/*");
}
 
Example 6
Source File: TestGetHTTP.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Test
public final void testDynamicHeaders() 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(org.apache.nifi.processors.standard.GetHTTP.class);
        controller.setProperty(org.apache.nifi.processors.standard.GetHTTP.CONNECTION_TIMEOUT, "5 secs");
        controller.setProperty(org.apache.nifi.processors.standard.GetHTTP.URL, destination);
        controller.setProperty(org.apache.nifi.processors.standard.GetHTTP.FILENAME, "testFile");
        controller.setProperty(org.apache.nifi.processors.standard.GetHTTP.ACCEPT_CONTENT_TYPE, "application/json");
        controller.setProperty(org.apache.nifi.processors.standard.GetHTTP.USER_AGENT, "testUserAgent");
        controller.setProperty("Static-Header", "StaticHeaderValue");
        controller.setProperty("EL-Header", "${now()}");

        controller.run();
        controller.assertTransferCount(org.apache.nifi.processors.standard.GetHTTP.REL_SUCCESS, 1);

        // shutdown web service
    } finally {
        server.shutdownServer();
    }
}
 
Example 7
Source File: MockSlackApiServer.java    From java-slack-sdk with MIT License 5 votes vote down vote up
public MockSlackApiServer(int port) {
    this.port = port;
    server = new Server(this.port);
    ServletHandler handler = new ServletHandler();
    server.setHandler(handler);
    handler.addServletWithMapping(MockSlackApi.class, "/*");
}
 
Example 8
Source File: TestGetHTTP.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@Test
public final void testDynamicHeaders() 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);
        controller.setProperty(GetHTTP.FILENAME, "testFile");
        controller.setProperty(GetHTTP.ACCEPT_CONTENT_TYPE, "application/json");
        controller.setProperty(GetHTTP.USER_AGENT, "testUserAgent");
        controller.setProperty("Static-Header", "StaticHeaderValue");
        controller.setProperty("EL-Header", "${now()}");

        controller.run();
        controller.assertTransferCount(GetHTTP.REL_SUCCESS, 1);

        // shutdown web service
    } finally {
        server.shutdownServer();
    }
}
 
Example 9
Source File: JettyOpenTsdb.java    From StatsAgg with Apache License 2.0 5 votes vote down vote up
public void startServer() {
    jettyServer_ = new Server(port_);
    jettyServer_.setStopAtShutdown(true);
    jettyServer_.setStopTimeout(stopServerTimeout_);
    ServletHandler handler = new ServletHandler();
    jettyServer_.setHandler(handler);
    handler.addServletWithMapping(OpenTsdb_Put.class, "/api/put");
    
    try {
        jettyServer_.start();
    }
    catch (Exception e) {
        logger.error(e.toString() + System.lineSeparator() + StackTrace.getStringFromStackTrace(e));
    }
}
 
Example 10
Source File: TestPostHTTP.java    From nifi with Apache License 2.0 5 votes vote down vote up
private void setup(final Map<String, String> sslProperties) throws Exception {
    // set up web service
    ServletHandler handler = new ServletHandler();
    handler.addServletWithMapping(CaptureServlet.class, "/*");

    // create the service
    server = new TestServer(sslProperties);
    server.addHandler(handler);
    server.startServer();

    servlet = (CaptureServlet) handler.getServlets()[0].getServlet();
    runner = TestRunners.newTestRunner(org.apache.nifi.processors.standard.PostHTTP.class);
}
 
Example 11
Source File: TestGetHTTP.java    From nifi with Apache License 2.0 5 votes vote down vote up
@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(org.apache.nifi.processors.standard.GetHTTP.class);
        controller.setProperty(org.apache.nifi.processors.standard.GetHTTP.CONNECTION_TIMEOUT, "5 secs");
        controller.setProperty(org.apache.nifi.processors.standard.GetHTTP.URL, destination+"/test_${literal(1)}.pdf");
        controller.setProperty(org.apache.nifi.processors.standard.GetHTTP.FILENAME, "test_${now():format('yyyy/MM/dd_HH:mm:ss')}");
        controller.setProperty(org.apache.nifi.processors.standard.GetHTTP.ACCEPT_CONTENT_TYPE, "application/json");
        controller.setProperty(org.apache.nifi.processors.standard.GetHTTP.USER_AGENT, "testUserAgent");

        controller.run();
        controller.assertTransferCount(org.apache.nifi.processors.standard.GetHTTP.REL_SUCCESS, 1);

        MockFlowFile response = controller.getFlowFilesForRelationship(org.apache.nifi.processors.standard.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();
    }
}
 
Example 12
Source File: TestGetHTTP.java    From nifi with Apache License 2.0 4 votes vote down vote up
/**
 * 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(org.apache.nifi.processors.standard.GetHTTP.class);
        this.controller.setProperty(org.apache.nifi.processors.standard.GetHTTP.CONNECTION_TIMEOUT, "5 secs");
        this.controller.setProperty(org.apache.nifi.processors.standard.GetHTTP.URL, destination+"/test_${literal(1)}.pdf");
        this.controller.setProperty(org.apache.nifi.processors.standard.GetHTTP.FILENAME, "test_${now():format('yyyy/MM/dd_HH:mm:ss')}");
        this.controller.setProperty(org.apache.nifi.processors.standard.GetHTTP.ACCEPT_CONTENT_TYPE, "application/json");
        this.controller.setProperty(org.apache.nifi.processors.standard.GetHTTP.USER_AGENT, "testUserAgent");

        // 204 - NO CONTENT
        servlet.setErrorToReturn(HttpServletResponse.SC_NO_CONTENT);
        this.controller.run();
        this.controller.assertTransferCount(org.apache.nifi.processors.standard.GetHTTP.REL_SUCCESS, 0);

        // 404 - NOT FOUND
        servlet.setErrorToReturn(HttpServletResponse.SC_NOT_FOUND);
        this.controller.run();
        this.controller.assertTransferCount(org.apache.nifi.processors.standard.GetHTTP.REL_SUCCESS, 0);

        // 500 - INTERNAL SERVER ERROR
        servlet.setErrorToReturn(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        this.controller.run();
        this.controller.assertTransferCount(org.apache.nifi.processors.standard.GetHTTP.REL_SUCCESS, 0);
    } finally {
        // shutdown web service
        server.shutdownServer();
    }
}
 
Example 13
Source File: TestPostHTTP.java    From nifi with Apache License 2.0 4 votes vote down vote up
@Test
public void testBatchWithMultipleUrls() throws Exception {
    CaptureServlet servletA, servletB;
    TestServer serverA, serverB;

    { // setup test servers
        setup(null);
        servletA = servlet;
        serverA = server;

        // set up second web service
        ServletHandler handler = new ServletHandler();
        handler.addServletWithMapping(CaptureServlet.class, "/*");

        // create the second service
        serverB = new TestServer(null);
        serverB.addHandler(handler);
        serverB.startServer();

        servletB = (CaptureServlet) handler.getServlets()[0].getServlet();
    }

    runner.setProperty(org.apache.nifi.processors.standard.PostHTTP.URL, "${url}"); // use EL for the URL
    runner.setProperty(org.apache.nifi.processors.standard.PostHTTP.SEND_AS_FLOWFILE, "true");
    runner.setProperty(org.apache.nifi.processors.standard.PostHTTP.MAX_BATCH_SIZE, "10 b");

    Set<String> expectedContentA = new HashSet<>();
    Set<String> expectedContentB = new HashSet<>();

    Set<String> actualContentA = new HashSet<>();
    Set<String> actualContentB = new HashSet<>();

    // enqueue 9 FlowFiles
    for (int i = 0; i < 9; i++) {
        enqueueWithURL("a" + i, serverA.getUrl());
        enqueueWithURL("b" + i, serverB.getUrl());

        expectedContentA.add("a" + i);
        expectedContentB.add("b" + i);
    }

    // MAX_BATCH_SIZE is 10 bytes, each file is 2 bytes, so 18 files should produce 4 batches
    for (int i = 0; i < 4; i++) {
        runner.run(1);
        runner.assertAllFlowFilesTransferred(org.apache.nifi.processors.standard.PostHTTP.REL_SUCCESS);
        final List<MockFlowFile> successFiles = runner.getFlowFilesForRelationship(org.apache.nifi.processors.standard.PostHTTP.REL_SUCCESS);
        assertFalse(successFiles.isEmpty());

        MockFlowFile mff = successFiles.get(0);
        final String urlAttr = mff.getAttribute("url");

        if (serverA.getUrl().equals(urlAttr)) {
            checkBatch(serverA, servletA, actualContentA, (actualContentA.isEmpty() ? 5 : 4));
        } else if (serverB.getUrl().equals(urlAttr)) {
            checkBatch(serverB, servletB, actualContentB, (actualContentB.isEmpty() ? 5 : 4));
        } else {
            fail("unexpected url attribute");
        }
    }

    assertEquals(expectedContentA, actualContentA);
    assertEquals(expectedContentB, actualContentB);

    // make sure everything transferred, nothing more to do
    runner.run(1);
    runner.assertAllFlowFilesTransferred(org.apache.nifi.processors.standard.PostHTTP.REL_SUCCESS, 0);
}
 
Example 14
Source File: TestGetHTTP.java    From localization_nifi with Apache License 2.0 4 votes vote down vote up
@Test
public final void testContentModifiedTwoServers() throws Exception {
    // set up web services
    ServletHandler handler1 = new ServletHandler();
    handler1.addServletWithMapping(RESTServiceContentModified.class, "/*");

    ServletHandler handler2 = new ServletHandler();
    handler2.addServletWithMapping(RESTServiceContentModified.class, "/*");

    // create the services
    TestServer server1 = new TestServer();
    server1.addHandler(handler1);

    TestServer server2 = new TestServer();
    server2.addHandler(handler2);

    try {
        server1.startServer();
        server2.startServer();

        // this is the base urls with the random ports
        String destination1 = server1.getUrl();
        String destination2 = server2.getUrl();

        // set up NiFi mock controller
        controller = TestRunners.newTestRunner(GetHTTP.class);
        controller.setProperty(GetHTTP.CONNECTION_TIMEOUT, "5 secs");
        controller.setProperty(GetHTTP.URL, destination1);
        controller.setProperty(GetHTTP.FILENAME, "testFile");
        controller.setProperty(GetHTTP.ACCEPT_CONTENT_TYPE, "application/json");

        controller.getStateManager().assertStateNotSet(GetHTTP.ETAG+":"+destination1, Scope.LOCAL);
        controller.getStateManager().assertStateNotSet(GetHTTP.LAST_MODIFIED+":"+destination1, Scope.LOCAL);
        controller.run(2);

        // verify the lastModified and entityTag are updated
        controller.getStateManager().assertStateNotEquals(GetHTTP.ETAG+":"+destination1, "", Scope.LOCAL);
        controller.getStateManager().assertStateNotEquals(GetHTTP.LAST_MODIFIED+":"+destination1, "Thu, 01 Jan 1970 00:00:00 GMT", Scope.LOCAL);

        // ran twice, but got one...which is good
        controller.assertTransferCount(GetHTTP.REL_SUCCESS, 1);

        controller.clearTransferState();

        controller.setProperty(GetHTTP.URL, destination2);
        controller.getStateManager().assertStateNotSet(GetHTTP.ETAG+":"+destination2, Scope.LOCAL);
        controller.getStateManager().assertStateNotSet(GetHTTP.LAST_MODIFIED+":"+destination2, Scope.LOCAL);

        controller.run(2);

        // ran twice, but got one...which is good
        controller.assertTransferCount(GetHTTP.REL_SUCCESS, 1);

        // verify the lastModified's and entityTags are updated
        controller.getStateManager().assertStateNotEquals(GetHTTP.ETAG+":"+destination1, "", Scope.LOCAL);
        controller.getStateManager().assertStateNotEquals(GetHTTP.LAST_MODIFIED+":"+destination1, "Thu, 01 Jan 1970 00:00:00 GMT", Scope.LOCAL);
        controller.getStateManager().assertStateNotEquals(GetHTTP.ETAG+":"+destination2, "", Scope.LOCAL);
        controller.getStateManager().assertStateNotEquals(GetHTTP.LAST_MODIFIED+":"+destination2, "Thu, 01 Jan 1970 00:00:00 GMT", Scope.LOCAL);

    } finally {
        // shutdown web services
        server1.shutdownServer();
        server2.shutdownServer();
    }
}
 
Example 15
Source File: JettyServer.java    From tutorials with MIT License 4 votes vote down vote up
void start() throws Exception {

        int maxThreads = 100;
        int minThreads = 10;
        int idleTimeout = 120;

        QueuedThreadPool threadPool = new QueuedThreadPool(maxThreads, minThreads, idleTimeout);

        server = new Server(threadPool);
        ServerConnector connector = new ServerConnector(server);
        connector.setPort(8090);
        server.setConnectors(new Connector[] { connector });

        ServletHandler servletHandler = new ServletHandler();
        server.setHandler(servletHandler);

        servletHandler.addServletWithMapping(BlockingServlet.class, "/status");
        servletHandler.addServletWithMapping(AsyncServlet.class, "/heavy/async");

        server.start();

    }
 
Example 16
Source File: ZMSClientTimeoutTest.java    From athenz with Apache License 2.0 4 votes vote down vote up
public void start() throws Exception {

            // Create a simple embedded jetty server on a given port

            server = new Server(port);

            // Define a raw servlet that does nothing but sleep for a configured
            // number of seconds so we get a read timeout from the client

            ServletHandler handler = new ServletHandler();
            server.setHandler(handler);
            handler.addServletWithMapping(SimpleServlet.class, "/*");

            // start our jetty server

            server.start();
        }
 
Example 17
Source File: JettyWebServer.java    From Doradus with Apache License 2.0 4 votes vote down vote up
private ServletHandler configureHandler(String servletClassName) {
    ServletHandler handler = new ServletHandler();
    handler.addServletWithMapping(servletClassName, "/*");
    return handler;
}
 
Example 18
Source File: TestHttpClient.java    From nifi with Apache License 2.0 4 votes vote down vote up
@BeforeClass
public static void setup() throws Exception {
    // Create embedded Jetty server
    // Use less threads to mitigate Gateway Timeout (504) with proxy test
    // Minimum thread pool size = (acceptors=2 + selectors=8 + request=1), defaults to max=200
    final QueuedThreadPool threadPool = new QueuedThreadPool(50);
    server = new Server(threadPool);

    final ContextHandlerCollection handlerCollection = new ContextHandlerCollection();

    final ServletContextHandler contextHandler = new ServletContextHandler();
    contextHandler.setContextPath("/nifi-api");

    final ServletContextHandler wrongPathContextHandler = new ServletContextHandler();
    wrongPathContextHandler.setContextPath("/wrong/nifi-api");

    handlerCollection.setHandlers(new Handler[]{contextHandler, wrongPathContextHandler});

    server.setHandler(handlerCollection);

    final ServletHandler servletHandler = new ServletHandler();
    contextHandler.insertHandler(servletHandler);

    final ServletHandler wrongPathServletHandler = new ServletHandler();
    wrongPathContextHandler.insertHandler(wrongPathServletHandler);

    final SslContextFactory sslContextFactory = new SslContextFactory();
    sslContextFactory.setKeyStorePath("src/test/resources/certs/keystore.jks");
    sslContextFactory.setKeyStorePassword("passwordpassword");
    sslContextFactory.setKeyStoreType("JKS");
    sslContextFactory.setProtocol(CertificateUtils.getHighestCurrentSupportedTlsProtocolVersion());
    sslContextFactory.setExcludeProtocols("TLS", "TLSv1", "TLSv1.1");

    httpConnector = new ServerConnector(server);

    final HttpConfiguration https = new HttpConfiguration();
    https.addCustomizer(new SecureRequestCustomizer());
    sslConnector = new ServerConnector(server,
            new SslConnectionFactory(sslContextFactory, "http/1.1"),
            new HttpConnectionFactory(https));
    logger.info("SSL Connector: " + sslConnector.dump());

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

    wrongPathServletHandler.addServletWithMapping(WrongSiteInfoServlet.class, "/site-to-site");

    servletHandler.addServletWithMapping(SiteInfoServlet.class, "/site-to-site");
    servletHandler.addServletWithMapping(PeersServlet.class, "/site-to-site/peers");

    servletHandler.addServletWithMapping(PortTransactionsAccessDeniedServlet.class, "/data-transfer/input-ports/input-access-denied-id/transactions");
    servletHandler.addServletWithMapping(PortTransactionsServlet.class, "/data-transfer/input-ports/input-running-id/transactions");
    servletHandler.addServletWithMapping(InputPortTransactionServlet.class, "/data-transfer/input-ports/input-running-id/transactions/transaction-id");
    servletHandler.addServletWithMapping(FlowFilesServlet.class, "/data-transfer/input-ports/input-running-id/transactions/transaction-id/flow-files");

    servletHandler.addServletWithMapping(PortTransactionsServlet.class, "/data-transfer/input-ports/input-timeout-id/transactions");
    servletHandler.addServletWithMapping(InputPortTransactionServlet.class, "/data-transfer/input-ports/input-timeout-id/transactions/transaction-id");
    servletHandler.addServletWithMapping(FlowFilesTimeoutServlet.class, "/data-transfer/input-ports/input-timeout-id/transactions/transaction-id/flow-files");

    servletHandler.addServletWithMapping(PortTransactionsServlet.class, "/data-transfer/input-ports/input-timeout-data-ex-id/transactions");
    servletHandler.addServletWithMapping(InputPortTransactionServlet.class, "/data-transfer/input-ports/input-timeout-data-ex-id/transactions/transaction-id");
    servletHandler.addServletWithMapping(FlowFilesTimeoutAfterDataExchangeServlet.class, "/data-transfer/input-ports/input-timeout-data-ex-id/transactions/transaction-id/flow-files");

    servletHandler.addServletWithMapping(PortTransactionsServlet.class, "/data-transfer/output-ports/output-running-id/transactions");
    servletHandler.addServletWithMapping(OutputPortTransactionServlet.class, "/data-transfer/output-ports/output-running-id/transactions/transaction-id");
    servletHandler.addServletWithMapping(FlowFilesServlet.class, "/data-transfer/output-ports/output-running-id/transactions/transaction-id/flow-files");

    servletHandler.addServletWithMapping(PortTransactionsServlet.class, "/data-transfer/output-ports/output-timeout-id/transactions");
    servletHandler.addServletWithMapping(OutputPortTransactionServlet.class, "/data-transfer/output-ports/output-timeout-id/transactions/transaction-id");
    servletHandler.addServletWithMapping(FlowFilesTimeoutServlet.class, "/data-transfer/output-ports/output-timeout-id/transactions/transaction-id/flow-files");

    servletHandler.addServletWithMapping(PortTransactionsServlet.class, "/data-transfer/output-ports/output-timeout-data-ex-id/transactions");
    servletHandler.addServletWithMapping(OutputPortTransactionServlet.class, "/data-transfer/output-ports/output-timeout-data-ex-id/transactions/transaction-id");
    servletHandler.addServletWithMapping(FlowFilesTimeoutAfterDataExchangeServlet.class, "/data-transfer/output-ports/output-timeout-data-ex-id/transactions/transaction-id/flow-files");

    server.start();

    logger.info("Starting server on port {} for HTTP, and {} for HTTPS", httpConnector.getLocalPort(), sslConnector.getLocalPort());

    startProxyServer();
    startProxyServerWithAuth();
}
 
Example 19
Source File: TestGetHTTP.java    From nifi with Apache License 2.0 4 votes vote down vote up
@Test
public final void testContentModified() throws Exception {
    // set up web service
    ServletHandler handler = new ServletHandler();
    handler.addServletWithMapping(RESTServiceContentModified.class, "/*");

    // create the service
    TestServer server = new TestServer();
    server.addHandler(handler);

    try {
        server.startServer();

        // this is the base url with the random port
        String destination = server.getUrl();

        // set up NiFi mock controller
        controller = TestRunners.newTestRunner(org.apache.nifi.processors.standard.GetHTTP.class);
        controller.setProperty(org.apache.nifi.processors.standard.GetHTTP.CONNECTION_TIMEOUT, "5 secs");
        controller.setProperty(org.apache.nifi.processors.standard.GetHTTP.URL, destination);
        controller.setProperty(org.apache.nifi.processors.standard.GetHTTP.FILENAME, "testFile");
        controller.setProperty(org.apache.nifi.processors.standard.GetHTTP.ACCEPT_CONTENT_TYPE, "application/json");

        controller.getStateManager().assertStateNotSet(org.apache.nifi.processors.standard.GetHTTP.ETAG, Scope.LOCAL);
        controller.getStateManager().assertStateNotSet(org.apache.nifi.processors.standard.GetHTTP.LAST_MODIFIED, Scope.LOCAL);
        controller.run(2);

        // verify the lastModified and entityTag are updated
        controller.getStateManager().assertStateNotEquals(org.apache.nifi.processors.standard.GetHTTP.ETAG+":"+destination, "", Scope.LOCAL);
        controller.getStateManager().assertStateNotEquals(org.apache.nifi.processors.standard.GetHTTP.LAST_MODIFIED+":"+destination, "Thu, 01 Jan 1970 00:00:00 GMT", Scope.LOCAL);

        // ran twice, but got one...which is good
        controller.assertTransferCount(org.apache.nifi.processors.standard.GetHTTP.REL_SUCCESS, 1);

        // verify remote.source flowfile attribute
        controller.getFlowFilesForRelationship(org.apache.nifi.processors.standard.GetHTTP.REL_SUCCESS).get(0).assertAttributeEquals("gethttp.remote.source", "localhost");
        controller.clearTransferState();

        // turn off checking for etag and lastModified
        RESTServiceContentModified.IGNORE_ETAG = true;
        RESTServiceContentModified.IGNORE_LAST_MODIFIED = true;
        controller.run(2);

        // ran twice, got two...which is good
        controller.assertTransferCount(org.apache.nifi.processors.standard.GetHTTP.REL_SUCCESS, 2);
        controller.clearTransferState();

        // turn on checking for etag
        RESTServiceContentModified.IGNORE_ETAG = false;
        controller.run(2);
        // ran twice, got 0...which is good
        controller.assertTransferCount(org.apache.nifi.processors.standard.GetHTTP.REL_SUCCESS, 0);

        // turn on checking for lastModified, but off for etag
        RESTServiceContentModified.IGNORE_LAST_MODIFIED = false;
        RESTServiceContentModified.IGNORE_ETAG = true;
        controller.run(2);
        // ran twice, got 0...which is good
        controller.assertTransferCount(org.apache.nifi.processors.standard.GetHTTP.REL_SUCCESS, 0);

        // turn off checking for lastModified, turn on checking for etag, but change the value
        RESTServiceContentModified.IGNORE_LAST_MODIFIED = true;
        RESTServiceContentModified.IGNORE_ETAG = false;
        RESTServiceContentModified.ETAG = 1;
        controller.run(2);

        // ran twice, got 1...but should have new cached etag
        controller.assertTransferCount(org.apache.nifi.processors.standard.GetHTTP.REL_SUCCESS, 1);
        String eTagStateValue = controller.getStateManager().getState(Scope.LOCAL).get(org.apache.nifi.processors.standard.GetHTTP.ETAG+":"+destination);
        assertEquals("1",org.apache.nifi.processors.standard.GetHTTP.parseStateValue(eTagStateValue).getValue());
        controller.clearTransferState();

        // turn off checking for Etag, turn on checking for lastModified, but change value
        RESTServiceContentModified.IGNORE_LAST_MODIFIED = false;
        RESTServiceContentModified.IGNORE_ETAG = true;
        RESTServiceContentModified.modificationDate = System.currentTimeMillis() / 1000 * 1000 + 5000;
        String lastMod = controller.getStateManager().getState(Scope.LOCAL).get(org.apache.nifi.processors.standard.GetHTTP.LAST_MODIFIED+":"+destination);
        controller.run(2);

        // ran twice, got 1...but should have new cached etag
        controller.assertTransferCount(org.apache.nifi.processors.standard.GetHTTP.REL_SUCCESS, 1);
        controller.getStateManager().assertStateNotEquals(org.apache.nifi.processors.standard.GetHTTP.LAST_MODIFIED+":"+destination, lastMod, Scope.LOCAL);
        controller.clearTransferState();

    } finally {
        // shutdown web service
        server.shutdownServer();
    }
}
 
Example 20
Source File: TestGetHTTP.java    From nifi with Apache License 2.0 4 votes vote down vote up
@Test
public final void testContentModifiedTwoServers() throws Exception {
    // set up web services
    ServletHandler handler1 = new ServletHandler();
    handler1.addServletWithMapping(RESTServiceContentModified.class, "/*");

    ServletHandler handler2 = new ServletHandler();
    handler2.addServletWithMapping(RESTServiceContentModified.class, "/*");

    // create the services
    TestServer server1 = new TestServer();
    server1.addHandler(handler1);

    TestServer server2 = new TestServer();
    server2.addHandler(handler2);

    try {
        server1.startServer();
        server2.startServer();

        // this is the base urls with the random ports
        String destination1 = server1.getUrl();
        String destination2 = server2.getUrl();

        // set up NiFi mock controller
        controller = TestRunners.newTestRunner(org.apache.nifi.processors.standard.GetHTTP.class);
        controller.setProperty(org.apache.nifi.processors.standard.GetHTTP.CONNECTION_TIMEOUT, "5 secs");
        controller.setProperty(org.apache.nifi.processors.standard.GetHTTP.URL, destination1);
        controller.setProperty(org.apache.nifi.processors.standard.GetHTTP.FILENAME, "testFile");
        controller.setProperty(org.apache.nifi.processors.standard.GetHTTP.ACCEPT_CONTENT_TYPE, "application/json");

        controller.getStateManager().assertStateNotSet(org.apache.nifi.processors.standard.GetHTTP.ETAG+":"+destination1, Scope.LOCAL);
        controller.getStateManager().assertStateNotSet(org.apache.nifi.processors.standard.GetHTTP.LAST_MODIFIED+":"+destination1, Scope.LOCAL);
        controller.run(2);

        // verify the lastModified and entityTag are updated
        controller.getStateManager().assertStateNotEquals(org.apache.nifi.processors.standard.GetHTTP.ETAG+":"+destination1, "", Scope.LOCAL);
        controller.getStateManager().assertStateNotEquals(org.apache.nifi.processors.standard.GetHTTP.LAST_MODIFIED+":"+destination1, "Thu, 01 Jan 1970 00:00:00 GMT", Scope.LOCAL);

        // ran twice, but got one...which is good
        controller.assertTransferCount(org.apache.nifi.processors.standard.GetHTTP.REL_SUCCESS, 1);

        controller.clearTransferState();

        controller.setProperty(org.apache.nifi.processors.standard.GetHTTP.URL, destination2);
        controller.getStateManager().assertStateNotSet(org.apache.nifi.processors.standard.GetHTTP.ETAG+":"+destination2, Scope.LOCAL);
        controller.getStateManager().assertStateNotSet(org.apache.nifi.processors.standard.GetHTTP.LAST_MODIFIED+":"+destination2, Scope.LOCAL);

        controller.run(2);

        // ran twice, but got one...which is good
        controller.assertTransferCount(org.apache.nifi.processors.standard.GetHTTP.REL_SUCCESS, 1);

        // verify the lastModified's and entityTags are updated
        controller.getStateManager().assertStateNotEquals(org.apache.nifi.processors.standard.GetHTTP.ETAG+":"+destination1, "", Scope.LOCAL);
        controller.getStateManager().assertStateNotEquals(org.apache.nifi.processors.standard.GetHTTP.LAST_MODIFIED+":"+destination1, "Thu, 01 Jan 1970 00:00:00 GMT", Scope.LOCAL);
        controller.getStateManager().assertStateNotEquals(org.apache.nifi.processors.standard.GetHTTP.ETAG+":"+destination2, "", Scope.LOCAL);
        controller.getStateManager().assertStateNotEquals(org.apache.nifi.processors.standard.GetHTTP.LAST_MODIFIED+":"+destination2, "Thu, 01 Jan 1970 00:00:00 GMT", Scope.LOCAL);

    } finally {
        // shutdown web services
        server1.shutdownServer();
        server2.shutdownServer();
    }
}