KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > blojsom > filter > PermalinkFilter


1 /**
2  * Copyright (c) 2003-2006, David A. Czarnecki
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * Redistributions of source code must retain the above copyright notice, this list of conditions and the
9  * following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
11  * following disclaimer in the documentation and/or other materials provided with the distribution.
12  * Neither the name of "David A. Czarnecki" and "blojsom" nor the names of its contributors may be used to
13  * endorse or promote products derived from this software without specific prior written permission.
14  * Products derived from this software may not be called "blojsom", nor may "blojsom" appear in their name,
15  * without prior written permission of David A. Czarnecki.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
18  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
19  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
20  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
21  * EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */

31 package org.blojsom.filter;
32
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35 import org.blojsom.util.BlojsomUtils;
36 import org.blojsom.util.BlojsomConstants;
37
38 import javax.servlet.*;
39 import javax.servlet.http.HttpServletRequest JavaDoc;
40 import javax.servlet.http.HttpServletRequestWrapper JavaDoc;
41 import java.io.IOException JavaDoc;
42 import java.util.*;
43 import java.util.regex.Matcher JavaDoc;
44 import java.util.regex.Pattern JavaDoc;
45
46 /**
47  * PermalinkFilter
48  *
49  * @author David Czarnecki
50  * @since blojsom 3.0
51  * @version $Id: PermalinkFilter.java,v 1.1 2006/03/20 21:31:15 czarneckid Exp $
52  */

53 public class PermalinkFilter implements Filter {
54
55     private static final Log _logger = LogFactory.getLog(PermalinkFilter.class);
56
57     private static final String JavaDoc YMD_PERMALINK_REGEX = "/(\\d\\d\\d\\d)/(\\d{1,2}+)/(\\d{1,2}+)/(.+)";
58     private static final Pattern JavaDoc YMD_PERMALINK_PATTERN = Pattern.compile(YMD_PERMALINK_REGEX, Pattern.UNICODE_CASE);
59     private static final String JavaDoc YMD_REGEX = "/(\\d\\d\\d\\d)/(\\d{1,2}+)/(\\d{1,2}+)/";
60     private static final Pattern JavaDoc YMD_PATTERN = Pattern.compile(YMD_REGEX, Pattern.UNICODE_CASE);
61     private static final String JavaDoc YM_REGEX = "/(\\d\\d\\d\\d)/(\\d{1,2}+)/";
62     private static final Pattern JavaDoc YM_PATTERN = Pattern.compile(YM_REGEX, Pattern.UNICODE_CASE);
63     private static final String JavaDoc Y_REGEX = "/(\\d\\d\\d\\d)/";
64     private static final Pattern JavaDoc Y_PATTERN = Pattern.compile(Y_REGEX, Pattern.UNICODE_CASE);
65
66     /**
67      * Default constructor.
68      */

69     public PermalinkFilter() {
70     }
71
72     /**
73      * Initialize the filter
74      *
75      * @param filterConfig {@link FilterConfig}
76      * @throws ServletException If there is an error initializing the filter
77      */

78     public void init(FilterConfig filterConfig) throws ServletException {
79     }
80
81     /**
82      * Remove the filter from service
83      */

84     public void destroy() {
85     }
86
87     /**
88      * Process the request.
89      * <p/>
90      * Processes requests of the form
91      * <ul>
92      * <li>/YYYY/MM/DD/permalink</li>
93      * <li>/YYYY/MM/DD/</li>
94      * <li>/YYYY/MM/</li>
95      * <li>/YYYY/</li>
96      * </ul>
97      *
98      * @param request {@link ServletRequest}
99      * @param response {@link ServletResponse}
100      * @param chain {@link FilterChain} to execute
101      * @throws IOException If there is an error executing the filter
102      * @throws ServletException If there is an error executing the filter
103      */

104     public void doFilter(ServletRequest JavaDoc request, ServletResponse response, FilterChain chain)
105             throws IOException JavaDoc, ServletException {
106         request.setCharacterEncoding(BlojsomConstants.UTF8);
107         
108         HttpServletRequest JavaDoc hreq = (HttpServletRequest JavaDoc) request;
109         String JavaDoc uri = hreq.getRequestURI();
110         StringBuffer JavaDoc url = hreq.getRequestURL();
111         String JavaDoc pathInfo = hreq.getPathInfo();
112         if (BlojsomUtils.checkNullOrBlank(pathInfo)) {
113             pathInfo = "/";
114         }
115
116         Matcher JavaDoc ymdpMatcher = YMD_PERMALINK_PATTERN.matcher(pathInfo);
117         Matcher JavaDoc ymdMatcher = YMD_PATTERN.matcher(pathInfo);
118         Matcher JavaDoc ymMatcher = YM_PATTERN.matcher(pathInfo);
119         Matcher JavaDoc yMatcher = Y_PATTERN.matcher(pathInfo);
120         Map extraParameters;
121
122         if (ymdpMatcher.find()) {
123             String JavaDoc year = ymdpMatcher.group(1);
124             String JavaDoc month = ymdpMatcher.group(2);
125             String JavaDoc day = ymdpMatcher.group(3);
126             String JavaDoc permalink = ymdpMatcher.group(4);
127             extraParameters = new HashMap();
128             extraParameters.put("year", new String JavaDoc[]{year});
129             extraParameters.put("month", new String JavaDoc[]{month});
130             extraParameters.put("day", new String JavaDoc[]{day});
131             extraParameters.put("permalink", new String JavaDoc[]{permalink});
132             String JavaDoc yearSubstring = year + "/";
133             int yearIndex = pathInfo.lastIndexOf(yearSubstring);
134             String JavaDoc pathinfo = pathInfo.substring(0, yearIndex);
135             yearIndex = uri.lastIndexOf(yearSubstring);
136             String JavaDoc URI = uri.substring(0, yearIndex);
137             yearIndex = url.lastIndexOf(yearSubstring);
138             String JavaDoc URL = url.substring(0, yearIndex);
139             _logger.debug("Handling YYYY/MM/DD/permalink request: " + pathinfo);
140             hreq = new PermalinkRequest(hreq, extraParameters, URI, URL, pathinfo);
141         } else if (ymdMatcher.find()) {
142             String JavaDoc year = ymdMatcher.group(1);
143             String JavaDoc month = ymdMatcher.group(2);
144             String JavaDoc day = ymdMatcher.group(3);
145             extraParameters = new HashMap();
146             extraParameters.put("year", new String JavaDoc[]{year});
147             extraParameters.put("month", new String JavaDoc[]{month});
148             extraParameters.put("day", new String JavaDoc[]{day});
149             String JavaDoc yearSubstring = year + "/";
150             int yearIndex = pathInfo.lastIndexOf(yearSubstring);
151             String JavaDoc pathinfo = pathInfo.substring(0, yearIndex);
152             yearIndex = uri.lastIndexOf(yearSubstring);
153             String JavaDoc URI = uri.substring(0, yearIndex);
154             yearIndex = url.lastIndexOf(yearSubstring);
155             String JavaDoc URL = url.substring(0, yearIndex);
156             hreq = new PermalinkRequest(hreq, extraParameters, URI, URL, pathinfo);
157             _logger.debug("Handling YYYY/MM/DD/ request: " + pathinfo);
158         } else if (ymMatcher.find()) {
159             String JavaDoc year = ymMatcher.group(1);
160             String JavaDoc month = ymMatcher.group(2);
161             extraParameters = new HashMap();
162             extraParameters.put("year", new String JavaDoc[]{year});
163             extraParameters.put("month", new String JavaDoc[]{month});
164             String JavaDoc yearSubstring = year + "/";
165             int yearIndex = pathInfo.lastIndexOf(yearSubstring);
166             String JavaDoc pathinfo = pathInfo.substring(0, yearIndex);
167             yearIndex = uri.lastIndexOf(yearSubstring);
168             String JavaDoc URI = uri.substring(0, yearIndex);
169             yearIndex = url.lastIndexOf(yearSubstring);
170             String JavaDoc URL = url.substring(0, yearIndex);
171             hreq = new PermalinkRequest(hreq, extraParameters, URI, URL, pathinfo);
172             _logger.debug("Handling YYYY/MM request: " + pathinfo);
173         } else if (yMatcher.find()) {
174             String JavaDoc year = yMatcher.group(1);
175             extraParameters = new HashMap();
176             extraParameters.put("year", new String JavaDoc[]{year});
177             String JavaDoc yearSubstring = year + "/";
178             int yearIndex = pathInfo.lastIndexOf(yearSubstring);
179             String JavaDoc pathinfo = pathInfo.substring(0, yearIndex);
180             yearIndex = uri.lastIndexOf(yearSubstring);
181             String JavaDoc URI = uri.substring(0, yearIndex);
182             yearIndex = url.lastIndexOf(yearSubstring);
183             String JavaDoc URL = url.substring(0, yearIndex);
184             hreq = new PermalinkRequest(hreq, extraParameters, URI, URL, pathinfo);
185             _logger.debug("Handling YYYY request: " + pathinfo);
186         } else {
187             // Check for a /category/permalink.html post
188
String JavaDoc permalinkSubstring = "/";
189             int permalinkIndex = pathInfo.substring(1).lastIndexOf(permalinkSubstring);
190             if (permalinkIndex != -1 && permalinkIndex < pathInfo.length() - 1) {
191                 extraParameters = new HashMap();
192                 if (request.getParameter("permalink") == null) {
193                     if (!"/".equals(pathInfo.substring(permalinkIndex + 1))) {
194                         extraParameters.put("permalink", new String JavaDoc[]{pathInfo.substring(permalinkIndex + 1)});
195                     }
196                 }
197                 String JavaDoc pathinfo = pathInfo.substring(0, permalinkIndex + 1);
198                 permalinkIndex = uri.lastIndexOf(permalinkSubstring);
199                 String JavaDoc URI = uri.substring(0, permalinkIndex + 1);
200                 permalinkIndex = url.lastIndexOf(permalinkSubstring);
201                 String JavaDoc URL = url.substring(0, permalinkIndex + 1);
202                 _logger.debug("Handling permalink request: " + pathinfo);
203                 hreq = new PermalinkRequest(hreq, extraParameters, URI, URL, pathinfo);
204             }
205         }
206
207         chain.doFilter(hreq, response);
208     }
209
210     /**
211      * Permalink request
212      */

213     public class PermalinkRequest extends HttpServletRequestWrapper JavaDoc {
214
215         private Map params;
216         private String JavaDoc uri;
217         private String JavaDoc url;
218         private String JavaDoc pathInfo;
219
220         /**
221          * Construct a new permalink request
222          *
223          * @param httpServletRequest {@link HttpServletRequest}
224          * @param params Parameters pulled from the URL
225          * @param uri URI
226          * @param url URL
227          * @param pathInfo Path information
228          */

229         public PermalinkRequest(HttpServletRequest JavaDoc httpServletRequest, Map params, String JavaDoc uri, String JavaDoc url, String JavaDoc pathInfo) {
230             super(httpServletRequest);
231             Map updatedParams = new HashMap(httpServletRequest.getParameterMap());
232             Iterator keys = params.keySet().iterator();
233             while (keys.hasNext()) {
234                 Object JavaDoc o = keys.next();
235                 updatedParams.put(o, params.get(o));
236             }
237
238             this.params = Collections.unmodifiableMap(updatedParams);
239             this.uri = uri;
240             this.url = url;
241             this.pathInfo = pathInfo;
242         }
243
244         /**
245          * Return the request URI
246          *
247          * @return Request URI
248          */

249         public String JavaDoc getRequestURI() {
250             return uri;
251         }
252
253         /**
254          * Return the request URL
255          *
256          * @return Request URL
257          */

258         public StringBuffer JavaDoc getRequestURL() {
259             return new StringBuffer JavaDoc(url);
260         }
261
262         /**
263          * Return the path information
264          *
265          * @return Path information
266          */

267         public String JavaDoc getPathInfo() {
268             return pathInfo;
269         }
270
271         /**
272          * Retrieve a named parameter
273          *
274          * @param name Parameter to retrieve
275          * @return Parameter value or <code>null</code> if the parameter is not found
276          */

277         public String JavaDoc getParameter(String JavaDoc name) {
278             String JavaDoc[] values = getParameterValues(name);
279             return (values != null) ? values[0] : null;
280         }
281
282         /**
283          * Retrieve the map of parameters
284          *
285          * @return Parameter map
286          */

287         public Map getParameterMap() {
288             return params;
289         }
290
291         /**
292          * Retrieve the parameter names
293          *
294          * @return {@link Enumeration} of parameter names
295          */

296         public Enumeration getParameterNames() {
297             return Collections.enumeration(params.keySet());
298         }
299
300         /**
301          * Retrieve a parameter value as a <code>String[]</code>
302          *
303          * @param name Parameter name
304          * @return Parameter value as <code>String[]</code> or <code>null</code> if the parameter is not found
305          */

306         public String JavaDoc[] getParameterValues(String JavaDoc name) {
307             return (String JavaDoc[]) params.get(name);
308         }
309
310         /**
311          * Set the path information for the request
312          *
313          * @param pathInfo New path information
314          */

315         public void setPathInfo(String JavaDoc pathInfo) {
316             this.pathInfo = pathInfo;
317         }
318     }
319 }
320
Popular Tags