1
19
20 package com.liferay.portlet.wsrp;
21
22 import com.liferay.portal.kernel.servlet.SessionMessages;
23 import com.liferay.portal.kernel.util.GetterUtil;
24 import com.liferay.portal.kernel.util.ParamUtil;
25 import com.liferay.portal.kernel.util.StringPool;
26 import com.liferay.portal.kernel.util.Validator;
27 import com.liferay.portal.model.Company;
28 import com.liferay.portal.util.PortalUtil;
29 import com.liferay.portal.util.WebKeys;
30 import com.liferay.portal.wsrp.util.WSRPUtil;
31 import com.liferay.portlet.StrutsPortlet;
32
33 import java.io.IOException;
34
35 import java.security.Principal;
36
37 import java.util.Map;
38
39 import javax.portlet.ActionRequest;
40 import javax.portlet.ActionResponse;
41 import javax.portlet.PortletConfig;
42 import javax.portlet.PortletException;
43 import javax.portlet.PortletMode;
44 import javax.portlet.PortletModeException;
45 import javax.portlet.PortletPreferences;
46 import javax.portlet.PortletRequest;
47 import javax.portlet.RenderRequest;
48 import javax.portlet.RenderResponse;
49 import javax.portlet.WindowStateException;
50
51 import oasis.names.tc.wsrp.v1.types.BlockingInteractionResponse;
52 import oasis.names.tc.wsrp.v1.types.MarkupContext;
53 import oasis.names.tc.wsrp.v1.types.MarkupResponse;
54 import oasis.names.tc.wsrp.v1.types.PersonName;
55 import oasis.names.tc.wsrp.v1.types.PortletDescription;
56 import oasis.names.tc.wsrp.v1.types.RegistrationData;
57 import oasis.names.tc.wsrp.v1.types.SessionContext;
58 import oasis.names.tc.wsrp.v1.types.UpdateResponse;
59 import oasis.names.tc.wsrp.v1.types.UserContext;
60 import oasis.names.tc.wsrp.v1.types.UserProfile;
61
62 import org.apache.wsrp4j.consumer.ConsumerEnvironment;
63 import org.apache.wsrp4j.consumer.GroupSession;
64 import org.apache.wsrp4j.consumer.InteractionRequest;
65 import org.apache.wsrp4j.consumer.MarkupRequest;
66 import org.apache.wsrp4j.consumer.PortletDriver;
67 import org.apache.wsrp4j.consumer.PortletKey;
68 import org.apache.wsrp4j.consumer.PortletSession;
69 import org.apache.wsrp4j.consumer.PortletWindowSession;
70 import org.apache.wsrp4j.consumer.Producer;
71 import org.apache.wsrp4j.consumer.ProducerRegistry;
72 import org.apache.wsrp4j.consumer.URLGenerator;
73 import org.apache.wsrp4j.consumer.URLTemplateComposer;
74 import org.apache.wsrp4j.consumer.User;
75 import org.apache.wsrp4j.consumer.UserSession;
76 import org.apache.wsrp4j.consumer.WSRPPortlet;
77 import org.apache.wsrp4j.consumer.driver.PortletKeyImpl;
78 import org.apache.wsrp4j.consumer.driver.ProducerImpl;
79 import org.apache.wsrp4j.consumer.driver.UserImpl;
80 import org.apache.wsrp4j.consumer.driver.WSRPPortletImpl;
81 import org.apache.wsrp4j.exception.ErrorCodes;
82 import org.apache.wsrp4j.exception.WSRPException;
83 import org.apache.wsrp4j.exception.WSRPXHelper;
84 import org.apache.wsrp4j.log.LogManager;
85 import org.apache.wsrp4j.log.Logger;
86 import org.apache.wsrp4j.util.ParameterChecker;
87
88
94 public class WSRPProxyPortlet extends StrutsPortlet {
95 public void init(PortletConfig config) throws PortletException {
96 _portletConfig = config;
97 super.init(config);
98 }
99
100 public void processAction(ActionRequest request, ActionResponse response)
101 throws PortletException, IOException {
102 boolean remoteInvocation = ParamUtil.get(request,
103 REMOTE_INVOCATION, false);
104
105 if (remoteInvocation) {
106 _processActionRemote(request, response);
107 }
108 else {
109 super.processAction(request, response);
110 }
111 }
112
113 public void render(RenderRequest request, RenderResponse response)
114 throws PortletException, IOException {
115
116 Exception producerConfigError = null;
117 try {
118 Producer producer = _getProducer(request.getPreferences());
119 request.setAttribute(WebKeys.WSRP_PRODUCER, producer);
120 }
121 catch (WSRPException e) {
122 producerConfigError = e;
123 SessionMessages.add(request, _portletConfig.getPortletName()
124 + ".configError", e);
125 }
126
127 PortletMode mode = request.getPortletMode();
128 if (mode.equals(PortletMode.VIEW)) {
129 if (producerConfigError == null) {
130 _renderRemote(request, response);
131 }
132 else {
133 super.render(request, response);
134 }
135 }
136 else {
137 boolean remoteInvocation = ParamUtil.get(request,
138 REMOTE_INVOCATION, false);
139
140 if (remoteInvocation && producerConfigError == null) {
141 _renderRemote(request, response);
142 }
143 else {
144 super.render(request, response);
145 }
146 }
147 }
148
149 public void _processActionRemote(ActionRequest request,
150 ActionResponse actionResponse) throws PortletException {
151 String MN = "processAction";
152 if (_logger.isLogging(Logger.TRACE_HIGH)) {
153 _logger.entry(Logger.TRACE_HIGH, MN);
154 }
155
156 try {
157 User user = _getUser(request);
159 String userID = null;
160 if (user != null) {
161 userID = user.getUserID();
162 }
163
164
167 PortletPreferences preferences = request.getPreferences();
168
169 PortletKey portletKey = _getPortletKey(preferences);
170 WSRPPortlet portlet = _getPortlet(portletKey, preferences);
171 PortletWindowSession windowSession = _getWindowSession(userID,
172 portlet, request);
173 PortletDriver portletDriver = _consumerEnv
174 .getPortletDriverRegistry().getPortletDriver(portlet);
175 InteractionRequest actionRequest = new WSRPRequestImpl(
176 windowSession, request, false);
177
178 BlockingInteractionResponse response = null;
180 try {
181 response = portletDriver.performBlockingInteraction(
182 actionRequest, userID);
183 _checker.check(response);
184
185 }
186 catch (java.rmi.RemoteException wsrpFault) {
187 WSRPXHelper.handleWSRPFault(_logger, wsrpFault);
188 }
189
190 if (response != null) {
192 UpdateResponse updateResponse = response.getUpdateResponse();
194 String redirectURL = response.getRedirectURL();
195
196 if (updateResponse != null) {
197 if (windowSession != null) {
199 _updateSessionContext(updateResponse
200 .getSessionContext(), windowSession
201 .getPortletSession());
202 windowSession.updateMarkupCache(updateResponse
203 .getMarkupContext());
204 }
205 _updatePortletContext(request, updateResponse
206 .getPortletContext(), portlet);
207
208 String navState = updateResponse.getNavigationalState();
211 if (navState != null) {
212 actionResponse.setRenderParameter(NAVIGATIONAL_STATE,
213 navState);
214 }
215
216 String newMode = updateResponse.getNewMode();
220 if (newMode != null) {
221 try {
222 actionResponse.setPortletMode(WSRPUtil
223 .fromWsrpMode(newMode));
224 }
225 catch (PortletModeException e) {
226 if (_logger.isLogging(Logger.INFO)) {
228 _logger.text(Logger.INFO, MN, "The portlet='"
229 + portlet.getPortletKey()
230 .getPortletHandle()
231 + "' does not support the mode="
232 + e.getMode());
233 }
234 }
235 }
236
237 String newWindowState = updateResponse.getNewWindowState();
241 if (newWindowState != null) {
242 try {
243 actionResponse.setWindowState(WSRPUtil
244 .fromWsrpWindowState(newWindowState));
245 }
246 catch (WindowStateException e) {
247 if (_logger.isLogging(Logger.INFO)) {
249 _logger
250 .text(
251 Logger.INFO,
252 MN,
253 "The portlet='"
254 + portlet
255 .getPortletKey()
256 .getPortletHandle()
257 + "' does not support the window state="
258 + e.getState());
259 }
260 }
261 }
262 }
263 else if (redirectURL != null) {
264 try {
267 actionResponse.sendRedirect(redirectURL);
268 }
269 catch (IOException ioEx) {
270 WSRPXHelper.throwX(_logger, Logger.ERROR,
271 "processAction",
272 ErrorCodes.COULD_NOT_FOLLOW_REDIRECT);
273 }
274 }
275 }
276
277 }
278 catch (WSRPException e) {
279 throw new PortletException(e);
280 }
281 finally {
282 if (_logger.isLogging(Logger.TRACE_HIGH)) {
283 _logger.exit(Logger.TRACE_HIGH, MN);
284 }
285 }
286 }
287
288 public void _renderRemote(RenderRequest request,
289 RenderResponse renderResponse) throws PortletException, IOException {
290
291 String MN = "render";
292 if (_logger.isLogging(Logger.TRACE_HIGH)) {
293 _logger.entry(Logger.TRACE_HIGH, MN);
294 }
295
296 try {
297 renderResponse.setContentType(request.getResponseContentType());
299
300 User user = _getUser(request);
302 String userID = null;
303 if (user != null) {
304 userID = user.getUserID();
305 }
306
307 PortletPreferences preferences = request.getPreferences();
310
311 PortletKey portletKey = _getPortletKey(preferences);
312 WSRPPortlet portlet = _getPortlet(portletKey, preferences);
313 PortletWindowSession windowSession = _getWindowSession(userID,
314 portlet, request);
315 PortletDriver portletDriver = _consumerEnv
316 .getPortletDriverRegistry().getPortletDriver(portlet);
317 MarkupRequest markupRequest = new WSRPRequestImpl(windowSession,
318 request, true);
319
320 synchronized (_urlGenLock) {
322
324 Company company = null;
325
326 try {
327 company = PortalUtil.getCompany(request);
328 }
329 catch (Exception e) {
330 throw new PortletException(e);
331 }
332
333 URLGenerator urlGenerator = new URLGeneratorImpl(
334 renderResponse, company.getKeyObj());
335 URLTemplateComposer templateComposer = _consumerEnv
336 .getTemplateComposer();
337 if (templateComposer != null) {
338 templateComposer.setURLGenerator(urlGenerator);
339 }
340
341 _consumerEnv.getURLRewriter().setURLGenerator(urlGenerator);
342 }
343
344 MarkupResponse response = null;
346 try {
347 response = portletDriver.getMarkup(markupRequest, userID);
348
349 _afterGetMarkup(request, response);
350
351 _checker.check(response);
352
353 }
354 catch (java.rmi.RemoteException wsrpFault) {
355 WSRPXHelper.handleWSRPFault(_logger, wsrpFault);
356 }
357
358 if (response != null) {
360 if (windowSession != null) {
361 _updateSessionContext(response.getSessionContext(),
362 windowSession.getPortletSession());
363 }
364 _processMarkupContext(response.getMarkupContext(), request,
365 renderResponse);
366 }
367
368 if (windowSession != null) {
370 windowSession.updateMarkupCache(null);
371 }
372
373 }
374 catch (WSRPException e) {
375 throw new PortletException("Error occured while retrieving markup",
376 e);
377 }
378 finally {
379 if (_logger.isLogging(Logger.TRACE_HIGH)) {
380 _logger.exit(Logger.TRACE_HIGH, MN);
381 }
382 }
383 }
384
385 protected String _processMarkupContext(MarkupContext markupContext,
386 RenderRequest renderRequest, RenderResponse renderResponse)
387 throws IOException, WSRPException {
388 final String MN = "processMarkupContext";
389
390 if (_logger.isLogging(Logger.TRACE_HIGH)) {
391 _logger.entry(Logger.TRACE_HIGH, MN);
392 }
393 String markup = null;
394
395 if (markupContext != null && renderResponse != null) {
396 String title = markupContext.getPreferredTitle();
398 if (title != null) {
399 renderResponse
400 .setTitle(getTitle(renderRequest) + " - " + title);
401 }
402
403 markup = markupContext.getMarkupString();
404 if (markup != null) {
405 try {
406 renderResponse.getWriter().write(markup);
407 }
408 catch (IOException e) {
409 WSRPXHelper.throwX(0, e);
410 }
411 }
412
413 }
415
416 if (_logger.isLogging(Logger.TRACE_HIGH)) {
417 _logger.exit(Logger.TRACE_HIGH, MN);
418 }
419
420 return markup;
421 }
422
423 protected PortletWindowSession _getWindowSession(String userID,
424 WSRPPortlet portlet, PortletRequest request) throws WSRPException {
425 PortletKey portletKey = portlet.getPortletKey();
426
427 javax.portlet.PortletSession jsrPortletSession = request
428 .getPortletSession();
429
430 _getProducer(request.getPreferences());
433
434
436 UserSession userSession = null;
437
438 synchronized (_sessionHdlrLock) {
439 SessionHandler sessionHandler = (SessionHandler) _consumerEnv
440 .getSessionHandler();
441 sessionHandler.setPortletSession(jsrPortletSession);
442
443
445 userSession = sessionHandler.getUserSession(portletKey
446 .getProducerId(), userID);
447 }
448
449 if (userSession != null) {
450
451 _customizeWindowSession(userSession, portlet, request.getPreferences());
452
453
455 PortletDescription portletDescription =
456 _getPortletDescription(portlet, request.getPreferences());
457
458 String groupID = portletDescription.getGroupID();
459
460 if (groupID == null) {
461 groupID = "default";
462 }
463
464 GroupSession groupSession = userSession.getGroupSession(groupID);
465
466 if (groupSession != null) {
467
468 String handle = _portletConfig.getPortletName();
470
471 PortletSession portletSession = groupSession
472 .getPortletSession(handle);
473
474 int sessionScope = javax.portlet.PortletSession.PORTLET_SCOPE;
475 boolean clearSessionCtx = GetterUtil.getBoolean(
476 (String) jsrPortletSession.getAttribute(
477 WebKeys.WSRP_NEW_SESSION, sessionScope));
478
479 if (clearSessionCtx) {
480 portletSession.setSessionContext(null);
481 jsrPortletSession.setAttribute(WebKeys.WSRP_NEW_SESSION,
482 "false");
483 }
484
485 if (portletSession != null) {
486
487
489 PortletWindowSession windowSession = portletSession
490 .getPortletWindowSession(handle);
491
492 return windowSession;
493 }
494 else {
495 WSRPXHelper.throwX(ErrorCodes.PORTLET_SESSION_NOT_FOUND);
496 }
497 }
498 else {
499 WSRPXHelper.throwX(ErrorCodes.GROUP_SESSION_NOT_FOUND);
500 }
501 }
502 else {
503 WSRPXHelper.throwX(ErrorCodes.USER_SESSION_NOT_FOUND);
504 }
505
506
508 return null;
509 }
510
511 protected void _updateSessionContext(SessionContext sessionContext,
512 PortletSession portletSession) {
513
514 if (portletSession != null && sessionContext != null) {
515 portletSession.setSessionContext(sessionContext);
516 }
517 }
518
519 protected void _updatePortletContext(PortletRequest request,
520 oasis.names.tc.wsrp.v1.types.PortletContext portletContext,
521 WSRPPortlet portlet) throws WSRPException {
522
523 if (portletContext != null && portlet != null) {
524
525 String newPortletHandle = portletContext.getPortletHandle();
526 PortletKey portletKey = portlet.getPortletKey();
527
528 if (newPortletHandle != null
529 && !newPortletHandle.equals(portletKey.getPortletHandle())) {
530
531 String producerID = portletKey.getProducerId();
533 PortletKey newPortletKey = new PortletKeyImpl(newPortletHandle,
534 producerID);
535 portlet = _createPortlet(newPortletKey, portlet.getParent());
536 _consumerEnv.getPortletRegistry().addPortlet(portlet);
537
538 PortletPreferences preferences = request.getPreferences();
540 try {
541 preferences.setValue("portlet-handle", newPortletHandle);
542 preferences.setValue("parent-handle", portlet.getParent());
543 preferences.store();
544 }
545 catch (Exception e) {
546 WSRPXHelper.throwX(0, e);
548 }
549
550 }
551
552 portlet.setPortletContext(portletContext);
553 }
554 }
555
556 protected User _getUser(PortletRequest request) {
557
558 User user = null;
559
560 Principal userPrincipal = request.getUserPrincipal();
561 if (userPrincipal != null) {
562 String userKey = userPrincipal.getName();
563
564 user = _consumerEnv.getUserRegistry().getUser(userKey);
565 if (user == null) {
566 user = new UserImpl(userKey);
567 UserContext userContext = new UserContext();
568 userContext.setProfile(_fillUserProfile(request));
569
570 userContext.setUserContextKey(userKey);
571 user.setUserContext(userContext);
572 _consumerEnv.getUserRegistry().addUser(user);
573 }
574 }
575
576 return user;
577 }
578
579 protected UserProfile _fillUserProfile(PortletRequest request) {
580
581 UserProfile userProfile = null;
582
583 Map userInfo = (Map) request.getAttribute(PortletRequest.USER_INFO);
584 if (userInfo != null) {
585 userProfile = new UserProfile();
586
587 PersonName personName = new PersonName();
588 personName.setPrefix((String) userInfo.get("user.name.prefix"));
589 personName.setGiven((String) userInfo.get("user.name.given"));
590 personName.setFamily((String) userInfo.get("user.name.family"));
591 personName.setMiddle((String) userInfo.get("user.name.middle"));
592 personName.setSuffix((String) userInfo.get("user.name.suffix"));
593 personName.setNickname((String) userInfo.get("user.name.nickName"));
594
595 userProfile.setName(personName);
596 }
597
598 return userProfile;
599 }
600
601 protected PortletKey _getPortletKey(PortletPreferences preferences) {
602 PortletKey portletKey = null;
603
604 String portletHandle = preferences.getValue(
605 "portlet-handle", StringPool.BLANK);
606
607 if (portletHandle != null) {
608 String producerID = _getProducerID(preferences);
609 if (producerID != null) {
610 portletKey = new PortletKeyImpl(portletHandle, producerID);
611 }
612 }
613
614 return portletKey;
615 }
616
617 protected WSRPPortlet _getPortlet(
618 PortletKey portletKey, PortletPreferences preferences)
619 throws WSRPException {
620
621 WSRPPortlet portlet = null;
622
623 if (portletKey != null) {
624
625 portlet = _consumerEnv.getPortletRegistry().getPortlet(portletKey);
626
627 if (portlet == null) {
628
629 String parentHandle = preferences.getValue(
630 "parent-handle", StringPool.BLANK);
631
632
634 if (Validator.isNull(parentHandle)) {
635 parentHandle = preferences.getValue(
636 "portlet-handle", StringPool.BLANK);;
637 }
638
639 portlet = _createPortlet(portletKey, parentHandle);
640
641 _consumerEnv.getPortletRegistry().addPortlet(portlet);
642 }
643 }
644
645 return portlet;
646 }
647
648 protected WSRPPortlet _createPortlet(PortletKey portletKey,
649 String parentHandle) {
650
651 WSRPPortlet portlet = new WSRPPortletImpl(portletKey);
652
653 oasis.names.tc.wsrp.v1.types.PortletContext portletContext =
654 new oasis.names.tc.wsrp.v1.types.PortletContext();
655
656 portletContext.setPortletHandle(portletKey.getPortletHandle());
657 portletContext.setPortletState(null);
658 portletContext.setExtensions(null);
659 portlet.setPortletContext(portletContext);
660
661 if (parentHandle != null) {
662 portlet.setParent(parentHandle);
663 }
664 else {
665 portlet.setParent(portletKey.getPortletHandle());
667 }
668
669 return portlet;
670 }
671
672 protected PortletDescription _getPortletDescription(
673 WSRPPortlet portlet, PortletPreferences preferences)
674 throws WSRPException {
675
676 Producer producer = _getProducer(preferences);
677
678 PortletDescription portletDesc =
679 producer.getPortletDescription(portlet.getParent());
680
681 if (portletDesc == null) {
682 WSRPXHelper.throwX(ErrorCodes.PORTLET_DESC_NOT_FOUND);
683 }
684
685 return portletDesc;
686 }
687
688 protected Producer _getProducer(PortletPreferences preferences)
689 throws WSRPException {
690
691 final String MN = "getProducer";
692
693 if (_logger.isLogging(Logger.TRACE_HIGH)) {
694 _logger.text(Logger.TRACE_HIGH, MN,
695 "Trying to load producer with ID :" +
696 _getProducerID(preferences));
697 }
698
699 String producerID = _getProducerID(preferences);
700
701 ProducerRegistry producerReg = _consumerEnv.getProducerRegistry();
702 Producer producer = producerReg.getProducer(producerID);
703
704 if (producer == null) {
705
706 String wsrpServiceUrl = preferences.getValue("wsrp-service-url",
707 StringPool.BLANK);
708 String markupEndpoint = preferences.getValue("markup-endpoint",
709 StringPool.BLANK);
710 String serviceDescriptionEndpoint = preferences.getValue(
711 "service-description-endpoint", StringPool.BLANK);
712 String registrationEndpoint = preferences.getValue(
713 "registration-endpoint", StringPool.BLANK);
714 String portletManagementEndpoint = preferences.getValue(
715 "portlet-management-endpoint", StringPool.BLANK);
716
717 markupEndpoint = wsrpServiceUrl + "/" + markupEndpoint;
718 serviceDescriptionEndpoint = wsrpServiceUrl + "/"
719 + serviceDescriptionEndpoint;
720 registrationEndpoint = wsrpServiceUrl + "/" + registrationEndpoint;
721 portletManagementEndpoint = wsrpServiceUrl + "/"
722 + portletManagementEndpoint;
723
724 RegistrationData regData = new RegistrationData();
725 regData.setConsumerName("Liferay WSRP Agent");
726 regData.setConsumerAgent("Liferay WSRP Agent");
727
728 producer = new ProducerImpl(producerID,
729 markupEndpoint, serviceDescriptionEndpoint,
730 registrationEndpoint, portletManagementEndpoint, regData);
731
732 producerReg.addProducer(producer);
733 }
734
735 if (producer == null) {
736 WSRPXHelper.throwX(_logger, Logger.ERROR, MN,
737 ErrorCodes.PRODUCER_DOES_NOT_EXIST);
738 }
739
740 return producer;
741 }
742
743 protected String _getProducerID(PortletPreferences preferences) {
744 String wsrpServiceUrl = preferences.getValue("wsrp-service-url",
745 StringPool.BLANK);
746 String markupEndpoint = preferences.getValue("markup-endpoint",
747 StringPool.BLANK);
748 String serviceDescriptionEndpoint = preferences.getValue(
749 "service-description-endpoint", StringPool.BLANK);
750 String registrationEndpoint = preferences.getValue(
751 "registration-endpoint", StringPool.BLANK);
752 String portletManagementEndpoint = preferences.getValue(
753 "portlet-management-endpoint", StringPool.BLANK);
754
755 StringBuilder sm = new StringBuilder();
756
757 sm.append(wsrpServiceUrl);
758 sm.append(StringPool.UNDERLINE);
759 sm.append(markupEndpoint);
760 sm.append(StringPool.UNDERLINE);
761 sm.append(serviceDescriptionEndpoint);
762 sm.append(StringPool.UNDERLINE);
763 sm.append(registrationEndpoint);
764 sm.append(StringPool.UNDERLINE);
765 sm.append(portletManagementEndpoint);
766 sm.append(StringPool.UNDERLINE);
767
768 String producerID = sm.toString();
769
770 return producerID;
771 }
772
773 protected void _afterGetMarkup(RenderRequest request,
774 MarkupResponse response) {
775 }
776
777 protected void _customizeWindowSession(UserSession userSession,
778 WSRPPortlet portlet, PortletPreferences preferences) {
779 }
780
781 protected static final ConsumerEnvironment _consumerEnv = new ConsumerEnvironmentImpl();
783
784 protected static final ParameterChecker _checker = new ParameterChecker();
786
787 private static final Logger _logger = LogManager.getLogManager().getLogger(
789 WSRPProxyPortlet.class);
790
791 private static final Object _urlGenLock = new Object();
793
794 private static final Object _sessionHdlrLock = new Object();
796
797 public static final String NAVIGATIONAL_STATE = "proxyportlet-updateResponse-navState";
799
800 public static final String REMOTE_INVOCATION = "proxyportlet-remoteInvocation";
801
802 private PortletConfig _portletConfig;
803
804 }