PermalinkFilter.java [plain text]
package org.blojsom.filter;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.blojsom.util.BlojsomUtils;
import org.blojsom.util.BlojsomConstants;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.IOException;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class PermalinkFilter implements Filter {
private static final Log _logger = LogFactory.getLog(PermalinkFilter.class);
private static final String YMD_PERMALINK_REGEX = "/(\\d\\d\\d\\d)/(\\d{1,2}+)/(\\d{1,2}+)/(.+)";
private static final Pattern YMD_PERMALINK_PATTERN = Pattern.compile(YMD_PERMALINK_REGEX, Pattern.UNICODE_CASE);
private static final String YMD_REGEX = "/(\\d\\d\\d\\d)/(\\d{1,2}+)/(\\d{1,2}+)/";
private static final Pattern YMD_PATTERN = Pattern.compile(YMD_REGEX, Pattern.UNICODE_CASE);
private static final String YM_REGEX = "/(\\d\\d\\d\\d)/(\\d{1,2}+)/";
private static final Pattern YM_PATTERN = Pattern.compile(YM_REGEX, Pattern.UNICODE_CASE);
private static final String Y_REGEX = "/(\\d\\d\\d\\d)/";
private static final Pattern Y_PATTERN = Pattern.compile(Y_REGEX, Pattern.UNICODE_CASE);
public PermalinkFilter() {
}
public void init(FilterConfig filterConfig) throws ServletException {
}
public void destroy() {
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
request.setCharacterEncoding(BlojsomConstants.UTF8);
HttpServletRequest hreq = (HttpServletRequest) request;
String uri = hreq.getRequestURI();
StringBuffer url = hreq.getRequestURL();
String pathInfo = hreq.getPathInfo();
if (BlojsomUtils.checkNullOrBlank(pathInfo)) {
pathInfo = "/";
}
Matcher ymdpMatcher = YMD_PERMALINK_PATTERN.matcher(pathInfo);
Matcher ymdMatcher = YMD_PATTERN.matcher(pathInfo);
Matcher ymMatcher = YM_PATTERN.matcher(pathInfo);
Matcher yMatcher = Y_PATTERN.matcher(pathInfo);
Map extraParameters;
if (ymdpMatcher.find()) {
String year = ymdpMatcher.group(1);
String month = ymdpMatcher.group(2);
String day = ymdpMatcher.group(3);
String permalink = ymdpMatcher.group(4);
extraParameters = new HashMap();
extraParameters.put("year", new String[]{year});
extraParameters.put("month", new String[]{month});
extraParameters.put("day", new String[]{day});
extraParameters.put("permalink", new String[]{permalink});
String yearSubstring = year + "/";
int yearIndex = pathInfo.lastIndexOf(yearSubstring);
String pathinfo = pathInfo.substring(0, yearIndex);
yearIndex = uri.lastIndexOf(yearSubstring);
String URI = uri.substring(0, yearIndex);
yearIndex = url.lastIndexOf(yearSubstring);
String URL = url.substring(0, yearIndex);
_logger.debug("Handling YYYY/MM/DD/permalink request: " + pathinfo);
hreq = new PermalinkRequest(hreq, extraParameters, URI, URL, pathinfo);
} else if (ymdMatcher.find()) {
String year = ymdMatcher.group(1);
String month = ymdMatcher.group(2);
String day = ymdMatcher.group(3);
extraParameters = new HashMap();
extraParameters.put("year", new String[]{year});
extraParameters.put("month", new String[]{month});
extraParameters.put("day", new String[]{day});
String yearSubstring = year + "/";
int yearIndex = pathInfo.lastIndexOf(yearSubstring);
String pathinfo = pathInfo.substring(0, yearIndex);
yearIndex = uri.lastIndexOf(yearSubstring);
String URI = uri.substring(0, yearIndex);
yearIndex = url.lastIndexOf(yearSubstring);
String URL = url.substring(0, yearIndex);
hreq = new PermalinkRequest(hreq, extraParameters, URI, URL, pathinfo);
_logger.debug("Handling YYYY/MM/DD/ request: " + pathinfo);
} else if (ymMatcher.find()) {
String year = ymMatcher.group(1);
String month = ymMatcher.group(2);
extraParameters = new HashMap();
extraParameters.put("year", new String[]{year});
extraParameters.put("month", new String[]{month});
String yearSubstring = year + "/";
int yearIndex = pathInfo.lastIndexOf(yearSubstring);
String pathinfo = pathInfo.substring(0, yearIndex);
yearIndex = uri.lastIndexOf(yearSubstring);
String URI = uri.substring(0, yearIndex);
yearIndex = url.lastIndexOf(yearSubstring);
String URL = url.substring(0, yearIndex);
hreq = new PermalinkRequest(hreq, extraParameters, URI, URL, pathinfo);
_logger.debug("Handling YYYY/MM request: " + pathinfo);
} else if (yMatcher.find()) {
String year = yMatcher.group(1);
extraParameters = new HashMap();
extraParameters.put("year", new String[]{year});
String yearSubstring = year + "/";
int yearIndex = pathInfo.lastIndexOf(yearSubstring);
String pathinfo = pathInfo.substring(0, yearIndex);
yearIndex = uri.lastIndexOf(yearSubstring);
String URI = uri.substring(0, yearIndex);
yearIndex = url.lastIndexOf(yearSubstring);
String URL = url.substring(0, yearIndex);
hreq = new PermalinkRequest(hreq, extraParameters, URI, URL, pathinfo);
_logger.debug("Handling YYYY request: " + pathinfo);
} else {
String permalinkSubstring = "/";
int permalinkIndex = pathInfo.lastIndexOf(permalinkSubstring);
if (permalinkIndex < pathInfo.length() - 1) {
extraParameters = new HashMap();
extraParameters.put("permalink", new String[]{pathInfo.substring(permalinkIndex + 1)});
String pathinfo = pathInfo.substring(0, permalinkIndex + 1);
permalinkIndex = uri.lastIndexOf(permalinkSubstring);
String URI = uri.substring(0, permalinkIndex + 1);
permalinkIndex = url.lastIndexOf(permalinkSubstring);
String URL = url.substring(0, permalinkIndex + 1);
_logger.debug("Handling permalink request: " + pathinfo);
hreq = new PermalinkRequest(hreq, extraParameters, URI, URL, pathinfo);
}
}
chain.doFilter(hreq, response);
}
public class PermalinkRequest extends HttpServletRequestWrapper {
private Map params;
private String uri;
private String url;
private String pathInfo;
public PermalinkRequest(HttpServletRequest httpServletRequest, Map params, String uri, String url, String pathInfo) {
super(httpServletRequest);
Map updatedParams = new HashMap(httpServletRequest.getParameterMap());
Iterator keys = params.keySet().iterator();
while (keys.hasNext()) {
Object o = keys.next();
updatedParams.put(o, params.get(o));
}
this.params = Collections.unmodifiableMap(updatedParams);
this.uri = uri;
this.url = url;
this.pathInfo = pathInfo;
}
public String getRequestURI() {
return uri;
}
public StringBuffer getRequestURL() {
return new StringBuffer(url);
}
public String getPathInfo() {
return pathInfo;
}
public String getParameter(String name) {
String[] values = getParameterValues(name);
return (values != null) ? values[0] : null;
}
public Map getParameterMap() {
return params;
}
public Enumeration getParameterNames() {
return Collections.enumeration(params.keySet());
}
public String[] getParameterValues(String name) {
return (String[]) params.get(name);
}
public void setPathInfo(String pathInfo) {
this.pathInfo = pathInfo;
}
}
}