001    // Copyright 2008 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    //     http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package org.apache.tapestry5.internal.services;
016    
017    import org.apache.tapestry5.services.HttpServletRequestFilter;
018    import org.apache.tapestry5.services.HttpServletRequestHandler;
019    
020    import javax.servlet.http.HttpServletRequest;
021    import javax.servlet.http.HttpServletResponse;
022    import java.io.IOException;
023    import java.util.Collection;
024    import java.util.regex.Pattern;
025    
026    public class IgnoredPathsFilter implements HttpServletRequestFilter
027    {
028        private final Pattern[] ignoredPatterns;
029    
030        public IgnoredPathsFilter(Collection<String> configuration)
031        {
032            ignoredPatterns = new Pattern[configuration.size()];
033    
034            int i = 0;
035    
036            for (String regexp : configuration)
037            {
038                Pattern p = Pattern.compile(regexp, Pattern.CASE_INSENSITIVE);
039    
040                ignoredPatterns[i++] = p;
041            }
042        }
043    
044        public boolean service(HttpServletRequest request, HttpServletResponse response, HttpServletRequestHandler handler)
045                throws IOException
046        {
047            // The servlet path should be "/", and path info is everything after that.
048    
049            String path = request.getServletPath();
050            String pathInfo = request.getPathInfo();
051    
052            if (pathInfo != null) path += pathInfo;
053    
054    
055            for (Pattern p : ignoredPatterns)
056            {
057                if (p.matcher(path).matches()) return false;
058            }
059    
060            // Not a match, so let it go.
061    
062            return handler.service(request, response);
063        }
064    }