1 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 ; 40 import javax.servlet.http.HttpServletRequestWrapper ; 41 import java.util.regex.Pattern ; 42 import java.util.regex.Matcher ; 43 import java.util.*; 44 import java.io.IOException ; 45 46 53 public class SkipEntriesFilter implements Filter { 54 55 private static final Log _logger = LogFactory.getLog(SkipEntriesFilter.class); 56 57 private static final String PAGE_WITH_NUMBER_REGEX = "/skip/(.+)/$"; 58 private static final Pattern PAGE_WITH_NUMBER_PATTERN = Pattern.compile(PAGE_WITH_NUMBER_REGEX, Pattern.UNICODE_CASE); 59 60 63 public SkipEntriesFilter() { 64 } 65 66 public void init(FilterConfig filterConfig) throws ServletException { 67 } 68 69 public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException , ServletException { 70 servletRequest.setCharacterEncoding(BlojsomConstants.UTF8); 71 72 HttpServletRequest hreq = (HttpServletRequest ) servletRequest; 73 String uri = hreq.getRequestURI(); 74 StringBuffer url = hreq.getRequestURL(); 75 String pathInfo = hreq.getPathInfo(); 76 if (BlojsomUtils.checkNullOrBlank(pathInfo)) { 77 pathInfo = "/"; 78 } 79 80 _logger.debug("Handling skip entries request: " + pathInfo); 81 82 Matcher pageNumberMatcher = PAGE_WITH_NUMBER_PATTERN.matcher(pathInfo); 83 Map extraParameters; 84 85 if (pageNumberMatcher.find()) { 86 String pageNumber = pageNumberMatcher.group(1); 87 88 extraParameters = new HashMap(); 89 extraParameters.put(BlojsomConstants.PAGE_NUMBER_PARAM, new String [] {pageNumber}); 90 91 String pageNumberSubstring = "/skip/" + pageNumber + "/"; 92 int pageNumberIndex = pathInfo.lastIndexOf(pageNumberSubstring) + 1; 93 String pathinfo = pathInfo.substring(0, pageNumberIndex); 94 pageNumberIndex = uri.lastIndexOf(pageNumberSubstring); 95 String URI = uri.substring(0, pageNumberIndex); 96 pageNumberIndex = url.lastIndexOf(pageNumberSubstring); 97 String 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 112 public class SkipEntriesPermalinkRequest extends HttpServletRequestWrapper { 113 114 private Map params; 115 private String uri; 116 private String url; 117 private String pathInfo; 118 119 127 public SkipEntriesPermalinkRequest(HttpServletRequest httpServletRequest, Map params, String uri, String url, String 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 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 150 public String getRequestURI() { 151 return uri; 152 } 153 154 159 public StringBuffer getRequestURL() { 160 return new StringBuffer (url); 161 } 162 163 168 public String getPathInfo() { 169 return pathInfo; 170 } 171 172 178 public String getParameter(String name) { 179 String [] values = getParameterValues(name); 180 return (values != null) ? values[0] : null; 181 } 182 183 188 public Map getParameterMap() { 189 return params; 190 } 191 192 197 public Enumeration getParameterNames() { 198 return Collections.enumeration(params.keySet()); 199 } 200 201 207 public String [] getParameterValues(String name) { 208 return (String []) params.get(name); 209 } 210 211 216 public void setPathInfo(String pathInfo) { 217 this.pathInfo = pathInfo; 218 } 219 } 220 } 221 | Popular Tags |