001: /*
002: * Copyright 2002 Sun Microsystems, Inc. All
003: * rights reserved. Use of this product is subject
004: * to license terms. Federal Acquisitions:
005: * Commercial Software -- Government Users
006: * Subject to Standard License Terms and
007: * Conditions.
008: *
009: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
010: * are trademarks or registered trademarks of Sun Microsystems,
011: * Inc. in the United States and other countries.
012: */
013: package com.sun.portal.portlet.impl;
014:
015: import javax.servlet.http.HttpServletRequest;
016: import javax.servlet.http.HttpServletResponse;
017:
018: import javax.portlet.PortletContext;
019: import javax.portlet.PortalContext;
020: import javax.portlet.PortletMode;
021: import javax.portlet.WindowState;
022:
023: import java.util.Map;
024: import java.util.Collections;
025:
026: import java.io.BufferedReader;
027: import java.io.IOException;
028: import java.io.StringWriter;
029:
030: import com.sun.portal.portletappengine.PortletAppEngineUtils;
031:
032: import com.sun.portal.portletcontainercommon.PortletContainerActionRequest;
033: import com.sun.portal.portletcontainercommon.PortletContainerActionResponse;
034: import com.sun.portal.portletcontainercommon.descriptor.PortletDescriptor;
035:
036: import java.util.logging.Logger;
037:
038: /**
039: * This class provides implementation of the ActionRequest interface.
040: */
041: public class ActionRequestImpl extends PortletRequestImpl implements
042: javax.portlet.ActionRequest {
043:
044: // Global variables
045: private HttpServletRequest _req;
046: private HttpServletResponse _res;
047: private PortletContainerActionRequest _pContReq;
048: private PortletContainerActionResponse _pContRes;
049: private StringWriter _writer;
050: private static Logger _logger;
051: private boolean _gotReader;
052: private boolean _gotInputStream;
053:
054: private static String ILLEGAL_OPERATION_ERROR = "Can not perform this function for portlet: ";
055: private static String UNSUPPORTED_ENCODING_ERROR = "The specified character encoding is not supported.";
056:
057: /**
058: * Initialize the global variables.
059: * <P>
060: * @param req The <code>HttpServletRequest</code> of the PAE
061: * @param res The <code>HttpServletResponse</code> of the PAE
062: * @param pContReq The <code>PortletContainerRequest</code>
063: * @param logger The <code>Logger</code> object
064: */
065: void init(HttpServletRequest req, HttpServletResponse res,
066: PortletContainerActionRequest pContReq,
067: PortletContainerActionResponse pContRes,
068: PortletContext context, PortalContext portalContext,
069: PortletDescriptor pDescriptor, StringWriter writer,
070: Logger logger) {
071:
072: super .init(req, res, pContReq, pContRes, context,
073: portalContext, pDescriptor, logger);
074:
075: _req = req;
076: _res = res;
077: _pContReq = pContReq;
078: _pContRes = pContRes;
079: _writer = writer;
080: _logger = logger;
081: _gotReader = false;
082: _gotInputStream = false;
083:
084: }
085:
086: /**
087: * Clears the global variables.
088: */
089: void clear() {
090: super .clear();
091: _req = null;
092: _res = null;
093: _pContReq = null;
094: _pContRes = null;
095: _writer = null;
096: _logger = null;
097: _gotReader = false;
098: _gotInputStream = false;
099: }
100:
101: /**
102: * Retrieves the body of the HTTP request from client to
103: * portal as binary data using
104: * an <CODE>InputStream</CODE>. Either this method or
105: * {@link #getReader} may be called to read the body, but 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: * @return an input stream containing the body of the request
113: *
114: * @exception java.lang.IllegalStateException
115: * if getReader was already called, or it is a
116: * HTTP POST data of type application/x-www-form-urlencoded
117: * @exception IOException
118: * if an input or output exception occurred
119: */
120: public java.io.InputStream getPortletInputStream()
121: throws IOException {
122:
123: // check if getReader is already called
124: if (_gotReader) {
125: throw new IllegalStateException(ILLEGAL_OPERATION_ERROR
126: + _pContReq.getPortletName());
127: }
128:
129: // check if mime type is application/x-window-form-urlencoded
130: if (getContentType() != null
131: && getContentType().equals(
132: "application/x-www-form-urlencoded")) {
133: throw new IllegalStateException(ILLEGAL_OPERATION_ERROR
134: + _pContReq.getPortletName());
135: }
136:
137: _gotInputStream = true;
138: return _req.getInputStream();
139:
140: }
141:
142: /**
143: * Returns the name of the character encoding used in the body of this request.
144: * This method returns <code>null</code> if the request
145: * does not specify a character encoding.
146: *
147: * @return a <code>String</code> containing the name of
148: * the chararacter encoding, or <code>null</code>
149: * if the request does not specify a character encoding.
150: */
151: public String getCharacterEncoding() {
152:
153: return _pContReq.getCharacterEncoding();
154: }
155:
156: /**
157: * Overrides the name of the character encoding used in the body of this
158: * request. This method must be called prior to reading input
159: * using {@link #getReader} or {@link #getPortletInputStream}.
160: * <p>
161: *
162: * @param enc a <code>String</code> containing the name of
163: * the chararacter encoding.
164: *
165: * @exception java.io.UnsupportedEncodingException if this
166: * is not a valid encoding
167: * @exception java.lang.IllegalStateException if this method is called after
168: * reading request parameters or reading input using
169: * <code>getReader()</code>
170: */
171:
172: public void setCharacterEncoding(String enc)
173: throws java.io.UnsupportedEncodingException {
174: if (_gotReader || _gotInputStream) {
175: throw new IllegalStateException(
176: "Illegal setting character encoding after getReader() or getPortletInputStream() is called");
177: }
178:
179: if (!enc.equals(getCharacterEncoding())) {
180: throw new java.io.UnsupportedEncodingException(
181: UNSUPPORTED_ENCODING_ERROR);
182: }
183:
184: _req.setCharacterEncoding(enc);
185: }
186:
187: /**
188: * Returns the MIME type of the body of the request,
189: * or null if the type is not known.
190: *
191: * @return a <code>String</code> containing the name
192: * of the MIME type of the request, or null
193: * if the type is not known
194: */
195: public String getContentType() {
196: return _req.getContentType();
197: }
198:
199: /**
200: * Returns the length, in bytes, of the request body
201: * and made available by the input stream, or -1 if the
202: * length is not known.
203: *
204: * @return an integer containing the length of the
205: * request body or -1 if the length is not known
206: *
207: */
208: public int getContentLength() {
209: int retVal = -1;
210:
211: if (getContentType() != null
212: && !getContentType().equals(
213: "application/x-www-form-urlencoded")) {
214: retVal = _req.getContentLength();
215: }
216: return retVal;
217: }
218:
219: /**
220: * Retrieves the body of the HTTP request from client to the portal
221: * as character data using
222: * a <code>BufferedReader</code>. The reader translates the character
223: * data according to the character encoding used on the body.
224: * Either this method or {@link #getPortletInputStream} may be called to read the
225: * body, not both.
226: *
227: * <p>
228: * For HTTP POST data of type application/x-www-form-urlencoded
229: * this method throws an <code>IllegalStateException</code>
230: * as this data has been already processed by the
231: * portal/portlet-container and is available as request parameters.
232: *
233: * @return a <code>BufferedReader</code>
234: * containing the body of the request
235: *
236: * @exception java.io.UnsupportedEncodingException
237: * if the character set encoding used is
238: * not supported and the text cannot be decoded
239: * @exception java.lang.IllegalStateException
240: * if {@link #getPortletInputStream} method
241: * has been called on this request, it is a
242: * HTTP POST data of type application/x-www-form-urlencoded.
243: * @exception java.io.IOException
244: * if an input or output exception occurred
245: *
246: * @see #getInputStream
247: */
248:
249: public BufferedReader getReader()
250: throws java.io.UnsupportedEncodingException,
251: java.io.IOException {
252:
253: // check if the getPortletInputStream has called
254: if (_gotInputStream) {
255: throw new IllegalStateException(
256: "Can not perform this function for portlet: "
257: + _pContReq.getPortletName());
258: }
259:
260: // check if mime type is application/x-window-form-urlencoded
261: if (getContentType() != null
262: && getContentType().equals(
263: "application/x-www-form-urlencoded")) {
264: throw new IllegalStateException(
265: "Can not perform this function for portlet: "
266: + _pContReq.getPortletName());
267: }
268:
269: _gotReader = true;
270: return _req.getReader();
271:
272: }
273:
274: /**
275: * Returns a <code>Map</code> of the parameters of this request.
276: * Request parameters are extra information sent with the request.
277: * The returned parameters are "x-www-form-urlencoded" decoded.
278: * <p>
279: * The values in the returned <code>Map</code> are from type
280: * String array (<code>String[]</code>).
281: * <p>
282: * If no parameters exist this method returns an empty <code>Map</code>.
283: *
284: * @return an immutable <code>Map</code> containing parameter names as
285: * keys and parameter values as map values, or an empty <code>Map</code>
286: * if no parameters exist. The keys in the parameter
287: * map are of type String. The values in the parameter map are of type
288: * String array (<code>String[]</code>).
289: */
290: public java.util.Map getParameterMap() {
291: Map map = _pContReq.getActionParameters();
292:
293: return Collections.unmodifiableMap(map);
294:
295: }
296:
297: /**
298: * Returns the current mode of the portlet.
299: *
300: * @return the portlet mode
301: */
302: public PortletMode getPortletMode() {
303: com.sun.portal.container.ChannelMode channelMode = _pContRes
304: .getNewChannelMode();
305: PortletMode portletMode = null;
306:
307: if (channelMode != null) {
308: portletMode = PortletAppEngineUtils
309: .getPortletMode(channelMode);
310: } else {
311: portletMode = PortletAppEngineUtils
312: .getPortletMode(_pContReq.getChannelMode());
313: }
314:
315: return portletMode;
316: }
317:
318: /**
319: * Returns the current window state of the portlet.
320: *
321: * @return the window state
322: */
323:
324: public WindowState getWindowState() {
325: com.sun.portal.container.WindowState newWindowState = _pContRes
326: .getNewWindowState();
327: WindowState windowState = null;
328: if (newWindowState != null) {
329: windowState = PortletAppEngineUtils
330: .getWindowState(newWindowState);
331: } else {
332: windowState = PortletAppEngineUtils
333: .getWindowState(_pContReq.getWindowState());
334: }
335:
336: return windowState;
337: }
338:
339: }
|