001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.wsrp;
022:
023: import com.liferay.portal.kernel.util.ContentTypes;
024: import com.liferay.portal.kernel.util.StringMaker;
025: import com.liferay.portal.model.Portlet;
026: import com.liferay.portal.service.PortletLocalServiceUtil;
027: import com.liferay.portal.util.PortalUtil;
028: import com.liferay.portal.wsrp.util.WSRPUtil;
029: import com.liferay.util.CollectionFactory;
030: import com.liferay.util.FileUtil;
031: import com.liferay.util.servlet.UploadPortletRequest;
032:
033: import java.io.File;
034: import java.io.IOException;
035:
036: import java.util.ArrayList;
037: import java.util.Collection;
038: import java.util.Enumeration;
039: import java.util.Iterator;
040: import java.util.List;
041: import java.util.Locale;
042: import java.util.Map;
043: import java.util.Set;
044:
045: import javax.portlet.ActionRequest;
046: import javax.portlet.PortletMode;
047: import javax.portlet.PortletRequest;
048:
049: import javax.servlet.http.HttpServletRequest;
050:
051: import oasis.names.tc.wsrp.v1.types.ClientData;
052: import oasis.names.tc.wsrp.v1.types.MarkupContext;
053: import oasis.names.tc.wsrp.v1.types.NamedString;
054: import oasis.names.tc.wsrp.v1.types.SessionContext;
055: import oasis.names.tc.wsrp.v1.types.UploadContext;
056:
057: import org.apache.wsrp4j.consumer.InteractionRequest;
058: import org.apache.wsrp4j.consumer.MarkupRequest;
059: import org.apache.wsrp4j.consumer.PortletWindowSession;
060: import org.apache.wsrp4j.consumer.driver.GenericWSRPBaseRequestImpl;
061: import org.apache.wsrp4j.log.LogManager;
062: import org.apache.wsrp4j.log.Logger;
063: import org.apache.wsrp4j.util.AuthenticationInfoHelper;
064: import org.apache.wsrp4j.util.Constants;
065: import org.apache.wsrp4j.util.LocaleHelper;
066: import org.apache.wsrp4j.util.Modes;
067: import org.apache.wsrp4j.util.WindowStates;
068:
069: /**
070: * <a href="WSRPRequestImpl.java.html"> <b><i>View Source</i></b></a>
071: *
072: * @author Michael Young
073: *
074: */
075: public class WSRPRequestImpl extends GenericWSRPBaseRequestImpl
076: implements InteractionRequest, MarkupRequest {
077:
078: public WSRPRequestImpl(PortletWindowSession windowSession,
079: PortletRequest portletRequest, boolean renderPhase) {
080:
081: this ._windowSession = windowSession;
082: this ._portletRequest = portletRequest;
083: this ._userAuth = AuthenticationInfoHelper
084: .getWsrpFromPortlet(portletRequest.getAuthType());
085:
086: _integrateParameters(renderPhase);
087: }
088:
089: public String getInteractionState() {
090: return _interactionState;
091: }
092:
093: public NamedString[] getFormParameters() {
094: return _formParameters;
095: }
096:
097: public UploadContext[] getUploadContexts() {
098: return _uploadContexts;
099: }
100:
101: public MarkupContext getCachedMarkup() {
102: if (_windowSession == null)
103: return null;
104:
105: return _windowSession.getCachedMarkup();
106: }
107:
108: public String getSessionID() {
109: if (this ._windowSession != null) {
110: SessionContext sessionCtx = this ._windowSession
111: .getPortletSession().getSessionContext();
112: if (sessionCtx != null) {
113: return sessionCtx.getSessionID();
114:
115: }
116: }
117:
118: return null;
119: }
120:
121: public String getPortletInstanceKey() {
122: return _windowSession.getWindowID();
123: }
124:
125: public String getNavigationalState() {
126: return _naviState;
127: }
128:
129: public String getWindowState() {
130: if (_currentState == null) {
131: //map portlet window states to wsrp:window states
132: javax.portlet.WindowState portletWindowState = _portletRequest
133: .getWindowState();
134: _currentState = WindowStates
135: .getWsrpStateFromJsrPortletState(portletWindowState)
136: .toString();
137: }
138:
139: return _currentState;
140: }
141:
142: public String getMode() {
143: if (_currentMode == null) {
144: //map jsr-168 modes to wsrp:modes
145: PortletMode portletMode = _portletRequest.getPortletMode();
146: _currentMode = Modes.getWsrpModeFromJsrPortletMode(
147: portletMode).toString();
148: }
149:
150: return _currentMode;
151: }
152:
153: public ClientData getClientData() {
154: // TODO: need to find out the client data here
155: return null;
156: }
157:
158: public String[] getLocales() {
159: if (this ._locales == null) {
160: Enumeration eLocales = _portletRequest.getLocales();
161: List wsrpLocales = new ArrayList();
162: while (eLocales.hasMoreElements()) {
163: Locale locale = (Locale) eLocales.nextElement();
164: wsrpLocales.add(LocaleHelper.getWsrpLocale(locale));
165: }
166:
167: _locales = (String[]) wsrpLocales.toArray(new String[0]);
168: }
169:
170: return _locales;
171: }
172:
173: public String[] getModes() {
174: final String MN = "getModes()";
175:
176: if (_logger.isLogging(Logger.TRACE_HIGH)) {
177: _logger.entry(Logger.TRACE_HIGH, MN);
178: }
179:
180: if (this ._modes != null) {
181: if (_logger.isLogging(Logger.TRACE_HIGH)) {
182: _logger.exit(Logger.TRACE_HIGH, MN);
183: }
184:
185: return this ._modes;
186: }
187:
188: long companyId = PortalUtil.getCompanyId(_portletRequest);
189: try {
190: Portlet portlet = PortletLocalServiceUtil.getPortletById(
191: companyId, _windowSession.getWindowID());
192: Map portletModesMap = portlet.getPortletModes();
193: Collection mimeTypes = portletModesMap.values();
194: Iterator it = mimeTypes.iterator();
195: Map portletModes = CollectionFactory.getSyncHashMap();
196:
197: for (int i = 0; it.hasNext(); i++) {
198: // Required
199: Set portletModesSet = (Set) it.next();
200:
201: Iterator it2 = portletModesSet.iterator();
202:
203: for (int j = 0; it2.hasNext(); j++) {
204: String mode = (String) it2.next();
205:
206: if (portletModes.get(mode) == null) {
207: portletModes.put(mode, mode);
208: }
209: }
210: }
211:
212: String[] wsrpModes = new String[portletModes.size()];
213: it = portletModes.values().iterator();
214: for (int i = 0; it.hasNext(); i++) {
215: String mode = (String) it.next();
216: wsrpModes[i] = WSRPUtil.toWsrpMode(mode);
217: }
218: this ._modes = wsrpModes;
219: } catch (Exception e) {
220: _logger.entry(Logger.ERROR,
221: "Could not get portlet definition", e);
222: }
223:
224: if (_logger.isLogging(Logger.TRACE_HIGH)) {
225: _logger.exit(Logger.TRACE_HIGH, MN);
226: }
227:
228: return this ._modes;
229: }
230:
231: public String[] getWindowStates() {
232: // TODO: for now we simply return what we know
233: // we should return what our environment supports
234: return WindowStates.getWindowStatesAsStringArray();
235: }
236:
237: public String[] getMimeTypes() {
238: // TODO: return whatever our environment supports
239: return null;
240: }
241:
242: public String[] getCharacterEncodingSet() {
243: // TODO: return whatever our environment supports
244: return null;
245: }
246:
247: public boolean isModeSupported(String wsrpMode) {
248: if (wsrpMode == null)
249: throw new IllegalArgumentException(
250: "A mode must not be null");
251:
252: return _portletRequest.isPortletModeAllowed(Modes
253: .getJsrPortletModeFromWsrpMode(Modes
254: .fromString(wsrpMode)));
255: }
256:
257: public boolean isWindowStateSupported(String wsrpWindowState) {
258: if (wsrpWindowState == null)
259: throw new IllegalArgumentException(
260: "A window state must not be null");
261:
262: return _portletRequest.isWindowStateAllowed(WindowStates
263: .getJsrPortletStateFromWsrpState(WindowStates
264: .fromString(wsrpWindowState)));
265: }
266:
267: public String getUserAuthentication() {
268: return this ._userAuth;
269: }
270:
271: private void _integrateParameters(boolean renderPhase) {
272: final String MN = "integrateParameter()";
273:
274: if (_logger.isLogging(Logger.TRACE_HIGH)) {
275: _logger.entry(Logger.TRACE_HIGH, MN);
276: }
277:
278: // interaction state
279: this ._interactionState = _portletRequest
280: .getParameter(Constants.INTERACTION_STATE);
281:
282: // check for navistate
283: // if navistate is stored as url parameter take this
284: // otherwise look for render param
285: this ._naviState = _portletRequest
286: .getParameter(Constants.NAVIGATIONAL_STATE);
287: if (this ._naviState == null) {
288: this ._naviState = _portletRequest
289: .getParameter(WSRPProxyPortlet.NAVIGATIONAL_STATE);
290: }
291:
292: ArrayList formParams = new ArrayList();
293: ArrayList uploadContexts = new ArrayList();
294:
295: HttpServletRequest httpReq = PortalUtil
296: .getHttpServletRequest(_portletRequest);
297:
298: String contentType = httpReq.getContentType();
299:
300: if (contentType != null
301: && contentType
302: .startsWith(ContentTypes.MULTIPART_FORM_DATA)
303: && !renderPhase) {
304:
305: // process file uploads
306:
307: ActionRequest actionRequest = (ActionRequest) _portletRequest;
308:
309: UploadPortletRequest upr = PortalUtil
310: .getUploadPortletRequest(actionRequest);
311:
312: Enumeration paramNames = upr.getParameterNames();
313:
314: while (paramNames.hasMoreElements()) {
315: String name = (String) paramNames.nextElement();
316:
317: if (_isReservedParameter(name)) {
318: continue;
319: }
320:
321: if (upr.isFormField(name)) {
322: _addFormField(formParams, name, upr
323: .getParameterValues(name));
324: } else {
325: UploadContext uploadContext = new UploadContext();
326:
327: String partContentType = upr.getContentType(name);
328:
329: uploadContext.setMimeType(partContentType);
330:
331: StringMaker sm = new StringMaker();
332:
333: sm.append("form-data; ");
334: sm.append("name=");
335: sm.append(name);
336: sm.append("; filename=");
337: sm.append(upr.getFileName(name));
338:
339: NamedString[] mimeAttributes = { new NamedString() };
340: mimeAttributes[0].setName("Content-Disposition");
341: mimeAttributes[0].setValue(sm.toString());
342:
343: uploadContext.setMimeAttributes(mimeAttributes);
344:
345: File file = upr.getFile(name);
346: byte[] fileBytes = null;
347:
348: try {
349: fileBytes = FileUtil.getBytes(file);
350: } catch (IOException e) {
351: throw new IllegalStateException(
352: "Error reading multi-part file");
353: }
354:
355: if (fileBytes == null) {
356: continue;
357: }
358:
359: uploadContext.setUploadData(fileBytes);
360:
361: uploadContexts.add(uploadContext);
362: }
363: }
364: } else {
365: _addFormFields(formParams);
366: }
367:
368: int formParamsSize = formParams.size();
369:
370: if (formParamsSize > 0) {
371: _formParameters = new NamedString[formParamsSize];
372: formParams.toArray(_formParameters);
373: }
374:
375: int uploadContextsSize = uploadContexts.size();
376:
377: if (uploadContextsSize > 0) {
378: _uploadContexts = new UploadContext[uploadContextsSize];
379: uploadContexts.toArray(_uploadContexts);
380: }
381:
382: if (_logger.isLogging(Logger.TRACE_HIGH)) {
383: _logger.exit(Logger.TRACE_HIGH, MN);
384: }
385: }
386:
387: private void _addFormFields(List formParams) {
388: Enumeration paramNames = _portletRequest.getParameterNames();
389:
390: while (paramNames.hasMoreElements()) {
391: String name = (String) paramNames.nextElement();
392:
393: String[] values = _portletRequest.getParameterValues(name);
394:
395: if (values == null) {
396: continue;
397: }
398:
399: _addFormField(formParams, name, values);
400: }
401: }
402:
403: private void _addFormField(List formParams, String name,
404: String values[]) {
405: for (int i = 0; i < values.length; i++) {
406: NamedString paramPair = new NamedString();
407: paramPair.setName(name);
408: paramPair.setValue(values[i]);
409:
410: formParams.add(paramPair);
411: }
412:
413: }
414:
415: private boolean _isReservedParameter(String name) {
416: if (Constants.isWsrpURLParam(name)
417: || name.equals(WSRPProxyPortlet.NAVIGATIONAL_STATE)
418: || name.equals(WSRPProxyPortlet.REMOTE_INVOCATION)) {
419:
420: return true;
421: } else {
422: return false;
423: }
424: }
425:
426: private final PortletRequest _portletRequest;
427:
428: private final PortletWindowSession _windowSession;
429:
430: private final String _userAuth;
431:
432: private NamedString[] _formParameters = null;
433:
434: private String _interactionState = null;
435:
436: private UploadContext[] _uploadContexts = null;
437:
438: private String _currentMode = null;
439:
440: private String _currentState = null;
441:
442: private String _naviState = null;
443:
444: // just for performance reasons we cache this info
445: private String[] _modes = null;
446:
447: private String[] _locales = null;
448:
449: protected Logger _logger = LogManager.getLogManager().getLogger(
450: WSRPRequestImpl.class);
451:
452: }
|