KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > website > filter > BotSessionFilter


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2006
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.website.filter;
25
26 import java.io.IOException JavaDoc;
27 import java.util.regex.Pattern JavaDoc;
28
29 import javax.servlet.FilterChain JavaDoc;
30 import javax.servlet.ServletException JavaDoc;
31 import javax.servlet.http.HttpServletRequest JavaDoc;
32 import javax.servlet.http.HttpServletResponse JavaDoc;
33 import javax.servlet.http.HttpServletResponseWrapper JavaDoc;
34
35 import org.springframework.web.filter.OncePerRequestFilter;
36
37 /**
38  * Filter that prevents URLs from being encoded if the request is originated
39  * by search engine robot/crawler.
40  *
41  * @author Felix Gnass [fgnass at neteye dot de]
42  * @since 6.4
43  */

44 public class BotSessionFilter extends OncePerRequestFilter {
45
46     public static final String JavaDoc DEFAULT_BOT_PATTERN = "bot|crawler|spider";
47     
48     private static final String JavaDoc USER_AGENT_HEADER = "User-Agent";
49     
50     private Pattern JavaDoc pattern = Pattern.compile(DEFAULT_BOT_PATTERN);
51     
52     /**
53      * Sets the regular expression that is used to check whether a User-Agent
54      * belongs to a robot/spider/crawler. The pattern does not need to match
55      * the whole User-Agent header, it is checked whether the patter is
56      * <i>included</i> within the string. Note that the pattern is
57      * <b>case sensitive</b> and the header is <b>converted to lower case</b>
58      * before the pattern is checked. The default is
59      * <code>bot|crawler|spider</code>.
60      */

61     public void setPattern(String JavaDoc pattern) {
62         this.pattern = Pattern.compile(pattern);
63     }
64     
65     /**
66      * Skips the filtering if a session id was requested using a cookie or
67      * the User-Agent header does not look like the one of a robot.
68      */

69     protected boolean shouldNotFilter(HttpServletRequest JavaDoc request) {
70         return request.isRequestedSessionIdFromCookie() || !isBot(request);
71     }
72     
73     /**
74      * Returns <code>true</code> if the botPattern is found in the
75      * User-Agent header. Note that the header is converted to lower case
76      * before the regular expression is checked.
77      */

78     protected boolean isBot(HttpServletRequest JavaDoc request) {
79         String JavaDoc agent = request.getHeader(USER_AGENT_HEADER);
80         if (agent != null) {
81             return pattern.matcher(agent.toLowerCase()).find();
82         }
83         return false;
84     }
85     
86     /**
87      * Passes the request on to the filter chain using a response wrapper that
88      * prevents URLs from being encoded.
89      */

90     protected final void doFilterInternal(HttpServletRequest JavaDoc request,
91             HttpServletResponse JavaDoc response, FilterChain JavaDoc filterChain)
92             throws ServletException JavaDoc, IOException JavaDoc {
93         
94         filterChain.doFilter(request, new NoRewriteResponse(response));
95     }
96     
97     /**
98      * Response wrapper that prevents URLs from being encoded.
99      */

100     private static class NoRewriteResponse extends HttpServletResponseWrapper JavaDoc {
101         
102         public NoRewriteResponse(HttpServletResponse JavaDoc response) {
103             super(response);
104         }
105         
106         public String JavaDoc encodeRedirectUrl(String JavaDoc url) {
107             return url;
108         }
109         
110         public String JavaDoc encodeRedirectURL(String JavaDoc url) {
111             return url;
112         }
113         
114         public String JavaDoc encodeUrl(String JavaDoc url) {
115             return url;
116         }
117         
118         public String JavaDoc encodeURL(String JavaDoc url) {
119             return url;
120         }
121     }
122
123 }
124
Popular Tags