Search in sources :

Example 71 with Server

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

the class ProxyTest method startProxy.

private void startProxy(HttpServlet proxyServlet, Map<String, String> initParams) throws Exception {
    QueuedThreadPool proxyPool = new QueuedThreadPool();
    proxyPool.setName("proxy");
    proxy = new Server(proxyPool);
    HttpConfiguration configuration = new HttpConfiguration();
    configuration.setSendDateHeader(false);
    configuration.setSendServerVersion(false);
    String value = initParams.get("outputBufferSize");
    if (value != null)
        configuration.setOutputBufferSize(Integer.valueOf(value));
    proxyConnector = new ServerConnector(proxy, new HTTP2ServerConnectionFactory(configuration));
    proxy.addConnector(proxyConnector);
    ServletContextHandler proxyContext = new ServletContextHandler(proxy, "/", true, false);
    ServletHolder proxyServletHolder = new ServletHolder(proxyServlet);
    proxyServletHolder.setInitParameters(initParams);
    proxyContext.addServlet(proxyServletHolder, "/*");
    proxy.start();
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) Server(org.eclipse.jetty.server.Server) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) HTTP2ServerConnectionFactory(org.eclipse.jetty.http2.server.HTTP2ServerConnectionFactory) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler)

Example 72 with Server

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

the class AbstractTest method prepareServer.

protected void prepareServer(ConnectionFactory... connectionFactories) {
    QueuedThreadPool serverExecutor = new QueuedThreadPool();
    serverExecutor.setName("server");
    server = new Server(serverExecutor);
    connector = new ServerConnector(server, 1, 1, connectionFactories);
    server.addConnector(connector);
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) Server(org.eclipse.jetty.server.Server) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool)

Example 73 with Server

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

the class ServerSupport method applyXmlConfigurations.

/**
     * Apply xml files to server startup, passing in ourselves as the 
     * "Server" instance.
     * 
     * @param server the server to apply the xml to
     * @param files the list of xml files
     * @return the Server implementation, after the xml is applied
     * @throws Exception if unable to apply the xml configuration
     */
public static Server applyXmlConfigurations(Server server, List<File> files) throws Exception {
    if (files == null || files.isEmpty())
        return server;
    Map<String, Object> lastMap = new HashMap<String, Object>();
    if (server != null)
        lastMap.put("Server", server);
    for (File xmlFile : files) {
        if (PluginLog.getLog() != null)
            PluginLog.getLog().info("Configuring Jetty from xml configuration file = " + xmlFile.getCanonicalPath());
        XmlConfiguration xmlConfiguration = new XmlConfiguration(Resource.toURL(xmlFile));
        //chain ids from one config file to another
        if (lastMap != null)
            xmlConfiguration.getIdMap().putAll(lastMap);
        //Set the system properties each time in case the config file set a new one
        Enumeration<?> ensysprop = System.getProperties().propertyNames();
        while (ensysprop.hasMoreElements()) {
            String name = (String) ensysprop.nextElement();
            xmlConfiguration.getProperties().put(name, System.getProperty(name));
        }
        xmlConfiguration.configure();
        lastMap = xmlConfiguration.getIdMap();
    }
    return (Server) lastMap.get("Server");
}
Also used : Server(org.eclipse.jetty.server.Server) HashMap(java.util.HashMap) XmlConfiguration(org.eclipse.jetty.xml.XmlConfiguration) File(java.io.File)

Example 74 with Server

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

the class TestMemcachedSessions method testMemcached.

@Test
public void testMemcached() throws Exception {
    String contextPath = "/";
    Server server = new Server(0);
    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    context.setContextPath("/");
    context.setResourceBase(System.getProperty("java.io.tmpdir"));
    server.setHandler(context);
    NullSessionCache dsc = new NullSessionCache(context.getSessionHandler());
    dsc.setSessionDataStore(new CachingSessionDataStore(new MemcachedSessionDataMap("localhost", "11211"), new NullSessionDataStore()));
    context.getSessionHandler().setSessionCache(dsc);
    // Add a test servlet
    ServletHolder h = new ServletHolder();
    h.setServlet(new TestServlet());
    context.addServlet(h, "/");
    try {
        server.start();
        int port = ((NetworkConnector) server.getConnectors()[0]).getLocalPort();
        HttpClient client = new HttpClient();
        client.start();
        try {
            int value = 42;
            ContentResponse response = client.GET("http://localhost:" + port + contextPath + "?action=set&value=" + value);
            assertEquals(HttpServletResponse.SC_OK, response.getStatus());
            String sessionCookie = response.getHeaders().get("Set-Cookie");
            assertTrue(sessionCookie != null);
            // Mangle the cookie, replacing Path with $Path, etc.
            sessionCookie = sessionCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path=");
            String resp = response.getContentAsString();
            assertEquals(resp.trim(), String.valueOf(value));
            // Be sure the session value is still there
            Request request = client.newRequest("http://localhost:" + port + contextPath + "?action=get");
            request.header("Cookie", sessionCookie);
            response = request.send();
            assertEquals(HttpServletResponse.SC_OK, response.getStatus());
            resp = response.getContentAsString();
            assertEquals(String.valueOf(value), resp.trim());
            //Delete the session
            request = client.newRequest("http://localhost:" + port + contextPath + "?action=del");
            request.header("Cookie", sessionCookie);
            response = request.send();
            assertEquals(HttpServletResponse.SC_OK, response.getStatus());
            //Check that the session is gone
            request = client.newRequest("http://localhost:" + port + contextPath + "?action=get");
            request.header("Cookie", sessionCookie);
            response = request.send();
            assertEquals(HttpServletResponse.SC_OK, response.getStatus());
            resp = response.getContentAsString();
            assertEquals("No session", resp.trim());
        } finally {
            client.stop();
        }
    } finally {
        server.stop();
    }
}
Also used : Server(org.eclipse.jetty.server.Server) CachingSessionDataStore(org.eclipse.jetty.server.session.CachingSessionDataStore) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpClient(org.eclipse.jetty.client.HttpClient) NullSessionDataStore(org.eclipse.jetty.server.session.NullSessionDataStore) NetworkConnector(org.eclipse.jetty.server.NetworkConnector) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) Test(org.junit.Test)

Example 75 with Server

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

the class JettyBootstrapActivator method start.

/* ------------------------------------------------------------ */
/**
     * Setup a new jetty Server, registers it as a service. Setup the Service
     * tracker for the jetty ContextHandlers that are in charge of deploying the
     * webapps. Setup the BundleListener that supports the extender pattern for
     * the jetty ContextHandler.
     * 
     * @param context the bundle context
     */
public void start(final BundleContext context) throws Exception {
    INSTANCE = this;
    // track other bundles and fragments attached to this bundle that we
    // should activate.
    _packageAdminServiceTracker = new PackageAdminServiceTracker(context);
    // track jetty Server instances that we should support as deployment targets
    _jettyServerServiceTracker = new ServiceTracker(context, context.createFilter("(objectclass=" + Server.class.getName() + ")"), new JettyServerServiceTracker());
    _jettyServerServiceTracker.open();
    // Create a default jetty instance right now.
    Server defaultServer = DefaultJettyAtJettyHomeHelper.startJettyAtJettyHome(context);
}
Also used : Server(org.eclipse.jetty.server.Server) ServiceTracker(org.osgi.util.tracker.ServiceTracker) PackageAdminServiceTracker(org.eclipse.jetty.osgi.boot.utils.internal.PackageAdminServiceTracker) JettyServerServiceTracker(org.eclipse.jetty.osgi.boot.internal.serverfactory.JettyServerServiceTracker) JettyServerServiceTracker(org.eclipse.jetty.osgi.boot.internal.serverfactory.JettyServerServiceTracker) PackageAdminServiceTracker(org.eclipse.jetty.osgi.boot.utils.internal.PackageAdminServiceTracker)

Aggregations

Server (org.eclipse.jetty.server.Server)577 ServerConnector (org.eclipse.jetty.server.ServerConnector)217 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)143 Test (org.junit.Test)119 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)113 HttpConfiguration (org.eclipse.jetty.server.HttpConfiguration)75 QueuedThreadPool (org.eclipse.jetty.util.thread.QueuedThreadPool)73 IOException (java.io.IOException)71 HttpConnectionFactory (org.eclipse.jetty.server.HttpConnectionFactory)67 WebAppContext (org.eclipse.jetty.webapp.WebAppContext)67 File (java.io.File)65 URI (java.net.URI)56 Before (org.junit.Before)50 SslContextFactory (org.eclipse.jetty.util.ssl.SslContextFactory)49 BeforeClass (org.junit.BeforeClass)48 ServletException (javax.servlet.ServletException)45 Connector (org.eclipse.jetty.server.Connector)42 LocalConnector (org.eclipse.jetty.server.LocalConnector)42 URL (java.net.URL)39 HttpServletRequest (javax.servlet.http.HttpServletRequest)39