1 31 package org.blojsom.filter; 32 33 import org.blojsom.util.BlojsomConstants; 34 import org.blojsom.util.BlojsomUtils; 35 import org.apache.commons.logging.Log; 36 import org.apache.commons.logging.LogFactory; 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.regex.Pattern ; 43 import java.util.regex.Matcher ; 44 import java.util.*; 45 46 74 public class PageFilter implements Filter { 75 76 private static final Log _logger = LogFactory.getLog(PageFilter.class); 77 private static final String PAGE_PATHINFO = "/page/"; 78 private static final String PAGE_REGEX = PAGE_PATHINFO + "$"; 79 private static final Pattern PAGE_PATTERN = Pattern.compile(PAGE_REGEX, Pattern.UNICODE_CASE); 80 private static final String USE_ROOT_BLOG_COMPATABILITY_IP = "use-root-blog-compatability"; 81 private static final boolean USE_ROOT_BLOG_COMPATABILITY_DEFAULT = false; 82 83 private boolean _useRootBlogCompatability = USE_ROOT_BLOG_COMPATABILITY_DEFAULT; 84 85 88 public PageFilter() { 89 } 90 91 97 public void init(FilterConfig filterConfig) throws ServletException { 98 _useRootBlogCompatability = Boolean.valueOf(filterConfig.getInitParameter(USE_ROOT_BLOG_COMPATABILITY_IP)).booleanValue(); 99 100 _logger.debug("Initialized page filter (Root blog compatability: " + _useRootBlogCompatability + ")"); 101 } 102 103 106 public void destroy() { 107 } 108 109 122 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException , ServletException { 123 request.setCharacterEncoding(BlojsomConstants.UTF8); 124 125 HttpServletRequest hreq = (HttpServletRequest ) request; 126 String uri = hreq.getRequestURI(); 127 StringBuffer url = hreq.getRequestURL(); 128 String pathInfo = hreq.getPathInfo(); 129 if (BlojsomUtils.checkNullOrBlank(pathInfo)) { 130 pathInfo = "/"; 131 } 132 133 _logger.debug("Handling page filter request: " + pathInfo); 134 135 Matcher pageMatcher = PAGE_PATTERN.matcher(pathInfo); 136 Map extraParameters; 137 138 if (pageMatcher.find()) { 139 int pageMatchIndex = pageMatcher.start(); 140 String blogIDPathInfo = null; 141 142 if (!_useRootBlogCompatability) { 143 int firstSlashAfterBlogID = pathInfo.substring(1).indexOf("/"); 144 blogIDPathInfo = pathInfo.substring(0, firstSlashAfterBlogID + 1) + "/"; 145 pathInfo = pathInfo.substring(firstSlashAfterBlogID + 1, pageMatchIndex); 146 } else { 147 pathInfo = pathInfo.substring(0, pageMatchIndex); 148 } 149 150 pageMatchIndex = uri.lastIndexOf(PAGE_PATHINFO); 151 String URI = uri.substring(0, pageMatchIndex) + "/"; 152 pageMatchIndex = url.lastIndexOf(PAGE_PATHINFO); 153 String URL = url.substring(0, pageMatchIndex) + "/"; 154 155 extraParameters = new HashMap(); 156 extraParameters.put("page", new String []{pathInfo}); 157 pathInfo = "/"; 158 159 if (!_useRootBlogCompatability) { 160 pathInfo = blogIDPathInfo; 161 } 162 163 hreq = new PagePermalinkRequst(hreq, extraParameters, URI, URL, pathInfo); 164 _logger.debug("Handling pathinfo: " + pathInfo + " uri: " + URI + " url: " + URL); 165 } 166 167 chain.doFilter(hreq, response); 168 } 169 170 173 public class PagePermalinkRequst extends HttpServletRequestWrapper { 174 175 private Map params; 176 private String uri; 177 private String url; 178 private String pathInfo; 179 180 187 public PagePermalinkRequst(HttpServletRequest httpServletRequest, Map params, String uri, String url, String pathInfo) { 188 super(httpServletRequest); 189 190 Map updatedParams = new HashMap(httpServletRequest.getParameterMap()); 191 Iterator keys = params.keySet().iterator(); 192 while (keys.hasNext()) { 193 Object o = keys.next(); 194 updatedParams.put(o, params.get(o)); 195 } 196 197 this.params = Collections.unmodifiableMap(updatedParams); 198 this.uri = uri; 199 this.url = url; 200 this.pathInfo = pathInfo; 201 } 202 203 208 public String getRequestURI() { 209 return uri; 210 } 211 212 217 public StringBuffer getRequestURL() { 218 return new StringBuffer (url); 219 } 220 221 226 public String getPathInfo() { 227 return pathInfo; 228 } 229 230 236 public String getParameter(String name) { 237 String [] values = getParameterValues(name); 238 return (values != null) ? values[0] : null; 239 } 240 241 246 public Map getParameterMap() { 247 return params; 248 } 249 250 255 public Enumeration getParameterNames() { 256 return Collections.enumeration(params.keySet()); 257 } 258 259 265 public String [] getParameterValues(String name) { 266 return (String []) params.get(name); 267 } 268 269 274 public void setPathInfo(String pathInfo) { 275 this.pathInfo = pathInfo; 276 } 277 } 278 } 279 | Popular Tags |