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.BlojsomUtils; 36 import org.blojsom.util.BlojsomConstants; 37 38 import javax.servlet.*; 39 import javax.servlet.http.HttpServletRequest ; 40 import javax.servlet.http.HttpServletRequestWrapper ; 41 import java.io.IOException ; 42 import java.util.*; 43 import java.util.regex.Matcher ; 44 import java.util.regex.Pattern ; 45 46 53 public class PermalinkFilter implements Filter { 54 55 private static final Log _logger = LogFactory.getLog(PermalinkFilter.class); 56 57 private static final String YMD_PERMALINK_REGEX = "/(\\d\\d\\d\\d)/(\\d{1,2}+)/(\\d{1,2}+)/(.+)"; 58 private static final Pattern YMD_PERMALINK_PATTERN = Pattern.compile(YMD_PERMALINK_REGEX, Pattern.UNICODE_CASE); 59 private static final String YMD_REGEX = "/(\\d\\d\\d\\d)/(\\d{1,2}+)/(\\d{1,2}+)/"; 60 private static final Pattern YMD_PATTERN = Pattern.compile(YMD_REGEX, Pattern.UNICODE_CASE); 61 private static final String YM_REGEX = "/(\\d\\d\\d\\d)/(\\d{1,2}+)/"; 62 private static final Pattern YM_PATTERN = Pattern.compile(YM_REGEX, Pattern.UNICODE_CASE); 63 private static final String Y_REGEX = "/(\\d\\d\\d\\d)/"; 64 private static final Pattern Y_PATTERN = Pattern.compile(Y_REGEX, Pattern.UNICODE_CASE); 65 66 69 public PermalinkFilter() { 70 } 71 72 78 public void init(FilterConfig filterConfig) throws ServletException { 79 } 80 81 84 public void destroy() { 85 } 86 87 104 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) 105 throws IOException , ServletException { 106 request.setCharacterEncoding(BlojsomConstants.UTF8); 107 108 HttpServletRequest hreq = (HttpServletRequest ) request; 109 String uri = hreq.getRequestURI(); 110 StringBuffer url = hreq.getRequestURL(); 111 String pathInfo = hreq.getPathInfo(); 112 if (BlojsomUtils.checkNullOrBlank(pathInfo)) { 113 pathInfo = "/"; 114 } 115 116 Matcher ymdpMatcher = YMD_PERMALINK_PATTERN.matcher(pathInfo); 117 Matcher ymdMatcher = YMD_PATTERN.matcher(pathInfo); 118 Matcher ymMatcher = YM_PATTERN.matcher(pathInfo); 119 Matcher yMatcher = Y_PATTERN.matcher(pathInfo); 120 Map extraParameters; 121 122 if (ymdpMatcher.find()) { 123 String year = ymdpMatcher.group(1); 124 String month = ymdpMatcher.group(2); 125 String day = ymdpMatcher.group(3); 126 String permalink = ymdpMatcher.group(4); 127 extraParameters = new HashMap(); 128 extraParameters.put("year", new String []{year}); 129 extraParameters.put("month", new String []{month}); 130 extraParameters.put("day", new String []{day}); 131 extraParameters.put("permalink", new String []{permalink}); 132 String yearSubstring = year + "/"; 133 int yearIndex = pathInfo.lastIndexOf(yearSubstring); 134 String pathinfo = pathInfo.substring(0, yearIndex); 135 yearIndex = uri.lastIndexOf(yearSubstring); 136 String URI = uri.substring(0, yearIndex); 137 yearIndex = url.lastIndexOf(yearSubstring); 138 String 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 year = ymdMatcher.group(1); 143 String month = ymdMatcher.group(2); 144 String day = ymdMatcher.group(3); 145 extraParameters = new HashMap(); 146 extraParameters.put("year", new String []{year}); 147 extraParameters.put("month", new String []{month}); 148 extraParameters.put("day", new String []{day}); 149 String yearSubstring = year + "/"; 150 int yearIndex = pathInfo.lastIndexOf(yearSubstring); 151 String pathinfo = pathInfo.substring(0, yearIndex); 152 yearIndex = uri.lastIndexOf(yearSubstring); 153 String URI = uri.substring(0, yearIndex); 154 yearIndex = url.lastIndexOf(yearSubstring); 155 String 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 year = ymMatcher.group(1); 160 String month = ymMatcher.group(2); 161 extraParameters = new HashMap(); 162 extraParameters.put("year", new String []{year}); 163 extraParameters.put("month", new String []{month}); 164 String yearSubstring = year + "/"; 165 int yearIndex = pathInfo.lastIndexOf(yearSubstring); 166 String pathinfo = pathInfo.substring(0, yearIndex); 167 yearIndex = uri.lastIndexOf(yearSubstring); 168 String URI = uri.substring(0, yearIndex); 169 yearIndex = url.lastIndexOf(yearSubstring); 170 String 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 year = yMatcher.group(1); 175 extraParameters = new HashMap(); 176 extraParameters.put("year", new String []{year}); 177 String yearSubstring = year + "/"; 178 int yearIndex = pathInfo.lastIndexOf(yearSubstring); 179 String pathinfo = pathInfo.substring(0, yearIndex); 180 yearIndex = uri.lastIndexOf(yearSubstring); 181 String URI = uri.substring(0, yearIndex); 182 yearIndex = url.lastIndexOf(yearSubstring); 183 String URL = url.substring(0, yearIndex); 184 hreq = new PermalinkRequest(hreq, extraParameters, URI, URL, pathinfo); 185 _logger.debug("Handling YYYY request: " + pathinfo); 186 } else { 187 String 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 []{pathInfo.substring(permalinkIndex + 1)}); 195 } 196 } 197 String pathinfo = pathInfo.substring(0, permalinkIndex + 1); 198 permalinkIndex = uri.lastIndexOf(permalinkSubstring); 199 String URI = uri.substring(0, permalinkIndex + 1); 200 permalinkIndex = url.lastIndexOf(permalinkSubstring); 201 String 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 213 public class PermalinkRequest extends HttpServletRequestWrapper { 214 215 private Map params; 216 private String uri; 217 private String url; 218 private String pathInfo; 219 220 229 public PermalinkRequest(HttpServletRequest httpServletRequest, Map params, String uri, String url, String pathInfo) { 230 super(httpServletRequest); 231 Map updatedParams = new HashMap(httpServletRequest.getParameterMap()); 232 Iterator keys = params.keySet().iterator(); 233 while (keys.hasNext()) { 234 Object 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 249 public String getRequestURI() { 250 return uri; 251 } 252 253 258 public StringBuffer getRequestURL() { 259 return new StringBuffer (url); 260 } 261 262 267 public String getPathInfo() { 268 return pathInfo; 269 } 270 271 277 public String getParameter(String name) { 278 String [] values = getParameterValues(name); 279 return (values != null) ? values[0] : null; 280 } 281 282 287 public Map getParameterMap() { 288 return params; 289 } 290 291 296 public Enumeration getParameterNames() { 297 return Collections.enumeration(params.keySet()); 298 } 299 300 306 public String [] getParameterValues(String name) { 307 return (String []) params.get(name); 308 } 309 310 315 public void setPathInfo(String pathInfo) { 316 this.pathInfo = pathInfo; 317 } 318 } 319 } 320 | Popular Tags |