View Javadoc

1   package net.sf.morph.web;
2   
3   import java.io.IOException;
4   
5   import javax.servlet.Filter;
6   import javax.servlet.FilterChain;
7   import javax.servlet.FilterConfig;
8   import javax.servlet.ServletException;
9   import javax.servlet.ServletRequest;
10  import javax.servlet.ServletResponse;
11  import javax.servlet.http.HttpServletRequest;
12  
13  import net.sf.morph.context.contexts.HttpServletContext;
14  
15  /**
16   * Stores an {@link net.sf.morph.context.support.HttpServletContext} in the
17   * HTTP request.
18   * 
19   * @author Matt Sgarlata
20   * @since Dec 20, 2004
21   */
22  public class MorphFilter implements Filter {
23  	
24  	public static final String DEFAULT_REQUEST_ATTRIBUTE = "morph";
25  	
26  	private String requestAttribute;
27  	
28  	public MorphFilter() {
29  		requestAttribute = DEFAULT_REQUEST_ATTRIBUTE;
30  	}
31  
32  	public void init(FilterConfig config) throws ServletException {
33  		// do nothing
34  	}
35  
36  	public void doFilter(ServletRequest request, ServletResponse response,
37  		FilterChain chain) throws IOException, ServletException {
38  		if (request.getAttribute(getRequestAttribute()) == null) {
39  			request.setAttribute(getRequestAttribute(),
40  				new HttpServletContext((HttpServletRequest) request));
41  		}
42  		
43  		chain.doFilter(request, response);
44  	}
45  
46  	public void destroy() {
47  		// do nothing
48  	}
49  
50  	public String getRequestAttribute() {
51  		return requestAttribute;
52  	}
53  	public void setRequestAttribute(String requestAttributeName) {
54  		this.requestAttribute = requestAttributeName;
55  	}
56  }