Java Code Examples for org.eclipse.jetty.server.Server#getConnectors()
The following examples show how to use
org.eclipse.jetty.server.Server#getConnectors() .
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: AthenzJettyContainerTest.java From athenz with Apache License 2.0 | 6 votes |
@Test public void testInitContainerOnlyHTTPSPort() { System.setProperty(AthenzConsts.ATHENZ_PROP_HTTP_PORT, "0"); System.setProperty(AthenzConsts.ATHENZ_PROP_HTTPS_PORT, "4443"); System.setProperty("yahoo.zms.debug.user_authority", "true"); AthenzJettyContainer container = AthenzJettyContainer.createJettyContainer(); assertNotNull(container); Server server = container.getServer(); Connector[] connectors = server.getConnectors(); assertEquals(connectors.length, 1); assertTrue(connectors[0].getProtocols().contains("http/1.1")); assertTrue(connectors[0].getProtocols().contains("ssl")); }
Example 2
Source File: AthenzJettyContainerTest.java From athenz with Apache License 2.0 | 6 votes |
@Test public void testInitContainerValidPorts() { System.setProperty(AthenzConsts.ATHENZ_PROP_HTTP_PORT, "4080"); System.setProperty(AthenzConsts.ATHENZ_PROP_HTTPS_PORT, "4443"); AthenzJettyContainer container = AthenzJettyContainer.createJettyContainer(); assertNotNull(container); Server server = container.getServer(); Connector[] connectors = server.getConnectors(); assertEquals(connectors.length, 2); assertTrue(connectors[0].getProtocols().contains("http/1.1")); assertTrue(connectors[1].getProtocols().contains("http/1.1")); assertTrue(connectors[1].getProtocols().contains("ssl")); }
Example 3
Source File: AthenzJettyContainerTest.java From athenz with Apache License 2.0 | 6 votes |
@Test public void testHttpConnectorsHttpOnly() { System.setProperty(AthenzConsts.ATHENZ_PROP_KEYSTORE_PATH, "file:///tmp/keystore"); System.setProperty(AthenzConsts.ATHENZ_PROP_KEYSTORE_TYPE, "PKCS12"); System.setProperty(AthenzConsts.ATHENZ_PROP_KEYSTORE_PASSWORD, "pass123"); System.setProperty(AthenzConsts.ATHENZ_PROP_TRUSTSTORE_PATH, "file:///tmp/truststore"); System.setProperty(AthenzConsts.ATHENZ_PROP_TRUSTSTORE_TYPE, "PKCS12"); System.setProperty(AthenzConsts.ATHENZ_PROP_TRUSTSTORE_PASSWORD, "pass123"); System.setProperty(AthenzConsts.ATHENZ_PROP_KEYMANAGER_PASSWORD, "pass123"); System.setProperty(AthenzConsts.ATHENZ_PROP_IDLE_TIMEOUT, "10001"); AthenzJettyContainer container = new AthenzJettyContainer(); container.createServer(100); HttpConfiguration httpConfig = container.newHttpConfiguration(); container.addHTTPConnectors(httpConfig, 8081, 0, 0); Server server = container.getServer(); Connector[] connectors = server.getConnectors(); assertEquals(connectors.length, 1); assertEquals(connectors[0].getIdleTimeout(), 10001); assertTrue(connectors[0].getProtocols().contains("http/1.1")); assertFalse(connectors[0].getProtocols().contains("ssl")); }
Example 4
Source File: JettyPlusIT.java From uavstack with Apache License 2.0 | 6 votes |
/** * startUAVServer */ public void startServer(Object... args) { Server server = (Server) args[0]; // integrate Tomcat log UAVServer.instance().setLog(new JettyLog("MonitorServer")); // start Monitor Server when server starts UAVServer.instance().start(new Object[] { UAVServer.ServerVendor.JETTY }); // get server port if (UAVServer.instance().getServerInfo(CaptureConstants.INFO_APPSERVER_LISTEN_PORT) == null) { // set port ServerConnector sc = (ServerConnector) server.getConnectors()[0]; String protocol = sc.getDefaultProtocol(); if (protocol.toLowerCase().indexOf("http") >= 0) { UAVServer.instance().putServerInfo(CaptureConstants.INFO_APPSERVER_LISTEN_PORT, sc.getPort()); } } }
Example 5
Source File: HttpServer.java From heroic with Apache License 2.0 | 6 votes |
private ServerConnector findServerConnector(Server server) { final Connector[] connectors = server.getConnectors(); if (connectors.length == 0) { throw new IllegalStateException("server has no connectors"); } for (final Connector c : connectors) { if (!(c instanceof ServerConnector)) { continue; } return (ServerConnector) c; } throw new IllegalStateException("Server has no associated ServerConnector"); }
Example 6
Source File: AthenzJettyContainerTest.java From athenz with Apache License 2.0 | 6 votes |
@Test public void testInitContainerInvalidHTTPPort() { System.setProperty(AthenzConsts.ATHENZ_PROP_HTTP_PORT, "-10"); System.setProperty(AthenzConsts.ATHENZ_PROP_HTTPS_PORT, "4443"); AthenzJettyContainer container = AthenzJettyContainer.createJettyContainer(); assertNotNull(container); Server server = container.getServer(); Connector[] connectors = server.getConnectors(); assertEquals(connectors.length, 2); assertTrue(connectors[0].getProtocols().contains("http/1.1")); assertTrue(connectors[1].getProtocols().contains("http/1.1")); assertTrue(connectors[1].getProtocols().contains("ssl")); }
Example 7
Source File: CuratorAdvertisementListener.java From snowizard-discovery with BSD 3-Clause "New" or "Revised" License | 6 votes |
@Override public void serverStarted(final Server server) { // Detect the port Jetty is listening on - works with configured and // random port for (final Connector connector : server.getConnectors()) { if (APPLICATION_CONNECTOR.equals(connector.getName())) { final ServerSocketChannel channel = (ServerSocketChannel) connector .getTransport(); try { final InetSocketAddress socket = (InetSocketAddress) channel .getLocalAddress(); advertiser.initListenInfo(socket.getPort()); advertiser.registerAvailability(); return; } catch (final Exception e) { LOGGER.error("Unable to register service in ZK", e); } } } }
Example 8
Source File: ThriftOverHttpClientTServletIntegrationTest.java From armeria with Apache License 2.0 | 6 votes |
private static Server startHttp1() throws Exception { final Server server = new Server(0); final ServletHandler handler = new ServletHandler(); handler.addServletWithMapping(newServletHolder(thriftServlet), TSERVLET_PATH); handler.addServletWithMapping(newServletHolder(rootServlet), "/"); handler.addFilterWithMapping(new FilterHolder(new ConnectionCloseFilter()), "/*", EnumSet.of(DispatcherType.REQUEST)); server.setHandler(handler); for (Connector c : server.getConnectors()) { for (ConnectionFactory f : c.getConnectionFactories()) { for (String p : f.getProtocols()) { if (p.startsWith("h2c")) { fail("Attempted to create a Jetty server without HTTP/2 support, but failed: " + f.getProtocols()); } } } } server.start(); return server; }
Example 9
Source File: AthenzJettyContainerTest.java From athenz with Apache License 2.0 | 6 votes |
@Test public void testInitContainerInvalidHTTPSPort() { System.setProperty(AthenzConsts.ATHENZ_PROP_HTTP_PORT, "4080"); System.setProperty(AthenzConsts.ATHENZ_PROP_HTTPS_PORT, "-10"); AthenzJettyContainer container = AthenzJettyContainer.createJettyContainer(); assertNotNull(container); Server server = container.getServer(); Connector[] connectors = server.getConnectors(); assertEquals(connectors.length, 2); assertTrue(connectors[0].getProtocols().contains("http/1.1")); assertTrue(connectors[1].getProtocols().contains("http/1.1")); assertTrue(connectors[1].getProtocols().contains("ssl")); }
Example 10
Source File: RequestPartIntegrationTests.java From java-technology-stack with MIT License | 5 votes |
@BeforeClass public static void startServer() throws Exception { // Let server pick its own random, available port. server = new Server(0); ServletContextHandler handler = new ServletContextHandler(); handler.setContextPath("/"); Class<?> config = CommonsMultipartResolverTestConfig.class; ServletHolder commonsResolverServlet = new ServletHolder(DispatcherServlet.class); commonsResolverServlet.setInitParameter("contextConfigLocation", config.getName()); commonsResolverServlet.setInitParameter("contextClass", AnnotationConfigWebApplicationContext.class.getName()); handler.addServlet(commonsResolverServlet, "/commons-resolver/*"); config = StandardMultipartResolverTestConfig.class; ServletHolder standardResolverServlet = new ServletHolder(DispatcherServlet.class); standardResolverServlet.setInitParameter("contextConfigLocation", config.getName()); standardResolverServlet.setInitParameter("contextClass", AnnotationConfigWebApplicationContext.class.getName()); standardResolverServlet.getRegistration().setMultipartConfig(new MultipartConfigElement("")); handler.addServlet(standardResolverServlet, "/standard-resolver/*"); server.setHandler(handler); server.start(); Connector[] connectors = server.getConnectors(); NetworkConnector connector = (NetworkConnector) connectors[0]; baseUrl = "http://localhost:" + connector.getLocalPort(); }
Example 11
Source File: AthenzJettyContainerTest.java From athenz with Apache License 2.0 | 5 votes |
@Test public void testHttpConnectorsBoth() { System.setProperty(AthenzConsts.ATHENZ_PROP_KEYSTORE_PATH, "/tmp/keystore"); System.setProperty(AthenzConsts.ATHENZ_PROP_KEYSTORE_TYPE, "PKCS12"); System.setProperty(AthenzConsts.ATHENZ_PROP_KEYSTORE_PASSWORD, "pass123"); System.setProperty(AthenzConsts.ATHENZ_PROP_TRUSTSTORE_PATH, "/tmp/truststore"); System.setProperty(AthenzConsts.ATHENZ_PROP_TRUSTSTORE_TYPE, "PKCS12"); System.setProperty(AthenzConsts.ATHENZ_PROP_TRUSTSTORE_PASSWORD, "pass123"); System.setProperty(AthenzConsts.ATHENZ_PROP_KEYMANAGER_PASSWORD, "pass123"); System.setProperty(AthenzConsts.ATHENZ_PROP_IDLE_TIMEOUT, "10001"); AthenzJettyContainer container = new AthenzJettyContainer(); container.createServer(100); HttpConfiguration httpConfig = container.newHttpConfiguration(); container.addHTTPConnectors(httpConfig, 8081, 8082, 0); Server server = container.getServer(); Connector[] connectors = server.getConnectors(); assertEquals(connectors.length, 2); assertEquals(connectors[0].getIdleTimeout(), 10001); assertTrue(connectors[0].getProtocols().contains("http/1.1")); assertTrue(connectors[1].getProtocols().contains("http/1.1")); assertTrue(connectors[1].getProtocols().contains("ssl")); }
Example 12
Source File: JettyUtil.java From warp10-platform with Apache License 2.0 | 5 votes |
public static void setSendServerVersion(Server server, boolean send) { // // Remove display of Server header // @see http://stackoverflow.com/questions/15652902/remove-the-http-server-header-in-jetty-9 // for(Connector y : server.getConnectors()) { for(ConnectionFactory x : y.getConnectionFactories()) { if(x instanceof HttpConnectionFactory) { ((HttpConnectionFactory)x).getHttpConfiguration().setSendServerVersion(send); } } } }
Example 13
Source File: ConsulServiceListener.java From dropwizard-consul with Apache License 2.0 | 5 votes |
@Override public void serverStarted(final Server server) { String applicationScheme = null; int applicationPort = -1; int adminPort = -1; for (Connector connector : server.getConnectors()) { @SuppressWarnings("resource") final ServerConnector serverConnector = (ServerConnector) connector; if (APPLICATION_NAME.equals(connector.getName())) { applicationPort = serverConnector.getLocalPort(); applicationScheme = getScheme(connector.getProtocols()); } else if (ADMIN_NAME.equals(connector.getName())) { adminPort = serverConnector.getLocalPort(); } else { applicationPort = serverConnector.getLocalPort(); applicationScheme = getScheme(connector.getProtocols()); adminPort = applicationPort; } } LOGGER.debug( "applicationScheme: {}, applicationPort: {}, adminPort: {}", applicationScheme, applicationPort, adminPort); register(applicationScheme, applicationPort, adminPort); }
Example 14
Source File: RequestPartIntegrationTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@BeforeClass public static void startServer() throws Exception { // Let server pick its own random, available port. server = new Server(0); ServletContextHandler handler = new ServletContextHandler(); handler.setContextPath("/"); Class<?> config = CommonsMultipartResolverTestConfig.class; ServletHolder commonsResolverServlet = new ServletHolder(DispatcherServlet.class); commonsResolverServlet.setInitParameter("contextConfigLocation", config.getName()); commonsResolverServlet.setInitParameter("contextClass", AnnotationConfigWebApplicationContext.class.getName()); handler.addServlet(commonsResolverServlet, "/commons-resolver/*"); config = StandardMultipartResolverTestConfig.class; ServletHolder standardResolverServlet = new ServletHolder(DispatcherServlet.class); standardResolverServlet.setInitParameter("contextConfigLocation", config.getName()); standardResolverServlet.setInitParameter("contextClass", AnnotationConfigWebApplicationContext.class.getName()); standardResolverServlet.getRegistration().setMultipartConfig(new MultipartConfigElement("")); handler.addServlet(standardResolverServlet, "/standard-resolver/*"); server.setHandler(handler); server.start(); Connector[] connectors = server.getConnectors(); NetworkConnector connector = (NetworkConnector) connectors[0]; baseUrl = "http://localhost:" + connector.getLocalPort(); }
Example 15
Source File: GerritRestClientTest.java From gerrit-rest-java-client with Apache License 2.0 | 5 votes |
public String startJetty(Class<? extends HttpServlet> loginServletClass) throws Exception { Server server = new Server(0); ResourceHandler resourceHandler = new ResourceHandler(); MimeTypes mimeTypes = new MimeTypes(); mimeTypes.addMimeMapping("json", "application/json"); resourceHandler.setMimeTypes(mimeTypes); URL url = this.getClass().getResource("."); resourceHandler.setBaseResource(new FileResource(url)); resourceHandler.setWelcomeFiles(new String[] {"changes.json", "projects.json", "account.json"}); ServletContextHandler servletContextHandler = new ServletContextHandler(); servletContextHandler.addServlet(loginServletClass, "/login/"); ServletContextHandler basicAuthContextHandler = new ServletContextHandler(ServletContextHandler.SECURITY); basicAuthContextHandler.setSecurityHandler(basicAuth("foo", "bar", "Gerrit Auth")); basicAuthContextHandler.setContextPath("/a"); HandlerCollection handlers = new HandlerCollection(); handlers.setHandlers(new Handler[] { servletContextHandler, resourceHandler, basicAuthContextHandler }); server.setHandler(handlers); server.start(); Connector connector = server.getConnectors()[0]; String host = "localhost"; int port = connector.getLocalPort(); return String.format("http://%s:%s", host, port); }
Example 16
Source File: JettyConnectionMetrics.java From micrometer with Apache License 2.0 | 5 votes |
public static void addToAllConnectors(Server server, MeterRegistry registry, Iterable<Tag> tags) { for (Connector connector : server.getConnectors()) { if (connector != null) { connector.addBean(new JettyConnectionMetrics(registry, tags)); } } }
Example 17
Source File: JettyServer.java From DataLink with Apache License 2.0 | 5 votes |
public void startup() throws Exception { Resource configXml = Resource.newSystemResource(DEFAULT_JETTY_CONFIG); XmlConfiguration configuration = new XmlConfiguration(configXml.getInputStream()); server = (Server) configuration.configure(); Integer port = config.getHttpPort(); if (port != null && port > 0) { Connector[] connectors = server.getConnectors(); for (Connector connector : connectors) { if (connector instanceof AbstractNetworkConnector) { ((AbstractNetworkConnector) connector).setPort(port); } } } Handler handler = server.getHandler(); if (handler != null && handler instanceof WebAppContext) { WebAppContext webAppContext = (WebAppContext) handler; String webAppPath = System.getProperty("webapp.conf"); logger.info("Web App Path is " + webAppPath); if (StringUtils.isBlank(webAppPath)) { webAppContext.setResourceBase(JettyServer.class.getResource("/webapp").toString()); } else { webAppContext.setResourceBase(webAppPath); } } server.start(); if (logger.isInfoEnabled()) { logger.info("##Jetty Embed Server is started."); } }
Example 18
Source File: ApplicationTest.java From attic-apex-malhar with Apache License 2.0 | 4 votes |
/** * Test of getApplication method, of class Application. */ @Test public void testGetApplication() throws Exception { Configuration conf = new Configuration(false); conf.addResource("dt-site-mobile.xml"); Server server = new Server(0); Servlet servlet = new SamplePubSubWebSocketServlet(); ServletHolder sh = new ServletHolder(servlet); ServletContextHandler contextHandler = new ServletContextHandler(server, "/", ServletContextHandler.SESSIONS); contextHandler.addServlet(sh, "/pubsub"); contextHandler.addServlet(sh, "/*"); server.start(); Connector[] connector = server.getConnectors(); conf.set("dt.attr.GATEWAY_CONNECT_ADDRESS", "localhost:" + connector[0].getLocalPort()); URI uri = PubSubHelper.getURI("localhost:" + connector[0].getLocalPort()); PubSubWebSocketOutputOperator<Object> outputOperator = new PubSubWebSocketOutputOperator<Object>(); outputOperator.setUri(uri); outputOperator.setTopic(conf.get("dt.application.MobileExample.operator.QueryLocation.topic")); PubSubWebSocketInputOperator<Map<String, String>> inputOperator = new PubSubWebSocketInputOperator<Map<String, String>>(); inputOperator.setUri(uri); inputOperator.setTopic(conf.get("dt.application.MobileExample.operator.LocationResults.topic")); CollectorTestSink<Object> sink = new CollectorTestSink<Object>(); inputOperator.outputPort.setSink(sink); Map<String, String> data = new HashMap<String, String>(); data.put("command", "add"); data.put("phone", "5559990"); Application app = new Application(); LocalMode lma = LocalMode.newInstance(); lma.prepareDAG(app, conf); LocalMode.Controller lc = lma.getController(); lc.setHeartbeatMonitoringEnabled(false); lc.runAsync(); Thread.sleep(5000); inputOperator.setup(null); outputOperator.setup(null); inputOperator.activate(null); outputOperator.beginWindow(0); outputOperator.input.process(data); outputOperator.endWindow(); inputOperator.beginWindow(0); int timeoutMillis = 5000; while (sink.collectedTuples.size() < 5 && timeoutMillis > 0) { inputOperator.emitTuples(); timeoutMillis -= 20; Thread.sleep(20); } inputOperator.endWindow(); lc.shutdown(); inputOperator.teardown(); outputOperator.teardown(); server.stop(); Assert.assertTrue("size of output is 5 ", sink.collectedTuples.size() == 5); for (Object obj : sink.collectedTuples) { Assert.assertEquals("Expected phone number", "5559990", ((Map<String, String>)obj).get("phone")); } }
Example 19
Source File: JettyManager.java From DataGenerator with Apache License 2.0 | 4 votes |
/** * Prepares a Jetty server for communicating with consumers. */ public void prepareServer() { try { server = new Server(0); jettyHandler = new AbstractHandler() { public void handle(String target, Request req, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/plain"); String[] operands = request.getRequestURI().split("/"); String name = ""; String command = ""; String value = ""; //operands[0] = "", request starts with a "/" if (operands.length >= 2) { name = operands[1]; } if (operands.length >= 3) { command = operands[2]; } if (operands.length >= 4) { value = operands[3]; } if (command.equals("report")) { //report a number of lines written response.getWriter().write(makeReport(name, value)); } else if (command.equals("request") && value.equals("block")) { //request a new block of work response.getWriter().write(requestBlock(name)); } else if (command.equals("request") && value.equals("name")) { //request a new name to report with response.getWriter().write(requestName()); } else { //non recognized response response.getWriter().write("exit"); } ((Request) request).setHandled(true); } }; server.setHandler(jettyHandler); // Select any available port server.start(); Connector[] connectors = server.getConnectors(); NetworkConnector nc = (NetworkConnector) connectors[0]; listeningPort = nc.getLocalPort(); hostName = InetAddress.getLocalHost().getHostName(); } catch (Exception e) { e.printStackTrace(); } }
Example 20
Source File: PubSubWebSocketOperatorTest.java From attic-apex-malhar with Apache License 2.0 | 4 votes |
@Test @SuppressWarnings("SleepWhileInLoop") public void testPubSubWebSocket() throws Exception { Server server = new Server(0); SamplePubSubWebSocketServlet servlet = new SamplePubSubWebSocketServlet(); ServletHolder sh = new ServletHolder(servlet); ServletContextHandler contextHandler = new ServletContextHandler(server, "/", ServletContextHandler.SESSIONS); contextHandler.addServlet(sh, "/pubsub"); contextHandler.addServlet(sh, "/*"); server.start(); Connector[] connector = server.getConnectors(); URI uri = PubSubHelper.getURI("localhost:" + connector[0].getLocalPort()); PubSubWebSocketOutputOperator<Object> outputOperator = new PubSubWebSocketOutputOperator<Object>(); outputOperator.setUri(uri); outputOperator.setTopic("testTopic"); PubSubWebSocketInputOperator<Object> inputOperator = new PubSubWebSocketInputOperator<Object>(); inputOperator.setUri(uri); inputOperator.setTopic("testTopic"); CollectorTestSink<Object> sink = new CollectorTestSink<Object>(); inputOperator.outputPort.setSink(sink); inputOperator.setup(null); outputOperator.setup(null); inputOperator.activate(null); long timeout = System.currentTimeMillis() + 3000; while (!servlet.hasSubscriber()) { Thread.sleep(10); if (System.currentTimeMillis() > timeout) { throw new TimeoutException("No subscribers connected after 3 seconds"); } } inputOperator.beginWindow(1000); outputOperator.beginWindow(1000); Map<String, String> data = new HashMap<String, String>(); data.put("hello", "world"); outputOperator.input.process(data); String stringData = "StringMessage"; outputOperator.input.process(stringData); int timeoutMillis = 2000; while (sink.collectedTuples.size() < 2 && timeoutMillis > 0) { inputOperator.emitTuples(); timeoutMillis -= 20; Thread.sleep(20); } outputOperator.endWindow(); inputOperator.endWindow(); Assert.assertTrue("tuples emitted", sink.collectedTuples.size() > 1); @SuppressWarnings("unchecked") Map<String, String> tuple = (Map<String, String>)sink.collectedTuples.get(0); Assert.assertEquals("Expects {\"hello\":\"world\"} as data", "world", tuple.get("hello")); String stringResult = (String)sink.collectedTuples.get(1); Assert.assertEquals("Expects {\"hello\":\"world\"} as data", stringData, stringResult); inputOperator.deactivate(); outputOperator.teardown(); inputOperator.teardown(); server.stop(); }