Search in sources :

Example 11 with WebAppContext

use of org.eclipse.jetty.webapp.WebAppContext in project jetty.project by eclipse.

the class ReloadedSessionMissingClassTest method testSessionReloadWithMissingClass.

@Test
public void testSessionReloadWithMissingClass() throws Exception {
    Resource.setDefaultUseCaches(false);
    String contextPath = "/foo";
    File unpackedWarDir = testdir.getEmptyPathDir().toFile();
    File webInfDir = new File(unpackedWarDir, "WEB-INF");
    webInfDir.mkdir();
    File webXml = new File(webInfDir, "web.xml");
    String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<web-app xmlns=\"http://java.sun.com/xml/ns/j2ee\"\n" + "         xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" + "         xsi:schemaLocation=\"http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd\"\n" + "         version=\"2.4\">\n" + "\n" + "<session-config>\n" + " <session-timeout>1</session-timeout>\n" + "</session-config>\n" + "</web-app>";
    FileWriter w = new FileWriter(webXml);
    w.write(xml);
    w.close();
    File foobarJar = MavenTestingUtils.getTestResourceFile("foobar.jar");
    File foobarNOfooJar = MavenTestingUtils.getTestResourceFile("foobarNOfoo.jar");
    URL[] foobarUrls = new URL[] { foobarJar.toURI().toURL() };
    URL[] barUrls = new URL[] { foobarNOfooJar.toURI().toURL() };
    URLClassLoader loaderWithFoo = new URLClassLoader(foobarUrls, Thread.currentThread().getContextClassLoader());
    URLClassLoader loaderWithoutFoo = new URLClassLoader(barUrls, Thread.currentThread().getContextClassLoader());
    DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
    cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
    SessionDataStoreFactory storeFactory = JdbcTestHelper.newSessionDataStoreFactory();
    ((AbstractSessionDataStoreFactory) storeFactory).setGracePeriodSec(TestServer.DEFAULT_SCAVENGE_SEC);
    TestServer server1 = new TestServer(0, TestServer.DEFAULT_MAX_INACTIVE, TestServer.DEFAULT_SCAVENGE_SEC, cacheFactory, storeFactory);
    WebAppContext webApp = server1.addWebAppContext(unpackedWarDir.getCanonicalPath(), contextPath);
    webApp.getSessionHandler().getSessionCache().setRemoveUnloadableSessions(true);
    webApp.setClassLoader(loaderWithFoo);
    webApp.addServlet("Bar", "/bar");
    server1.start();
    int port1 = server1.getPort();
    try (StacklessLogging stackless = new StacklessLogging(Log.getLogger("org.eclipse.jetty.server.session"))) {
        HttpClient client = new HttpClient();
        client.start();
        try {
            // Perform one request to server1 to create a session
            ContentResponse response = client.GET("http://localhost:" + port1 + contextPath + "/bar?action=set");
            assertEquals(HttpServletResponse.SC_OK, response.getStatus());
            String sessionCookie = response.getHeaders().get("Set-Cookie");
            assertTrue(sessionCookie != null);
            String sessionId = (String) webApp.getServletContext().getAttribute("foo");
            assertNotNull(sessionId);
            // Mangle the cookie, replacing Path with $Path, etc.
            sessionCookie = sessionCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path=");
            //Stop the webapp
            webApp.stop();
            webApp.setClassLoader(loaderWithoutFoo);
            //restart webapp
            webApp.start();
            Request request = client.newRequest("http://localhost:" + port1 + contextPath + "/bar?action=get");
            request.header("Cookie", sessionCookie);
            response = request.send();
            assertEquals(HttpServletResponse.SC_OK, response.getStatus());
            String afterStopSessionId = (String) webApp.getServletContext().getAttribute("foo.session");
            Boolean fooPresent = (Boolean) webApp.getServletContext().getAttribute("foo.present");
            assertFalse(fooPresent);
            assertNotNull(afterStopSessionId);
            assertTrue(!afterStopSessionId.equals(sessionId));
        } finally {
            client.stop();
        }
    } finally {
        server1.stop();
    }
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) FileWriter(java.io.FileWriter) Request(org.eclipse.jetty.client.api.Request) URL(java.net.URL) WebAppContext(org.eclipse.jetty.webapp.WebAppContext) URLClassLoader(java.net.URLClassLoader) HttpClient(org.eclipse.jetty.client.HttpClient) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) File(java.io.File) Test(org.junit.Test)

Example 12 with WebAppContext

use of org.eclipse.jetty.webapp.WebAppContext in project jetty.project by eclipse.

the class TestServer method addWebAppContext.

public WebAppContext addWebAppContext(String warPath, String contextPath) throws Exception {
    WebAppContext context = new WebAppContext(_contexts, warPath, contextPath);
    SessionHandler sessionHandler = newSessionHandler();
    sessionHandler.setSessionIdManager(_sessionIdManager);
    sessionHandler.setMaxInactiveInterval(_maxInactivePeriod);
    context.setSessionHandler(sessionHandler);
    return context;
}
Also used : WebAppContext(org.eclipse.jetty.webapp.WebAppContext)

Example 13 with WebAppContext

use of org.eclipse.jetty.webapp.WebAppContext in project jetty.project by eclipse.

the class JmxIT method startJetty.

public static void startJetty() throws Exception {
    File target = MavenTestingUtils.getTargetDir();
    File jettyBase = new File(target, "test-base");
    File webapps = new File(jettyBase, "webapps");
    File war = new File(webapps, "jmx-webapp.war");
    //create server instance
    __server = new Server(0);
    //set up the webapp
    WebAppContext context = new WebAppContext();
    context.setWar(war.getCanonicalPath());
    context.setContextPath("/jmx-webapp");
    Configuration.ClassList classlist = Configuration.ClassList.setServerDefault(__server);
    classlist.addBefore("org.eclipse.jetty.webapp.JettyWebXmlConfiguration", "org.eclipse.jetty.annotations.AnnotationConfiguration");
    context.setAttribute("org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern", ".*/javax.servlet-[^/]*\\.jar$|.*/servlet-api-[^/]*\\.jar$");
    __server.setHandler(context);
    //set up jmx remote
    MBeanContainer mbContainer = new MBeanContainer(ManagementFactory.getPlatformMBeanServer());
    __server.addBean(mbContainer);
    JMXServiceURL serviceUrl = new JMXServiceURL("rmi", "localhost", 1099, "/jndi/rmi://localhost:1099/jmxrmi");
    ConnectorServer jmxConnServer = new ConnectorServer(serviceUrl, "org.eclipse.jetty.jmx:name=rmiconnectorserver");
    __server.addBean(jmxConnServer);
    //start server
    __server.start();
    //remember chosen port
    __port = ((NetworkConnector) __server.getConnectors()[0]).getLocalPort();
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) WebAppContext(org.eclipse.jetty.webapp.WebAppContext) Server(org.eclipse.jetty.server.Server) ConnectorServer(org.eclipse.jetty.jmx.ConnectorServer) Configuration(org.eclipse.jetty.webapp.Configuration) MBeanContainer(org.eclipse.jetty.jmx.MBeanContainer) ConnectorServer(org.eclipse.jetty.jmx.ConnectorServer) File(java.io.File)

Example 14 with WebAppContext

use of org.eclipse.jetty.webapp.WebAppContext in project jetty.project by eclipse.

the class LargeContainerTest method testEcho.

@Test
public void testEcho() throws Exception {
    WSServer wsb = new WSServer(testdir, "app");
    wsb.copyWebInf("large-echo-config-web.xml");
    wsb.copyEndpoint(LargeEchoDefaultSocket.class);
    try {
        wsb.start();
        URI uri = wsb.getServerBaseURI();
        WebAppContext webapp = wsb.createWebAppContext();
        wsb.deployWebapp(webapp);
        // wsb.dump();
        WebSocketClient client = new WebSocketClient(bufferPool);
        try {
            client.getPolicy().setMaxTextMessageSize(128 * 1024);
            client.start();
            JettyEchoSocket clientEcho = new JettyEchoSocket();
            Future<Session> foo = client.connect(clientEcho, uri.resolve("echo/large"));
            // wait for connect
            foo.get(1, TimeUnit.SECONDS);
            // The message size should be bigger than default, but smaller than the limit that LargeEchoSocket specifies
            byte[] txt = new byte[100 * 1024];
            Arrays.fill(txt, (byte) 'o');
            String msg = new String(txt, StandardCharsets.UTF_8);
            clientEcho.sendMessage(msg);
            Queue<String> msgs = clientEcho.awaitMessages(1);
            Assert.assertEquals("Expected message", msg, msgs.poll());
        } finally {
            client.stop();
        }
    } finally {
        wsb.stop();
    }
}
Also used : WebAppContext(org.eclipse.jetty.webapp.WebAppContext) WebSocketClient(org.eclipse.jetty.websocket.client.WebSocketClient) URI(java.net.URI) Session(org.eclipse.jetty.websocket.api.Session) Test(org.junit.Test)

Example 15 with WebAppContext

use of org.eclipse.jetty.webapp.WebAppContext in project jetty.project by eclipse.

the class PingPongTest method startServer.

@BeforeClass
public static void startServer() throws Exception {
    File testdir = MavenTestingUtils.getTargetTestingDir(PingPongTest.class.getName());
    server = new WSServer(testdir, "app");
    server.copyWebInf("pong-config-web.xml");
    server.copyClass(PongContextListener.class);
    server.copyClass(PongMessageEndpoint.class);
    server.copyClass(PongSocket.class);
    server.start();
    serverUri = server.getServerBaseURI();
    WebAppContext webapp = server.createWebAppContext();
    server.deployWebapp(webapp);
}
Also used : WebAppContext(org.eclipse.jetty.webapp.WebAppContext) File(java.io.File) BeforeClass(org.junit.BeforeClass)

Aggregations

WebAppContext (org.eclipse.jetty.webapp.WebAppContext)291 Server (org.eclipse.jetty.server.Server)108 File (java.io.File)74 HandlerCollection (org.eclipse.jetty.server.handler.HandlerCollection)38 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)35 IOException (java.io.IOException)31 ServerConnector (org.eclipse.jetty.server.ServerConnector)31 DefaultHandler (org.eclipse.jetty.server.handler.DefaultHandler)31 Test (org.junit.Test)27 ArrayList (java.util.ArrayList)22 URL (java.net.URL)21 URISyntaxException (java.net.URISyntaxException)20 Handler (org.eclipse.jetty.server.Handler)17 HttpConnectionFactory (org.eclipse.jetty.server.HttpConnectionFactory)17 ContextHandler (org.eclipse.jetty.server.handler.ContextHandler)15 SslContextFactory (org.eclipse.jetty.util.ssl.SslContextFactory)15 WebXmlConfiguration (org.eclipse.jetty.webapp.WebXmlConfiguration)15 HttpConfiguration (org.eclipse.jetty.server.HttpConfiguration)14 ContextHandlerCollection (org.eclipse.jetty.server.handler.ContextHandlerCollection)14 Resource (org.eclipse.jetty.util.resource.Resource)13