1313package com .amazonaws .serverless .proxy .internal .servlet ;
1414
1515import javax .servlet .DispatcherType ;
16+ import javax .servlet .ServletContext ;
1617import javax .servlet .http .HttpServletRequest ;
18+
1719import java .util .Collections ;
1820import java .util .HashMap ;
21+ import java .util .List ;
1922import java .util .Map ;
2023
2124/**
2629 * For example, the Spring implementation creates the ServletContext when the application is initialized the first time
2730 * and creates a FitlerChainManager to execute its filters for each request.
2831 */
29- public abstract class FilterChainManager <ServletContextType > {
32+ public abstract class FilterChainManager <ServletContextType extends ServletContext > {
3033
3134 //-------------------------------------------------------------
3235 // Variables - Protected
3336 //-------------------------------------------------------------
3437
35- protected static final String PATH_PART_SEPARATOR = "/" ;
38+ static final String PATH_PART_SEPARATOR = "/" ;
3639
3740
3841 //-------------------------------------------------------------
@@ -41,7 +44,7 @@ public abstract class FilterChainManager<ServletContextType> {
4144
4245 // we use the synchronizedMap because we do not expect high concurrency on this object. Lambda only allows one
4346 // event at a time per container
44- private Map <TargetCacheKey , FilterChainHolder > filterCache = Collections .synchronizedMap (new HashMap <TargetCacheKey , FilterChainHolder >());
47+ private Map <TargetCacheKey , List < FilterHolder >> filterCache = Collections .synchronizedMap (new HashMap <TargetCacheKey , List < FilterHolder > >());
4548 private int filtersSize = -1 ;
4649 protected ServletContextType servletContext ;
4750
@@ -50,7 +53,7 @@ public abstract class FilterChainManager<ServletContextType> {
5053 // Constructors
5154 //-------------------------------------------------------------
5255
53- protected FilterChainManager (ServletContextType context ) {
56+ FilterChainManager (ServletContextType context ) {
5457 servletContext = context ;
5558 }
5659
@@ -80,7 +83,7 @@ protected FilterChainManager(ServletContextType context) {
8083 * @param request The incoming servlet request
8184 * @return A <code>FilterChainHolder</code> object that can be used to apply the filters to the request
8285 */
83- public FilterChainHolder getFilterChain (final HttpServletRequest request ) {
86+ FilterChainHolder getFilterChain (final HttpServletRequest request ) {
8487 String targetPath = request .getServletPath ();
8588 DispatcherType type = request .getDispatcherType ();
8689
@@ -127,17 +130,22 @@ public FilterChainHolder getFilterChain(final HttpServletRequest request) {
127130
128131 /**
129132 * Retrieves a filter chain from the cache. The cache is lazily loaded as filter chains are requested. If the chain
130- * is not available in the cache, the method returns null.
133+ * is not available in the cache, the method returns null. This method returns a new instance of FilterChainHolder
134+ * initialized with the cached list of {@link FilterHolder} objects
131135 * @param type The dispatcher type for the incoming request
132136 * @param targetPath The request path - this is extracted with the <code>getPath</code> method of the request object
133137 * @return A populated FilterChainHolder
134138 */
135- protected FilterChainHolder getFilterChainCache (final DispatcherType type , final String targetPath ) {
139+ private FilterChainHolder getFilterChainCache (final DispatcherType type , final String targetPath ) {
136140 TargetCacheKey key = new TargetCacheKey ();
137141 key .setDispatcherType (type );
138142 key .setTargetPath (targetPath );
139143
140- return filterCache .get (key );
144+ if (!filterCache .containsKey (key )) {
145+ return null ;
146+ }
147+
148+ return new FilterChainHolder (filterCache .get (key ));
141149 }
142150
143151
@@ -150,7 +158,7 @@ protected FilterChainHolder getFilterChainCache(final DispatcherType type, final
150158 * @param targetPath The target path in the API
151159 * @param holder The FilterChainHolder object to save in the cache
152160 */
153- protected void putFilterChainCache (final DispatcherType type , final String targetPath , final FilterChainHolder holder ) {
161+ private void putFilterChainCache (final DispatcherType type , final String targetPath , final FilterChainHolder holder ) {
154162 TargetCacheKey key = new TargetCacheKey ();
155163 key .setDispatcherType (type );
156164 key .setTargetPath (targetPath );
@@ -159,8 +167,8 @@ protected void putFilterChainCache(final DispatcherType type, final String targe
159167 if (key .hashCode () == -1 ) {
160168 return ;
161169 }
170+ filterCache .put (key , holder .getFilters ());
162171
163- filterCache .put (key , holder );
164172 }
165173
166174
@@ -171,7 +179,7 @@ protected void putFilterChainCache(final DispatcherType type, final String targe
171179 * @param mapping The mapping path stored in the filter registration
172180 * @return true if the given mapping path can apply to the target, false otherwise.
173181 */
174- protected boolean pathMatches (final String target , final String mapping ) {
182+ boolean pathMatches (final String target , final String mapping ) {
175183 // easiest case, they are exactly the same
176184 if (target .toLowerCase ().equals (mapping .toLowerCase ())) {
177185 return true ;
@@ -275,34 +283,20 @@ public int hashCode() {
275283
276284 @ Override
277285 public boolean equals (Object key ) {
278- if (!key .getClass ().isAssignableFrom (TargetCacheKey .class )) {
279- return false ;
280- } else {
281- return hashCode () == key .hashCode ();
282- }
286+ return key .getClass ().isAssignableFrom (TargetCacheKey .class ) && hashCode () == key .hashCode ();
283287 }
284288
285289
286290 //-------------------------------------------------------------
287291 // Methods - Getter/Setter
288292 //-------------------------------------------------------------
289293
290- public String getTargetPath () {
291- return targetPath ;
292- }
293-
294-
295- public void setTargetPath (String targetPath ) {
294+ void setTargetPath (String targetPath ) {
296295 this .targetPath = targetPath ;
297296 }
298297
299298
300- public DispatcherType getDispatcherType () {
301- return dispatcherType ;
302- }
303-
304-
305- public void setDispatcherType (DispatcherType dispatcherType ) {
299+ void setDispatcherType (DispatcherType dispatcherType ) {
306300 this .dispatcherType = dispatcherType ;
307301 }
308302 }
0 commit comments