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();
}
}
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;
}
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();
}
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();
}
}
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);
}
Aggregations