1 package org.jmage.mapper;
2
3 import org.apache.log4j.Logger;
4
5 import javax.servlet.*;
6 import javax.servlet.http.HttpServletRequest;
7 import javax.servlet.http.HttpServletResponse;
8 import java.io.IOException;
9 import java.util.Enumeration;
10
11 /***
12 * Filters http servlet requests for GET image and redirects to filtered images based
13 * rules.
14 */
15 public class InterceptorMapper implements Filter {
16 public static final String JMAGE_MATCHED = "JMAGE_MATCHED";
17 public static final String JMAGE_FILTERED = "JMAGE_FILTERED";
18 public static final String JMAGE_INTERNAL = "JMAGE_INTERNAL";
19
20 private FilterConfig filterConfig;
21 private ServletContext servletContext;
22
23 protected static Logger log = Logger.getLogger(InterceptorMapper.class.getName());
24 private static final String ENTERED = " entered filtermapper, attempting to match request: ";
25 private static final String INTERNAL = " received internal resourcemanager request, no matching will be attempted at this time: ";
26 private static final String ALREADY_PROCESSED = " already processed request, no matching will be attempted at this time: ";
27 private static final String NOT_MATCHED = "unable to match any patterns, skipping request: ";
28 private static final String MATCHED = " matched pattern, forwarded request: ";
29 private static final String IMAGE_PREFIX = "/jmage?image=";
30 private static final String CHAIN_PREFIX = "&chain=chain:";
31 private static final String TRUE = "TRUE";
32
33 public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
34 HttpServletRequest request = (HttpServletRequest) servletRequest;
35 HttpServletResponse response = (HttpServletResponse) servletResponse;
36 String url = request.getRequestURL().toString();
37
38 if (!TRUE.equals(request.getHeader(JMAGE_INTERNAL))) {
39 Object filtered = request.getAttribute(JMAGE_FILTERED);
40
41 if (!(Boolean.TRUE).equals(filtered)) {
42 request.setAttribute(JMAGE_FILTERED, Boolean.TRUE);
43 if (log.isDebugEnabled()) log.debug(ENTERED + url);
44 Enumeration paramNames = filterConfig.getInitParameterNames();
45
46 boolean matched = false;
47 while (paramNames.hasMoreElements()) {
48 String regexp = (String) paramNames.nextElement();
49
50 if (url.indexOf(regexp) > -1 &&
51 !(Boolean.TRUE).equals(request.getAttribute(JMAGE_MATCHED))) {
52 request.setAttribute(JMAGE_MATCHED, Boolean.TRUE);
53 String jmageWrapper = IMAGE_PREFIX + url + CHAIN_PREFIX + filterConfig.getInitParameter(regexp);
54 RequestDispatcher requestdispatcher = servletContext.getRequestDispatcher(jmageWrapper);
55 matched = true;
56 if (log.isInfoEnabled()) log.info(MATCHED + url + "to: " + jmageWrapper);
57 requestdispatcher.forward(request, response);
58 break;
59 }
60 }
61 if (!matched) {
62 if (log.isDebugEnabled()) log.debug(NOT_MATCHED + url);
63 filterChain.doFilter(request, response);
64 }
65 } else {
66 if (log.isDebugEnabled()) log.debug(ALREADY_PROCESSED + url);
67 filterChain.doFilter(request, response);
68 }
69 } else {
70 if (log.isDebugEnabled()) log.debug(INTERNAL + url);
71 filterChain.doFilter(request, response);
72 }
73 }
74
75 public void init(FilterConfig filterConfig) throws ServletException {
76 this.filterConfig = filterConfig;
77 this.servletContext = filterConfig.getServletContext();
78 }
79
80 public void destroy() {
81 this.filterConfig = null;
82 this.servletContext = null;
83 }
84 }