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();
}
}
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();
}
}
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();
}
}
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);
}
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());
}
Aggregations