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 53 public class FeedFilter implements Filter { 54 55 private static final Log _logger = LogFactory.getLog(FeedFilter.class); 56 57 private static final String FEED_WITH_TYPE_REGEX = "/feed/(.+)/$"; 58 private static final Pattern FEED_WITH_TYPE_PATTERN = Pattern.compile(FEED_WITH_TYPE_REGEX, Pattern.UNICODE_CASE); 59 private static final String FEED_NO_TYPE_REGEX = "/feed/$"; 60 private static final Pattern FEED_NO_TYPE_PATTERN = Pattern.compile(FEED_NO_TYPE_REGEX, Pattern.UNICODE_CASE); 61 private static final String DEFAULT_FEED_TYPE = "rss2"; 62 private static final String DEFAULT_FEED_TYPE_IP = "default-feed-type"; 63 private static final String PERMALINK_EXTENSIONS_IP = "permalink-extensions"; 64 private static final String [] DEFAULT_PERMALINK_EXTENSIONS = {".html", ".txt"}; 65 66 private String _defaultFeedType = DEFAULT_FEED_TYPE; 67 private String [] _permalinkExtensions; 68 69 72 public FeedFilter() { 73 } 74 75 81 public void init(FilterConfig filterConfig) throws ServletException { 82 String defaultFeedType = filterConfig.getInitParameter(DEFAULT_FEED_TYPE_IP); 83 if (!BlojsomUtils.checkNullOrBlank(defaultFeedType)) { 84 _defaultFeedType = defaultFeedType; 85 } 86 87 _permalinkExtensions = DEFAULT_PERMALINK_EXTENSIONS; 88 String permalinkExtensions = filterConfig.getInitParameter(PERMALINK_EXTENSIONS_IP); 89 if (!BlojsomUtils.checkNullOrBlank(permalinkExtensions)) { 90 _permalinkExtensions = BlojsomUtils.parseOnlyCommaList(permalinkExtensions, true); 91 } 92 93 _logger.debug("Initialized feed filter using default feed type: " + _defaultFeedType); 94 } 95 96 99 public void destroy() { 100 } 101 102 115 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException , ServletException { 116 request.setCharacterEncoding(BlojsomConstants.UTF8); 117 118 HttpServletRequest hreq = (HttpServletRequest ) request; 119 String uri = hreq.getRequestURI(); 120 StringBuffer url = hreq.getRequestURL(); 121 String pathInfo = hreq.getPathInfo(); 122 if (BlojsomUtils.checkNullOrBlank(pathInfo)) { 123 pathInfo = "/"; 124 } 125 126 _logger.debug("Handling feed filter request: " + pathInfo); 127 128 Matcher feedWithTypeMatcher = FEED_WITH_TYPE_PATTERN.matcher(pathInfo); 129 Matcher feedNoTypeMatcher = FEED_NO_TYPE_PATTERN.matcher(pathInfo); 130 Map extraParameters; 131 132 if (feedWithTypeMatcher.find()) { 133 String feedType = feedWithTypeMatcher.group(1); 134 135 extraParameters = new HashMap(); 136 extraParameters.put("flavor", new String [] {feedType}); 137 138 String feedTypeSubstring = "/feed/" + feedType + "/"; 139 int feedTypeIndex = pathInfo.lastIndexOf(feedTypeSubstring) + 1; 140 String pathinfo = pathInfo.substring(0, feedTypeIndex); 141 boolean matchedPermalink = false; 142 for (int i = 0; i < _permalinkExtensions.length; i++) { 143 String defaultPermalinkExtension = _permalinkExtensions[i]; 144 145 if (pathinfo.endsWith(defaultPermalinkExtension + "/")) { 146 matchedPermalink = true; 147 break; 148 } 149 } 150 151 feedTypeIndex = uri.lastIndexOf(feedTypeSubstring); 152 String URI = uri.substring(0, feedTypeIndex); 153 feedTypeIndex = url.lastIndexOf(feedTypeSubstring); 154 String URL = url.substring(0, feedTypeIndex); 155 156 if (matchedPermalink) { 157 pathinfo = pathinfo.substring(0, pathinfo.length() - 1); 158 } else { 159 URI += "/"; 160 URL += "/"; 161 } 162 163 _logger.debug("Handling feed type: " + feedType + " with path info: " + pathinfo + " URI: " + URI + " URL: " + URL); 164 hreq = new FeedPermalinkRequst(hreq, extraParameters, URI, URL, pathinfo); 165 } else if (feedNoTypeMatcher.find()) { 166 extraParameters = new HashMap(); 167 extraParameters.put("flavor", new String []{_defaultFeedType}); 168 169 String feedTypeSubstring = "/feed/"; 170 int feedTypeIndex = pathInfo.lastIndexOf(feedTypeSubstring) + 1; 171 String pathinfo = pathInfo.substring(0, feedTypeIndex); 172 boolean matchedPermalink = false; 173 for (int i = 0; i < DEFAULT_PERMALINK_EXTENSIONS.length; i++) { 174 String defaultPermalinkExtension = DEFAULT_PERMALINK_EXTENSIONS[i]; 175 176 if (pathinfo.endsWith(defaultPermalinkExtension + "/")) { 177 matchedPermalink = true; 178 break; 179 } 180 } 181 182 feedTypeIndex = uri.lastIndexOf(feedTypeSubstring); 183 String URI = uri.substring(0, feedTypeIndex); 184 feedTypeIndex = url.lastIndexOf(feedTypeSubstring); 185 String URL = url.substring(0, feedTypeIndex); 186 187 if (matchedPermalink) { 188 pathinfo = pathinfo.substring(0, pathinfo.length() - 1); 189 } else { 190 URI += "/"; 191 URL += "/"; 192 } 193 194 _logger.debug("Handling default feed type: " + _defaultFeedType + " with path info: " + pathinfo + " URI: " + URI + " URL: " + URL); 195 hreq = new FeedPermalinkRequst(hreq, extraParameters, URI, URL, pathinfo); 196 } 197 198 chain.doFilter(hreq, response); 199 } 200 201 204 public class FeedPermalinkRequst extends HttpServletRequestWrapper { 205 206 private Map params; 207 private String uri; 208 private String url; 209 private String pathInfo; 210 211 219 public FeedPermalinkRequst(HttpServletRequest httpServletRequest, Map params, String uri, String url, String pathInfo) { 220 super(httpServletRequest); 221 222 Map updatedParams = new HashMap(httpServletRequest.getParameterMap()); 223 Iterator keys = params.keySet().iterator(); 224 while (keys.hasNext()) 225 226 { 227 Object o = keys.next(); 228 updatedParams.put(o, params.get(o)); 229 } 230 231 this.params = Collections.unmodifiableMap(updatedParams); 232 this.uri = uri; 233 this.url = url; 234 this.pathInfo = pathInfo; 235 } 236 237 242 public String getRequestURI() { 243 return uri; 244 } 245 246 251 public StringBuffer getRequestURL() { 252 return new StringBuffer (url); 253 } 254 255 260 public String getPathInfo() { 261 return pathInfo; 262 } 263 264 270 public String getParameter(String name) { 271 String [] values = getParameterValues(name); 272 return (values != null) ? values[0] : null; 273 } 274 275 280 public Map getParameterMap() { 281 return params; 282 } 283 284 289 public Enumeration getParameterNames() { 290 return Collections.enumeration(params.keySet()); 291 } 292 293 299 public String [] getParameterValues(String name) { 300 return (String []) params.get(name); 301 } 302 303 308 public void setPathInfo(String pathInfo) { 309 this.pathInfo = pathInfo; 310 } 311 } 312 } 313 | Popular Tags |