Java 类org.eclipse.jetty.server.NetworkConnector 实例源码

项目:osgi-remote-jersey    文件:JerseyExtenderTestComponent.java   
/**
 * 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;
                }
            }
        }
    }
}
项目:whois    文件:ZonemasterDummy.java   
@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);
}
项目:whois    文件:CrowdServerDummy.java   
@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);
}
项目:spring4-understanding    文件:RequestPartIntegrationTests.java   
@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();
}
项目:spring4-understanding    文件:JettyWebSocketTestServer.java   
@Override
public void start() throws Exception {
    this.jettyServer.start();

    Connector[] connectors = jettyServer.getConnectors();
    NetworkConnector connector = (NetworkConnector) connectors[0];
    this.port = connector.getLocalPort();
}
项目:spring4-understanding    文件:AbstractJettyServerTestCase.java   
@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;
}
项目:spring4-understanding    文件:AbstractJettyServerTestCase.java   
@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();
}
项目:polygene-java    文件:AbstractJettyMixin.java   
@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;
}
项目:bootique-jetty    文件:ServerFactory.java   
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;
    }
项目:samza    文件:SamzaRestService.java   
/**
 * 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");
}
项目:samza    文件:TestSamzaRestService.java   
@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();
}
项目:writelatex-git-bridge    文件:MockSnapshotServer.java   
public void start() {
    try {
        server.start();
    } catch (Exception e) {
        Log.warn("Exception when trying to start server", e);
    }
    port = ((NetworkConnector) server.getConnectors()[0]).getLocalPort();
}
项目:whois    文件:JettyBootstrap.java   
@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;
}
项目:bootique-jetty    文件:ConnectorDescriptor.java   
public ConnectorDescriptor(NetworkConnector connector) {
    this.connector = connector;
}
项目:armeria    文件:ThriftOverHttpClientTServletIntegrationTest.java   
private static int http1Port() {
    return ((NetworkConnector) http1server.getConnectors()[0]).getLocalPort();
}
项目:armeria    文件:ThriftOverHttpClientTServletIntegrationTest.java   
private static int http2Port() {
    return ((NetworkConnector) http2server.getConnectors()[0]).getLocalPort();
}
项目:package-drone    文件:SystemServiceImpl.java   
/**
 * @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;
}
项目:stagemonitor    文件:AbstractEmbeddedServerTest.java   
protected int getPort() {
    return ((NetworkConnector)server.getConnectors()[0]).getLocalPort();
}
项目:rack-servlet    文件:ExampleServer.java   
public int getPort() {
  Connector[] connectors = server.getConnectors();
  NetworkConnector connector = (NetworkConnector) connectors[0];
  return connector.getLocalPort();
}
项目:rack-servlet    文件:ExampleServerTest.java   
public int getPort() {
  Connector[] connectors = server.getConnectors();
  NetworkConnector connector = (NetworkConnector) connectors[0];
  return connector.getLocalPort();
}
项目:rack-servlet    文件:ExampleServerTest.java   
public int getPort() {
  Connector[] connectors = server.getConnectors();
  NetworkConnector connector = (NetworkConnector) connectors[0];
  return connector.getLocalPort();
}