View Javadoc

1   /**
2    * This file Copyright (c) 2003-2010 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.context;
35  
36  import info.magnolia.cms.beans.runtime.MultipartForm;
37  import info.magnolia.cms.core.AggregationState;
38  import info.magnolia.cms.security.Security;
39  import info.magnolia.objectfactory.Components;
40  
41  import javax.servlet.ServletContext;
42  import javax.servlet.ServletException;
43  import javax.servlet.ServletRequest;
44  import javax.servlet.http.HttpServletRequest;
45  import javax.servlet.http.HttpServletResponse;
46  import javax.servlet.http.HttpSession;
47  import javax.servlet.jsp.PageContext;
48  import java.io.IOException;
49  import java.io.Writer;
50  import java.util.Enumeration;
51  import java.util.HashMap;
52  import java.util.Map;
53  import java.util.Stack;
54  
55  
56  /**
57   * Context implementation keeping track of the current request, response, servletContext and pageContext.
58   * @author Sameer Charles
59   * @version $Id: WebContextImpl.java 36506 2010-08-18 12:05:34Z had $
60   */
61  public class WebContextImpl extends UserContextImpl implements WebContext {
62  
63      private static final long serialVersionUID = 222L;
64  
65      private HttpServletRequest request;
66  
67      private HttpServletResponse response;
68  
69      private ServletContext servletContext;
70  
71      /**
72       * the jsp page context.
73       */
74      private PageContext pageContext;
75  
76      protected AggregationState aggregationState;
77  
78      private Stack<HttpServletResponse> responseStack = new Stack<HttpServletResponse>();
79  
80      private Stack<HttpServletRequest> requestStack = new Stack<HttpServletRequest>();
81  
82      /**
83       * Use init to initialize the object.
84       */
85      public WebContextImpl() {
86      }
87  
88      public void init(HttpServletRequest request, HttpServletResponse response, ServletContext servletContext) {
89          this.request = request;
90          this.response = response;
91          this.servletContext = servletContext;
92          //reset();
93          //setUser(getAnonymousUser());
94          setAttributeStrategy(new RequestAttributeStrategy(this));
95          setRepositoryStrategy(new DefaultRepositoryStrategy(this));
96      }
97  
98      public AggregationState getAggregationState() {
99          if (aggregationState == null) {
100             aggregationState = Components.getComponentProvider().newInstance(AggregationState.class);
101         }
102         return aggregationState;
103     }
104 
105     /**
106      * This will only reset the original URI/URL by calling {@link AggregationState#resetURIs()}.
107      */
108     public void resetAggregationState() {
109         getAggregationState().resetURIs();
110     }
111 
112     /**
113      * Get form object assembled by <code>MultipartRequestFilter</code>.
114      * @return multipart form object
115      */
116     public MultipartForm getPostedForm() {
117         return (MultipartForm) getAttribute(MultipartForm.REQUEST_ATTRIBUTE_NAME, LOCAL_SCOPE);
118     }
119 
120     /**
121      * Get parameter value as string.
122      * @return parameter value
123      */
124     public String getParameter(String name) {
125         return this.request.getParameter(name);
126     }
127 
128     /**
129      * Get parameter values as string[].
130      * @return parameter values
131      */
132     public String[] getParameterValues(String name) {
133         return this.request.getParameterValues(name);
134     }
135 
136     /**
137      * Get parameter values as a Map<String, String> (unlike HttpServletRequest.getParameterMap() which returns a Map<String,
138      * String[]>, so don't expect to retrieve multiple-valued form parameters here).
139      * @return parameter values
140      */
141     public Map<String, String> getParameters() {
142         Map<String, String> map = new HashMap<String, String>();
143         Enumeration<String> paramEnum = this.request.getParameterNames();
144         while (paramEnum.hasMoreElements()) {
145             final String name = paramEnum.nextElement();
146             map.put(name, this.request.getParameter(name));
147         }
148         return map;
149     }
150 
151     /**
152      * Avoid the call to this method where ever possible.
153      * @return Returns the request.
154      */
155     public HttpServletRequest getRequest() {
156         return this.request;
157     }
158 
159     public HttpServletResponse getResponse() {
160         return response;
161     }
162 
163     public String getContextPath() {
164         return this.request.getContextPath();
165     }
166 
167     /**
168      * Does an include using the request that was set when setting up this context, or using the
169      * request wrapped by the pageContext if existing.
170      */
171     public void include(final String path, final Writer out) throws ServletException, IOException {
172         try {
173             final ServletRequest requestToUse = /*pageContext != null ? pageContext.getRequest() :*/ this.getRequest();
174             final HttpServletResponse responseToUse = /*(pageContext != null && pageContext.getResponse() instanceof HttpServletResponse) ? (HttpServletResponse) pageContext.getResponse() :*/ response;
175             final WriterResponseWrapper wrappedResponse = new WriterResponseWrapper(responseToUse, out);
176 
177             requestToUse.getRequestDispatcher(path).include(requestToUse, wrappedResponse);
178         }
179         catch (ServletException e) {
180             throw new RuntimeException(e);
181         }
182         catch (IOException e) {
183             throw new RuntimeException(e);
184         }
185     }
186 
187     /**
188      * {@inheritDoc}
189      */
190     public PageContext getPageContext() {
191         return pageContext;
192     }
193 
194     /**
195      * {@inheritDoc}
196      */
197     public void setPageContext(PageContext pageContext) {
198         this.pageContext = pageContext;
199     }
200 
201     /**
202      * {@inheritDoc}
203      */
204     public ServletContext getServletContext() {
205         return servletContext;
206     }
207 
208     public void login() {
209         setRepositoryStrategy(new DefaultRepositoryStrategy(this));
210     }
211 
212     /**
213      * Closes opened JCR sessions and invalidates the current HttpSession.
214      * @see #release()
215      */
216     public void logout() {
217         releaseJCRSessions();
218 
219         HttpSession session = this.request.getSession(false);
220         if (session != null) {
221             session.invalidate();
222         }
223         login(Security.getAnonymousUser());
224     }
225 
226     /**
227      * Closes opened JCR sessions.
228      */
229     public void release() {
230         releaseJCRSessions();
231         this.request = null;
232         this.response = null;
233     }
234 
235     protected void releaseJCRSessions() {
236         getRepositoryStrategy().release();
237     }
238 
239     /* (non-Javadoc)
240      * @see info.magnolia.context.WebContext#pop()
241      */
242     public void pop() {
243         request = requestStack.pop();
244         response = responseStack.pop();
245     }
246 
247     /* (non-Javadoc)
248      * @see info.magnolia.context.WebContext#push(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
249      */
250     public void push(HttpServletRequest request, HttpServletResponse response) {
251         requestStack.push(this.request);
252         this.request = request;
253         responseStack.push(this.response);
254         this.response = response;
255     }
256 }