Search in sources :

Example 71 with FilterHolder

use of org.eclipse.jetty.servlet.FilterHolder in project hadoop by apache.

the class HttpServer2 method addFilter.

@Override
public void addFilter(String name, String classname, Map<String, String> parameters) {
    FilterHolder filterHolder = getFilterHolder(name, classname, parameters);
    final String[] USER_FACING_URLS = { "*.html", "*.jsp" };
    FilterMapping fmap = getFilterMapping(name, USER_FACING_URLS);
    defineFilter(webAppContext, filterHolder, fmap);
    LOG.info("Added filter " + name + " (class=" + classname + ") to context " + webAppContext.getDisplayName());
    final String[] ALL_URLS = { "/*" };
    fmap = getFilterMapping(name, ALL_URLS);
    for (Map.Entry<ServletContextHandler, Boolean> e : defaultContexts.entrySet()) {
        if (e.getValue()) {
            ServletContextHandler ctx = e.getKey();
            defineFilter(ctx, filterHolder, fmap);
            LOG.info("Added filter " + name + " (class=" + classname + ") to context " + ctx.getDisplayName());
        }
    }
    filterNames.add(name);
}
Also used : FilterHolder(org.eclipse.jetty.servlet.FilterHolder) FilterMapping(org.eclipse.jetty.servlet.FilterMapping) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap)

Example 72 with FilterHolder

use of org.eclipse.jetty.servlet.FilterHolder in project hadoop by apache.

the class HttpServer2 method addGlobalFilter.

@Override
public void addGlobalFilter(String name, String classname, Map<String, String> parameters) {
    final String[] ALL_URLS = { "/*" };
    FilterHolder filterHolder = getFilterHolder(name, classname, parameters);
    FilterMapping fmap = getFilterMapping(name, ALL_URLS);
    defineFilter(webAppContext, filterHolder, fmap);
    for (ServletContextHandler ctx : defaultContexts.keySet()) {
        defineFilter(ctx, filterHolder, fmap);
    }
    LOG.info("Added global filter '" + name + "' (class=" + classname + ")");
}
Also used : FilterHolder(org.eclipse.jetty.servlet.FilterHolder) FilterMapping(org.eclipse.jetty.servlet.FilterMapping) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler)

Example 73 with FilterHolder

use of org.eclipse.jetty.servlet.FilterHolder in project hadoop by apache.

the class HttpServer2 method getFilterHolder.

private static FilterHolder getFilterHolder(String name, String classname, Map<String, String> parameters) {
    FilterHolder holder = new FilterHolder();
    holder.setName(name);
    holder.setClassName(classname);
    if (parameters != null) {
        holder.setInitParameters(parameters);
    }
    return holder;
}
Also used : FilterHolder(org.eclipse.jetty.servlet.FilterHolder)

Example 74 with FilterHolder

use of org.eclipse.jetty.servlet.FilterHolder in project hadoop by apache.

the class TestWebDelegationToken method testRawHttpCalls.

@Test
public void testRawHttpCalls() throws Exception {
    final Server jetty = createJettyServer();
    ServletContextHandler context = new ServletContextHandler();
    context.setContextPath("/foo");
    jetty.setHandler(context);
    context.addFilter(new FilterHolder(AFilter.class), "/*", EnumSet.of(DispatcherType.REQUEST));
    context.addServlet(new ServletHolder(PingServlet.class), "/bar");
    try {
        jetty.start();
        URL nonAuthURL = new URL(getJettyURL() + "/foo/bar");
        URL authURL = new URL(getJettyURL() + "/foo/bar?authenticated=foo");
        // unauthenticated access to URL
        HttpURLConnection conn = (HttpURLConnection) nonAuthURL.openConnection();
        Assert.assertEquals(HttpURLConnection.HTTP_UNAUTHORIZED, conn.getResponseCode());
        // authenticated access to URL
        conn = (HttpURLConnection) authURL.openConnection();
        Assert.assertEquals(HttpURLConnection.HTTP_OK, conn.getResponseCode());
        // unauthenticated access to get delegation token
        URL url = new URL(nonAuthURL.toExternalForm() + "?op=GETDELEGATIONTOKEN");
        conn = (HttpURLConnection) url.openConnection();
        Assert.assertEquals(HttpURLConnection.HTTP_UNAUTHORIZED, conn.getResponseCode());
        // authenticated access to get delegation token
        url = new URL(authURL.toExternalForm() + "&op=GETDELEGATIONTOKEN&renewer=foo");
        conn = (HttpURLConnection) url.openConnection();
        Assert.assertEquals(HttpURLConnection.HTTP_OK, conn.getResponseCode());
        ObjectMapper mapper = new ObjectMapper();
        Map map = mapper.readValue(conn.getInputStream(), Map.class);
        String dt = (String) ((Map) map.get("Token")).get("urlString");
        Assert.assertNotNull(dt);
        // delegation token access to URL
        url = new URL(nonAuthURL.toExternalForm() + "?delegation=" + dt);
        conn = (HttpURLConnection) url.openConnection();
        Assert.assertEquals(HttpURLConnection.HTTP_OK, conn.getResponseCode());
        // delegation token and authenticated access to URL
        url = new URL(authURL.toExternalForm() + "&delegation=" + dt);
        conn = (HttpURLConnection) url.openConnection();
        Assert.assertEquals(HttpURLConnection.HTTP_OK, conn.getResponseCode());
        // renewew delegation token, unauthenticated access to URL
        url = new URL(nonAuthURL.toExternalForm() + "?op=RENEWDELEGATIONTOKEN&token=" + dt);
        conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("PUT");
        Assert.assertEquals(HttpURLConnection.HTTP_UNAUTHORIZED, conn.getResponseCode());
        // renewew delegation token, authenticated access to URL
        url = new URL(authURL.toExternalForm() + "&op=RENEWDELEGATIONTOKEN&token=" + dt);
        conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("PUT");
        Assert.assertEquals(HttpURLConnection.HTTP_OK, conn.getResponseCode());
        // renewew delegation token, authenticated access to URL, not renewer
        url = new URL(getJettyURL() + "/foo/bar?authenticated=bar&op=RENEWDELEGATIONTOKEN&token=" + dt);
        conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("PUT");
        Assert.assertEquals(HttpURLConnection.HTTP_FORBIDDEN, conn.getResponseCode());
        // cancel delegation token, nonauthenticated access to URL
        url = new URL(nonAuthURL.toExternalForm() + "?op=CANCELDELEGATIONTOKEN&token=" + dt);
        conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("PUT");
        Assert.assertEquals(HttpURLConnection.HTTP_OK, conn.getResponseCode());
        // cancel canceled delegation token, nonauthenticated access to URL
        url = new URL(nonAuthURL.toExternalForm() + "?op=CANCELDELEGATIONTOKEN&token=" + dt);
        conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("PUT");
        Assert.assertEquals(HttpURLConnection.HTTP_NOT_FOUND, conn.getResponseCode());
        // get new delegation token
        url = new URL(authURL.toExternalForm() + "&op=GETDELEGATIONTOKEN&renewer=foo");
        conn = (HttpURLConnection) url.openConnection();
        Assert.assertEquals(HttpURLConnection.HTTP_OK, conn.getResponseCode());
        mapper = new ObjectMapper();
        map = mapper.readValue(conn.getInputStream(), Map.class);
        dt = (String) ((Map) map.get("Token")).get("urlString");
        Assert.assertNotNull(dt);
        // cancel delegation token, authenticated access to URL
        url = new URL(authURL.toExternalForm() + "&op=CANCELDELEGATIONTOKEN&token=" + dt);
        conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("PUT");
        Assert.assertEquals(HttpURLConnection.HTTP_OK, conn.getResponseCode());
    } finally {
        jetty.stop();
    }
}
Also used : FilterHolder(org.eclipse.jetty.servlet.FilterHolder) HttpURLConnection(java.net.HttpURLConnection) Server(org.eclipse.jetty.server.Server) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) Map(java.util.Map) HashMap(java.util.HashMap) URL(java.net.URL) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 75 with FilterHolder

use of org.eclipse.jetty.servlet.FilterHolder in project hadoop by apache.

the class TestWebDelegationToken method testExternalDelegationTokenSecretManager.

@Test
public void testExternalDelegationTokenSecretManager() throws Exception {
    DummyDelegationTokenSecretManager secretMgr = new DummyDelegationTokenSecretManager();
    final Server jetty = createJettyServer();
    ServletContextHandler context = new ServletContextHandler();
    context.setContextPath("/foo");
    jetty.setHandler(context);
    context.addFilter(new FilterHolder(AFilter.class), "/*", EnumSet.of(DispatcherType.REQUEST));
    context.addServlet(new ServletHolder(PingServlet.class), "/bar");
    try {
        secretMgr.startThreads();
        context.setAttribute(DelegationTokenAuthenticationFilter.DELEGATION_TOKEN_SECRET_MANAGER_ATTR, secretMgr);
        jetty.start();
        URL authURL = new URL(getJettyURL() + "/foo/bar?authenticated=foo");
        DelegationTokenAuthenticatedURL.Token token = new DelegationTokenAuthenticatedURL.Token();
        DelegationTokenAuthenticatedURL aUrl = new DelegationTokenAuthenticatedURL();
        aUrl.getDelegationToken(authURL, token, FOO_USER);
        Assert.assertNotNull(token.getDelegationToken());
        Assert.assertEquals(new Text("fooKind"), token.getDelegationToken().getKind());
    } finally {
        jetty.stop();
        secretMgr.stopThreads();
    }
}
Also used : FilterHolder(org.eclipse.jetty.servlet.FilterHolder) Server(org.eclipse.jetty.server.Server) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) AuthenticationToken(org.apache.hadoop.security.authentication.server.AuthenticationToken) Text(org.apache.hadoop.io.Text) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) URL(java.net.URL) Test(org.junit.Test)

Aggregations

FilterHolder (org.eclipse.jetty.servlet.FilterHolder)84 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)44 Test (org.junit.Test)35 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)32 Server (org.eclipse.jetty.server.Server)21 CountDownLatch (java.util.concurrent.CountDownLatch)18 Filter (javax.servlet.Filter)11 IOException (java.io.IOException)10 URL (java.net.URL)10 DispatcherType (javax.servlet.DispatcherType)9 ServletException (javax.servlet.ServletException)8 AuthenticationToken (org.apache.hadoop.security.authentication.server.AuthenticationToken)8 ServerConnector (org.eclipse.jetty.server.ServerConnector)8 File (java.io.File)7 HttpURLConnection (java.net.HttpURLConnection)7 PrivilegedActionException (java.security.PrivilegedActionException)7 AuthenticationException (org.apache.hadoop.security.authentication.client.AuthenticationException)7 FilterMapping (org.eclipse.jetty.servlet.FilterMapping)7 WebAppContext (org.eclipse.jetty.webapp.WebAppContext)7 List (java.util.List)6