001: /*
002: * @author <a href="mailto:novotny@gridsphere.org">Jason Novotny</a>
003: * @version $Id: ActionRequestImpl.java 6385 2007-10-25 14:02:26Z wehrens $
004: */
005: package org.gridsphere.portlet.impl;
006:
007: import javax.portlet.ActionRequest;
008: import javax.portlet.PortletContext;
009: import javax.portlet.PortletRequest;
010: import javax.portlet.RenderRequest;
011: import javax.servlet.http.HttpServletRequest;
012: import java.io.IOException;
013: import java.io.UnsupportedEncodingException;
014:
015: /**
016: * The <CODE>ActionRequest</CODE> represents the request sent to the portlet
017: * to handle an action.
018: * It extends the PortletRequest interface to provide action request
019: * information to portlets.<br>
020: * The portlet container creates an <CODE>ActionRequest</CODE> object and
021: * passes it as argument to the portlet's <CODE>processAction</CODE> method.
022: *
023: * @see PortletRequest
024: * @see RenderRequest
025: */
026: public class ActionRequestImpl extends PortletRequestImpl implements
027: ActionRequest {
028:
029: /**
030: * Constructor creates a proxy for a HttpServletRequest
031: * All PortletRequest objects come from request or session attributes
032: *
033: * @param req the HttpServletRequest
034: * @param portletContext the PortletContext
035: */
036: public ActionRequestImpl(HttpServletRequest req,
037: PortletContext portletContext) {
038: super (req, portletContext);
039: }
040:
041: /**
042: * Retrieves the body of the HTTP request from client to
043: * portal as binary data using
044: * an <CODE>InputStream</CODE>. Either this method or
045: * {@link #getReader} may be called to read the body, but not both.
046: * <p/>
047: * For HTTP POST data of type application/x-www-form-urlencoded
048: * this method throws an <code>IllegalStateException</code>
049: * as this data has been already processed by the
050: * portal/portlet-container and is available as request parameters.
051: *
052: * @return an input stream containing the body of the request
053: * @throws IllegalStateException if getReader was already called, or it is a
054: * HTTP POST data of type application/x-www-form-urlencoded
055: * @throws java.io.IOException if an input or output exception occurred
056: */
057: public java.io.InputStream getPortletInputStream()
058: throws IOException, IllegalStateException {
059: HttpServletRequest req = (HttpServletRequest) super
060: .getRequest();
061: // 'POST' replaced by 'post' for XHTML 1.0 Strict compliance
062: if (req.getMethod().toUpperCase().equals("POST")) {
063: String contentType = req.getContentType();
064: if (contentType == null
065: || contentType
066: .equals("application/x-www-form-urlencoded")) {
067: throw new IllegalStateException(
068: "User request HTTP POST data is of type application/x-www-form-urlencoded. This data has been already processed by the portal/portlet-container and is available as request parameters.");
069: }
070: }
071: hasReader = true;
072: return super .getRequest().getInputStream();
073: }
074:
075: /**
076: * Overrides the name of the character encoding used in the body of this
077: * request. This method must be called prior to reading input
078: * using {@link #getReader} or {@link #getPortletInputStream}.
079: * <p/>
080: * This method only sets the character set for the Reader that the
081: * {@link #getReader} method returns.
082: *
083: * @param enc a <code>String</code> containing the name of
084: * the chararacter encoding.
085: * @exception java.io.UnsupportedEncodingException if this is not a valid encoding
086: * @exception IllegalStateException if this method is called after
087: * reading request parameters or reading input using
088: * <code>getReader()</code>
089: */
090: public void setCharacterEncoding(String enc)
091: throws UnsupportedEncodingException, IllegalStateException {
092: if (hasReader) {
093: throw new IllegalStateException(
094: "Cannot invoke this method after using getReader or reading request parameters!");
095: }
096: super .getRequest().setCharacterEncoding(enc);
097: }
098:
099: /**
100: * Retrieves the body of the HTTP request from the client to the portal
101: * as character data using
102: * a <code>BufferedReader</code>. The reader translates the character
103: * data according to the character encoding used on the body.
104: * Either this method or {@link #getPortletInputStream} may be called to read the
105: * body, not both.
106: * <p/>
107: * For HTTP POST data of type application/x-www-form-urlencoded
108: * this method throws an <code>IllegalStateException</code>
109: * as this data has been already processed by the
110: * portal/portlet-container and is available as request parameters.
111: *
112: * @throws java.io.UnsupportedEncodingException
113: * if the character set encoding used is
114: * not supported and the text cannot be decoded
115: * @throws IllegalStateException if {@link #getPortletInputStream} method
116: * has been called on this request, it is a
117: * HTTP POST data of type application/x-www-form-urlencoded.
118: * @throws java.io.IOException if an input or output exception occurred
119: * @return a <code>BufferedReader</code>
120: * containing the body of the request
121: * @see #getPortletInputStream
122: */
123: public java.io.BufferedReader getReader()
124: throws java.io.IOException {
125: hasReader = true;
126: return super .getRequest().getReader();
127: }
128:
129: /**
130: * Returns the name of the character encoding used in the body of this request.
131: * This method returns <code>null</code> if the request
132: * does not specify a character encoding.
133: *
134: * @return a <code>String</code> containing the name of
135: * the chararacter encoding, or <code>null</code>
136: * if the request does not specify a character encoding.
137: */
138: public String getCharacterEncoding() {
139: return super .getRequest().getCharacterEncoding();
140: }
141:
142: /**
143: * Returns the MIME type of the body of the request,
144: * or null if the type is not known.
145: *
146: * @return a <code>String</code> containing the name
147: * of the MIME type of the request, or null
148: * if the type is not known.
149: */
150: public String getContentType() {
151: return super .getRequest().getContentType();
152: }
153:
154: /**
155: * Returns the length, in bytes, of the request body
156: * which is made available by the input stream, or -1 if the
157: * length is not known.
158: *
159: * @return an integer containing the length of the
160: * request body or -1 if the length is not known
161: */
162: public int getContentLength() {
163: return super.getRequest().getContentLength();
164: }
165:
166: }
|