From 19b298f6124f98770c0831dc9e13ddfccb525a3c Mon Sep 17 00:00:00 2001 From: Haohui Mai Date: Wed, 18 Mar 2015 22:31:59 -0700 Subject: [PATCH] HADOOP-10703. HttpServer2 creates multiple authentication filters. Contributed by Benoy Antony. --- .../hadoop-common/CHANGES.txt | 3 ++ .../org/apache/hadoop/http/HttpServer2.java | 43 ++++++++++++++----- 2 files changed, 36 insertions(+), 10 deletions(-) diff --git a/hadoop-common-project/hadoop-common/CHANGES.txt b/hadoop-common-project/hadoop-common/CHANGES.txt index ab88d27b36e..e542b129f87 100644 --- a/hadoop-common-project/hadoop-common/CHANGES.txt +++ b/hadoop-common-project/hadoop-common/CHANGES.txt @@ -1121,6 +1121,9 @@ Release 2.7.0 - UNRELEASED ZKDelegationTokenSecretManager go down when old token cannot be deleted. (Arun Suresh via atm) + HADOOP-10703. HttpServer2 creates multiple authentication filters. + (Benoy Antony via wheat9) + Release 2.6.1 - UNRELEASED INCOMPATIBLE CHANGES diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/http/HttpServer2.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/http/HttpServer2.java index 80831e976aa..566861e046f 100644 --- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/http/HttpServer2.java +++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/http/HttpServer2.java @@ -594,15 +594,18 @@ public final class HttpServer2 implements FilterContainer { public void addFilter(String name, String classname, Map parameters) { + FilterHolder filterHolder = getFilterHolder(name, classname, parameters); final String[] USER_FACING_URLS = { "*.html", "*.jsp" }; - defineFilter(webAppContext, name, classname, parameters, USER_FACING_URLS); + 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 e : defaultContexts.entrySet()) { if (e.getValue()) { Context ctx = e.getKey(); - defineFilter(ctx, name, classname, parameters, ALL_URLS); + defineFilter(ctx, filterHolder, fmap); LOG.info("Added filter " + name + " (class=" + classname + ") to context " + ctx.getDisplayName()); } @@ -614,9 +617,11 @@ public final class HttpServer2 implements FilterContainer { public void addGlobalFilter(String name, String classname, Map parameters) { final String[] ALL_URLS = { "/*" }; - defineFilter(webAppContext, name, classname, parameters, ALL_URLS); + FilterHolder filterHolder = getFilterHolder(name, classname, parameters); + FilterMapping fmap = getFilterMapping(name, ALL_URLS); + defineFilter(webAppContext, filterHolder, fmap); for (Context ctx : defaultContexts.keySet()) { - defineFilter(ctx, name, classname, parameters, ALL_URLS); + defineFilter(ctx, filterHolder, fmap); } LOG.info("Added global filter '" + name + "' (class=" + classname + ")"); } @@ -626,17 +631,35 @@ public final class HttpServer2 implements FilterContainer { */ public static void defineFilter(Context ctx, String name, String classname, Map parameters, String[] urls) { + FilterHolder filterHolder = getFilterHolder(name, classname, parameters); + FilterMapping fmap = getFilterMapping(name, urls); + defineFilter(ctx, filterHolder, fmap); + } - FilterHolder holder = new FilterHolder(); - holder.setName(name); - holder.setClassName(classname); - holder.setInitParameters(parameters); + /** + * Define a filter for a context and set up default url mappings. + */ + private static void defineFilter(Context ctx, FilterHolder holder, + FilterMapping fmap) { + ServletHandler handler = ctx.getServletHandler(); + handler.addFilter(holder, fmap); + } + + private static FilterMapping getFilterMapping(String name, String[] urls) { FilterMapping fmap = new FilterMapping(); fmap.setPathSpecs(urls); fmap.setDispatches(Handler.ALL); fmap.setFilterName(name); - ServletHandler handler = ctx.getServletHandler(); - handler.addFilter(holder, fmap); + return fmap; + } + + private static FilterHolder getFilterHolder(String name, String classname, + Map parameters) { + FilterHolder holder = new FilterHolder(); + holder.setName(name); + holder.setClassName(classname); + holder.setInitParameters(parameters); + return holder; } /**