KickJava   Java API By Example, From Geeks To Geeks.

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


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.BlojsomConstants;
36 import org.blojsom.util.BlojsomUtils;
37
38 import javax.servlet.*;
39 import javax.servlet.http.HttpServletRequest JavaDoc;
40 import javax.servlet.http.HttpServletRequestWrapper JavaDoc;
41 import java.util.regex.Pattern JavaDoc;
42 import java.util.regex.Matcher JavaDoc;
43 import java.util.*;
44 import java.io.IOException JavaDoc;
45
46 /**
47  * Page Entries filter
48  *
49  * @author David Czarnecki
50  * @since blojsom 3.0
51  * @version $Id: SkipEntriesFilter.java,v 1.1 2006/03/28 16:14:12 czarneckid Exp $
52  */

53 public class SkipEntriesFilter implements Filter {
54
55     private static final Log _logger = LogFactory.getLog(SkipEntriesFilter.class);
56
57     private static final String JavaDoc PAGE_WITH_NUMBER_REGEX = "/skip/(.+)/$";
58     private static final Pattern JavaDoc PAGE_WITH_NUMBER_PATTERN = Pattern.compile(PAGE_WITH_NUMBER_REGEX, Pattern.UNICODE_CASE);
59
60     /**
61      * Construct a new instance of the page entries filter
62      */

63     public SkipEntriesFilter() {
64     }
65
66     public void init(FilterConfig filterConfig) throws ServletException {
67     }
68
69     public void doFilter(ServletRequest JavaDoc servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException JavaDoc, ServletException {
70         servletRequest.setCharacterEncoding(BlojsomConstants.UTF8);
71
72         HttpServletRequest JavaDoc hreq = (HttpServletRequest JavaDoc) servletRequest;
73         String JavaDoc uri = hreq.getRequestURI();
74         StringBuffer JavaDoc url = hreq.getRequestURL();
75         String JavaDoc pathInfo = hreq.getPathInfo();
76         if (BlojsomUtils.checkNullOrBlank(pathInfo)) {
77             pathInfo = "/";
78         }
79
80         _logger.debug("Handling skip entries request: " + pathInfo);
81
82         Matcher JavaDoc pageNumberMatcher = PAGE_WITH_NUMBER_PATTERN.matcher(pathInfo);
83         Map extraParameters;
84
85         if (pageNumberMatcher.find()) {
86             String JavaDoc pageNumber = pageNumberMatcher.group(1);
87
88             extraParameters = new HashMap();
89             extraParameters.put(BlojsomConstants.PAGE_NUMBER_PARAM, new String JavaDoc[] {pageNumber});
90
91             String JavaDoc pageNumberSubstring = "/skip/" + pageNumber + "/";
92             int pageNumberIndex = pathInfo.lastIndexOf(pageNumberSubstring) + 1;
93             String JavaDoc pathinfo = pathInfo.substring(0, pageNumberIndex);
94             pageNumberIndex = uri.lastIndexOf(pageNumberSubstring);
95             String JavaDoc URI = uri.substring(0, pageNumberIndex);
96             pageNumberIndex = url.lastIndexOf(pageNumberSubstring);
97             String JavaDoc URL = url.substring(0, pageNumberIndex);
98
99             _logger.debug("Handling skip entries page: " + pageNumber + " with path info: " + pathinfo + " URI: " + URI + " URL: " + URL);
100             hreq = new SkipEntriesPermalinkRequest(hreq, extraParameters, URI, URL, pathinfo);
101         }
102
103         filterChain.doFilter(hreq, servletResponse);
104     }
105
106     public void destroy() {
107     }
108
109     /**
110      * Page number request
111      */

112     public class SkipEntriesPermalinkRequest extends HttpServletRequestWrapper JavaDoc {
113
114         private Map params;
115         private String JavaDoc uri;
116         private String JavaDoc url;
117         private String JavaDoc pathInfo;
118
119         /**
120          *
121          * @param httpServletRequest
122          * @param params
123          * @param uri
124          * @param url
125          * @param pathInfo
126          */

127         public SkipEntriesPermalinkRequest(HttpServletRequest JavaDoc httpServletRequest, Map params, String JavaDoc uri, String JavaDoc url, String JavaDoc pathInfo) {
128             super(httpServletRequest);
129
130             Map updatedParams = new HashMap(httpServletRequest.getParameterMap());
131             Iterator keys = params.keySet().iterator();
132             while (keys.hasNext())
133
134             {
135                 Object JavaDoc o = keys.next();
136                 updatedParams.put(o, params.get(o));
137             }
138
139             this.params = Collections.unmodifiableMap(updatedParams);
140             this.uri = uri;
141             this.url = url;
142             this.pathInfo = pathInfo;
143         }
144
145         /**
146          * Return the request URI
147          *
148          * @return Request URI
149          */

150         public String JavaDoc getRequestURI() {
151             return uri;
152         }
153
154         /**
155          * Return the request URL
156          *
157          * @return Request URL
158          */

159         public StringBuffer JavaDoc getRequestURL() {
160             return new StringBuffer JavaDoc(url);
161         }
162
163         /**
164          * Return the path information
165          *
166          * @return Path information
167          */

168         public String JavaDoc getPathInfo() {
169             return pathInfo;
170         }
171
172         /**
173          * Retrieve a named parameter
174          *
175          * @param name Parameter to retrieve
176          * @return Parameter value or <code>null</code> if the parameter is not found
177          */

178         public String JavaDoc getParameter(String JavaDoc name) {
179             String JavaDoc[] values = getParameterValues(name);
180             return (values != null) ? values[0] : null;
181         }
182
183         /**
184          * Retrieve the map of parameters
185          *
186          * @return Parameter map
187          */

188         public Map getParameterMap() {
189             return params;
190         }
191
192         /**
193          * Retrieve the parameter names
194          *
195          * @return {@link java.util.Enumeration} of parameter names
196          */

197         public Enumeration getParameterNames() {
198             return Collections.enumeration(params.keySet());
199         }
200
201         /**
202          * Retrieve a parameter value as a <code>String[]</code>
203          *
204          * @param name Parameter name
205          * @return Parameter value as <code>String[]</code> or <code>null</code> if the parameter is not found
206          */

207         public String JavaDoc[] getParameterValues(String JavaDoc name) {
208             return (String JavaDoc[]) params.get(name);
209         }
210
211         /**
212          * Set the path information for the request
213          *
214          * @param pathInfo New path information
215          */

216         public void setPathInfo(String JavaDoc pathInfo) {
217             this.pathInfo = pathInfo;
218         }
219     }
220 }
221
Popular Tags