1
16
17 package org.apache.wsrp4j.consumer.driver;
18
19 import oasis.names.tc.wsrp.v1.intf.WSRP_v1_Markup_PortType;
20 import oasis.names.tc.wsrp.v1.intf.WSRP_v1_PortletManagement_PortType;
21 import oasis.names.tc.wsrp.v1.types.BlockingInteractionResponse;
22 import oasis.names.tc.wsrp.v1.types.ClientData;
23 import oasis.names.tc.wsrp.v1.types.ClonePortlet;
24 import oasis.names.tc.wsrp.v1.types.CookieProtocol;
25 import oasis.names.tc.wsrp.v1.types.DestroyPortlets;
26 import oasis.names.tc.wsrp.v1.types.DestroyPortletsResponse;
27 import oasis.names.tc.wsrp.v1.types.GetMarkup;
28 import oasis.names.tc.wsrp.v1.types.GetPortletDescription;
29 import oasis.names.tc.wsrp.v1.types.GetPortletProperties;
30 import oasis.names.tc.wsrp.v1.types.GetPortletPropertyDescription;
31 import oasis.names.tc.wsrp.v1.types.InitCookie;
32 import oasis.names.tc.wsrp.v1.types.InteractionParams;
33 import oasis.names.tc.wsrp.v1.types.InvalidCookieFault;
34 import oasis.names.tc.wsrp.v1.types.MarkupContext;
35 import oasis.names.tc.wsrp.v1.types.MarkupParams;
36 import oasis.names.tc.wsrp.v1.types.MarkupResponse;
37 import oasis.names.tc.wsrp.v1.types.PerformBlockingInteraction;
38 import oasis.names.tc.wsrp.v1.types.PortletContext;
39 import oasis.names.tc.wsrp.v1.types.PortletDescription;
40 import oasis.names.tc.wsrp.v1.types.PortletDescriptionResponse;
41 import oasis.names.tc.wsrp.v1.types.PortletPropertyDescriptionResponse;
42 import oasis.names.tc.wsrp.v1.types.PropertyList;
43 import oasis.names.tc.wsrp.v1.types.RegistrationContext;
44 import oasis.names.tc.wsrp.v1.types.ReleaseSessions;
45 import oasis.names.tc.wsrp.v1.types.ReturnAny;
46 import oasis.names.tc.wsrp.v1.types.RuntimeContext;
47 import oasis.names.tc.wsrp.v1.types.ServiceDescription;
48 import oasis.names.tc.wsrp.v1.types.SetPortletProperties;
49 import oasis.names.tc.wsrp.v1.types.StateChange;
50 import oasis.names.tc.wsrp.v1.types.Templates;
51 import oasis.names.tc.wsrp.v1.types.UserContext;
52
53 import org.apache.wsrp4j.consumer.ConsumerEnvironment;
54 import org.apache.wsrp4j.consumer.GroupSessionMgr;
55 import org.apache.wsrp4j.consumer.InteractionRequest;
56 import org.apache.wsrp4j.consumer.MarkupRequest;
57 import org.apache.wsrp4j.consumer.PortletDriver;
58 import org.apache.wsrp4j.consumer.Producer;
59 import org.apache.wsrp4j.consumer.URLRewriter;
60 import org.apache.wsrp4j.consumer.URLTemplateComposer;
61 import org.apache.wsrp4j.consumer.User;
62 import org.apache.wsrp4j.consumer.UserSessionMgr;
63 import org.apache.wsrp4j.consumer.WSRPBaseRequest;
64 import org.apache.wsrp4j.consumer.WSRPPortlet;
65 import org.apache.wsrp4j.exception.WSRPException;
66 import org.apache.wsrp4j.exception.WSRPXHelper;
67 import org.apache.wsrp4j.log.LogManager;
68 import org.apache.wsrp4j.log.Logger;
69 import org.apache.wsrp4j.util.Constants;
70 import org.apache.wsrp4j.util.ParameterChecker;
71
72
75 public class PortletDriverImpl implements PortletDriver {
76
77 private WSRPPortlet portlet = null;
78
79 private WSRP_v1_Markup_PortType markupPort = null;
80
81 private WSRP_v1_PortletManagement_PortType portletPort = null;
82
83 private ConsumerEnvironment consumerEnv = null;
84
85 private Producer producer = null;
86
87 private ParameterChecker parameterChecker = null;
88
89 private CookieProtocol initCookie = CookieProtocol.none;
90
91 private static final Logger logger = LogManager.getLogManager().getLogger(
93 PortletDriverImpl.class);
94
95 public PortletDriverImpl(WSRPPortlet portlet,
96 ConsumerEnvironment consumerEnv) throws WSRPException {
97 parameterChecker = new ParameterChecker();
98
99 this.consumerEnv = consumerEnv;
100 this.portlet = portlet;
101 this.producer = consumerEnv.getProducerRegistry().getProducer(
102 portlet.getPortletKey().getProducerId());
103
104 portletPort = producer.getPortletManagementInterface();
105
106 ServiceDescription serviceDescription = producer
107 .getServiceDescription(false);
108 if (serviceDescription != null) {
109 this.initCookie = serviceDescription.getRequiresInitCookie();
110 if (initCookie == null) {
111 initCookie = CookieProtocol.none; }
113 }
114 }
115
116
121 public WSRPPortlet getPortlet() {
122 return this.portlet;
123 }
124
125 private void resetInitCookie(String userID) throws WSRPException {
126
127 UserSessionMgr userSession = this.consumerEnv.getSessionHandler()
128 .getUserSession(getPortlet().getPortletKey().getProducerId(),
129 userID);
130
131 if (initCookie.getValue().equalsIgnoreCase(CookieProtocol._none)) {
132
133 userSession.setInitCookieDone(false);
134
135 }
136 else if (initCookie.getValue().equalsIgnoreCase(
137 CookieProtocol._perGroup)) {
138
139 PortletDescription portletDescription = null;
140 try {
141
142 portletDescription = producer
143 .getPortletDescription(getPortlet().getParent());
144
145 }
146 catch (WSRPException e) {
147
148
150 }
151
152 String groupID = null;
153 if (portletDescription != null) {
154 groupID = portletDescription.getGroupID();
155 }
156
157 if (groupID != null) {
158 GroupSessionMgr groupSession = userSession
159 .getGroupSession(groupID);
160 groupSession.setInitCookieDone(false);
161 }
162 }
163 }
164
165 private void checkInitCookie(String userID) throws WSRPException {
166 UserSessionMgr userSession = this.consumerEnv.getSessionHandler()
167 .getUserSession(getPortlet().getPortletKey().getProducerId(),
168 userID);
169
170 if (initCookie.getValue().equalsIgnoreCase(CookieProtocol._perUser)) {
171
172 this.markupPort = userSession.getWSRPBaseService();
173
174 if (!userSession.isInitCookieDone()) {
175
176 userSession.setInitCookieRequired(true);
177 initCookie();
178 userSession.setInitCookieDone(true);
179 }
180
181 }
182 else if (initCookie.getValue().equalsIgnoreCase(
183 CookieProtocol._perGroup)) {
184
185 PortletDescription portletDescription = producer
186 .getPortletDescription(getPortlet().getParent());
187 String groupID = null;
188 if (portletDescription != null) {
189 groupID = portletDescription.getGroupID();
190 }
191
192 if (groupID != null) {
193 GroupSessionMgr groupSession = userSession
194 .getGroupSession(groupID);
195
196 this.markupPort = groupSession.getWSRPBaseService();
197
198 if (!groupSession.isInitCookieDone()) {
199 groupSession.setInitCookieRequired(true);
200 initCookie();
201 groupSession.setInitCookieDone(true);
202 }
203
204 }
205 else {
206 }
210 }
211 else {
212 this.markupPort = userSession.getWSRPBaseService();
213 }
214
215 }
216
217 private MarkupParams getMarkupParams(WSRPBaseRequest request) {
218
219 MarkupParams markupParams = new MarkupParams();
220 ClientData clientData = new ClientData();
221
222 clientData.setUserAgent(producer.getRegistrationData()
224 .getConsumerAgent());
225 markupParams.setClientData(clientData);
226 markupParams.setSecureClientCommunication(false);
227 markupParams.setLocales(consumerEnv.getSupportedLocales());
228
229 markupParams.setMimeTypes(consumerEnv.getMimeTypes());
230
231 markupParams.setMode(request.getMode());
232 markupParams.setWindowState(request.getWindowState());
233 markupParams.setNavigationalState(request.getNavigationalState());
234 markupParams.setMarkupCharacterSets(consumerEnv
235 .getCharacterEncodingSet());
236 markupParams.setValidateTag(null);
238 markupParams.setValidNewModes(consumerEnv.getSupportedModes());
241 markupParams.setValidNewWindowStates(consumerEnv
242 .getSupportedWindowStates());
243
244 markupParams.setExtensions(null);
245
246 return markupParams;
247 }
248
249 private RuntimeContext getRuntimeContext(WSRPBaseRequest request) {
250 RuntimeContext runtimeContext = new RuntimeContext();
251 runtimeContext.setUserAuthentication(consumerEnv
252 .getUserAuthentication());
253 runtimeContext.setPortletInstanceKey(request.getPortletInstanceKey());
254
255 URLTemplateComposer templateComposer = consumerEnv
256 .getTemplateComposer();
257 if (templateComposer != null) {
258 runtimeContext.setNamespacePrefix(templateComposer
259 .getNamespacePrefix());
260 }
261
262 Boolean doesUrlTemplateProcess = null;
263 try {
264
265 PortletDescription desc = producer
266 .getPortletDescription(getPortlet().getParent());
267
268 if (desc != null) {
269
270 doesUrlTemplateProcess = desc.getDoesUrlTemplateProcessing();
271 }
272
273 }
274 catch (WSRPException e) {
275
276 }
279
280 if (doesUrlTemplateProcess != null && templateComposer != null
281 && doesUrlTemplateProcess.booleanValue()) {
282 Templates templates = new Templates();
283 templates.setBlockingActionTemplate(templateComposer
284 .createBlockingActionTemplate(true, true, true, true));
285 templates.setRenderTemplate(templateComposer.createRenderTemplate(
286 true, true, true, true));
287 templates.setDefaultTemplate(templateComposer
288 .createDefaultTemplate(true, true, true, true));
289 templates.setResourceTemplate(templateComposer
290 .createResourceTemplate(true, true, true, true));
291 templates
292 .setSecureBlockingActionTemplate(templateComposer
293 .createSecureBlockingActionTemplate(true, true,
294 true, true));
295 templates.setSecureRenderTemplate(templateComposer
296 .createSecureRenderTemplate(true, true, true, true));
297 templates.setSecureDefaultTemplate(templateComposer
298 .createSecureDefaultTemplate(true, true, true, true));
299 templates.setSecureResourceTemplate(templateComposer
300 .createSecureResourceTemplate(true, true, true, true));
301 runtimeContext.setTemplates(templates);
302 }
303
304 runtimeContext.setSessionID(request.getSessionID());
305 runtimeContext.setExtensions(null);
306
307 return runtimeContext;
308 }
309
310 private UserContext getUserContext(String userID) {
311 UserContext userContext = null;
312
313 if (userID != null) {
314 User user = consumerEnv.getUserRegistry().getUser(userID);
315
316 if (user != null) {
317 userContext = user.getUserContext();
318 }
319 }
320
321
324 if (userContext == null) {
325 userContext = new UserContext();
326 userContext.setUserContextKey("dummyUserContextKey");
327 }
328
329 return userContext;
330 }
331
332 private InteractionParams getInteractionParams(
333 InteractionRequest actionRequest) {
334 InteractionParams interactionParams = new InteractionParams();
335
336 interactionParams.setPortletStateChange(consumerEnv
337 .getPortletStateChange());
338
339 if (!portlet.isConsumerConfigured()
343 && interactionParams.getPortletStateChange().toString()
344 .equalsIgnoreCase(StateChange._readWrite)) {
345 interactionParams
346 .setPortletStateChange(StateChange.cloneBeforeWrite);
347 }
348
349 interactionParams.setInteractionState(actionRequest
350 .getInteractionState());
351 interactionParams.setFormParameters(actionRequest.getFormParameters());
352 interactionParams.setUploadContexts(actionRequest.getUploadContexts());
353 interactionParams.setExtensions(null);
354
355 return interactionParams;
356 }
357
358
365 public MarkupResponse getMarkup(MarkupRequest markupRequest, String userID)
366 throws WSRPException {
367 checkInitCookie(userID);
368
369 MarkupResponse response = null;
370
371 try {
372
373 MarkupContext markupContext = null;
374 if ((markupContext = markupRequest.getCachedMarkup()) == null) {
375
376 GetMarkup request = new GetMarkup();
378
379 request.setPortletContext(getPortlet().getPortletContext());
380 request.setMarkupParams(getMarkupParams(markupRequest));
381 request.setRuntimeContext(getRuntimeContext(markupRequest));
382
383 RegistrationContext regCtx = producer.getRegistrationContext();
384 if (regCtx != null)
385 request.setRegistrationContext(regCtx);
386
387 UserContext userCtx = getUserContext(userID);
388 if (userCtx != null)
389 request.setUserContext(getUserContext(userID));
390
391 response = markupPort.getMarkup(request);
392
393 parameterChecker.check(response);
394
395 }
396 else {
397
398 response = new MarkupResponse();
399 response.setMarkupContext(markupContext);
400 }
401
402 Boolean requiresRewriting = response.getMarkupContext()
403 .getRequiresUrlRewriting();
404 requiresRewriting = requiresRewriting == null ? Boolean.FALSE
405 : requiresRewriting;
406
407 if (requiresRewriting.booleanValue()) {
408
410 URLRewriter urlRewriter = consumerEnv.getURLRewriter();
411 String rewrittenMarkup = urlRewriter.rewriteURLs(response
412 .getMarkupContext().getMarkupString());
413
414 if (rewrittenMarkup != null) {
415 response.getMarkupContext()
416 .setMarkupString(rewrittenMarkup);
417 }
418 }
419
420 }
421 catch (InvalidCookieFault cookieFault) {
422
423 resetInitCookie(userID);
425
426 getMarkup(markupRequest, userID);
428
429 }
430 catch (java.rmi.RemoteException wsrpFault) {
431
432 WSRPXHelper.handleWSRPFault(logger, wsrpFault);
433
434 }
435
436 return response;
437 }
438
439
444 public BlockingInteractionResponse performBlockingInteraction(
445 InteractionRequest actionRequest, String userID)
446 throws WSRPException {
447
448 checkInitCookie(userID);
449
450 BlockingInteractionResponse response = null;
451
452 try {
453 PerformBlockingInteraction request = new PerformBlockingInteraction();
454
455 request.setPortletContext(getPortlet().getPortletContext());
456 request.setInteractionParams(getInteractionParams(actionRequest));
457 request.setMarkupParams(getMarkupParams(actionRequest));
458 request.setRuntimeContext(getRuntimeContext(actionRequest));
459
460 RegistrationContext regCtx = producer.getRegistrationContext();
461 if (regCtx != null)
462 request.setRegistrationContext(regCtx);
463
464 UserContext userCtx = getUserContext(userID);
465 if (userCtx != null)
466 request.setUserContext(userCtx);
467
468 response = markupPort.performBlockingInteraction(request);
469
470 parameterChecker.check(response);
471
472 }
473 catch (InvalidCookieFault cookieFault) {
474
475 resetInitCookie(userID);
477
478 performBlockingInteraction(actionRequest, userID);
480
481 }
482 catch (java.rmi.RemoteException wsrpFault) {
483
484 WSRPXHelper.handleWSRPFault(logger, wsrpFault);
485 }
486
487 return response;
488 }
489
490
495 public PortletContext clonePortlet(String userID) throws WSRPException {
496 ClonePortlet request = new ClonePortlet();
497
498 request.setPortletContext(getPortlet().getPortletContext());
499
500 RegistrationContext regCtx = producer.getRegistrationContext();
501 if (regCtx != null)
502 request.setRegistrationContext(regCtx);
503
504 UserContext userCtx = getUserContext(userID);
505 if (userCtx != null)
506 request.setUserContext(userCtx);
507
508 PortletContext response = null;
509
510 try {
511
512 response = portletPort.clonePortlet(request);
513 parameterChecker.check(response, Constants.NILLABLE_FALSE);
514
515 }
516 catch (java.rmi.RemoteException wsrpFault) {
517
518 WSRPXHelper.handleWSRPFault(logger, wsrpFault);
519 }
520
521 return response;
522 }
523
524
527 public DestroyPortletsResponse destroyPortlets(String[] portletHandles,
528 String userID) throws WSRPException {
529 DestroyPortlets request = new DestroyPortlets();
530
531 RegistrationContext regCtx = producer.getRegistrationContext();
532 if (regCtx != null)
533 request.setRegistrationContext(regCtx);
534
535 request.setPortletHandles(portletHandles);
536
537 DestroyPortletsResponse response = null;
538 try {
539
540 response = portletPort.destroyPortlets(request);
541 parameterChecker.check(response);
542
543 }
544 catch (java.rmi.RemoteException wsrpFault) {
545
546 WSRPXHelper.handleWSRPFault(logger, wsrpFault);
547 }
548
549 return response;
550 }
551
552
556 public ReturnAny releaseSessions(String[] sessionIDs, String userID)
557 throws WSRPException {
558
559 checkInitCookie(userID);
560
561 ReleaseSessions request = new ReleaseSessions();
562
563 RegistrationContext regCtx = producer.getRegistrationContext();
564 if (regCtx != null)
565 request.setRegistrationContext(regCtx);
566
567 request.setSessionIDs(sessionIDs);
568
569 ReturnAny response = null;
570 try {
571
572 response = markupPort.releaseSessions(request);
573
574 }
575 catch (java.rmi.RemoteException wsrpFault) {
576
577 WSRPXHelper.handleWSRPFault(logger, wsrpFault);
578 }
579
580 return response;
581 }
582
583
586 public void initCookie() throws WSRPException {
587 InitCookie request = new InitCookie();
588
589 RegistrationContext regCtx = producer.getRegistrationContext();
590 if (regCtx != null)
591 request.setRegistrationContext(regCtx);
592
593 try {
594
595 markupPort.initCookie(request);
596
597 }
598 catch (java.rmi.RemoteException wsrpFault) {
599 WSRPXHelper.handleWSRPFault(logger, wsrpFault);
600 }
601 }
602
603
611 public PortletDescriptionResponse getPortletDescription(String userID,
612 String[] desiredLocales) throws WSRPException {
613 GetPortletDescription request = new GetPortletDescription();
614
615 RegistrationContext regCtx = producer.getRegistrationContext();
616 if (regCtx != null)
617 request.setRegistrationContext(regCtx);
618
619 request.setPortletContext(getPortlet().getPortletContext());
620
621 UserContext userCtx = getUserContext(userID);
622 if (userCtx != null)
623 request.setUserContext(userCtx);
624
625 request.setDesiredLocales(desiredLocales);
626
627 PortletDescriptionResponse response = null;
628
629 try {
630
631 response = portletPort.getPortletDescription(request);
632 parameterChecker.check(response);
633
634 }
635 catch (java.rmi.RemoteException wsrpFault) {
636
637 WSRPXHelper.handleWSRPFault(logger, wsrpFault);
638 }
639
640 return response;
641 }
642
643
650 public PortletPropertyDescriptionResponse getPortletPropertyDescription(
651 String userID) throws WSRPException {
652 GetPortletPropertyDescription request = new GetPortletPropertyDescription();
653 request.setPortletContext(getPortlet().getPortletContext());
654
655 RegistrationContext regCtx = producer.getRegistrationContext();
656 if (regCtx != null)
657 request.setRegistrationContext(regCtx);
658
659 UserContext userCtx = getUserContext(userID);
660 if (userCtx != null)
661 request.setUserContext(userCtx);
662
663 request.setDesiredLocales(consumerEnv.getSupportedLocales());
664
665 PortletPropertyDescriptionResponse response = null;
666
667 try {
668
669 response = portletPort.getPortletPropertyDescription(request);
670 parameterChecker.check(response);
671
672 }
673 catch (java.rmi.RemoteException wsrpFault) {
674
675 WSRPXHelper.handleWSRPFault(logger, wsrpFault);
676 }
677
678 return response;
679 }
680
681
689 public PropertyList getPortletProperties(String[] names, String userID)
690 throws WSRPException {
691 GetPortletProperties request = new GetPortletProperties();
692 request.setPortletContext(getPortlet().getPortletContext());
693 request.setNames(names);
694
695 RegistrationContext regCtx = producer.getRegistrationContext();
696 if (regCtx != null)
697 request.setRegistrationContext(regCtx);
698
699 UserContext userCtx = getUserContext(userID);
700 if (userCtx != null)
701 request.setUserContext(userCtx);
702
703 PropertyList response = null;
704
705 try {
706
707 response = portletPort.getPortletProperties(request);
708 parameterChecker.check(response, Constants.NILLABLE_FALSE);
709
710 }
711 catch (java.rmi.RemoteException wsrpFault) {
712
713 WSRPXHelper.handleWSRPFault(logger, wsrpFault);
714 }
715
716 return response;
717 }
718
719
725 public PortletContext setPortletProperties(PropertyList properties,
726 String userID) throws WSRPException {
727 SetPortletProperties request = new SetPortletProperties();
728 request.setPortletContext(getPortlet().getPortletContext());
729
730 RegistrationContext regCtx = producer.getRegistrationContext();
731 if (regCtx != null)
732 request.setRegistrationContext(regCtx);
733
734 UserContext userCtx = getUserContext(userID);
735 if (userCtx != null)
736 request.setUserContext(userCtx);
737 request.setPropertyList(properties);
738
739 PortletContext response = null;
740
741 try {
742
743 response = portletPort.setPortletProperties(request);
744 parameterChecker.check(response, Constants.NILLABLE_FALSE);
745
746 }
747 catch (java.rmi.RemoteException wsrpFault) {
748
749 WSRPXHelper.handleWSRPFault(logger, wsrpFault);
750 }
751
752 return response;
753 }
754 }