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.portal.wsrp;
022:
023: import com.liferay.portal.PortalException;
024: import com.liferay.portal.SystemException;
025: import com.liferay.portal.kernel.util.ContentTypes;
026: import com.liferay.portal.model.Group;
027: import com.liferay.portal.model.Layout;
028: import com.liferay.portal.model.Portlet;
029: import com.liferay.portal.model.PortletPreferencesIds;
030: import com.liferay.portal.model.User;
031: import com.liferay.portal.model.impl.GroupImpl;
032: import com.liferay.portal.service.GroupLocalServiceUtil;
033: import com.liferay.portal.service.LayoutLocalServiceUtil;
034: import com.liferay.portal.service.PortletLocalServiceUtil;
035: import com.liferay.portal.service.PortletPreferencesLocalServiceUtil;
036: import com.liferay.portal.service.UserLocalServiceUtil;
037: import com.liferay.portal.wsrp.util.WSRPUtil;
038: import com.liferay.portlet.ActionRequestFactory;
039: import com.liferay.portlet.ActionRequestImpl;
040: import com.liferay.portlet.ActionResponseImpl;
041: import com.liferay.portlet.CachePortlet;
042: import com.liferay.portlet.PortletConfigFactory;
043: import com.liferay.portlet.PortletInstanceFactory;
044: import com.liferay.portlet.PortletPreferencesFactoryUtil;
045: import com.liferay.portlet.RenderRequestFactory;
046: import com.liferay.portlet.RenderRequestImpl;
047: import com.liferay.portlet.RenderResponseImpl;
048:
049: import java.io.IOException;
050:
051: import java.util.HashMap;
052: import java.util.Locale;
053: import java.util.Map;
054:
055: import javax.portlet.PortletConfig;
056: import javax.portlet.PortletContext;
057: import javax.portlet.PortletMode;
058: import javax.portlet.PortletPreferences;
059: import javax.portlet.WindowState;
060:
061: import javax.servlet.ServletContext;
062: import javax.servlet.http.HttpServletRequest;
063:
064: import oasis.names.tc.wsrp.v1.types.BlockingInteractionResponse;
065: import oasis.names.tc.wsrp.v1.types.GetMarkup;
066: import oasis.names.tc.wsrp.v1.types.InteractionParams;
067: import oasis.names.tc.wsrp.v1.types.MarkupContext;
068: import oasis.names.tc.wsrp.v1.types.MarkupParams;
069: import oasis.names.tc.wsrp.v1.types.MarkupResponse;
070: import oasis.names.tc.wsrp.v1.types.NamedString;
071: import oasis.names.tc.wsrp.v1.types.PerformBlockingInteraction;
072: import oasis.names.tc.wsrp.v1.types.UpdateResponse;
073:
074: import org.apache.wsrp4j.exception.WSRPException;
075: import org.apache.wsrp4j.producer.provider.PortletInvoker;
076: import org.apache.wsrp4j.producer.provider.Provider;
077: import org.apache.wsrp4j.producer.util.Base64;
078: import org.apache.wsrp4j.producer.util.ObjectDeserializer;
079: import org.apache.wsrp4j.producer.util.ObjectSerializer;
080: import org.apache.wsrp4j.util.LocaleHelper;
081:
082: /**
083: * <a href="PortletInvokerImpl.java.html"><b><i>View Source</i></b></a>
084: *
085: * @author Michael Young
086: *
087: */
088: public class PortletInvokerImpl implements PortletInvoker {
089:
090: public PortletInvokerImpl(Provider provider) {
091: _provider = provider;
092: }
093:
094: public MarkupResponse invokeGetMarkup(GetMarkup getMarkup)
095: throws WSRPException {
096: String portletHandle = getMarkup.getPortletContext()
097: .getPortletHandle();
098:
099: MarkupResponse markupResponse = new MarkupResponse();
100: MarkupContext markupContext = new MarkupContext();
101: markupResponse.setMarkupContext(markupContext);
102:
103: try {
104: MarkupParams markupParams = getMarkup.getMarkupParams();
105:
106: // window state
107: String wsrpWindowState = markupParams.getWindowState();
108: WindowState windowState = WSRPUtil
109: .fromWsrpWindowState(wsrpWindowState);
110:
111: // portlet mode
112: String wsrpMode = markupParams.getMode();
113: PortletMode mode = WSRPUtil.fromWsrpMode(wsrpMode);
114:
115: ServletContext ctx = WSRPUtil.getServletContext();
116: long companyId = WSRPUtil.getCompanyId();
117:
118: Portlet portlet = PortletLocalServiceUtil.getPortletById(
119: companyId, portletHandle);
120: CachePortlet cachePortlet = PortletInstanceFactory.create(
121: portlet, ctx);
122: PortletConfig portletConfig = PortletConfigFactory.create(
123: portlet, ctx);
124: PortletContext portletCtx = portletConfig
125: .getPortletContext();
126:
127: Locale reqLocale = LocaleHelper.getLocale(markupParams
128: .getLocales()[0]);
129: String reqMimeType = markupParams.getMimeTypes()[0];
130: Map renderParameters = _getRenderParameters(markupParams);
131: HttpServletRequest httpReq = new WSRPServletRequest(
132: WSRPUtil.getHttpServletRequest(), reqLocale,
133: reqMimeType, renderParameters);
134: PortletPreferencesIds portletPreferencesIds = PortletPreferencesFactoryUtil
135: .getPortletPreferencesIds(httpReq, portlet
136: .getPortletId());
137: PortletPreferences portletPreferences = PortletPreferencesLocalServiceUtil
138: .getPreferences(portletPreferencesIds);
139:
140: // this gets the default layout of the general guest
141: User user = UserLocalServiceUtil.getDefaultUser(companyId);
142: Layout layout = _getDefaultUserLayout(user.getCompanyId());
143:
144: RenderRequestImpl renderRequest = RenderRequestFactory
145: .create(httpReq, portlet, cachePortlet, portletCtx,
146: windowState, mode, portletPreferences,
147: layout.getPlid());
148: WSRPServletResponse res = new WSRPServletResponse();
149:
150: RenderResponseImpl renderResponse = new WSRPRenderResponseImpl(
151: getMarkup, _provider,
152: (RenderRequestImpl) renderRequest, res, portlet
153: .getPortletId(), companyId, layout
154: .getPlid());
155: renderResponse.setContentType("text/html");
156: renderRequest.defineObjects(portletConfig, renderResponse);
157: cachePortlet.render(renderRequest, renderResponse);
158: markupContext.setMarkupString(res.getString());
159: String contentType = renderResponse.getContentType();
160: if (contentType == null) {
161: contentType = ContentTypes.TEXT_HTML_UTF8;
162: }
163: markupContext.setMimeType(contentType);
164: markupContext.setLocale(renderResponse.getLocale()
165: .getLanguage());
166: markupContext.setPreferredTitle(renderResponse.getTitle());
167:
168: RenderRequestFactory.recycle(renderRequest);
169: } catch (Exception e) {
170: e.printStackTrace();
171: }
172:
173: return markupResponse;
174: }
175:
176: public BlockingInteractionResponse invokePerformBlockingInteraction(
177: PerformBlockingInteraction pbo) throws WSRPException {
178: String portletHandle = pbo.getPortletContext()
179: .getPortletHandle();
180:
181: BlockingInteractionResponse bir = new BlockingInteractionResponse();
182: UpdateResponse updateRes = new UpdateResponse();
183: bir.setUpdateResponse(updateRes);
184:
185: try {
186: MarkupParams markupParams = pbo.getMarkupParams();
187:
188: // window state
189: String wsrpWindowState = markupParams.getWindowState();
190: WindowState windowState = WSRPUtil
191: .fromWsrpWindowState(wsrpWindowState);
192:
193: // portlet mode
194: String wsrpMode = markupParams.getMode();
195: PortletMode mode = WSRPUtil.fromWsrpMode(wsrpMode);
196:
197: ServletContext ctx = WSRPUtil.getServletContext();
198: long companyId = WSRPUtil.getCompanyId();
199:
200: Portlet portlet = PortletLocalServiceUtil.getPortletById(
201: companyId, portletHandle);
202: CachePortlet cachePortlet = PortletInstanceFactory.create(
203: portlet, ctx);
204: PortletConfig portletConfig = PortletConfigFactory.create(
205: portlet, ctx);
206: PortletContext portletCtx = portletConfig
207: .getPortletContext();
208:
209: Locale reqLocale = LocaleHelper.getLocale(markupParams
210: .getLocales()[0]);
211: String reqMimeType = markupParams.getMimeTypes()[0];
212: InteractionParams interactionParams = pbo
213: .getInteractionParams();
214: Map actionParameters = _getActionParameters(interactionParams);
215: HttpServletRequest httpReq = new WSRPServletRequest(
216: WSRPUtil.getHttpServletRequest(), reqLocale,
217: reqMimeType, actionParameters);
218: PortletPreferencesIds portletPreferencesIds = PortletPreferencesFactoryUtil
219: .getPortletPreferencesIds(httpReq, portlet
220: .getPortletId());
221: PortletPreferences portletPreferences = PortletPreferencesLocalServiceUtil
222: .getPreferences(portletPreferencesIds);
223:
224: // this gets the default layout of the general guest
225: User user = UserLocalServiceUtil.getDefaultUser(companyId);
226: Layout layout = _getDefaultUserLayout(user.getCompanyId());
227:
228: ActionRequestImpl actionRequest = ActionRequestFactory
229: .create(httpReq, portlet, cachePortlet, portletCtx,
230: windowState, mode, portletPreferences,
231: layout.getPlid());
232: WSRPServletResponse res = new WSRPServletResponse();
233:
234: ActionResponseImpl actionResponse = new WSRPActionResponseImpl(
235: pbo, _provider, actionRequest, res, portlet
236: .getPortletId(), user, layout, windowState,
237: mode);
238:
239: cachePortlet.processAction(actionRequest, actionResponse);
240: String newNavState = null;
241:
242: Map renderParams = _getRenderParameters(httpReq,
243: actionResponse);
244: newNavState = Base64.encode(ObjectSerializer
245: .serialize(renderParams));
246:
247: updateRes.setNavigationalState(newNavState);
248:
249: ActionRequestFactory.recycle(actionRequest);
250: } catch (Exception e) {
251: e.printStackTrace();
252: }
253:
254: return bir;
255: }
256:
257: private Map _getActionParameters(InteractionParams interactionParams) {
258: Map actionParams = _getParameters(interactionParams
259: .getInteractionState());
260:
261: Map formParams = _getFormParameters(interactionParams
262: .getFormParameters());
263:
264: actionParams.putAll(formParams);
265:
266: return actionParams;
267: }
268:
269: private Layout _getDefaultUserLayout(long companyId)
270: throws PortalException, SystemException {
271:
272: Group group = GroupLocalServiceUtil.getGroup(companyId,
273: GroupImpl.GUEST);
274:
275: long plid = LayoutLocalServiceUtil.getDefaultPlid(group
276: .getGroupId(), false);
277:
278: return LayoutLocalServiceUtil.getLayout(plid);
279: }
280:
281: private static Map _getFormParameters(NamedString[] params) {
282: Map formParameters = new HashMap();
283:
284: if (params != null) {
285: for (int i = 0; i < params.length; i++) {
286: String parameterName = params[i].getName();
287:
288: if (formParameters.containsKey(parameterName)) {
289: // Handle case in which parameter name has multiple values
290: String[] currentValues = (String[]) formParameters
291: .get(parameterName);
292: String[] newValues = new String[currentValues.length + 1];
293: System.arraycopy(currentValues, 0, newValues, 0,
294: currentValues.length);
295: newValues[currentValues.length] = params[i]
296: .getValue();
297: formParameters.put(parameterName, newValues);
298: } else {
299: String[] values = { params[i].getValue() };
300: formParameters.put(parameterName, values);
301: }
302: }
303: }
304:
305: return formParameters;
306: }
307:
308: private Map _getParameters(String paramString) {
309: Map navParams = new HashMap();
310:
311: if (paramString != null) {
312: byte[] desMap = Base64.decode(paramString);
313:
314: Map naviState = null;
315:
316: try {
317: naviState = ObjectDeserializer.deserializeMap(desMap);
318: } catch (IOException iox) {
319: } catch (ClassNotFoundException classNotFoundEx) {
320: }
321:
322: if (naviState != null) {
323: navParams.putAll(naviState);
324: }
325: }
326:
327: return navParams;
328: }
329:
330: private Map _getRenderParameters(MarkupParams markupParams) {
331: return _getParameters(markupParams.getNavigationalState());
332: }
333:
334: private Map _getRenderParameters(HttpServletRequest req,
335: ActionResponseImpl res) {
336: Map params = new HashMap();
337: params.putAll(res.getRenderParameters());
338:
339: String[] plid = req.getParameterValues("p_l_id");
340: params.put("p_l_id", plid);
341:
342: String[] portletName = req.getParameterValues("p_p_id");
343: params.put("p_p_id", portletName);
344:
345: String[] action = { "0" };
346: params.put("p_p_action", action);
347:
348: String[] windowState = req.getParameterValues("p_p_state");
349: params.put("p_p_state", windowState);
350:
351: String[] portletMode = req.getParameterValues("p_p_mode");
352: params.put("p_p_mode", portletMode);
353:
354: return params;
355: }
356:
357: private Provider _provider;
358:
359: }
|