Search in sources :

Example 46 with Connector

use of org.eclipse.jetty.server.Connector in project camel by apache.

the class ExplicitHttpsSslContextParametersRouteTest method createRouteBuilder.

// END SNIPPET: e2
@Override
protected RouteBuilder createRouteBuilder() throws Exception {
    return new RouteBuilder() {

        public void configure() throws Exception {
            // START SNIPPET: e1
            // create SSL select channel connectors for port 9080 and 9090
            Map<Integer, Connector> connectors = new HashMap<Integer, Connector>();
            connectors.put(port1, createSslSocketConnector(getContext(), port1));
            connectors.put(port2, createSslSocketConnector(getContext(), port2));
            JettyHttpComponent jetty = getContext().getComponent("jetty", JettyHttpComponent.class);
            jetty.setSslSocketConnectors(connectors);
            // END SNIPPET: e1
            from("jetty:https://localhost:" + port1 + "/test").to("mock:a");
            Processor proc = new Processor() {

                public void process(Exchange exchange) throws Exception {
                    exchange.getOut().setBody("<b>Hello World</b>");
                }
            };
            from("jetty:https://localhost:" + port1 + "/hello").process(proc);
            from("jetty:https://localhost:" + port2 + "/test").to("mock:b");
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) Connector(org.eclipse.jetty.server.Connector) Processor(org.apache.camel.Processor) RouteBuilder(org.apache.camel.builder.RouteBuilder) HashMap(java.util.HashMap)

Example 47 with Connector

use of org.eclipse.jetty.server.Connector in project jetty-runtime by GoogleCloudPlatform.

the class DeploymentCheck method lifeCycleStarted.

@Override
public void lifeCycleStarted(LifeCycle bean) {
    if (bean instanceof Server) {
        Server server = (Server) bean;
        Connector[] connectors = server.getConnectors();
        if (connectors == null || connectors.length == 0) {
            server.dumpStdErr();
            throw new IllegalStateException("No Connector");
        } else if (!Arrays.stream(connectors).allMatch(Connector::isStarted)) {
            server.dumpStdErr();
            throw new IllegalStateException("Connector not started");
        }
        ContextHandler context = server.getChildHandlerByClass(ContextHandler.class);
        if (context == null || !context.isAvailable()) {
            server.dumpStdErr();
            throw new IllegalStateException("No Available Context");
        }
    }
}
Also used : ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) Connector(org.eclipse.jetty.server.Connector) Server(org.eclipse.jetty.server.Server)

Example 48 with Connector

use of org.eclipse.jetty.server.Connector in project http-request by kevinsawicki.

the class ServerTestCase method setUp.

/**
   * Set up server with handler
   *
   * @param handler
   * @return port
   * @throws Exception
   */
public static String setUp(final Handler handler) throws Exception {
    server = new Server();
    if (handler != null)
        server.setHandler(handler);
    Connector connector = new SelectChannelConnector();
    connector.setPort(0);
    server.setConnectors(new Connector[] { connector });
    server.start();
    proxy = new Server();
    Connector proxyConnector = new SelectChannelConnector();
    proxyConnector.setPort(0);
    proxy.setConnectors(new Connector[] { proxyConnector });
    ServletHandler proxyHandler = new ServletHandler();
    RequestHandler proxyCountingHandler = new RequestHandler() {

        @Override
        public void handle(Request request, HttpServletResponse response) {
            proxyHitCount.incrementAndGet();
            String auth = request.getHeader("Proxy-Authorization");
            auth = auth.substring(auth.indexOf(' ') + 1);
            try {
                auth = B64Code.decode(auth, CHARSET_UTF8);
            } catch (UnsupportedEncodingException e) {
                throw new RuntimeException(e);
            }
            int colon = auth.indexOf(':');
            proxyUser.set(auth.substring(0, colon));
            proxyPassword.set(auth.substring(colon + 1));
            request.setHandled(false);
        }
    };
    HandlerList handlerList = new HandlerList();
    handlerList.addHandler(proxyCountingHandler);
    handlerList.addHandler(proxyHandler);
    proxy.setHandler(handlerList);
    ServletHolder proxyHolder = proxyHandler.addServletWithMapping("org.eclipse.jetty.servlets.ProxyServlet", "/");
    proxyHolder.setAsyncSupported(true);
    proxy.start();
    proxyPort = proxyConnector.getLocalPort();
    return "http://localhost:" + connector.getLocalPort();
}
Also used : HandlerList(org.eclipse.jetty.server.handler.HandlerList) Connector(org.eclipse.jetty.server.Connector) SelectChannelConnector(org.eclipse.jetty.server.nio.SelectChannelConnector) ServletHandler(org.eclipse.jetty.servlet.ServletHandler) Server(org.eclipse.jetty.server.Server) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) UnsupportedEncodingException(java.io.UnsupportedEncodingException) SelectChannelConnector(org.eclipse.jetty.server.nio.SelectChannelConnector)

Example 49 with Connector

use of org.eclipse.jetty.server.Connector in project jetty.project by eclipse.

the class WebInfConfiguration method getCanonicalNameForWebAppTmpDir.

/**
     * Create a canonical name for a webapp temp directory.
     * <p>
     * The form of the name is:
     * 
     * <pre>"jetty-"+host+"-"+port+"-"+resourceBase+"-_"+context+"-"+virtualhost+"-"+randomdigits+".dir"</pre>
     *
     *  host and port uniquely identify the server
     *  context and virtual host uniquely identify the webapp
     *  randomdigits ensure every tmp directory is unique
     *  
     * @param context the context to get the canonical name from 
     * @return the canonical name for the webapp temp directory
     */
public static String getCanonicalNameForWebAppTmpDir(WebAppContext context) {
    StringBuffer canonicalName = new StringBuffer();
    canonicalName.append("jetty-");
    //get the host and the port from the first connector
    Server server = context.getServer();
    if (server != null) {
        Connector[] connectors = context.getServer().getConnectors();
        if (connectors.length > 0) {
            //Get the host
            String host = null;
            int port = 0;
            if (connectors != null && (connectors[0] instanceof NetworkConnector)) {
                NetworkConnector connector = (NetworkConnector) connectors[0];
                host = connector.getHost();
                port = connector.getLocalPort();
                if (port < 0)
                    port = connector.getPort();
            }
            if (host == null)
                host = "0.0.0.0";
            canonicalName.append(host);
            //Get the port
            canonicalName.append("-");
            //if not available (eg no connectors or connector not started),
            //try getting one that was configured.
            canonicalName.append(port);
            canonicalName.append("-");
        }
    }
    //Resource  base
    try {
        Resource resource = context.getBaseResource();
        if (resource == null) {
            if (context.getWar() == null || context.getWar().length() == 0)
                throw new IllegalStateException("No resourceBase or war set for context");
            // Set dir or WAR
            resource = context.newResource(context.getWar());
        }
        String tmp = URIUtil.decodePath(resource.getURL().getPath());
        if (tmp.endsWith("/"))
            tmp = tmp.substring(0, tmp.length() - 1);
        if (tmp.endsWith("!"))
            tmp = tmp.substring(0, tmp.length() - 1);
        //get just the last part which is the filename
        int i = tmp.lastIndexOf("/");
        canonicalName.append(tmp.substring(i + 1, tmp.length()));
        canonicalName.append("-");
    } catch (Exception e) {
        LOG.warn("Can't generate resourceBase as part of webapp tmp dir name: " + e);
        LOG.debug(e);
    }
    //Context name
    String contextPath = context.getContextPath();
    contextPath = contextPath.replace('/', '_');
    contextPath = contextPath.replace('\\', '_');
    canonicalName.append(contextPath);
    //Virtual host (if there is one)
    canonicalName.append("-");
    String[] vhosts = context.getVirtualHosts();
    if (vhosts == null || vhosts.length <= 0)
        canonicalName.append("any");
    else
        canonicalName.append(vhosts[0]);
    // sanitize
    for (int i = 0; i < canonicalName.length(); i++) {
        char c = canonicalName.charAt(i);
        if (!Character.isJavaIdentifierPart(c) && "-.".indexOf(c) < 0)
            canonicalName.setCharAt(i, '.');
    }
    canonicalName.append("-");
    return canonicalName.toString();
}
Also used : NetworkConnector(org.eclipse.jetty.server.NetworkConnector) Connector(org.eclipse.jetty.server.Connector) Server(org.eclipse.jetty.server.Server) JarResource(org.eclipse.jetty.util.resource.JarResource) Resource(org.eclipse.jetty.util.resource.Resource) NetworkConnector(org.eclipse.jetty.server.NetworkConnector) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException)

Example 50 with Connector

use of org.eclipse.jetty.server.Connector in project jetty.project by eclipse.

the class DebugHandler method doStop.

/* (non-Javadoc)
     * @see org.eclipse.jetty.server.handler.HandlerWrapper#doStop()
     */
@Override
protected void doStop() throws Exception {
    super.doStop();
    _print.close();
    for (Connector connector : getServer().getConnectors()) if (connector instanceof AbstractConnector)
        ((AbstractConnector) connector).removeBean(this);
}
Also used : AbstractConnector(org.eclipse.jetty.server.AbstractConnector) Connector(org.eclipse.jetty.server.Connector) AbstractConnector(org.eclipse.jetty.server.AbstractConnector)

Aggregations

Connector (org.eclipse.jetty.server.Connector)79 Server (org.eclipse.jetty.server.Server)41 ServerConnector (org.eclipse.jetty.server.ServerConnector)25 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)22 HttpConfiguration (org.eclipse.jetty.server.HttpConfiguration)17 HttpConnectionFactory (org.eclipse.jetty.server.HttpConnectionFactory)15 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)13 IOException (java.io.IOException)12 NetworkConnector (org.eclipse.jetty.server.NetworkConnector)12 Test (org.junit.Test)11 SslContextFactory (org.eclipse.jetty.util.ssl.SslContextFactory)10 SelectChannelConnector (org.eclipse.jetty.server.nio.SelectChannelConnector)8 Test (org.testng.annotations.Test)8 Handler (org.eclipse.jetty.server.Handler)7 QueuedThreadPool (org.eclipse.jetty.util.thread.QueuedThreadPool)7 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)6 SecureRequestCustomizer (org.eclipse.jetty.server.SecureRequestCustomizer)6 SslConnectionFactory (org.eclipse.jetty.server.SslConnectionFactory)6 ServletException (javax.servlet.ServletException)5