Java Code Examples for org.eclipse.jetty.server.NetworkConnector#getLocalPort()

The following examples show how to use org.eclipse.jetty.server.NetworkConnector#getLocalPort() . 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: RequestPartIntegrationTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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 2
Source File: JettyWebSocketTestServer.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public void start() throws Exception {
	this.jettyServer.start();
	this.contextHandler.start();

	Connector[] connectors = jettyServer.getConnectors();
	NetworkConnector connector = (NetworkConnector) connectors[0];
	this.port = connector.getLocalPort();
}
 
Example 3
Source File: RequestPartIntegrationTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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 4
Source File: JettyWebSocketTestServer.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public void start() throws Exception {
	this.jettyServer.start();
	this.contextHandler.start();

	Connector[] connectors = jettyServer.getConnectors();
	NetworkConnector connector = (NetworkConnector) connectors[0];
	this.port = connector.getLocalPort();
}
 
Example 5
Source File: ODataTestServer.java    From syndesis with Apache License 2.0 5 votes vote down vote up
private String serverBaseUri(NetworkConnector connector) {
    if (connector == null) {
        return null;
    }

    ContextHandler context = getChildHandlerByClass(ContextHandler.class);

    try {
        String protocol = connector.getDefaultConnectionFactory().getProtocol();
        String scheme = "http";
        if (protocol.startsWith("SSL-") || protocol.equals("SSL"))
            scheme = "https";

        String host = connector.getHost();
        if (context != null && context.getVirtualHosts() != null && context.getVirtualHosts().length > 0)
            host = context.getVirtualHosts()[0];
        if (host == null)
            host = InetAddress.getLocalHost().getHostAddress();

        String path = context == null ? null : context.getContextPath();
        if (path == null) {
            path = FORWARD_SLASH;
        }

        URI uri = new URI(scheme, null, host, connector.getLocalPort(), path, null, null);
        return uri.toString();
    }
    catch(Exception e) {
        LOG.error("Uri error", e);
        return null;
    }
}
 
Example 6
Source File: HttpManagement.java    From qpid-broker-j with Apache License 2.0 5 votes vote down vote up
@Override
public int getBoundPort(final HttpPort httpPort)
{
    NetworkConnector c = _portConnectorMap.get(httpPort);
    if (c != null)
    {
        return c.getLocalPort();
    }
    else
    {
        return -1;
    }
}
 
Example 7
Source File: RequestPartIntegrationTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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 8
Source File: JettyWebSocketTestServer.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public void start() throws Exception {
	this.jettyServer.start();

	Connector[] connectors = jettyServer.getConnectors();
	NetworkConnector connector = (NetworkConnector) connectors[0];
	this.port = connector.getLocalPort();
}
 
Example 9
Source File: AbstractJettyServerTestCase.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example 10
Source File: AbstractJettyServerTestCase.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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();
}
 
Example 11
Source File: GatewayServer.java    From knox with Apache License 2.0 5 votes vote down vote up
public InetSocketAddress[] getAddresses() {
  InetSocketAddress[] addresses = new InetSocketAddress[ jetty.getConnectors().length ];
  for( int i=0, n=addresses.length; i<n; i++ ) {
    NetworkConnector connector = (NetworkConnector)jetty.getConnectors()[ i ];
    String host = connector.getHost();
    if( host == null ) {
      addresses[ i ] = new InetSocketAddress( connector.getLocalPort() );
    } else {
      addresses[ i ] = new InetSocketAddress( host, connector.getLocalPort() );
    }
  }
  return addresses;
}
 
Example 12
Source File: CampServer.java    From brooklyn-server with Apache License 2.0 4 votes vote down vote up
public Integer getPort() {
    if (server==null) return null;
    NetworkConnector networkConnector = (NetworkConnector) server.getConnectors()[0];
    return networkConnector.getLocalPort();
}
 
Example 13
Source File: JettyManager.java    From DataGenerator with Apache License 2.0 4 votes vote down vote up
/**
 * 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 14
Source File: ExampleServer.java    From rack-servlet with Apache License 2.0 4 votes vote down vote up
public int getPort() {
  Connector[] connectors = server.getConnectors();
  NetworkConnector connector = (NetworkConnector) connectors[0];
  return connector.getLocalPort();
}
 
Example 15
Source File: ExampleServerTest.java    From rack-servlet with Apache License 2.0 4 votes vote down vote up
public int getPort() {
  Connector[] connectors = server.getConnectors();
  NetworkConnector connector = (NetworkConnector) connectors[0];
  return connector.getLocalPort();
}
 
Example 16
Source File: ExampleServerTest.java    From rack-servlet with Apache License 2.0 4 votes vote down vote up
public int getPort() {
  Connector[] connectors = server.getConnectors();
  NetworkConnector connector = (NetworkConnector) connectors[0];
  return connector.getLocalPort();
}