nextapp.echo2.webrender.Connection Maven / Gradle / Ivy
The newest version!
/*
* This file is part of the Echo Web Application Framework (hereinafter "Echo").
* Copyright (C) 2002-2009 NextApp, Inc.
*
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*/
package nextapp.echo2.webrender;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* A representation of a connection to the server by the client, encapsulating
* the servlet request and response objects, and providing access to the
* relevant application instance.
* Connection
s also manage the life-cycle of
* UserInstance
s within the HttpSession
.
*/
public class Connection {
/**
* Prefix to use for user instance HttpSession
keys.
*/
private static final String USER_INSTANCE_SESSION_KEY_PREFIX = "Echo2UserInstance";
private HttpServletRequest request;
private HttpServletResponse response;
private WebRenderServlet servlet;
private UserInstance userInstance;
private Map propertyMap;
/**
* Creates a connection
object that will handle the given
* request and response. The UserInstance
will be acquired from the session
* if one exists. A session will NOT be created if one does not exist.
*
* @param servlet the WebRenderServlet
generating the connection
* @param request the HTTP request object that was passed to the servlet
* @param response the HTTP response object that was passed to the servlet
*/
Connection(WebRenderServlet servlet, HttpServletRequest request, HttpServletResponse response) throws IOException,
ServletException {
super();
this.servlet = servlet;
this.request = request;
this.response = response;
// Configure connection for Multipart Request if required.
String contentType = request.getContentType();
if (contentType != null && contentType.startsWith(ContentType.MULTIPART_FORM_DATA.getMimeType())) {
if (WebRenderServlet.getMultipartRequestWrapper() == null) {
throw new WebRenderServletException("MultipartRequestWrapper was never set and client made an HTTP request "
+ "encoded as multipart/form-data.");
}
this.request = WebRenderServlet.getMultipartRequestWrapper().getWrappedRequest(request);
}
HttpSession session = request.getSession(false);
if (session != null) {
userInstance = (UserInstance) session.getAttribute(getSessionKey());
}
}
/**
* Disposes of the UserInstance
associated with this
* Connection
.
*/
void disposeUserInstance() {
HttpSession session = request.getSession(false);
if (session != null) {
getUserInstance().setServletUri(null);
session.removeAttribute(getSessionKey());
}
}
/**
* Returns the OutputStream
object that may be used to
* generate a response. This method may be called once. If it is called,
* the getPrintWriter() method may not be called. This method wraps a
* call to HttpServletResponse.getOutputStream()
. The
* OutputStream
will be closed by the servlet container.
*
* @return the OutputStream
object that may be used to
* generate a response to the client
*/
public OutputStream getOutputStream() {
try {
return response.getOutputStream();
} catch (IOException ex) {
throw new WebRenderServletException("Unable to get PrintWriter.", ex);
}
}
/**
* Returns a property from the Connection
-persistent
* property map. (Properties are disposed of when Connection
* has completed).
*
* @param key the property key (for namespacing purposes, keys should
* be prefaced with the full class name of the object setting the
* property)
* @return the property value
*/
public Object getProperty(String key) {
return propertyMap == null ? null : propertyMap.get(key);
}
/**
* Returns the HttpServletRequest
wrapped by this
* Connection
.
*
* @return the HttpServletRequest
wrapped by this
* Connection
*/
public HttpServletRequest getRequest() {
return request;
}
/**
* Returns the HttpServletResponse
wrapped by this
* Connection
.
*
* @return the HttpServletResponse
wrapped by this
* Connection
*/
public HttpServletResponse getResponse() {
return response;
}
/**
* Determines the HttpSession
key value in which the
* associated UserInstance
should be stored.
*
* @return the HttpSession
key
*/
private String getSessionKey() {
return USER_INSTANCE_SESSION_KEY_PREFIX + ":" + servlet.getServletName();
}
/**
* Returns the WebRenderServlet
wrapped by this
* Connection
.
*
* @return the WebRenderServlet
wrapped by this
* Connection
*/
public WebRenderServlet getServlet() {
return servlet;
}
/**
* Returns the UserInstance
associated with
* this connection. If the session has not been initialized, null is
* returned.
*
* @return the UserInstance
associated with
* this connection
*/
public UserInstance getUserInstance() {
return userInstance;
}
/**
* Returns the PrintWriter
object that may be used to
* generate a response. This method may be called once. If it is called,
* the getOuputStream() method may not be called. This method wraps a
* call to HttpServletResponse.getWriter()
. The
* PrintWriter
will be closed by the servlet container.
*
* @return the PrintWriter
object that may be used to
* generate a response to the client
*/
public PrintWriter getWriter() {
try {
return response.getWriter();
} catch (IOException ex) {
throw new WebRenderServletException("Unable to get PrintWriter.", ex);
}
}
/**
* Initializes the state of a new UserInstance
and associates
* it with this Connection
and the underlying
* HttpSession
*
* @param userInstance the UserInstance
*/
void initUserInstance(UserInstance userInstance) {
this.userInstance = userInstance;
userInstance.setServletUri(request.getRequestURI());
HttpSession session = request.getSession(true);
session.setAttribute(getSessionKey(), userInstance);
}
/**
* Sets the content type of the response.
* This method will automatically append a character encoding to
* non-binary content types.
*
* @param contentType the content type of the response
*/
public void setContentType(ContentType contentType) {
UserInstance userInstance = getUserInstance();
if (contentType.isBinary() || userInstance == null) {
response.setContentType(contentType.getMimeType());
} else {
response.setContentType(contentType.getMimeType() + "; charset=" + userInstance.getCharacterEncoding());
}
}
/**
* Sets a property in the Connection
-persistent
* property map. (Properties are disposed of when Connection
* has completed).
*
* @param key the property key (for namespacing purposes, keys should
* be prefaced with the full class name of the object setting the
* property)
* @param value the new property value
*/
public void setProperty(String key, Object value) {
if (propertyMap == null) {
propertyMap = new HashMap();
}
propertyMap.put(key, value);
}
}