Author: markt
Date: Fri Oct  9 19:53:51 2015
New Revision: 1707804

URL: http://svn.apache.org/viewvc?rev=1707804&view=rev
Log:
Take advantage of GenericFilter

Modified:
    tomcat/trunk/java/org/apache/catalina/filters/CorsFilter.java
    tomcat/trunk/java/org/apache/catalina/filters/RemoteIpFilter.java
    
tomcat/trunk/test/org/apache/catalina/startup/DuplicateMappingParamFilter.java

Modified: tomcat/trunk/java/org/apache/catalina/filters/CorsFilter.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/filters/CorsFilter.java?rev=1707804&r1=1707803&r2=1707804&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/filters/CorsFilter.java (original)
+++ tomcat/trunk/java/org/apache/catalina/filters/CorsFilter.java Fri Oct  9 
19:53:51 2015
@@ -27,9 +27,8 @@ import java.util.List;
 import java.util.Locale;
 import java.util.Set;
 
-import javax.servlet.Filter;
 import javax.servlet.FilterChain;
-import javax.servlet.FilterConfig;
+import javax.servlet.GenericFilter;
 import javax.servlet.ServletException;
 import javax.servlet.ServletRequest;
 import javax.servlet.ServletResponse;
@@ -42,8 +41,8 @@ import org.apache.tomcat.util.res.String
 
 /**
  * <p>
- * A {@link Filter} that enable client-side cross-origin requests by
- * implementing W3C's CORS (<b>C</b>ross-<b>O</b>rigin <b>R</b>esource
+ * A {@link javax.servlet.Filter} that enable client-side cross-origin requests
+ * by implementing W3C's CORS (<b>C</b>ross-<b>O</b>rigin <b>R</b>esource
  * <b>S</b>haring) specification for resources. Each {@link HttpServletRequest}
  * request is inspected as per specification, and appropriate response headers
  * are added to {@link HttpServletResponse}.
@@ -76,8 +75,9 @@ import org.apache.tomcat.util.res.String
  * @see <a href="http://www.w3.org/TR/cors/";>CORS specification</a>
  *
  */
-public final class CorsFilter implements Filter {
+public final class CorsFilter extends GenericFilter {
 
+    private static final long serialVersionUID = 1L;
     private static final Log log = LogFactory.getLog(CorsFilter.class);
     private static final StringManager sm = 
StringManager.getManager(CorsFilter.class);
 
@@ -86,7 +86,7 @@ public final class CorsFilter implements
      * A {@link Collection} of origins consisting of zero or more origins that
      * are allowed access to the resource.
      */
-    private final Collection<String> allowedOrigins;
+    private final Collection<String> allowedOrigins = new HashSet<>();
 
     /**
      * Determines if any origin is allowed to make request.
@@ -97,20 +97,20 @@ public final class CorsFilter implements
      * A {@link Collection} of methods consisting of zero or more methods that
      * are supported by the resource.
      */
-    private final Collection<String> allowedHttpMethods;
+    private final Collection<String> allowedHttpMethods = new HashSet<>();
 
     /**
      * A {@link Collection} of headers consisting of zero or more header field
      * names that are supported by the resource.
      */
-    private final Collection<String> allowedHttpHeaders;
+    private final Collection<String> allowedHttpHeaders = new HashSet<>();
 
     /**
      * A {@link Collection} of exposed headers consisting of zero or more 
header
      * field names of headers other than the simple response headers that the
      * resource might use and can be exposed.
      */
-    private final Collection<String> exposedHeaders;
+    private final Collection<String> exposedHeaders = new HashSet<>();
 
     /**
      * A supports credentials flag that indicates whether the resource supports
@@ -131,14 +131,6 @@ public final class CorsFilter implements
     private boolean decorateRequest;
 
 
-    public CorsFilter() {
-        this.allowedOrigins = new HashSet<>();
-        this.allowedHttpMethods = new HashSet<>();
-        this.allowedHttpHeaders = new HashSet<>();
-        this.exposedHeaders = new HashSet<>();
-    }
-
-
     @Override
     public void doFilter(final ServletRequest servletRequest,
             final ServletResponse servletResponse, final FilterChain 
filterChain)
@@ -185,34 +177,25 @@ public final class CorsFilter implements
 
 
     @Override
-    public void init(final FilterConfig filterConfig) throws ServletException {
+    public void init() throws ServletException {
         // Initialize defaults
         parseAndStore(DEFAULT_ALLOWED_ORIGINS, DEFAULT_ALLOWED_HTTP_METHODS,
                 DEFAULT_ALLOWED_HTTP_HEADERS, DEFAULT_EXPOSED_HEADERS,
                 DEFAULT_SUPPORTS_CREDENTIALS, DEFAULT_PREFLIGHT_MAXAGE,
                 DEFAULT_DECORATE_REQUEST);
 
-        if (filterConfig != null) {
-            String configAllowedOrigins = filterConfig
-                    .getInitParameter(PARAM_CORS_ALLOWED_ORIGINS);
-            String configAllowedHttpMethods = filterConfig
-                    .getInitParameter(PARAM_CORS_ALLOWED_METHODS);
-            String configAllowedHttpHeaders = filterConfig
-                    .getInitParameter(PARAM_CORS_ALLOWED_HEADERS);
-            String configExposedHeaders = filterConfig
-                    .getInitParameter(PARAM_CORS_EXPOSED_HEADERS);
-            String configSupportsCredentials = filterConfig
-                    .getInitParameter(PARAM_CORS_SUPPORT_CREDENTIALS);
-            String configPreflightMaxAge = filterConfig
-                    .getInitParameter(PARAM_CORS_PREFLIGHT_MAXAGE);
-            String configDecorateRequest = filterConfig
-                    .getInitParameter(PARAM_CORS_REQUEST_DECORATE);
-
-            parseAndStore(configAllowedOrigins, configAllowedHttpMethods,
-                    configAllowedHttpHeaders, configExposedHeaders,
-                    configSupportsCredentials, configPreflightMaxAge,
-                    configDecorateRequest);
-        }
+        String configAllowedOrigins = 
getInitParameter(PARAM_CORS_ALLOWED_ORIGINS);
+        String configAllowedHttpMethods = 
getInitParameter(PARAM_CORS_ALLOWED_METHODS);
+        String configAllowedHttpHeaders = 
getInitParameter(PARAM_CORS_ALLOWED_HEADERS);
+        String configExposedHeaders = 
getInitParameter(PARAM_CORS_EXPOSED_HEADERS);
+        String configSupportsCredentials = 
getInitParameter(PARAM_CORS_SUPPORT_CREDENTIALS);
+        String configPreflightMaxAge = 
getInitParameter(PARAM_CORS_PREFLIGHT_MAXAGE);
+        String configDecorateRequest = 
getInitParameter(PARAM_CORS_REQUEST_DECORATE);
+
+        parseAndStore(configAllowedOrigins, configAllowedHttpMethods,
+                configAllowedHttpHeaders, configExposedHeaders,
+                configSupportsCredentials, configPreflightMaxAge,
+                configDecorateRequest);
     }
 
 
@@ -474,12 +457,6 @@ public final class CorsFilter implements
     }
 
 
-    @Override
-    public void destroy() {
-        // NOOP
-    }
-
-
     /**
      * Decorates the {@link HttpServletRequest}, with CORS attributes.
      * <ul>
@@ -887,7 +864,9 @@ public final class CorsFilter implements
 
 
     /**
-     * Returns a {@link Set} of headers that should be exposed by browser.
+     * Obtain the headers to expose.
+     *
+     * @return the headers that should be exposed by browser.
      */
     public Collection<String> getExposedHeaders() {
         return exposedHeaders;
@@ -896,6 +875,9 @@ public final class CorsFilter implements
 
     /**
      * Determines is supports credentials is enabled.
+     *
+     * @return <code>true</code> if the use of credentials is supported
+     *         otherwise <code>false</code>
      */
     public boolean isSupportsCredentials() {
         return supportsCredentials;
@@ -1128,37 +1110,39 @@ public final class CorsFilter implements
 
     // ----------------------------------------Filter Config Init param-name(s)
     /**
-     * Key to retrieve allowed origins from {@link FilterConfig}.
+     * Key to retrieve allowed origins from {@link javax.servlet.FilterConfig}.
      */
     public static final String PARAM_CORS_ALLOWED_ORIGINS =
             "cors.allowed.origins";
 
     /**
-     * Key to retrieve support credentials from {@link FilterConfig}.
+     * Key to retrieve support credentials from
+     * {@link javax.servlet.FilterConfig}.
      */
     public static final String PARAM_CORS_SUPPORT_CREDENTIALS =
             "cors.support.credentials";
 
     /**
-     * Key to retrieve exposed headers from {@link FilterConfig}.
+     * Key to retrieve exposed headers from {@link javax.servlet.FilterConfig}.
      */
     public static final String PARAM_CORS_EXPOSED_HEADERS =
             "cors.exposed.headers";
 
     /**
-     * Key to retrieve allowed headers from {@link FilterConfig}.
+     * Key to retrieve allowed headers from {@link javax.servlet.FilterConfig}.
      */
     public static final String PARAM_CORS_ALLOWED_HEADERS =
             "cors.allowed.headers";
 
     /**
-     * Key to retrieve allowed methods from {@link FilterConfig}.
+     * Key to retrieve allowed methods from {@link javax.servlet.FilterConfig}.
      */
     public static final String PARAM_CORS_ALLOWED_METHODS =
             "cors.allowed.methods";
 
     /**
-     * Key to retrieve preflight max age from {@link FilterConfig}.
+     * Key to retrieve preflight max age from
+     * {@link javax.servlet.FilterConfig}.
      */
     public static final String PARAM_CORS_PREFLIGHT_MAXAGE =
             "cors.preflight.maxage";

Modified: tomcat/trunk/java/org/apache/catalina/filters/RemoteIpFilter.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/filters/RemoteIpFilter.java?rev=1707804&r1=1707803&r2=1707804&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/filters/RemoteIpFilter.java (original)
+++ tomcat/trunk/java/org/apache/catalina/filters/RemoteIpFilter.java Fri Oct  
9 19:53:51 2015
@@ -33,9 +33,8 @@ import java.util.Locale;
 import java.util.Map;
 import java.util.regex.Pattern;
 
-import javax.servlet.Filter;
 import javax.servlet.FilterChain;
-import javax.servlet.FilterConfig;
+import javax.servlet.GenericFilter;
 import javax.servlet.ServletException;
 import javax.servlet.ServletRequest;
 import javax.servlet.ServletResponse;
@@ -440,7 +439,10 @@ import org.apache.tomcat.util.res.String
  * </p>
  * <hr>
  */
-public class RemoteIpFilter implements Filter {
+public class RemoteIpFilter extends GenericFilter {
+
+    private static final long serialVersionUID = 1L;
+
     public static class XForwardedRequest extends HttpServletRequestWrapper {
 
         static final ThreadLocal<SimpleDateFormat[]> threadLocalDateFormats = 
new ThreadLocal<SimpleDateFormat[]>() {
@@ -778,11 +780,6 @@ public class RemoteIpFilter implements F
      */
     private Pattern trustedProxies = null;
 
-    @Override
-    public void destroy() {
-        // NOOP
-    }
-
     public void doFilter(HttpServletRequest request, HttpServletResponse 
response, FilterChain chain) throws IOException, ServletException {
 
         if (internalProxies != null &&
@@ -977,50 +974,50 @@ public class RemoteIpFilter implements F
     }
 
     @Override
-    public void init(FilterConfig filterConfig) throws ServletException {
-        if (filterConfig.getInitParameter(INTERNAL_PROXIES_PARAMETER) != null) 
{
-            
setInternalProxies(filterConfig.getInitParameter(INTERNAL_PROXIES_PARAMETER));
+    public void init() throws ServletException {
+        if (getInitParameter(INTERNAL_PROXIES_PARAMETER) != null) {
+            setInternalProxies(getInitParameter(INTERNAL_PROXIES_PARAMETER));
         }
 
-        if (filterConfig.getInitParameter(PROTOCOL_HEADER_PARAMETER) != null) {
-            
setProtocolHeader(filterConfig.getInitParameter(PROTOCOL_HEADER_PARAMETER));
+        if (getInitParameter(PROTOCOL_HEADER_PARAMETER) != null) {
+            setProtocolHeader(getInitParameter(PROTOCOL_HEADER_PARAMETER));
         }
 
-        if 
(filterConfig.getInitParameter(PROTOCOL_HEADER_HTTPS_VALUE_PARAMETER) != null) {
-            
setProtocolHeaderHttpsValue(filterConfig.getInitParameter(PROTOCOL_HEADER_HTTPS_VALUE_PARAMETER));
+        if (getInitParameter(PROTOCOL_HEADER_HTTPS_VALUE_PARAMETER) != null) {
+            
setProtocolHeaderHttpsValue(getInitParameter(PROTOCOL_HEADER_HTTPS_VALUE_PARAMETER));
         }
 
-        if (filterConfig.getInitParameter(PORT_HEADER_PARAMETER) != null) {
-            
setPortHeader(filterConfig.getInitParameter(PORT_HEADER_PARAMETER));
+        if (getInitParameter(PORT_HEADER_PARAMETER) != null) {
+            setPortHeader(getInitParameter(PORT_HEADER_PARAMETER));
         }
 
-        if (filterConfig.getInitParameter(CHANGE_LOCAL_PORT_PARAMETER) != 
null) {
-            
setChangeLocalPort(Boolean.parseBoolean(filterConfig.getInitParameter(CHANGE_LOCAL_PORT_PARAMETER)));
+        if (getInitParameter(CHANGE_LOCAL_PORT_PARAMETER) != null) {
+            
setChangeLocalPort(Boolean.parseBoolean(getInitParameter(CHANGE_LOCAL_PORT_PARAMETER)));
         }
 
-        if (filterConfig.getInitParameter(PROXIES_HEADER_PARAMETER) != null) {
-            
setProxiesHeader(filterConfig.getInitParameter(PROXIES_HEADER_PARAMETER));
+        if (getInitParameter(PROXIES_HEADER_PARAMETER) != null) {
+            setProxiesHeader(getInitParameter(PROXIES_HEADER_PARAMETER));
         }
 
-        if (filterConfig.getInitParameter(REMOTE_IP_HEADER_PARAMETER) != null) 
{
-            
setRemoteIpHeader(filterConfig.getInitParameter(REMOTE_IP_HEADER_PARAMETER));
+        if (getInitParameter(REMOTE_IP_HEADER_PARAMETER) != null) {
+            setRemoteIpHeader(getInitParameter(REMOTE_IP_HEADER_PARAMETER));
         }
 
-        if (filterConfig.getInitParameter(TRUSTED_PROXIES_PARAMETER) != null) {
-            
setTrustedProxies(filterConfig.getInitParameter(TRUSTED_PROXIES_PARAMETER));
+        if (getInitParameter(TRUSTED_PROXIES_PARAMETER) != null) {
+            setTrustedProxies(getInitParameter(TRUSTED_PROXIES_PARAMETER));
         }
 
-        if (filterConfig.getInitParameter(HTTP_SERVER_PORT_PARAMETER) != null) 
{
+        if (getInitParameter(HTTP_SERVER_PORT_PARAMETER) != null) {
             try {
-                
setHttpServerPort(Integer.parseInt(filterConfig.getInitParameter(HTTP_SERVER_PORT_PARAMETER)));
+                
setHttpServerPort(Integer.parseInt(getInitParameter(HTTP_SERVER_PORT_PARAMETER)));
             } catch (NumberFormatException e) {
                 throw new NumberFormatException("Illegal " + 
HTTP_SERVER_PORT_PARAMETER + " : " + e.getMessage());
             }
         }
 
-        if (filterConfig.getInitParameter(HTTPS_SERVER_PORT_PARAMETER) != 
null) {
+        if (getInitParameter(HTTPS_SERVER_PORT_PARAMETER) != null) {
             try {
-                
setHttpsServerPort(Integer.parseInt(filterConfig.getInitParameter(HTTPS_SERVER_PORT_PARAMETER)));
+                
setHttpsServerPort(Integer.parseInt(getInitParameter(HTTPS_SERVER_PORT_PARAMETER)));
             } catch (NumberFormatException e) {
                 throw new NumberFormatException("Illegal " + 
HTTPS_SERVER_PORT_PARAMETER + " : " + e.getMessage());
             }

Modified: 
tomcat/trunk/test/org/apache/catalina/startup/DuplicateMappingParamFilter.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/catalina/startup/DuplicateMappingParamFilter.java?rev=1707804&r1=1707803&r2=1707804&view=diff
==============================================================================
--- 
tomcat/trunk/test/org/apache/catalina/startup/DuplicateMappingParamFilter.java 
(original)
+++ 
tomcat/trunk/test/org/apache/catalina/startup/DuplicateMappingParamFilter.java 
Fri Oct  9 19:53:51 2015
@@ -18,9 +18,8 @@ package org.apache.catalina.startup;
 
 import java.io.IOException;
 
-import javax.servlet.Filter;
 import javax.servlet.FilterChain;
-import javax.servlet.FilterConfig;
+import javax.servlet.GenericFilter;
 import javax.servlet.ServletException;
 import javax.servlet.ServletRequest;
 import javax.servlet.ServletResponse;
@@ -34,23 +33,14 @@ import javax.servlet.annotation.WebFilte
  */
 @WebFilter(value = "/param", filterName="paramDFilter",
         urlPatterns = { "/param1" , "/param2" })
-public class DuplicateMappingParamFilter implements Filter {
+public class DuplicateMappingParamFilter extends GenericFilter {
 
+    private static final long serialVersionUID = 1L;
 
     @Override
-    public void init(FilterConfig filterConfig) throws ServletException {
-        // NO-OP
-    }
-
-    @Override
-    public void doFilter(ServletRequest req, ServletResponse res,
-            FilterChain chain) throws ServletException, IOException {
+    public void doFilter(ServletRequest req, ServletResponse res, FilterChain 
chain)
+            throws ServletException, IOException {
         chain.doFilter(req, res);
     }
-
-    @Override
-    public void destroy() {
-        // destroy
-    }
 }
 



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to