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