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