1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
98
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
125
126
127
128 public static ReferenceList getPortletTitles( )
129 {
130 ReferenceList refList = new ReferenceList( );
131 HttpServletRequest request = LocalVariables.getRequest( );
132
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
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
199 PlutoServices tempPS = PlutoServices.getServices();
200 PortletRegistryService tempPRS = tempPS.getPortletRegistryService();
201
202 PortletDefinition tempPD = null;
203
204
205
206
207
208
209 PlutoSession plutoSession = PlutoSession.findSession( request );
210 PortletWindowImpl portletWindow = plutoSession.getPortletWindow( String.valueOf( nPortletID ), strPortletName );
211
212
213
214
215
216 PortalURLParser parser = PortalURLParserImpl.getParser();
217
218
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
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
280
281
282
283
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
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;
309
310
311
312
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
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
349
350
351
352
353
354 private static boolean requestAction( int nPortletID, String strPortletName )
355 {
356 init();
357 HttpServletRequest request = LocalVariables.getRequest( );
358
359 LuteceHttpServletResponse response = new LuteceHttpServletResponse( LocalVariables.getResponse( ) );
360
361 PortalRequestContext pReqCtx = new PortalRequestContext(_config.getServletContext(), request, response);
362
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
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
416
417
418
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
438
439
440
441
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
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 }