KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nextapp > echo2 > webrender > Connection


1 /*
2  * This file is part of the Echo Web Application Framework (hereinafter "Echo").
3  * Copyright (C) 2002-2005 NextApp, Inc.
4  *
5  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6  *
7  * The contents of this file are subject to the Mozilla Public License Version
8  * 1.1 (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  * http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * Alternatively, the contents of this file may be used under the terms of
18  * either the GNU General Public License Version 2 or later (the "GPL"), or
19  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
20  * in which case the provisions of the GPL or the LGPL are applicable instead
21  * of those above. If you wish to allow use of your version of this file only
22  * under the terms of either the GPL or the LGPL, and not to allow others to
23  * use your version of this file under the terms of the MPL, indicate your
24  * decision by deleting the provisions above and replace them with the notice
25  * and other provisions required by the GPL or the LGPL. If you do not delete
26  * the provisions above, a recipient may use your version of this file under
27  * the terms of any one of the MPL, the GPL or the LGPL.
28  */

29
30 package nextapp.echo2.webrender;
31
32 import java.io.IOException JavaDoc;
33 import java.io.OutputStream JavaDoc;
34 import java.io.PrintWriter JavaDoc;
35 import java.util.HashMap JavaDoc;
36 import java.util.Map JavaDoc;
37
38 import javax.servlet.ServletException JavaDoc;
39 import javax.servlet.http.HttpServletRequest JavaDoc;
40 import javax.servlet.http.HttpServletResponse JavaDoc;
41 import javax.servlet.http.HttpSession JavaDoc;
42
43 /**
44  * A representation of a connection to the server by the client, encapsulating
45  * the servlet request and response objects, and providing access to the
46  * relevant application instance.
47  * <code>Connection</code>s also manage the life-cycle of
48  * <code>UserInstance</code>s within the <code>HttpSession</code>.
49  */

50 public class Connection {
51
52     /**
53      * Prefix to use for user instance <code>HttpSession</code> keys.
54      */

55     private static final String JavaDoc USER_INSTANCE_SESSION_KEY_PREFIX = "Echo2UserInstance";
56
57     private HttpServletRequest JavaDoc request;
58     private HttpServletResponse JavaDoc response;
59     private WebRenderServlet servlet;
60     private UserInstance userInstance;
61     private Map JavaDoc propertyMap;
62     
63     /**
64      * Creates a <code>connection</code> object that will handle the given
65      * request and response. The <code>UserInstance</code> will be acquired from the session
66      * if one exists. A session will NOT be created if one does not exist.
67      *
68      * @param servlet the <code>WebRenderServlet</code> generating the connection
69      * @param request the HTTP request object that was passed to the servlet
70      * @param response the HTTP response object that was passed to the servlet
71      */

72     Connection(WebRenderServlet servlet, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws IOException JavaDoc,
73             ServletException JavaDoc {
74         super();
75
76         this.servlet = servlet;
77         this.request = request;
78         this.response = response;
79
80         // Configure connection for Multipart Request if required.
81
String JavaDoc contentType = request.getContentType();
82         if (contentType != null && contentType.startsWith(ContentType.MULTIPART_FORM_DATA.getMimeType())) {
83             if (WebRenderServlet.getMultipartRequestWrapper() == null) {
84                 throw new WebRenderServletException("MultipartRequestWrapper was never set and client made an HTTP request "
85                         + "encoded as multipart/form-data.");
86             }
87             this.request = WebRenderServlet.getMultipartRequestWrapper().getWrappedRequest(request);
88         }
89
90         HttpSession JavaDoc session = request.getSession(false);
91         if (session != null) {
92             userInstance = (UserInstance) session.getAttribute(getSessionKey());
93         }
94     }
95
96     /**
97      * Disposes of the <code>UserInstance</code> associated with this
98      * <code>Connection</code>.
99      */

100     void disposeUserInstance() {
101         HttpSession JavaDoc session = request.getSession(false);
102         if (session != null) {
103             getUserInstance().setServletUri(null);
104             session.removeAttribute(getSessionKey());
105         }
106     }
107
108     /**
109      * Returns the <code>OutputStream</code> object that may be used to
110      * generate a response. This method may be called once. If it is called,
111      * the getPrintWriter() method may not be called. This method wraps a
112      * call to <code>HttpServletResponse.getOutputStream()</code>. The
113      * <code>OutputStream</code> will be closed by the servlet container.
114      *
115      * @return the <code>OutputStream</code> object that may be used to
116      * generate a response to the client
117      */

118     public OutputStream JavaDoc getOutputStream() {
119         try {
120             return response.getOutputStream();
121         } catch (IOException JavaDoc ex) {
122             throw new WebRenderServletException("Unable to get PrintWriter.", ex);
123         }
124     }
125     
126     /**
127      * Returns a property from the <code>Connection</code>-persistent
128      * property map. (Properties are disposed of when <code>Connection</code>
129      * has completed).
130      *
131      * @param key the property key (for namespacing purposes, keys should
132      * be prefaced with the full class name of the object setting the
133      * property)
134      * @return the property value
135      */

136     public Object JavaDoc getProperty(String JavaDoc key) {
137         return propertyMap == null ? null : propertyMap.get(key);
138     }
139
140     /**
141      * Returns the <code>HttpServletRequest</code> wrapped by this
142      * <code>Connection</code>.
143      *
144      * @return the <code>HttpServletRequest</code> wrapped by this
145      * <code>Connection</code>
146      */

147     public HttpServletRequest JavaDoc getRequest() {
148         return request;
149     }
150
151     /**
152      * Returns the <code>HttpServletResponse</code> wrapped by this
153      * <code>Connection</code>.
154      *
155      * @return the <code>HttpServletResponse</code> wrapped by this
156      * <code>Connection</code>
157      */

158     public HttpServletResponse JavaDoc getResponse() {
159         return response;
160     }
161
162     /**
163      * Determines the <code>HttpSession</code> key value in which the
164      * associated <code>UserInstance</code> should be stored.
165      *
166      * @return the <code>HttpSession</code> key
167      */

168     private String JavaDoc getSessionKey() {
169         return USER_INSTANCE_SESSION_KEY_PREFIX + ":" + servlet.getServletName();
170     }
171
172     /**
173      * Returns the <code>WebRenderServlet</code> wrapped by this
174      * <code>Connection</code>.
175      *
176      * @return the <code>WebRenderServlet</code> wrapped by this
177      * <code>Connection</code>
178      */

179     public WebRenderServlet getServlet() {
180         return servlet;
181     }
182
183     /**
184      * Returns the <code>UserInstance</code> associated with
185      * this connection. If the session has not been initialized, null is
186      * returned.
187      *
188      * @return the <code>UserInstance</code> associated with
189      * this connection
190      */

191     public UserInstance getUserInstance() {
192         return userInstance;
193     }
194
195     /**
196      * Returns the <code>PrintWriter</code> object that may be used to
197      * generate a response. This method may be called once. If it is called,
198      * the getOuputStream() method may not be called. This method wraps a
199      * call to <code>HttpServletResponse.getWriter()</code>. The
200      * <code>PrintWriter</code> will be closed by the servlet container.
201      *
202      * @return the <code>PrintWriter</code> object that may be used to
203      * generate a response to the client
204      */

205     public PrintWriter JavaDoc getWriter() {
206         try {
207             return response.getWriter();
208         } catch (IOException JavaDoc ex) {
209             throw new WebRenderServletException("Unable to get PrintWriter.", ex);
210         }
211     }
212
213     /**
214      * Initializes the state of a new <code>UserInstance</code> and associates
215      * it with this <code>Connection</code> and the underlying
216      * <code>HttpSession</code>
217      *
218      * @param userInstance the <code>UserInstance</code>
219      */

220     void initUserInstance(UserInstance userInstance) {
221         this.userInstance = userInstance;
222         userInstance.setServletUri(request.getRequestURI());
223         HttpSession JavaDoc session = request.getSession(true);
224         session.setAttribute(getSessionKey(), userInstance);
225     }
226
227     /**
228      * Sets the content type of the response.
229      * This method will automatically append a character encoding to
230      * non-binary content types.
231      *
232      * @param contentType the content type of the response
233      */

234     public void setContentType(ContentType contentType) {
235         UserInstance userInstance = getUserInstance();
236         if (contentType.isBinary() || userInstance == null) {
237             response.setContentType(contentType.getMimeType());
238         } else {
239             response.setContentType(contentType.getMimeType() + "; charset=" + userInstance.getCharacterEncoding());
240         }
241     }
242     
243     /**
244      * Sets a property inthe <code>Connection</code>-persistent
245      * property map. (Properties are disposed of when <code>Connection</code>
246      * has completed).
247      *
248      * @param key the property key (for namespacing purposes, keys should
249      * be prefaced with the full class name of the object setting the
250      * property)
251      * @param value the new property value
252      */

253     public void setProperty(String JavaDoc key, Object JavaDoc value) {
254         if (propertyMap == null) {
255             propertyMap = new HashMap JavaDoc();
256         }
257         propertyMap.put(key, value);
258     }
259 }
Popular Tags