KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > wsrp > consumer > impl > PortletDriverImpl


1 /*
2 * Copyright 2001-2004 The eXo platform SARL All rights reserved.
3 * Please look at license.txt in info directory for more license detail.
4 */

5
6 package org.exoplatform.services.wsrp.consumer.impl;
7
8 import org.apache.commons.logging.Log;
9 import org.exoplatform.container.PortalContainer;
10 import org.exoplatform.services.log.LogService;
11 import org.exoplatform.services.wsrp.consumer.*;
12 import org.exoplatform.services.wsrp.exceptions.Faults;
13 import org.exoplatform.services.wsrp.exceptions.WSRPException;
14 import org.exoplatform.services.wsrp.intf.WSRP_v1_Markup_PortType;
15 import org.exoplatform.services.wsrp.intf.WSRP_v1_PortletManagement_PortType;
16 import org.exoplatform.services.wsrp.type.*;
17
18
19 /**
20  * The implementation of this class is based on the WSRP4J
21  * project
22  */

23 public class PortletDriverImpl implements PortletDriver {
24
25   private WSRPPortlet portlet = null;
26   private WSRP_v1_Markup_PortType markupPort = null;
27   private WSRP_v1_PortletManagement_PortType portletPort = null;
28   private ConsumerEnvironment consumerEnv = null;
29   private Producer producer = null;
30
31   private CookieProtocol initCookie = CookieProtocol.none;
32   private Log log;
33
34
35   public PortletDriverImpl(WSRPPortlet portlet) throws WSRPException {
36     this.consumerEnv = (ConsumerEnvironment) PortalContainer.getInstance().
37         getComponentInstanceOfType(ConsumerEnvironment.class);
38     this.log = ((LogService) PortalContainer.getInstance().getComponentInstanceOfType(LogService.class)).
39         getLog("org.exoplatform.services.wsrp.consumer");
40     this.portlet = portlet;
41     this.producer = consumerEnv.getProducerRegistry().
42         getProducer(portlet.getPortletKey().getProducerId());
43     portletPort = producer.getPortletManagementInterface();
44     ServiceDescription serviceDescription = producer.getServiceDescription(false);
45     if (serviceDescription != null) {
46       this.initCookie = serviceDescription.getRequiresInitCookie();
47       log.debug("Requires cookie initialization : " + initCookie.getValue());
48       if (initCookie == null) {
49         initCookie = CookieProtocol.none; // TODO - get from config
50
}
51     }
52   }
53
54   public WSRPPortlet getPortlet() {
55     return this.portlet;
56   }
57
58   private void resetInitCookie(UserSessionMgr userSession) throws WSRPException {
59     log.debug("reset cookies");
60     if (initCookie.getValue().equalsIgnoreCase(CookieProtocol._none)) {
61       userSession.setInitCookieDone(false);
62     } else if (initCookie.getValue().equalsIgnoreCase(CookieProtocol._perGroup)) {
63       PortletDescription portletDescription = null;
64       try {
65         portletDescription = producer.getPortletDescription(getPortlet().getParent());
66       } catch (WSRPException e) {
67         e.printStackTrace();
68       }
69       String JavaDoc groupID = null;
70       if (portletDescription != null) {
71         groupID = portletDescription.getGroupID();
72       }
73       if (groupID != null) {
74         GroupSessionMgr groupSession = (GroupSessionMgr) userSession.getGroupSession(groupID);
75         groupSession.setInitCookieDone(false);
76       }
77     }
78   }
79
80   private void checkInitCookie(UserSessionMgr userSession) throws WSRPException {
81     log.debug("init cookies : " + initCookie.getValue());
82     if (initCookie.getValue().equalsIgnoreCase(CookieProtocol._perUser)) {
83       log.debug("cookies management per user");
84       if (!userSession.isInitCookieDone()) {
85         log.debug("Init cookies : " + userSession);
86         this.markupPort = userSession.getWSRPBaseService();
87         userSession.setInitCookieRequired(true);
88         initCookie();
89         userSession.setInitCookieDone(true);
90       }
91     } else if (initCookie.getValue().equalsIgnoreCase(CookieProtocol._perGroup)) {
92       log.debug("cookies management per group");
93       PortletDescription portletDescription = producer.getPortletDescription(getPortlet().getParent());
94       String JavaDoc groupID = null;
95       if (portletDescription != null) {
96         groupID = portletDescription.getGroupID();
97         log.debug("Group Id used for cookies management : " + groupID);
98       }
99       if (groupID != null) {
100         GroupSessionMgr groupSession = (GroupSessionMgr) userSession.getGroupSession(groupID);
101         this.markupPort = groupSession.getWSRPBaseService();
102         if (!groupSession.isInitCookieDone()) {
103           log.debug("Group session in init cookies : " + groupSession);
104           groupSession.setInitCookieRequired(true);
105           initCookie();
106           groupSession.setInitCookieDone(true);
107         }
108       } else {
109         // means either we have no service description from the producer containg the portlet
110
// or the producer specified initCookieRequired perGroup but didn't provide
111
// a groupID in the portlet description
112
}
113     } else {
114       this.markupPort = userSession.getWSRPBaseService();
115     }
116   }
117
118   private MarkupParams getMarkupParams(WSRPBaseRequest request) {
119     MarkupParams markupParams = new MarkupParams();
120     ClientData clientData = new ClientData();
121     // lets just set this to the consumer agent for now
122
if (producer.getRegistrationData() != null)
123       clientData.setUserAgent(producer.getRegistrationData().getConsumerAgent());
124     markupParams.setClientData(clientData);
125     markupParams.setSecureClientCommunication(false);
126     markupParams.setLocales(consumerEnv.getSupportedLocales());
127     markupParams.setMimeTypes(consumerEnv.getMimeTypes());
128     markupParams.setMode(request.getMode());
129     markupParams.setWindowState(request.getWindowState());
130     markupParams.setNavigationalState(request.getNavigationalState());
131     markupParams.setMarkupCharacterSets(consumerEnv.getCharacterEncodingSet());
132     markupParams.setValidateTag(null); // TODO ValidateTag
133
// TODO: Set only modes and window states that are supported by the portlet as
134
// described in it's portlet description.
135
markupParams.setValidNewModes(consumerEnv.getSupportedModes());
136     markupParams.setValidNewWindowStates(consumerEnv.getSupportedWindowStates());
137     markupParams.setExtensions(null);
138     return markupParams;
139   }
140
141   private RuntimeContext getRuntimeContext(WSRPBaseRequest request, String JavaDoc path) {
142     RuntimeContext runtimeContext = new RuntimeContext();
143     runtimeContext.setUserAuthentication(consumerEnv.getUserAuthentication());
144     runtimeContext.setPortletInstanceKey(request.getPortletInstanceKey());
145     URLTemplateComposer templateComposer = consumerEnv.getTemplateComposer();
146
147     if (templateComposer != null) {
148       runtimeContext.setNamespacePrefix(templateComposer.getNamespacePrefix());
149     }
150     Boolean JavaDoc doesUrlTemplateProcess = null;
151     try {
152       PortletDescription desc = producer.getPortletDescription(getPortlet().getParent());
153       if (desc != null) {
154         doesUrlTemplateProcess = desc.getDoesUrlTemplateProcessing();
155       }
156     } catch (WSRPException e) {
157       // do nothing since exception has been logged already
158
// continue with assumption that portlet does not support template processing
159
}
160     if (doesUrlTemplateProcess != null && templateComposer != null
161         && doesUrlTemplateProcess.booleanValue()) {
162       Templates templates = new Templates();
163       templates.setBlockingActionTemplate(templateComposer.createBlockingActionTemplate(path));
164       templates.setRenderTemplate(templateComposer.createRenderTemplate(path));
165       templates.setDefaultTemplate(templateComposer.createDefaultTemplate(path));
166       templates.setResourceTemplate(templateComposer.createResourceTemplate(path));
167       templates.setSecureBlockingActionTemplate(templateComposer.createSecureBlockingActionTemplate(path));
168       templates.setSecureRenderTemplate(templateComposer.createSecureRenderTemplate(path));
169       templates.setSecureDefaultTemplate(templateComposer.createSecureDefaultTemplate(path));
170       templates.setSecureResourceTemplate(templateComposer.createSecureResourceTemplate(path));
171       runtimeContext.setTemplates(templates);
172     }
173     runtimeContext.setSessionID(request.getSessionID());
174     runtimeContext.setExtensions(null);
175     return runtimeContext;
176   }
177
178   private UserContext getUserContext(UserSessionMgr userSession) {
179     UserContext userContext = null;
180     if (userSession.getUserID() != null) {
181       User user = consumerEnv.getUserRegistry().getUser(userSession.getUserID());
182
183       if (user != null) {
184         userContext = user.getUserContext();
185       }
186     }
187     // workaround for Oracle bug, always send a userContext with dummy value
188
// if none was provided
189
if (userContext == null) {
190       userContext = new UserContext();
191       userContext.setUserContextKey("dummyUserContextKey");
192     }
193     return userContext;
194   }
195
196   private InteractionParams getInteractionParams(InteractionRequest actionRequest) {
197     InteractionParams interactionParams = new InteractionParams();
198     interactionParams.setPortletStateChange(consumerEnv.getPortletStateChange());
199     if (!portlet.isConsumerConfigured() &&
200         interactionParams.getPortletStateChange().toString().equalsIgnoreCase(StateChange._readWrite)) {
201       interactionParams.setPortletStateChange(StateChange.cloneBeforeWrite);
202     }
203     interactionParams.setInteractionState(actionRequest.getInteractionState());
204     interactionParams.setFormParameters(actionRequest.getFormParameters());
205     interactionParams.setUploadContexts(null);
206     interactionParams.setExtensions(null);
207     return interactionParams;
208   }
209
210   public MarkupResponse getMarkup(WSRPMarkupRequest markupRequest,
211                                   UserSessionMgr userSession,
212                                   String JavaDoc path)
213       throws WSRPException {
214     checkInitCookie(userSession);
215     MarkupResponse response = null;
216     try {
217       MarkupContext markupContext = null;
218       if ((markupContext = markupRequest.getCachedMarkup()) == null) {
219         log.debug("get non cached markup");
220         MarkupRequest request = new MarkupRequest();
221         request.setPortletContext(getPortlet().getPortletContext());
222         request.setMarkupParams(getMarkupParams(markupRequest));
223         request.setRuntimeContext(getRuntimeContext(markupRequest, path));
224         RegistrationContext regCtx = producer.getRegistrationContext();
225         if (regCtx != null) {
226           log.debug("Registration context used in getMarkup : " + regCtx.getRegistrationHandle());
227           request.setRegistrationContext(regCtx);
228         }
229         UserContext userCtx = getUserContext(userSession);
230         if (userCtx != null) {
231           request.setUserContext(getUserContext(userSession));
232         }
233         response = markupPort.getMarkup(request);
234       } else {
235         log.debug("get cached markup");
236         response = new MarkupResponse();
237         response.setMarkupContext(markupContext);
238       }
239       Boolean JavaDoc requiresRewriting = response.getMarkupContext().getRequiresUrlRewriting();
240       log.debug("requires URL rewriting : " + requiresRewriting);
241       requiresRewriting = requiresRewriting == null ? Boolean.FALSE : requiresRewriting;
242       if (requiresRewriting.booleanValue()) {
243         URLRewriter urlRewriter = consumerEnv.getURLRewriter();
244
245         String JavaDoc rewrittenMarkup = urlRewriter.rewriteURLs(path, response.getMarkupContext().getMarkupString());
246         if (rewrittenMarkup != null) {
247           response.getMarkupContext().setMarkupString(rewrittenMarkup);
248         }
249       }
250     } catch (InvalidCookieFault cookieFault) {
251       log.error("Problem with cookies ", cookieFault);
252       //throw new WSRPException(Faults.INVALID_COOKIE_FAULT, cookieFault);
253
resetInitCookie(userSession);
254       getMarkup(markupRequest, userSession, path);
255     } catch (java.rmi.RemoteException JavaDoc wsrpFault) {
256       log.error("Remote exception ", wsrpFault);
257       throw new WSRPException(Faults.OPERATION_FAILED_FAULT, wsrpFault);
258     }
259     return response;
260   }
261
262   public BlockingInteractionResponse performBlockingInteraction(InteractionRequest actionRequest,
263                                                                 UserSessionMgr userSession,
264                                                                 String JavaDoc path)
265       throws WSRPException {
266     checkInitCookie(userSession);
267     BlockingInteractionResponse response = null;
268     try {
269       BlockingInteractionRequest request = new BlockingInteractionRequest();
270       request.setPortletContext(getPortlet().getPortletContext());
271       request.setInteractionParams(getInteractionParams(actionRequest));
272       request.setMarkupParams(getMarkupParams(actionRequest));
273       request.setRuntimeContext(getRuntimeContext(actionRequest, path));
274       RegistrationContext regCtx = producer.getRegistrationContext();
275       if (regCtx != null) {
276         request.setRegistrationContext(regCtx);
277       }
278       UserContext userCtx = getUserContext(userSession);
279       if (userCtx != null) {
280         request.setUserContext(userCtx);
281       }
282       response = markupPort.performBlockingInteraction(request);
283     } catch (InvalidCookieFault cookieFault) {
284       resetInitCookie(userSession);
285       performBlockingInteraction(actionRequest, userSession, path);
286     } catch (java.rmi.RemoteException JavaDoc wsrpFault) {
287       throw new WSRPException();
288     }
289     return response;
290   }
291
292   public PortletContext clonePortlet(UserSessionMgr userSession) throws WSRPException {
293     ClonePortletRequest request = new ClonePortletRequest();
294     request.setPortletContext(getPortlet().getPortletContext());
295     RegistrationContext regCtx = producer.getRegistrationContext();
296     if (regCtx != null) {
297       request.setRegistrationContext(regCtx);
298     }
299     UserContext userCtx = getUserContext(userSession);
300     if (userCtx != null) {
301       request.setUserContext(userCtx);
302     }
303     PortletContext response = null;
304     try {
305       response = portletPort.clonePortlet(request);
306     } catch (java.rmi.RemoteException JavaDoc wsrpFault) {
307       throw new WSRPException();
308     }
309
310     return response;
311   }
312
313   public DestroyPortletsResponse destroyPortlets(String JavaDoc[] portletHandles,
314                                                  UserSessionMgr userSession)
315       throws WSRPException {
316     DestroyPortletsRequest request = new DestroyPortletsRequest();
317     RegistrationContext regCtx = producer.getRegistrationContext();
318     if (regCtx != null) {
319       request.setRegistrationContext(regCtx);
320     }
321     request.setPortletHandles(portletHandles);
322     DestroyPortletsResponse response = null;
323     try {
324       response = portletPort.destroyPortlets(request);
325     } catch (java.rmi.RemoteException JavaDoc wsrpFault) {
326       throw new WSRPException();
327     }
328     return response;
329   }
330
331   public ReturnAny releaseSessions(String JavaDoc[] sessionIDs,
332                                    UserSessionMgr userSession)
333       throws WSRPException {
334     checkInitCookie(userSession);
335     ReleaseSessionsRequest request = new ReleaseSessionsRequest();
336     RegistrationContext regCtx = producer.getRegistrationContext();
337     if (regCtx != null) {
338       request.setRegistrationContext(regCtx);
339     }
340     request.setSessionIDs(sessionIDs);
341     ReturnAny response = null;
342     try {
343       response = markupPort.releaseSessions(request);
344     } catch (java.rmi.RemoteException JavaDoc wsrpFault) {
345       throw new WSRPException();
346     }
347     return response;
348   }
349
350   public void initCookie() throws WSRPException {
351     InitCookieRequest request = new InitCookieRequest();
352     RegistrationContext regCtx = producer.getRegistrationContext();
353     if (regCtx != null) {
354       log.debug("Registration context use d in initCookie : " + regCtx.getRegistrationHandle());
355       request.setRegistrationContext(regCtx);
356     }
357     try {
358       log.debug("Call initCookie on Markup Port");
359       markupPort.initCookie(request);
360     } catch (java.rmi.RemoteException JavaDoc wsrpFault) {
361       log.error("Problem while initializing cookies", wsrpFault);
362       throw new WSRPException("Problem while initializing cookies", wsrpFault);
363     }
364   }
365
366   public PortletDescriptionResponse getPortletDescription(UserSessionMgr userSession,
367                                                           String JavaDoc[] desiredLocales)
368       throws WSRPException {
369     PortletDescriptionRequest request = new PortletDescriptionRequest();
370     RegistrationContext regCtx = producer.getRegistrationContext();
371     if (regCtx != null) {
372       request.setRegistrationContext(regCtx);
373     }
374     request.setPortletContext(getPortlet().getPortletContext());
375     UserContext userCtx = getUserContext(userSession);
376     if (userCtx != null) {
377       request.setUserContext(userCtx);
378     }
379     request.setDesiredLocales(desiredLocales);
380     PortletDescriptionResponse response = null;
381     try {
382       response = portletPort.getPortletDescription(request);
383     } catch (java.rmi.RemoteException JavaDoc wsrpFault) {
384       throw new WSRPException();
385     }
386     return response;
387   }
388
389
390   public PortletPropertyDescriptionResponse getPortletPropertyDescription(UserSessionMgr userSession)
391       throws WSRPException {
392     PortletPropertyDescriptionRequest request = new PortletPropertyDescriptionRequest();
393     request.setPortletContext(getPortlet().getPortletContext());
394     RegistrationContext regCtx = producer.getRegistrationContext();
395     if (regCtx != null) {
396       request.setRegistrationContext(regCtx);
397     }
398     UserContext userCtx = getUserContext(userSession);
399     if (userCtx != null) {
400       request.setUserContext(userCtx);
401     }
402     request.setDesiredLocales(consumerEnv.getSupportedLocales());
403     PortletPropertyDescriptionResponse response = null;
404     try {
405       response = portletPort.getPortletPropertyDescription(request);
406     } catch (java.rmi.RemoteException JavaDoc wsrpFault) {
407       throw new WSRPException();
408     }
409     return response;
410   }
411
412   public PropertyList getPortletProperties(String JavaDoc[] names,
413                                            UserSessionMgr userSession)
414       throws WSRPException {
415     GetPortletPropertiesRequest request = new GetPortletPropertiesRequest();
416     request.setPortletContext(getPortlet().getPortletContext());
417     request.setNames(names);
418     RegistrationContext regCtx = producer.getRegistrationContext();
419     if (regCtx != null) {
420       request.setRegistrationContext(regCtx);
421     }
422     UserContext userCtx = getUserContext(userSession);
423     if (userCtx != null) {
424       request.setUserContext(userCtx);
425     }
426     PropertyList response = null;
427     try {
428       response = portletPort.getPortletProperties(request);
429     } catch (java.rmi.RemoteException JavaDoc wsrpFault) {
430       throw new WSRPException();
431     }
432     return response;
433   }
434
435   public PortletContext setPortletProperties(PropertyList properties,
436                                              UserSessionMgr userSession)
437       throws WSRPException {
438     SetPortletPropertiesRequest request = new SetPortletPropertiesRequest();
439     request.setPortletContext(getPortlet().getPortletContext());
440     RegistrationContext regCtx = producer.getRegistrationContext();
441     if (regCtx != null) {
442       request.setRegistrationContext(regCtx);
443     }
444     UserContext userCtx = getUserContext(userSession);
445     if (userCtx != null) {
446       request.setUserContext(userCtx);
447     }
448     request.setPropertyList(properties);
449     PortletContext response = null;
450     try {
451       response = portletPort.setPortletProperties(request);
452     } catch (java.rmi.RemoteException JavaDoc wsrpFault) {
453       throw new WSRPException();
454     }
455     return response;
456   }
457 }
458
Popular Tags