View Javadoc

1   package com.eyeq.pivot4j.pentaho.servlet;
2   
3   import javax.servlet.ServletRequestAttributeEvent;
4   import javax.servlet.ServletRequestEvent;
5   import javax.servlet.http.HttpServletRequest;
6   import javax.servlet.http.HttpServletRequestWrapper;
7   import javax.servlet.http.HttpSession;
8   
9   import org.apache.commons.lang.ObjectUtils;
10  import org.apache.myfaces.webapp.StartupServletContextListener;
11  
12  public class PluginServletRequest extends HttpServletRequestWrapper {
13  
14  	public static final String PLUGIN_PREFIX = "/plugin";
15  
16  	public static final String SERVLET_PATH = "/pivot4j/faces";
17  
18  	private PluginServletContext servletContext;
19  
20  	/**
21  	 * @param servletContext
22  	 * @param request
23  	 */
24  	public PluginServletRequest(PluginServletContext servletContext,
25  			HttpServletRequest request) {
26  		super(request);
27  
28  		this.servletContext = servletContext;
29  	}
30  
31  	/**
32  	 * @return the servletContext
33  	 */
34  	protected PluginServletContext getServletContext() {
35  		return servletContext;
36  	}
37  
38  	public void initialize() {
39  		StartupServletContextListener listener = servletContext.getListener();
40  		listener.requestInitialized(new ServletRequestEvent(servletContext,
41  				this));
42  	}
43  
44  	public void destroy() {
45  		StartupServletContextListener listener = servletContext.getListener();
46  		listener.requestDestroyed(new ServletRequestEvent(servletContext, this));
47  	}
48  
49  	/**
50  	 * @see javax.servlet.http.HttpServletRequestWrapper#getServletPath()
51  	 */
52  	@Override
53  	public String getServletPath() {
54  		return PLUGIN_PREFIX + SERVLET_PATH;
55  	}
56  
57  	/**
58  	 * @see javax.servlet.http.HttpServletRequestWrapper#getPathInfo()
59  	 */
60  	@Override
61  	public String getPathInfo() {
62  		String pathInfo = super.getPathInfo();
63  
64  		if (pathInfo.startsWith(SERVLET_PATH)) {
65  			pathInfo = pathInfo.substring(SERVLET_PATH.length());
66  		}
67  
68  		return pathInfo;
69  	}
70  
71  	/**
72  	 * @see javax.servlet.ServletRequestWrapper#setAttribute(java.lang.String,
73  	 *      java.lang.Object)
74  	 */
75  	@Override
76  	public void setAttribute(String name, Object value) {
77  		Object oldValue = getAttribute(name);
78  
79  		super.setAttribute(name, value);
80  
81  		StartupServletContextListener listener = servletContext.getListener();
82  
83  		if (oldValue == null) {
84  			listener.attributeAdded(new ServletRequestAttributeEvent(
85  					servletContext, this, name, value));
86  		} else if (!ObjectUtils.equals(oldValue, value)) {
87  			listener.attributeReplaced(new ServletRequestAttributeEvent(
88  					servletContext, this, name, value));
89  		}
90  	}
91  
92  	/**
93  	 * @see javax.servlet.ServletRequestWrapper#removeAttribute(java.lang.String)
94  	 */
95  	@Override
96  	public void removeAttribute(String name) {
97  		Object value = getAttribute(name);
98  
99  		super.removeAttribute(name);
100 
101 		StartupServletContextListener listener = servletContext.getListener();
102 		listener.attributeRemoved(new ServletRequestAttributeEvent(
103 				servletContext, this, name, value));
104 	}
105 
106 	/**
107 	 * @see javax.servlet.http.HttpServletRequestWrapper#getSession()
108 	 */
109 	@Override
110 	public HttpSession getSession() {
111 		HttpSession session = super.getSession();
112 
113 		if (!(session instanceof PluginServletSession)) {
114 			session = new PluginServletSession(servletContext, session);
115 		}
116 
117 		return session;
118 	}
119 
120 	/**
121 	 * @see javax.servlet.http.HttpServletRequestWrapper#getSession(boolean)
122 	 */
123 	@Override
124 	public HttpSession getSession(boolean create) {
125 		HttpSession session = super.getSession(create);
126 
127 		if (!(session instanceof PluginServletSession)) {
128 			session = new PluginServletSession(servletContext, session);
129 		}
130 
131 		return session;
132 	}
133 }