View Javadoc
1   /*
2    * Copyright (c) 2002-2017, Mairie de Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  package fr.paris.lutece.plugins.jsr286.pluto;
35  
36  import java.io.IOException;
37  import java.util.Iterator;
38  import java.util.List;
39  
40  import javax.portlet.PortletException;
41  import javax.servlet.RequestDispatcher;
42  import javax.servlet.ServletConfig;
43  import javax.servlet.ServletContext;
44  import javax.servlet.http.HttpServletRequest;
45  import javax.servlet.http.HttpServletResponse;
46  
47  import org.apache.pluto.container.PortletContainer;
48  import org.apache.pluto.container.PortletContainerException;
49  import org.apache.pluto.container.PortletWindow;
50  import org.apache.pluto.container.driver.DriverPortletContext;
51  import org.apache.pluto.container.driver.PlutoServices;
52  import org.apache.pluto.container.driver.PortletContextService;
53  import org.apache.pluto.container.driver.PortletRegistryService;
54  import org.apache.pluto.container.driver.RequiredContainerServices;
55  import org.apache.pluto.container.impl.PortletContainerFactory;
56  import org.apache.pluto.container.om.portlet.PortletDefinition;
57  import org.apache.pluto.driver.config.impl.DriverConfigurationImpl;
58  import org.apache.pluto.driver.container.DefaultOptionalContainerServices;
59  import org.apache.pluto.driver.container.PortalDriverServicesImpl;
60  import org.apache.pluto.driver.core.PortalRequestContext;
61  import org.apache.pluto.driver.core.PortletWindowImpl;
62  
63  import org.apache.pluto.driver.services.impl.resource.ResourceConfig;
64  import org.apache.pluto.driver.services.impl.resource.StaticServletContextResourceConfigFactory;
65  import org.apache.pluto.driver.services.impl.resource.RenderConfigServiceImpl;
66  import org.apache.pluto.driver.services.impl.resource.SupportedModesServiceImpl;
67  import org.apache.pluto.driver.services.impl.resource.SupportedWindowStateServiceImpl;
68  import org.apache.pluto.driver.services.portal.PortletWindowConfig;
69  import org.apache.pluto.driver.url.PortalURL;
70  import org.apache.pluto.driver.url.PortalURLParser;
71  import org.apache.pluto.driver.url.impl.PortalURLParserImpl;
72  import org.apache.pluto.driver.container.PortletContextManager;
73  import org.apache.pluto.container.impl.RequestDispatcherServiceImpl;
74  import org.apache.pluto.container.impl.PortletAppDescriptorServiceImpl;
75  
76  
77  import fr.paris.lutece.plugins.jsr286.pluto.Buttons;
78  
79  import fr.paris.lutece.plugins.jsr286.pluto.LuteceHttpServletResponse;
80  import fr.paris.lutece.portal.web.LocalVariables;
81  import fr.paris.lutece.util.ReferenceItem;
82  import fr.paris.lutece.util.ReferenceList;
83  
84  public class LuteceToPlutoConnector 
85  {
86  	public static int TYPE_ACTION = 1;
87  	public static int TYPE_RESOURCE = 2;
88  	public static int TYPE_RENDER = 3;
89  	private static PortletContainer _portletContainer;
90  	private static ServletConfig _config;
91  	
92  	public static void init()
93  	{
94  		try {
95  			_config = LocalVariables.getConfig(  );
96  			RequiredContainerServicesImpl requiredServices = new RequiredContainerServicesImpl();		
97  			/*DefaultOptionalContainerServices dopc = new DefaultOptionalContainerServices();	
98  			dopc.getPortletContextService().register(_config);	*/		
99  			PortalDriverServicesImpl pdriverservice = new PortalDriverServicesImpl((RequiredContainerServices) requiredServices, null);
100 			
101 			if(PlutoServices.getServices() == null)
102 			{
103 				PlutoServices test = new PlutoServices(pdriverservice);
104 			}
105 			
106 			_portletContainer = PortletContainerFactory.getInstance().createContainer(
107 					"portletContainer", pdriverservice);
108 			
109 	        PortletContextService contextService = PlutoServices.getServices().getPortletContextService();
110 	        pdriverservice.setPortletContextService(contextService);
111 	        
112 	        try {
113 				String applicationName = contextService.register(_config);
114 			} catch (PortletContainerException e2) {				
115 				e2.printStackTrace();
116 			}
117 			_portletContainer.init();			
118 		} catch (Exception e) {
119 			e.printStackTrace();
120 		}
121 	}
122 	
123 	 /**
124      * Return titles list of defined portlets
125      *
126      * @return titles list of defined portlets
127      */
128     public static ReferenceList getPortletTitles(  )
129     {
130     	ReferenceList refList = new ReferenceList(  );
131     	HttpServletRequest request = LocalVariables.getRequest(  );
132     	//init();
133     	PortletContextService contextService = PlutoServices.getServices().getPortletContextService();
134     	Iterator<DriverPortletContext> it = contextService.getPortletContexts();
135         while(it.hasNext())
136         {
137         	DriverPortletContext context = it.next();
138         	String contextName = context.getApplicationName();
139 			List<? extends PortletDefinition> portletList = context.getPortletApplicationDefinition().getPortlets();
140 			Iterator<? extends PortletDefinition> portletIterator = portletList.iterator();
141 			while(portletIterator.hasNext())
142 			{
143 				PortletDefinition pDef = portletIterator.next();				
144 				ReferenceItem item = new ReferenceItem();
145 				item.setCode( contextName + "/" + pDef.getPortletName() );
146 				item.setName( contextName + "/" + pDef.getPortletName() );
147 				refList.add(item);
148 			}
149 			
150         }
151         return refList;
152     }
153     public static PortletWindowImpl initPortletWindow( int nPortletID, String strPortletName, String strContextName )
154     {
155     	init();
156     	HttpServletRequest request = LocalVariables.getRequest(  );		
157 		LuteceHttpServletResponse response = new LuteceHttpServletResponse( LocalVariables.getResponse(  ) );
158 		 
159 		PortalRequestContext pReqCtx = new PortalRequestContext(_config.getServletContext(), request, response);
160 		 
161 		String strPortletID = String.valueOf(nPortletID);
162 		PortletWindowConfig portletWindowConfig = new PortletWindowConfig();		 
163 		portletWindowConfig.setContextPath(
164 				"/" + strContextName);      
165 		portletWindowConfig.setMetaInfo(
166 		 		PortletWindowConfig.parseMetaInfo(strPortletID));
167 		portletWindowConfig.setPortletName(strPortletName);
168 		
169 		PortalURL portalUrl = PortalURLParserImpl.getParser().parse(request);
170  
171     	PortletWindowImpl portletWindow = new PortletWindowImpl(
172          		_portletContainer, portletWindowConfig, portalUrl, nPortletID);
173     	
174     	return portletWindow;
175     }
176     
177 	public static String render( int nPortletID, String strPortletName, PortletWindow portletWindow2 )
178     {
179 		init();				
180 		
181         HttpServletRequest request = LocalVariables.getRequest(  );        
182         LuteceHttpServletResponse response = new LuteceHttpServletResponse( LocalVariables.getResponse(  ) );
183         
184         PortalRequestContext pReqCtx = new PortalRequestContext(_config.getServletContext(), request, response);
185 		// Retrieve the portlet window 
186         
187         String strPortletID = String.valueOf(nPortletID);
188         PortletWindowConfig portletWindowConfig = new PortletWindowConfig();
189         
190         portletWindowConfig.setContextPath(
191         		request.getContextPath());       
192         portletWindowConfig.setMetaInfo(
193         		PortletWindowConfig.parseMetaInfo(strPortletID));
194         portletWindowConfig.setPortletName(strPortletName);
195         
196         PortalURL portalUrl = PortalURLParserImpl.getParser().parse(request);        
197         
198         //try {
199         	PlutoServices tempPS = PlutoServices.getServices();        	
200         	PortletRegistryService tempPRS = tempPS.getPortletRegistryService();        	
201         	
202         	PortletDefinition tempPD = null;//tempPRS.getPortlet(portletWindowConfig.getContextPath(), portletWindowConfig.getPortletName());
203         	
204 		/*} catch (PortletContainerException e1) {
205 			// TODO Auto-generated catch block
206 			e1.printStackTrace();
207 		}*/
208     	// Retrieve the portlet window
209         PlutoSession plutoSession = PlutoSession.findSession( request );
210         PortletWindowImpl portletWindow = plutoSession.getPortletWindow( String.valueOf( nPortletID ), strPortletName );
211         
212        
213         /*PortletWindowImpl portletWindow = new PortletWindowImpl(
214         		_portletContainer, portletWindowConfig, portalUrl, nPortletID);  */   
215         
216         PortalURLParser parser = PortalURLParserImpl.getParser();
217         
218         //PropertyConfigServiceImpl propertyService = new PropertyConfigServiceImpl();
219         ResourceConfig resourceConfig =  StaticServletContextResourceConfigFactory.getResourceConfig( );
220         RenderConfigServiceImpl renderConfig = new RenderConfigServiceImpl( resourceConfig );
221         RequestDispatcherServiceImpl requestDispatcherService = new RequestDispatcherServiceImpl(  );
222         PortletAppDescriptorServiceImpl portletAppDescriptorService = new PortletAppDescriptorServiceImpl(  );
223         PortletContextManager portletContextManager = new PortletContextManager(requestDispatcherService, portletAppDescriptorService);
224         SupportedModesServiceImpl supportedModeService = new SupportedModesServiceImpl(resourceConfig, portletContextManager, portletContextManager);
225         SupportedWindowStateServiceImpl supportedWindowService = new SupportedWindowStateServiceImpl(resourceConfig, portletContextManager);
226         
227         DriverConfigurationImpl driverConfig = new DriverConfigurationImpl(parser
228         		, resourceConfig
229         		, renderConfig
230         		,supportedModeService
231         		,supportedWindowService);
232         pReqCtx.getServletContext().setAttribute("driverConfig", driverConfig);
233      
234         
235         try {
236         	_portletContainer.doLoad( portletWindow, request, response );        	
237 			_portletContainer.doRender( portletWindow, request, response);				
238 			
239 			return response.getBufferString();
240 		} catch (PortletException e) {
241 			
242 			e.printStackTrace();
243 			return e.getMessage();
244 		} catch (IOException e) {
245 			
246 			e.printStackTrace();
247 			return e.getMessage();
248 		} catch (PortletContainerException e) {
249 			
250 			e.printStackTrace();
251 			return e.getMessage();
252 		} catch (Exception e) {
253 			e.printStackTrace();
254 			return e.getMessage();
255 		}
256     }
257 	
258 	public static boolean request( int nPortletId, String strPortletName, int type )
259 	{
260 	    // Restore main parameters from the ThreadLocal
261 	    HttpServletRequest request = LocalVariables.getRequest(  );	
262 	    
263 	    if(type == TYPE_ACTION)
264 	    {
265 	    	return LuteceToPlutoConnector.requestAction( nPortletId, strPortletName );
266 	    }  	 
267 	    else if(type == TYPE_RESOURCE)
268 	    {
269 	    	return LuteceToPlutoConnector.requestServeResource( nPortletId, strPortletName );
270 	    }
271 	    else if(type == TYPE_RENDER)
272 	    {
273 	    	return LuteceToPlutoConnector.requestRender( nPortletId, strPortletName );
274 	    }
275 	    return false;
276 	}
277 	
278 	 /**
279      * Serve Resource
280      *
281      * @param nPortletID Lutece portlet ID
282      * @param strPortletName JSR 168 portlet name (ID)
283      * @return <code>true</code> for a normal processing, <code>false</code> for the need to send a redirect to the client.
284      */
285     private static boolean requestServeResource( int nPortletID, String strPortletName )
286     {	
287     	init();
288     	HttpServletRequest request = LocalVariables.getRequest(  );
289     	HttpServletResponse response = LocalVariables.getResponse(  );
290 
291 
292     	PortalRequestContext pReqCtx = new PortalRequestContext(_config.getServletContext(), request, response);
293     	// Retrieve the portlet window 
294 
295     	String strPortletID = String.valueOf(nPortletID);
296     	PortletWindowConfig portletWindowConfig = new PortletWindowConfig();
297 
298     	portletWindowConfig.setContextPath(
299     			request.getContextPath());       
300     	portletWindowConfig.setMetaInfo(
301     			PortletWindowConfig.parseMetaInfo(strPortletID));
302     	portletWindowConfig.setPortletName(strPortletName);         
303     	PortalURL portalUrl = PortalURLParserImpl.getParser().parse(request); 
304 
305     	PlutoServices tempPS = PlutoServices.getServices();     	
306     	PortletRegistryService tempPRS = tempPS.getPortletRegistryService();        	
307 
308     	PortletDefinition tempPD = null;//tempPRS.getPortlet(portletWindowConfig.getContextPath(), portletWindowConfig.getPortletName());
309 
310 		/*} catch (PortletContainerException e1) {
311 			// TODO Auto-generated catch block
312 			e1.printStackTrace();
313 		}*/
314      	 PlutoSession plutoSession = PlutoSession.findSession( request );
315          PortletWindowImpl portletWindow = plutoSession.getPortletWindow( String.valueOf( nPortletID ), strPortletName );      	
316 	     
317 
318          ServletContext servletContext = _config.getServletContext();
319          PortalURLParser parser = PortalURLParserImpl.getParser();
320          //PropertyConfigServiceImpl propertyService = new PropertyConfigServiceImpl();
321          ResourceConfig resourceConfig =  StaticServletContextResourceConfigFactory.getResourceConfig( );
322          RenderConfigServiceImpl renderConfig = new RenderConfigServiceImpl( resourceConfig );
323          RequestDispatcherServiceImpl requestDispatcherService = new RequestDispatcherServiceImpl(  );
324          PortletAppDescriptorServiceImpl portletAppDescriptorService = new PortletAppDescriptorServiceImpl(  );
325          PortletContextManager portletContextManager = new PortletContextManager(requestDispatcherService, portletAppDescriptorService);
326          SupportedModesServiceImpl supportedModeService = new SupportedModesServiceImpl(resourceConfig, portletContextManager, portletContextManager);
327          SupportedWindowStateServiceImpl supportedWindowService = new SupportedWindowStateServiceImpl(resourceConfig, portletContextManager);
328         
329 	     DriverConfigurationImpl driverConfig = new DriverConfigurationImpl(parser
330 	     		, resourceConfig
331 	     		, renderConfig
332 	     		,supportedModeService
333 	     		,supportedWindowService);	     
334 	     pReqCtx.getServletContext().setAttribute("driverConfig", driverConfig);
335         try
336         {        	
337             _portletContainer.doServeResource( portletWindow, request, response );
338         }
339         catch ( Throwable e )
340         {
341            e.printStackTrace();
342            return false;
343         }
344 
345         return true;
346     }
347     /**
348      * Realise an action
349      *
350      * @param nPortletID Lutece portlet ID
351      * @param strPortletName JSR 286 portlet name (ID)
352      * @return <code>true</code> for a normal processing, <code>false</code> for the need to send a redirect to the client.
353      */
354     private static boolean requestAction( int nPortletID, String strPortletName )
355     {	
356     	init();
357     	 HttpServletRequest request = LocalVariables.getRequest(  );
358          //HttpServletResponse response = LocalVariables.getResponse(  );
359          LuteceHttpServletResponse response = new LuteceHttpServletResponse( LocalVariables.getResponse(  ) );
360          
361          PortalRequestContext pReqCtx = new PortalRequestContext(_config.getServletContext(), request, response);
362  		// Retrieve the portlet window 
363          
364          String strPortletID = String.valueOf(nPortletID);
365          PortletWindowConfig portletWindowConfig = new PortletWindowConfig();           
366          portletWindowConfig.setContextPath(
367          		request.getContextPath());       
368          portletWindowConfig.setMetaInfo(
369          		PortletWindowConfig.parseMetaInfo(strPortletID));
370          portletWindowConfig.setPortletName(strPortletName);         
371          PortalURL portalUrl = PortalURLParserImpl.getParser().parse(request);      
372 
373       
374          PlutoServices tempPS = PlutoServices.getServices();
375 
376          PortletRegistryService tempPRS = tempPS.getPortletRegistryService();        	
377 
378          PortletDefinition tempPD = null;
379 
380          PlutoSession plutoSession = PlutoSession.findSession( request );
381          PortletWindowImpl portletWindow = plutoSession.getPortletWindow( String.valueOf( nPortletID ), strPortletName );      	
382 
383 
384          ServletContext servletContext = _config.getServletContext();
385          PortalURLParser parser = PortalURLParserImpl.getParser();
386          //PropertyConfigServiceImpl propertyService = new PropertyConfigServiceImpl();
387          ResourceConfig resourceConfig =  StaticServletContextResourceConfigFactory.getResourceConfig( );
388          RenderConfigServiceImpl renderConfig = new RenderConfigServiceImpl( resourceConfig );
389          RequestDispatcherServiceImpl requestDispatcherService = new RequestDispatcherServiceImpl(  );
390          PortletAppDescriptorServiceImpl portletAppDescriptorService = new PortletAppDescriptorServiceImpl(  );
391          PortletContextManager portletContextManager = new PortletContextManager(requestDispatcherService, portletAppDescriptorService);
392          SupportedModesServiceImpl supportedModeService = new SupportedModesServiceImpl(resourceConfig, portletContextManager, portletContextManager);
393          SupportedWindowStateServiceImpl supportedWindowService = new SupportedWindowStateServiceImpl(resourceConfig, portletContextManager);
394 	     
395 	     DriverConfigurationImpl driverConfig = new DriverConfigurationImpl(parser
396 	     		, resourceConfig
397 	     		, renderConfig
398 	     		,supportedModeService
399 	     		,supportedWindowService);	     
400 	     pReqCtx.getServletContext().setAttribute("driverConfig", driverConfig);
401         try
402         {
403         	_portletContainer.doLoad( portletWindow, request, response );
404             _portletContainer.doAction( portletWindow, request, response );
405         }
406         catch ( Throwable e )
407         {
408            e.printStackTrace();
409            return false;
410         }
411 
412         return true;
413     }
414     /**
415      * Realise a render action (change mode and state)
416      *
417      * @param nPortletID Lutece portlet ID
418      * @return <code>true</code> for a normal processing, <code>false</code> for the need to send a redirect to the client.
419      */
420     private static boolean requestRender( int nPortletID, String strPortletName )
421     {
422     	HttpServletRequest request = LocalVariables.getRequest(  );
423     	
424     	PlutoSession plutoSession = PlutoSession.findSession( request );
425         PortletWindowImpl portletWindow = plutoSession.getPortletWindow( String.valueOf( nPortletID ), strPortletName );
426         
427         String strMode = request.getParameter("pm");
428     	String strWindowState = request.getParameter("ws");
429     	portletWindow.setPortletMode(strMode);
430     	portletWindow.setWindowState(strWindowState);        
431         
432        
433     	return requestAction(nPortletID, strPortletName);
434     }
435     
436     /**
437      * Return list of buttons that must be displayed on the portlet titlebar.
438      *
439      * @param nPortletID Lutece portlet ID
440      * @param strPortletName JSR 286 portlet name (ID)
441      * @return Return button defined for this
442      */
443     public static Buttons getButtons( int nPortletID, String strPortletName )
444     {        
445         final Buttons buttons = new Buttons(  ); 
446         
447         HttpServletRequest request = LocalVariables.getRequest(  );        
448         LuteceHttpServletResponse response = new LuteceHttpServletResponse( LocalVariables.getResponse(  ) );
449         
450 		// Retrieve the portlet window   
451 		PlutoSession plutoSession = PlutoSession.findSession( request );
452 		PortletWindowImpl portletWindow = plutoSession.getPortletWindow( String.valueOf( nPortletID ), strPortletName );
453 		
454         buttons.init( request, response, portletWindow.getPortletDefinition(  ), portletWindow );
455         return buttons;
456     }
457 }