/** * Bind method for the Jetty server. The test port is filled here. * * @param server * The Jetty server. */ public void bindServer(final Server server) { Connector[] connectors = server.getConnectors(); for (int i = 0, n = connectors.length; (i < n) && (testPort == 0); i++) { if (connectors[i] instanceof NetworkConnector) { NetworkConnector networkConnector = (NetworkConnector) connectors[i]; List<String> protocols = networkConnector.getProtocols(); boolean httpAvailable = false; Iterator<String> protocolIterator = protocols.iterator(); while (!httpAvailable && protocolIterator.hasNext()) { String protocol = protocolIterator.next(); if (protocol.toLowerCase().startsWith("http")) { httpAvailable = true; } } if (httpAvailable) { int localPort = networkConnector.getLocalPort(); if (localPort > 0) { testPort = localPort; } } } } }
@PostConstruct @RetryFor(attempts = 5, value = Exception.class) public void start() { server = new Server(0); server.setHandler(new ZonemasterHandler()); try { server.start(); } catch (Exception e) { throw new RuntimeException(e); } this.port = ((NetworkConnector)server.getConnectors()[0]).getLocalPort(); final String baseUrl = String.format("http://localhost:%s/zonemaster", getPort()); LOGGER.info("Zonemaster dummy server restUrl: {}", baseUrl); ReflectionTestUtils.setField(zonemasterClient, "baseUrl", baseUrl); }
@PostConstruct @RetryFor(attempts = 5, value = Exception.class) public void start() { server = new Server(0); server.setHandler(new CrowdTestHandler()); try { server.start(); } catch (Exception e) { throw new RuntimeException(e); } this.port = ((NetworkConnector)server.getConnectors()[0]).getLocalPort(); final String restUrl = String.format("http://localhost:%s/crowd", getPort()); LOGGER.info("Crowd dummy server restUrl: {}", restUrl); ReflectionTestUtils.setField(crowdClient, "restUrl", restUrl); }
@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(); }
@Override public void start() throws Exception { this.jettyServer.start(); Connector[] connectors = jettyServer.getConnectors(); NetworkConnector connector = (NetworkConnector) connectors[0]; this.port = connector.getLocalPort(); }
@BeforeClass public static void startJettyServer() throws Exception { // Let server pick its own random, available port. jettyServer = new Server(0); ServletContextHandler handler = new ServletContextHandler(); byte[] bytes = helloWorld.getBytes("utf-8"); handler.addServlet(new ServletHolder(new GetServlet(bytes, textContentType)), "/get"); handler.addServlet(new ServletHolder(new GetServlet(new byte[0], textContentType)), "/get/nothing"); handler.addServlet(new ServletHolder(new GetServlet(bytes, null)), "/get/nocontenttype"); handler.addServlet( new ServletHolder(new PostServlet(helloWorld, "/post/1", bytes, textContentType)), "/post"); handler.addServlet( new ServletHolder(new JsonPostServlet("/jsonpost/1", jsonContentType)), "/jsonpost"); handler.addServlet(new ServletHolder(new StatusCodeServlet(204)), "/status/nocontent"); handler.addServlet(new ServletHolder(new StatusCodeServlet(304)), "/status/notmodified"); handler.addServlet(new ServletHolder(new ErrorServlet(404)), "/status/notfound"); handler.addServlet(new ServletHolder(new ErrorServlet(500)), "/status/server"); handler.addServlet(new ServletHolder(new UriServlet()), "/uri/*"); handler.addServlet(new ServletHolder(new MultipartServlet()), "/multipart"); handler.addServlet(new ServletHolder(new FormServlet()), "/form"); handler.addServlet(new ServletHolder(new DeleteServlet()), "/delete"); handler.addServlet( new ServletHolder(new PutServlet(helloWorld, bytes, textContentType)), "/put"); jettyServer.setHandler(handler); jettyServer.start(); Connector[] connectors = jettyServer.getConnectors(); NetworkConnector connector = (NetworkConnector) connectors[0]; port = connector.getLocalPort(); baseUrl = "http://localhost:" + port; }
@BeforeClass public static void startJettyServer() throws Exception { // Let server pick its own random, available port. jettyServer = new Server(0); ServletContextHandler handler = new ServletContextHandler(); handler.setContextPath("/"); handler.addServlet(new ServletHolder(new EchoServlet()), "/echo"); handler.addServlet(new ServletHolder(new ParameterServlet()), "/params"); handler.addServlet(new ServletHolder(new StatusServlet(200)), "/status/ok"); handler.addServlet(new ServletHolder(new StatusServlet(404)), "/status/notfound"); handler.addServlet(new ServletHolder(new MethodServlet("DELETE")), "/methods/delete"); handler.addServlet(new ServletHolder(new MethodServlet("GET")), "/methods/get"); handler.addServlet(new ServletHolder(new MethodServlet("HEAD")), "/methods/head"); handler.addServlet(new ServletHolder(new MethodServlet("OPTIONS")), "/methods/options"); handler.addServlet(new ServletHolder(new PostServlet()), "/methods/post"); handler.addServlet(new ServletHolder(new MethodServlet("PUT")), "/methods/put"); handler.addServlet(new ServletHolder(new MethodServlet("PATCH")), "/methods/patch"); jettyServer.setHandler(handler); jettyServer.start(); Connector[] connectors = jettyServer.getConnectors(); NetworkConnector connector = (NetworkConnector) connectors[0]; baseUrl = "http://localhost:" + connector.getLocalPort(); }
@Override @SuppressWarnings( "ValueOfIncrementOrDecrementUsed" ) public final Interface[] interfacesServed() { Connector[] connectors = server.getConnectors(); Interface[] result = new Interface[ connectors.length ]; int index = 0; for( Connector connector : connectors ) { if( connector instanceof NetworkConnector ) { NetworkConnector netConnector = (NetworkConnector) connector; String host = configuration().hostName().get(); if( host == null ) { host = netConnector.getHost(); if( host == null ) // If serving all interfaces. { try { host = InetAddress.getLocalHost().getHostAddress(); } catch( UnknownHostException e ) { throw new InternalError( "UnknownHost for local interface.", e ); } } } result[ index++] = new InterfaceImpl( host, netConnector.getPort(), servedProtocol() ); } } return result; }
public Server createServer(Set<MappedServlet> servlets, Set<MappedFilter> filters, Set<MappedListener> listeners) { ThreadPool threadPool = createThreadPool(); Server server = new Server(threadPool); server.setStopAtShutdown(true); server.setStopTimeout(1000L); server.setHandler(createHandler(servlets, filters, listeners)); if (maxFormContentSize > 0) { server.setAttribute("org.eclipse.jetty.server.Request.maxFormContentSize", maxFormContentSize); } if (maxFormKeys > 0) { server.setAttribute("org.eclipse.jetty.server.Request.maxFormKeys", maxFormKeys); } createRequestLog(server); Collection<ConnectorFactory> connectorFactories = connectorFactories(server); Collection<ConnectorDescriptor> connectorDescriptors = new ArrayList<>(2); if (connectorFactories.isEmpty()) { LOGGER.warn("Jetty starts with no connectors configured. Is that expected?"); } else { connectorFactories.forEach(cf -> { NetworkConnector connector = cf.createConnector(server); server.addConnector(connector); connectorDescriptors.add(new ConnectorDescriptor(connector)); }); } server.addLifeCycleListener(new ServerLifecycleLogger(connectorDescriptors, context)); return server; }
/** * Starts the server asynchronously. To stop the server, see {@link #stop()}. * * @throws Exception if the server could not be successfully started. */ public void start() throws Exception { metricsReporters.forEach((reporterName, metricsReporter) -> { log.info("Registering the metrics reporter : {}, with source : {}.", reporterName, METRICS_SOURCE); metricsReporter.register(METRICS_SOURCE, metricsRegistry); log.info("Starting the metrics reporter : {}.", reporterName); metricsReporter.start(); }); log.info("Starting server on port {}", ((NetworkConnector) server.getConnectors()[0]).getPort()); server.start(); log.info("Server is running"); }
@Test public void testStartShouldStartTheMetricsReportersAndServer() throws Exception { NetworkConnector connector = Mockito.mock(NetworkConnector.class); int testServerPort = 100; Mockito.doReturn(testServerPort).when(connector).getPort(); Mockito.when(server.getConnectors()).thenReturn(new NetworkConnector[]{connector}); Mockito.doNothing().when(server).start(); samzaRestService.start(); Mockito.verify(metricsReporter).start(); Mockito.verify(metricsReporter).register("SamzaRest", metricsRegistry); Mockito.verify(server).start(); }
public void start() { try { server.start(); } catch (Exception e) { Log.warn("Exception when trying to start server", e); } port = ((NetworkConnector) server.getConnectors()[0]).getLocalPort(); }
@RetryFor(attempts = 5, value = Exception.class) private Server createAndStartServer(int port, HandlerList handlers) throws Exception { final Server server = new Server(port); server.setHandler(handlers); server.setStopAtShutdown(true); server.start(); this.port = ((NetworkConnector)server.getConnectors()[0]).getLocalPort(); LOGGER.info("Jetty started on port {}", this.port); return server; }
public ConnectorDescriptor(NetworkConnector connector) { this.connector = connector; }
private static int http1Port() { return ((NetworkConnector) http1server.getConnectors()[0]).getLocalPort(); }
private static int http2Port() { return ((NetworkConnector) http2server.getConnectors()[0]).getLocalPort(); }
/** * @deprecated this won't work with pax web */ @Deprecated @SuppressWarnings ( "resource" ) protected String makePrefixFromJetty () { if ( this.server == null ) { return null; } for ( final Connector c : this.server.getConnectors () ) { if ( ! ( c instanceof NetworkConnector ) ) { continue; } final NetworkConnector nc = (NetworkConnector)c; final StringBuilder sb = new StringBuilder (); String protocol = "http"; final List<String> protos = nc.getProtocols (); for ( final String proto : protos ) { // this seems to be that way jetty does it // http://git.eclipse.org/c/jetty/org.eclipse.jetty.project.git/tree/jetty-server/src/main/java/org/eclipse/jetty/server/Server.java if ( proto.startsWith ( "SSL-" ) ) { protocol = "https"; break; } } sb.append ( protocol ).append ( "://" ); if ( nc.getHost () == null ) { sb.append ( this.hostname ); } else { sb.append ( nc.getHost () ); } if ( "http".equals ( protocol ) && nc.getPort () == 80 ) { // no port } else if ( "https".equals ( protocol ) && nc.getPort () == 443 ) { // no port } else { sb.append ( ':' ).append ( nc.getPort () ); } return sb.toString (); } return null; }
protected int getPort() { return ((NetworkConnector)server.getConnectors()[0]).getLocalPort(); }
public int getPort() { Connector[] connectors = server.getConnectors(); NetworkConnector connector = (NetworkConnector) connectors[0]; return connector.getLocalPort(); }