Search in sources :

Example 91 with WebAppContext

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

the class SessionRenewTest method doTest.

/**
     *  Perform the test by making a request to create a session
     * then another request that will renew the session id.
     * @param verifier the class that verifies the session id changes in cache/store
     * @throws Exception
     */
public void doTest(RenewalVerifier verifier) throws Exception {
    String contextPath = "";
    String servletMapping = "/server";
    WebAppContext context = _server.addWebAppContext(".", contextPath);
    context.setParentLoaderPriority(true);
    context.addServlet(TestServlet.class, servletMapping);
    TestHttpSessionIdListener testListener = new TestHttpSessionIdListener();
    context.addEventListener(testListener);
    HttpClient client = new HttpClient();
    try {
        _server.start();
        int port = _server.getPort();
        client.start();
        //make a request to create a session
        ContentResponse response = client.GET("http://localhost:" + port + contextPath + servletMapping + "?action=create");
        assertEquals(HttpServletResponse.SC_OK, response.getStatus());
        String sessionCookie = response.getHeaders().get("Set-Cookie");
        assertTrue(sessionCookie != null);
        assertFalse(testListener.isCalled());
        //make a request to change the sessionid
        Request request = client.newRequest("http://localhost:" + port + contextPath + servletMapping + "?action=renew");
        request.header("Cookie", sessionCookie);
        ContentResponse renewResponse = request.send();
        assertEquals(HttpServletResponse.SC_OK, renewResponse.getStatus());
        String renewSessionCookie = renewResponse.getHeaders().get("Set-Cookie");
        assertNotNull(renewSessionCookie);
        assertNotSame(sessionCookie, renewSessionCookie);
        assertTrue(testListener.isCalled());
        if (verifier != null)
            verifier.verify(context, TestServer.extractSessionId(sessionCookie), TestServer.extractSessionId(renewSessionCookie));
    } finally {
        client.stop();
        _server.stop();
    }
}
Also used : WebAppContext(org.eclipse.jetty.webapp.WebAppContext) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpClient(org.eclipse.jetty.client.HttpClient) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest)

Example 92 with WebAppContext

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

the class ServiceWebAppProvider method serviceRemoved.

/* ------------------------------------------------------------ */
/**
     * @param context the webapp
     */
public boolean serviceRemoved(ServiceReference serviceRef, ContextHandler context) {
    if (context == null || !(context instanceof WebAppContext))
        return false;
    String watermark = (String) serviceRef.getProperty(OSGiWebappConstants.WATERMARK);
    if (watermark != null && !"".equals(watermark))
        //this service represents a contexthandler that will be deregistered as a service by another of our deployers
        return false;
    App app = _serviceMap.remove(serviceRef);
    if (app != null) {
        getDeploymentManager().removeApp(app);
        return true;
    }
    return false;
}
Also used : App(org.eclipse.jetty.deploy.App) WebAppContext(org.eclipse.jetty.webapp.WebAppContext)

Example 93 with WebAppContext

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

the class ServiceWebAppProvider method serviceAdded.

/* ------------------------------------------------------------ */
/**
     * A webapp that was deployed as an osgi service has been added,
     * and we want to deploy it.
     * 
     * @param context the webapp
     */
public boolean serviceAdded(ServiceReference serviceRef, ContextHandler context) {
    if (context == null || !(context instanceof WebAppContext))
        return false;
    String watermark = (String) serviceRef.getProperty(OSGiWebappConstants.WATERMARK);
    if (watermark != null && !"".equals(watermark))
        //this service represents a webapp that has already been registered as a service by another of our deployers
        return false;
    WebAppContext webApp = (WebAppContext) context;
    Dictionary properties = new Hashtable<String, String>();
    String contextPath = (String) serviceRef.getProperty(OSGiWebappConstants.RFC66_WEB_CONTEXTPATH);
    if (contextPath == null)
        contextPath = (String) serviceRef.getProperty(OSGiWebappConstants.SERVICE_PROP_CONTEXT_PATH);
    if (contextPath == null)
        //No context path
        return false;
    String base = (String) serviceRef.getProperty(OSGiWebappConstants.JETTY_WAR_FOLDER_PATH);
    if (base == null)
        base = (String) serviceRef.getProperty(OSGiWebappConstants.JETTY_WAR_RESOURCE_PATH);
    if (base == null)
        base = (String) serviceRef.getProperty(OSGiWebappConstants.SERVICE_PROP_WAR);
    if (base == null)
        //No webapp base
        return false;
    String webdefaultXml = (String) serviceRef.getProperty(OSGiWebappConstants.JETTY_DEFAULT_WEB_XML_PATH);
    if (webdefaultXml == null)
        webdefaultXml = (String) serviceRef.getProperty(OSGiWebappConstants.SERVICE_PROP_DEFAULT_WEB_XML_PATH);
    if (webdefaultXml != null)
        properties.put(OSGiWebappConstants.JETTY_DEFAULT_WEB_XML_PATH, webdefaultXml);
    String webXml = (String) serviceRef.getProperty(OSGiWebappConstants.JETTY_WEB_XML_PATH);
    if (webXml == null)
        webXml = (String) serviceRef.getProperty(OSGiWebappConstants.SERVICE_PROP_WEB_XML_PATH);
    if (webXml != null)
        properties.put(OSGiWebappConstants.JETTY_WEB_XML_PATH, webXml);
    String extraClassPath = (String) serviceRef.getProperty(OSGiWebappConstants.JETTY_EXTRA_CLASSPATH);
    if (extraClassPath == null)
        extraClassPath = (String) serviceRef.getProperty(OSGiWebappConstants.SERVICE_PROP_EXTRA_CLASSPATH);
    if (extraClassPath != null)
        properties.put(OSGiWebappConstants.JETTY_EXTRA_CLASSPATH, extraClassPath);
    String bundleInstallOverride = (String) serviceRef.getProperty(OSGiWebappConstants.JETTY_BUNDLE_INSTALL_LOCATION_OVERRIDE);
    if (bundleInstallOverride == null)
        bundleInstallOverride = (String) serviceRef.getProperty(OSGiWebappConstants.SERVICE_PROP_BUNDLE_INSTALL_LOCATION_OVERRIDE);
    if (bundleInstallOverride != null)
        properties.put(OSGiWebappConstants.JETTY_BUNDLE_INSTALL_LOCATION_OVERRIDE, bundleInstallOverride);
    String requiredTlds = (String) serviceRef.getProperty(OSGiWebappConstants.REQUIRE_TLD_BUNDLE);
    if (requiredTlds == null)
        requiredTlds = (String) serviceRef.getProperty(OSGiWebappConstants.SERVICE_PROP_REQUIRE_TLD_BUNDLE);
    if (requiredTlds != null)
        properties.put(OSGiWebappConstants.REQUIRE_TLD_BUNDLE, requiredTlds);
    ClassLoader cl = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader(getServerInstanceWrapper().getParentClassLoaderForWebapps());
    try {
        String originId = getOriginId(serviceRef.getBundle(), base);
        ServiceApp app = new ServiceApp(getDeploymentManager(), this, serviceRef.getBundle(), properties, originId);
        app.setContextPath(contextPath);
        app.setWebAppPath(base);
        //set the pre=made webapp instance
        app.setWebAppContext(webApp);
        _serviceMap.put(serviceRef, app);
        getDeploymentManager().addApp(app);
        return true;
    } finally {
        Thread.currentThread().setContextClassLoader(cl);
    }
}
Also used : WebAppContext(org.eclipse.jetty.webapp.WebAppContext) Dictionary(java.util.Dictionary) Hashtable(java.util.Hashtable)

Example 94 with WebAppContext

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

the class TestJettyOSGiBootWebAppAsService method testBundle.

@Test
public void testBundle() throws Exception {
    // now test getting a static file
    HttpClient client = new HttpClient();
    try {
        client.start();
        ContentResponse response = client.GET("http://127.0.0.1:" + TestJettyOSGiBootCore.DEFAULT_HTTP_PORT + "/acme/index.html");
        assertEquals(HttpStatus.OK_200, response.getStatus());
        String content = new String(response.getContent());
        assertTrue(content.indexOf("<h1>Test OSGi WebAppA</h1>") != -1);
        response = client.GET("http://127.0.0.1:" + TestJettyOSGiBootCore.DEFAULT_HTTP_PORT + "/acme/mime");
        assertEquals(HttpStatus.OK_200, response.getStatus());
        content = new String(response.getContent());
        assertTrue(content.indexOf("MIMETYPE=application/gzip") != -1);
        response = client.GET("http://127.0.0.1:" + "9999" + "/acme/index.html");
        assertEquals(HttpStatus.OK_200, response.getStatus());
        content = new String(response.getContent());
        assertTrue(content.indexOf("<h1>Test OSGi WebAppB</h1>") != -1);
    } finally {
        client.stop();
    }
    ServiceReference[] refs = bundleContext.getServiceReferences(WebAppContext.class.getName(), null);
    assertNotNull(refs);
    assertEquals(2, refs.length);
    WebAppContext wac = (WebAppContext) bundleContext.getService(refs[0]);
    assertEquals("/acme", wac.getContextPath());
    wac = (WebAppContext) bundleContext.getService(refs[1]);
    assertEquals("/acme", wac.getContextPath());
}
Also used : WebAppContext(org.eclipse.jetty.webapp.WebAppContext) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpClient(org.eclipse.jetty.client.HttpClient) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 95 with WebAppContext

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

the class ConcatServletTest method testWEBINFResourceIsNotServed.

@Test
public void testWEBINFResourceIsNotServed() throws Exception {
    File directoryFile = MavenTestingUtils.getTargetTestingDir();
    Path directoryPath = directoryFile.toPath();
    Path hiddenDirectory = directoryPath.resolve("WEB-INF");
    Files.createDirectories(hiddenDirectory);
    Path hiddenResource = hiddenDirectory.resolve("one.js");
    try (OutputStream output = Files.newOutputStream(hiddenResource)) {
        output.write("function() {}".getBytes(StandardCharsets.UTF_8));
    }
    String contextPath = "";
    WebAppContext context = new WebAppContext(server, directoryPath.toString(), contextPath);
    server.setHandler(context);
    String concatPath = "/concat";
    context.addServlet(ConcatServlet.class, concatPath);
    server.start();
    // Verify that I can get the file programmatically, as required by the spec.
    Assert.assertNotNull(context.getServletContext().getResource("/WEB-INF/one.js"));
    // Having a path segment and then ".." triggers a special case
    // that the ConcatServlet must detect and avoid.
    String uri = contextPath + concatPath + "?/trick/../WEB-INF/one.js";
    String request = "" + "GET " + uri + " HTTP/1.1\r\n" + "Host: localhost\r\n" + "Connection: close\r\n" + "\r\n";
    String response = connector.getResponses(request);
    Assert.assertTrue(response.startsWith("HTTP/1.1 404 "));
    // Make sure ConcatServlet behaves well if it's case insensitive.
    uri = contextPath + concatPath + "?/trick/../web-inf/one.js";
    request = "" + "GET " + uri + " HTTP/1.1\r\n" + "Host: localhost\r\n" + "Connection: close\r\n" + "\r\n";
    response = connector.getResponses(request);
    Assert.assertTrue(response.startsWith("HTTP/1.1 404 "));
    // Make sure ConcatServlet behaves well if encoded.
    uri = contextPath + concatPath + "?/trick/..%2FWEB-INF%2Fone.js";
    request = "" + "GET " + uri + " HTTP/1.1\r\n" + "Host: localhost\r\n" + "Connection: close\r\n" + "\r\n";
    response = connector.getResponses(request);
    Assert.assertTrue(response.startsWith("HTTP/1.1 404 "));
    // Make sure ConcatServlet cannot see file system files.
    uri = contextPath + concatPath + "?/trick/../../" + directoryFile.getName();
    request = "" + "GET " + uri + " HTTP/1.1\r\n" + "Host: localhost\r\n" + "Connection: close\r\n" + "\r\n";
    response = connector.getResponses(request);
    Assert.assertTrue(response.startsWith("HTTP/1.1 404 "));
}
Also used : Path(java.nio.file.Path) WebAppContext(org.eclipse.jetty.webapp.WebAppContext) OutputStream(java.io.OutputStream) File(java.io.File) Test(org.junit.Test)

Aggregations

WebAppContext (org.eclipse.jetty.webapp.WebAppContext)142 Server (org.eclipse.jetty.server.Server)58 File (java.io.File)37 Test (org.junit.Test)29 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)20 ServerConnector (org.eclipse.jetty.server.ServerConnector)18 URL (java.net.URL)16 ArrayList (java.util.ArrayList)11 IOException (java.io.IOException)10 URI (java.net.URI)10 ContextHandler (org.eclipse.jetty.server.handler.ContextHandler)9 FileWriter (java.io.FileWriter)7 Configuration (org.apache.hadoop.conf.Configuration)7 HashLoginService (org.eclipse.jetty.security.HashLoginService)7 HttpConnectionFactory (org.eclipse.jetty.server.HttpConnectionFactory)7 FilterHolder (org.eclipse.jetty.servlet.FilterHolder)7 ServletMapping (org.eclipse.jetty.servlet.ServletMapping)7 BeforeClass (org.junit.BeforeClass)7 OutputStream (java.io.OutputStream)6 InitialContext (javax.naming.InitialContext)6