1 16 package org.apache.cocoon.portal.wsrp.consumer; 17 18 import oasis.names.tc.wsrp.v1.intf.WSRP_v1_Markup_PortType; 19 import oasis.names.tc.wsrp.v1.intf.WSRP_v1_PortletManagement_PortType; 20 import oasis.names.tc.wsrp.v1.types.BlockingInteractionResponse; 21 import oasis.names.tc.wsrp.v1.types.ClientData; 22 import oasis.names.tc.wsrp.v1.types.CookieProtocol; 23 import oasis.names.tc.wsrp.v1.types.DestroyPortletsResponse; 24 import oasis.names.tc.wsrp.v1.types.Extension; 25 import oasis.names.tc.wsrp.v1.types.InteractionParams; 26 import oasis.names.tc.wsrp.v1.types.InvalidCookieFault; 27 import oasis.names.tc.wsrp.v1.types.MarkupContext; 28 import oasis.names.tc.wsrp.v1.types.MarkupParams; 29 import oasis.names.tc.wsrp.v1.types.MarkupResponse; 30 import oasis.names.tc.wsrp.v1.types.MarkupType; 31 import oasis.names.tc.wsrp.v1.types.PortletContext; 32 import oasis.names.tc.wsrp.v1.types.PortletDescription; 33 import oasis.names.tc.wsrp.v1.types.PortletDescriptionResponse; 34 import oasis.names.tc.wsrp.v1.types.PortletPropertyDescriptionResponse; 35 import oasis.names.tc.wsrp.v1.types.PropertyList; 36 import oasis.names.tc.wsrp.v1.types.RegistrationContext; 37 import oasis.names.tc.wsrp.v1.types.ReturnAny; 38 import oasis.names.tc.wsrp.v1.types.RuntimeContext; 39 import oasis.names.tc.wsrp.v1.types.ServiceDescription; 40 import oasis.names.tc.wsrp.v1.types.StateChange; 41 import oasis.names.tc.wsrp.v1.types.Templates; 42 import oasis.names.tc.wsrp.v1.types.UserContext; 43 import oasis.names.tc.wsrp.v1.types.ClonePortlet; 44 import oasis.names.tc.wsrp.v1.types.DestroyPortlets; 45 import oasis.names.tc.wsrp.v1.types.GetMarkup; 46 import oasis.names.tc.wsrp.v1.types.GetPortletDescription; 47 import oasis.names.tc.wsrp.v1.types.GetPortletProperties; 48 import oasis.names.tc.wsrp.v1.types.GetPortletPropertyDescription; 49 import oasis.names.tc.wsrp.v1.types.InitCookie; 50 import oasis.names.tc.wsrp.v1.types.PerformBlockingInteraction; 51 import oasis.names.tc.wsrp.v1.types.ReleaseSessions; 52 import oasis.names.tc.wsrp.v1.types.SetPortletProperties; 53 54 import org.apache.avalon.framework.logger.AbstractLogEnabled; 55 import org.apache.cocoon.portal.PortalService; 56 import org.apache.cocoon.portal.wsrp.logging.WSRPLogger; 57 import org.apache.commons.lang.ArrayUtils; 58 import org.apache.wsrp4j.consumer.ConsumerEnvironment; 59 import org.apache.wsrp4j.consumer.GroupSessionMgr; 60 import org.apache.wsrp4j.consumer.InteractionRequest; 61 import org.apache.wsrp4j.consumer.MarkupRequest; 62 import org.apache.wsrp4j.consumer.PortletDriver; 63 import org.apache.wsrp4j.consumer.Producer; 64 import org.apache.wsrp4j.consumer.URLRewriter; 65 import org.apache.wsrp4j.consumer.URLTemplateComposer; 66 import org.apache.wsrp4j.consumer.User; 67 import org.apache.wsrp4j.consumer.UserSessionMgr; 68 import org.apache.wsrp4j.consumer.WSRPBaseRequest; 69 import org.apache.wsrp4j.consumer.WSRPPortlet; 70 import org.apache.wsrp4j.exception.WSRPException; 71 import org.apache.wsrp4j.exception.WSRPXHelper; 72 import org.apache.wsrp4j.log.Logger; 73 import org.apache.wsrp4j.util.Constants; 74 import org.apache.wsrp4j.util.ParameterChecker; 75 76 80 public class PortletDriverImpl 81 extends AbstractLogEnabled 82 implements PortletDriver, RequiresConsumerEnvironment, RequiresPortalService { 83 84 protected WSRPPortlet portlet; 85 protected WSRP_v1_Markup_PortType markupPort; 86 protected WSRP_v1_PortletManagement_PortType portletPort; 87 protected ConsumerEnvironment consumerEnv; 88 protected Producer producer; 89 protected ParameterChecker parameterChecker; 90 protected CookieProtocol initCookie = CookieProtocol.none; 91 protected PortletDescription desc; 92 93 94 protected Logger logger; 95 96 97 protected PortalService service; 98 99 102 public void enableLogging(org.apache.avalon.framework.logger.Logger arg0) { 103 super.enableLogging(arg0); 104 this.logger = new WSRPLogger(arg0); 105 } 106 107 110 public void setConsumerEnvironment(ConsumerEnvironment env) { 111 this.consumerEnv = env; 112 } 113 114 117 public void setPortalService(PortalService service) { 118 this.service = service; 119 } 120 121 126 public void init(WSRPPortlet portlet) 127 throws WSRPException { 128 this.parameterChecker = new ParameterChecker(); 129 130 this.portlet = portlet; 131 this.producer = consumerEnv.getProducerRegistry().getProducer(portlet.getPortletKey().getProducerId()); 132 133 this.portletPort = this.producer.getPortletManagementInterface(); 134 this.desc = this.producer.getPortletDescription(this.portlet.getPortletKey().getPortletHandle()); 135 136 ServiceDescription serviceDescription = this.producer.getServiceDescription(false); 137 if (serviceDescription != null) { 138 this.initCookie = serviceDescription.getRequiresInitCookie(); 139 if (initCookie == null) { 140 initCookie = CookieProtocol.none; } 142 } 143 } 144 145 150 public WSRPPortlet getPortlet() { 151 return this.portlet; 152 } 153 154 protected void resetInitCookie(String userID) throws WSRPException { 155 156 UserSessionMgr userSession = 157 this.consumerEnv.getSessionHandler().getUserSession( 158 getPortlet().getPortletKey().getProducerId(), 159 userID); 160 161 if (initCookie.getValue().equalsIgnoreCase(CookieProtocol._none)) { 162 163 userSession.setInitCookieDone(false); 164 165 } else if (initCookie.getValue().equalsIgnoreCase(CookieProtocol._perGroup)) { 166 167 PortletDescription portletDescription = null; 168 try { 169 170 portletDescription = producer.getPortletDescription(getPortlet().getParent()); 171 172 } catch (WSRPException e) { 173 174 176 } 177 178 String groupID = null; 179 if (portletDescription != null) { 180 groupID = portletDescription.getGroupID(); 181 } 182 183 if (groupID != null) { 184 GroupSessionMgr groupSession = userSession.getGroupSession(groupID); 185 groupSession.setInitCookieDone(false); 186 } 187 } 188 } 189 190 protected void checkInitCookie(String userID) throws WSRPException { 191 UserSessionMgr userSession = 192 this.consumerEnv.getSessionHandler().getUserSession( 193 getPortlet().getPortletKey().getProducerId(), 194 userID); 195 196 if (initCookie.getValue().equalsIgnoreCase(CookieProtocol._perUser)) { 197 198 this.markupPort = userSession.getWSRPBaseService(); 199 200 if (!userSession.isInitCookieDone()) { 201 202 userSession.setInitCookieRequired(true); 203 initCookie(); 204 userSession.setInitCookieDone(true); 205 } 206 207 } else if (initCookie.getValue().equalsIgnoreCase(CookieProtocol._perGroup)) { 208 209 PortletDescription portletDescription = producer.getPortletDescription(getPortlet().getParent()); 210 String groupID = null; 211 if (portletDescription != null) { 212 groupID = portletDescription.getGroupID(); 213 } 214 215 if (groupID != null) { 216 GroupSessionMgr groupSession = userSession.getGroupSession(groupID); 217 218 this.markupPort = groupSession.getWSRPBaseService(); 219 220 if (!groupSession.isInitCookieDone()) { 221 groupSession.setInitCookieRequired(true); 222 initCookie(); 223 groupSession.setInitCookieDone(true); 224 } 225 226 } else { 227 } 231 } else { 232 this.markupPort = userSession.getWSRPBaseService(); 233 } 234 235 } 236 237 protected MarkupParams getMarkupParams(WSRPBaseRequest request) { 238 239 MarkupParams markupParams = new MarkupParams(); 240 ClientData clientData = null; 241 242 if (producer.getRegistrationData() != null) { 244 clientData = new ClientData(); 245 clientData.setUserAgent(producer.getRegistrationData().getConsumerAgent()); 246 } 247 markupParams.setClientData(clientData); 248 markupParams.setSecureClientCommunication(this.service.getComponentManager().getLinkService().isSecure()); 249 markupParams.setLocales(consumerEnv.getSupportedLocales()); 250 251 markupParams.setMimeTypes(consumerEnv.getMimeTypes()); 252 253 markupParams.setMode(request.getMode()); 254 markupParams.setWindowState(request.getWindowState()); 255 markupParams.setNavigationalState(request.getNavigationalState()); 256 markupParams.setMarkupCharacterSets(consumerEnv.getCharacterEncodingSet()); 257 markupParams.setValidateTag(null); 259 final MarkupType markupType = this.desc.getMarkupTypes(0); 261 markupParams.setValidNewModes(this.getValidValues(consumerEnv.getSupportedModes(), markupType.getModes())); 262 markupParams.setValidNewWindowStates(this.getValidValues(consumerEnv.getSupportedWindowStates(), markupType.getWindowStates())); 263 264 markupParams.setExtensions(this.getMarkupParamsExtensions()); 265 266 return markupParams; 267 } 268 269 275 protected String [] getValidValues(String [] supported, String [] allowed) { 276 String [] validModes = new String [0]; 277 for(int i=0; i < supported.length; i++ ) { 278 if ( ArrayUtils.contains(allowed, supported[i]) ) { 279 String [] newValidModes = new String [validModes.length+1]; 280 System.arraycopy(validModes, 0, newValidModes, 0, validModes.length); 281 newValidModes[validModes.length] = supported[i]; 282 validModes = newValidModes; 283 } 284 } 285 return validModes; 286 } 287 288 291 protected RuntimeContext getRuntimeContext(WSRPBaseRequest request) { 292 RuntimeContext runtimeContext = new RuntimeContext(); 293 runtimeContext.setUserAuthentication(consumerEnv.getUserAuthentication()); 294 runtimeContext.setPortletInstanceKey(request.getPortletInstanceKey()); 295 296 URLTemplateComposer templateComposer = consumerEnv.getTemplateComposer(); 297 if (templateComposer != null) { 298 runtimeContext.setNamespacePrefix(templateComposer.getNamespacePrefix()); 299 } 300 301 Boolean doesUrlTemplateProcess = null; 302 try { 303 304 PortletDescription desc = producer.getPortletDescription(getPortlet().getParent()); 305 306 if (desc != null) { 307 308 doesUrlTemplateProcess = desc.getDoesUrlTemplateProcessing(); 309 } 310 311 } catch (WSRPException e) { 312 313 } 316 317 if (doesUrlTemplateProcess != null && templateComposer != null && doesUrlTemplateProcess.booleanValue()) { 318 Templates templates = new Templates(); 319 templates.setBlockingActionTemplate(templateComposer.createBlockingActionTemplate(true, true, true, true)); 320 templates.setRenderTemplate(templateComposer.createRenderTemplate(true, true, true, true)); 321 templates.setDefaultTemplate(templateComposer.createDefaultTemplate(true, true, true, true)); 322 templates.setResourceTemplate(templateComposer.createResourceTemplate(true, true, true, true)); 323 templates.setSecureBlockingActionTemplate( 324 templateComposer.createSecureBlockingActionTemplate(true, true, true, true)); 325 templates.setSecureRenderTemplate(templateComposer.createSecureRenderTemplate(true, true, true, true)); 326 templates.setSecureDefaultTemplate(templateComposer.createSecureDefaultTemplate(true, true, true, true)); 327 templates.setSecureResourceTemplate(templateComposer.createSecureResourceTemplate(true, true, true, true)); 328 runtimeContext.setTemplates(templates); 329 } 330 331 runtimeContext.setSessionID(request.getSessionID()); 332 runtimeContext.setExtensions(null); 333 334 return runtimeContext; 335 } 336 337 protected UserContext getUserContext(String userID) { 338 UserContext userContext = null; 339 340 if (userID != null) { 341 User user = consumerEnv.getUserRegistry().getUser(userID); 342 343 if (user != null) { 344 userContext = user.getUserContext(); 345 } 346 } 347 348 351 if (userContext == null) { 352 userContext = new UserContext(); 353 userContext.setUserContextKey("dummyUserContextKey"); 354 } 355 356 return userContext; 357 } 358 359 protected InteractionParams getInteractionParams(InteractionRequest actionRequest) { 360 InteractionParams interactionParams = new InteractionParams(); 361 362 interactionParams.setPortletStateChange(consumerEnv.getPortletStateChange()); 363 364 if (!portlet.isConsumerConfigured() && 368 interactionParams.getPortletStateChange().toString().equalsIgnoreCase(StateChange._readWrite)) { 369 interactionParams.setPortletStateChange(StateChange.cloneBeforeWrite); 370 } 371 372 interactionParams.setInteractionState(actionRequest.getInteractionState()); 373 interactionParams.setFormParameters(actionRequest.getFormParameters()); 374 interactionParams.setUploadContexts(null); 375 interactionParams.setExtensions(null); 376 377 return interactionParams; 378 } 379 380 387 public MarkupResponse getMarkup(MarkupRequest markupRequest, 388 String userID) 389 throws WSRPException { 390 checkInitCookie(userID); 391 392 MarkupResponse response = null; 393 394 try { 395 396 MarkupContext markupContext = null; 397 if ((markupContext = markupRequest.getCachedMarkup()) == null) { 398 399 GetMarkup request = new GetMarkup(); 401 402 request.setPortletContext(getPortlet().getPortletContext()); 403 request.setMarkupParams(getMarkupParams(markupRequest)); 404 request.setRuntimeContext(getRuntimeContext(markupRequest)); 405 406 RegistrationContext regCtx = producer.getRegistrationContext(); 407 if (regCtx != null) 408 request.setRegistrationContext(regCtx); 409 410 UserContext userCtx = getUserContext(userID); 411 if (userCtx != null) { 412 request.setUserContext(getUserContext(userID)); 413 } 414 415 response = markupPort.getMarkup(request); 416 417 parameterChecker.check(response); 418 419 } else { 420 421 response = new MarkupResponse(); 422 response.setMarkupContext(markupContext); 423 } 424 425 Boolean requiresRewriting = response.getMarkupContext().getRequiresUrlRewriting(); 426 requiresRewriting = requiresRewriting == null ? Boolean.FALSE : requiresRewriting; 427 428 if (requiresRewriting.booleanValue()) { 429 431 URLRewriter urlRewriter = consumerEnv.getURLRewriter(); 432 String rewrittenMarkup = urlRewriter.rewriteURLs(response.getMarkupContext().getMarkupString()); 433 434 if (rewrittenMarkup != null) { 435 response.getMarkupContext().setMarkupString(rewrittenMarkup); 436 } 437 } 438 439 } catch (InvalidCookieFault cookieFault) { 440 441 resetInitCookie(userID); 443 444 response = getMarkup(markupRequest, userID); 446 447 } catch (java.rmi.RemoteException wsrpFault) { 448 449 WSRPXHelper.handleWSRPFault(logger, wsrpFault); 450 451 } 452 453 return response; 454 } 455 456 461 public BlockingInteractionResponse performBlockingInteraction(InteractionRequest actionRequest, 462 String userID) 463 throws WSRPException { 464 465 checkInitCookie(userID); 466 467 BlockingInteractionResponse response = null; 468 469 try { 470 PerformBlockingInteraction request = new PerformBlockingInteraction(); 471 472 request.setPortletContext(getPortlet().getPortletContext()); 473 request.setInteractionParams(getInteractionParams(actionRequest)); 474 request.setMarkupParams(getMarkupParams(actionRequest)); 475 request.setRuntimeContext(getRuntimeContext(actionRequest)); 476 477 RegistrationContext regCtx = producer.getRegistrationContext(); 478 if (regCtx != null) 479 request.setRegistrationContext(regCtx); 480 481 UserContext userCtx = getUserContext(userID); 482 if (userCtx != null) 483 request.setUserContext(userCtx); 484 485 response = markupPort.performBlockingInteraction(request); 486 487 parameterChecker.check(response); 488 489 } catch (InvalidCookieFault cookieFault) { 490 491 resetInitCookie(userID); 493 494 response = performBlockingInteraction(actionRequest, userID); 496 497 } catch (java.rmi.RemoteException wsrpFault) { 498 499 WSRPXHelper.handleWSRPFault(logger, wsrpFault); 500 } 501 502 return response; 503 } 504 505 510 public PortletContext clonePortlet(String userID) throws WSRPException { 511 ClonePortlet request = new ClonePortlet(); 512 513 request.setPortletContext(getPortlet().getPortletContext()); 514 515 RegistrationContext regCtx = producer.getRegistrationContext(); 516 if (regCtx != null) 517 request.setRegistrationContext(regCtx); 518 519 UserContext userCtx = getUserContext(userID); 520 if (userCtx != null) 521 request.setUserContext(userCtx); 522 523 PortletContext response = null; 524 525 try { 526 527 response = portletPort.clonePortlet(request); 528 parameterChecker.check(response, Constants.NILLABLE_FALSE); 529 530 } catch (java.rmi.RemoteException wsrpFault) { 531 532 WSRPXHelper.handleWSRPFault(logger, wsrpFault); 533 } 534 535 return response; 536 } 537 538 541 public DestroyPortletsResponse destroyPortlets(String [] portletHandles, 542 String userID) 543 throws WSRPException { 544 DestroyPortlets request = new DestroyPortlets(); 545 546 RegistrationContext regCtx = producer.getRegistrationContext(); 547 if (regCtx != null) 548 request.setRegistrationContext(regCtx); 549 550 request.setPortletHandles(portletHandles); 551 552 DestroyPortletsResponse response = null; 553 try { 554 555 response = portletPort.destroyPortlets(request); 556 parameterChecker.check(response); 557 558 } catch (java.rmi.RemoteException wsrpFault) { 559 560 WSRPXHelper.handleWSRPFault(logger, wsrpFault); 561 } 562 563 return response; 564 } 565 566 570 public ReturnAny releaseSessions(String [] sessionIDs, 571 String userID) 572 throws WSRPException { 573 574 checkInitCookie(userID); 575 576 ReleaseSessions request = new ReleaseSessions(); 577 578 RegistrationContext regCtx = producer.getRegistrationContext(); 579 if (regCtx != null) 580 request.setRegistrationContext(regCtx); 581 582 request.setSessionIDs(sessionIDs); 583 584 ReturnAny response = null; 585 try { 586 587 response = markupPort.releaseSessions(request); 588 589 } catch (java.rmi.RemoteException wsrpFault) { 590 591 WSRPXHelper.handleWSRPFault(logger, wsrpFault); 592 } 593 594 return response; 595 } 596 597 600 public void initCookie() throws WSRPException { 601 InitCookie request = new InitCookie(); 602 603 RegistrationContext regCtx = producer.getRegistrationContext(); 604 if (regCtx != null) 605 request.setRegistrationContext(regCtx); 606 607 try { 608 609 markupPort.initCookie(request); 610 611 } catch (java.rmi.RemoteException wsrpFault) { 612 WSRPXHelper.handleWSRPFault(logger, wsrpFault); 613 } 614 } 615 616 624 public PortletDescriptionResponse getPortletDescription(String userID, 625 String [] desiredLocales) 626 throws WSRPException { 627 GetPortletDescription request = new GetPortletDescription(); 628 629 RegistrationContext regCtx = producer.getRegistrationContext(); 630 if (regCtx != null) 631 request.setRegistrationContext(regCtx); 632 633 request.setPortletContext(getPortlet().getPortletContext()); 634 635 UserContext userCtx = getUserContext(userID); 636 if (userCtx != null) 637 request.setUserContext(userCtx); 638 639 request.setDesiredLocales(desiredLocales); 640 641 PortletDescriptionResponse response = null; 642 643 try { 644 645 response = portletPort.getPortletDescription(request); 646 parameterChecker.check(response); 647 648 } catch (java.rmi.RemoteException wsrpFault) { 649 650 WSRPXHelper.handleWSRPFault(logger, wsrpFault); 651 } 652 653 return response; 654 } 655 656 663 public PortletPropertyDescriptionResponse getPortletPropertyDescription(String userID) 664 throws WSRPException { 665 GetPortletPropertyDescription request = new GetPortletPropertyDescription(); 666 request.setPortletContext(getPortlet().getPortletContext()); 667 668 RegistrationContext regCtx = producer.getRegistrationContext(); 669 if (regCtx != null) 670 request.setRegistrationContext(regCtx); 671 672 UserContext userCtx = getUserContext(userID); 673 if (userCtx != null) 674 request.setUserContext(userCtx); 675 676 request.setDesiredLocales(consumerEnv.getSupportedLocales()); 677 678 PortletPropertyDescriptionResponse response = null; 679 680 try { 681 682 response = portletPort.getPortletPropertyDescription(request); 683 parameterChecker.check(response); 684 685 } catch (java.rmi.RemoteException wsrpFault) { 686 687 WSRPXHelper.handleWSRPFault(logger, wsrpFault); 688 } 689 690 return response; 691 } 692 693 701 public PropertyList getPortletProperties(String [] names, String userID) 702 throws WSRPException { 703 GetPortletProperties request = new GetPortletProperties(); 704 request.setPortletContext(getPortlet().getPortletContext()); 705 request.setNames(names); 706 707 RegistrationContext regCtx = producer.getRegistrationContext(); 708 if (regCtx != null) 709 request.setRegistrationContext(regCtx); 710 711 UserContext userCtx = getUserContext(userID); 712 if (userCtx != null) 713 request.setUserContext(userCtx); 714 715 PropertyList response = null; 716 717 try { 718 719 response = portletPort.getPortletProperties(request); 720 parameterChecker.check(response, Constants.NILLABLE_FALSE); 721 722 } catch (java.rmi.RemoteException wsrpFault) { 723 724 WSRPXHelper.handleWSRPFault(logger, wsrpFault); 725 } 726 727 return response; 728 } 729 730 736 public PortletContext setPortletProperties(PropertyList properties, String userID) 737 throws WSRPException { 738 SetPortletProperties request = new SetPortletProperties(); 739 request.setPortletContext(getPortlet().getPortletContext()); 740 741 RegistrationContext regCtx = producer.getRegistrationContext(); 742 if (regCtx != null) 743 request.setRegistrationContext(regCtx); 744 745 UserContext userCtx = getUserContext(userID); 746 if (userCtx != null) 747 request.setUserContext(userCtx); 748 request.setPropertyList(properties); 749 750 PortletContext response = null; 751 752 try { 753 754 response = portletPort.setPortletProperties(request); 755 parameterChecker.check(response, Constants.NILLABLE_FALSE); 756 757 } catch (java.rmi.RemoteException wsrpFault) { 758 759 WSRPXHelper.handleWSRPFault(logger, wsrpFault); 760 } 761 762 return response; 763 } 764 765 768 protected Extension[] getMarkupParamsExtensions() { 769 return null; 771 } 772 } 773 | Popular Tags |