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.portal.service.content;
35
36 import fr.paris.lutece.portal.service.init.LuteceInitException;
37 import fr.paris.lutece.portal.service.message.SiteMessage;
38 import fr.paris.lutece.portal.service.message.SiteMessageException;
39 import fr.paris.lutece.portal.service.message.SiteMessageService;
40 import fr.paris.lutece.portal.service.portal.PortalService;
41 import fr.paris.lutece.portal.service.security.LuteceUser;
42 import fr.paris.lutece.portal.service.security.SecurityService;
43 import fr.paris.lutece.portal.service.security.UserNotSignedException;
44 import fr.paris.lutece.portal.service.spring.SpringContextService;
45 import fr.paris.lutece.portal.service.template.AppTemplateService;
46 import fr.paris.lutece.portal.service.util.AppException;
47 import fr.paris.lutece.portal.service.util.AppLogService;
48 import fr.paris.lutece.portal.web.xpages.XPage;
49 import fr.paris.lutece.portal.web.xpages.XPageApplication;
50 import fr.paris.lutece.portal.web.xpages.XPageApplicationEntry;
51 import fr.paris.lutece.util.html.HtmlTemplate;
52
53 import org.springframework.beans.factory.NoSuchBeanDefinitionException;
54
55 import java.util.Collection;
56 import java.util.HashMap;
57 import java.util.List;
58 import java.util.Map;
59
60 import javax.servlet.http.HttpServletRequest;
61 import javax.servlet.http.HttpSession;
62
63
64
65
66
67
68
69
70
71
72
73 public class XPageAppService extends ContentService
74 {
75 public static final String PARAM_XPAGE_APP = "page";
76 private static final String CONTENT_SERVICE_NAME = "XPageAppService";
77 private static final String MESSAGE_ERROR_APP_BODY = "portal.util.message.errorXpageApp";
78 private static final String ATTRIBUTE_XPAGE = "LUTECE_XPAGE_";
79 private static Map<String, XPageApplicationEntry> _mapApplications = new HashMap<String, XPageApplicationEntry>( );
80
81
82
83
84
85
86 public static void registerXPageApplication( XPageApplicationEntry entry )
87 throws LuteceInitException
88 {
89 try
90 {
91 if ( entry.getClassName( ) == null )
92 {
93 String applicationBeanName = entry.getPluginName( ) + ".xpage." + entry.getId( );
94
95 if ( !SpringContextService.getContext( ).containsBean( applicationBeanName ) )
96 {
97 throw new LuteceInitException( "Error instantiating XPageApplication : " + entry.getId( ) +
98 " - Could not find bean named " + applicationBeanName,
99 new NoSuchBeanDefinitionException( applicationBeanName ) );
100 }
101 }
102 else
103 {
104
105 Class.forName( entry.getClassName( ) ).newInstance( );
106 }
107
108 _mapApplications.put( entry.getId( ), entry );
109 AppLogService.info( "New XPage application registered : " + entry.getId( )
110 + ( entry.isEnabled( ) ? "" : " (disabled)" ) );
111 }
112 catch ( ClassNotFoundException e )
113 {
114 throw new LuteceInitException( "Error instantiating XPageApplication : " + entry.getId( ) + " - " +
115 e.getCause( ), e );
116 }
117 catch ( InstantiationException e )
118 {
119 throw new LuteceInitException( "Error instantiating XPageApplication : " + entry.getId( ) + " - " +
120 e.getCause( ), e );
121 }
122 catch ( IllegalAccessException e )
123 {
124 throw new LuteceInitException( "Error instantiating XPageApplication : " + entry.getId( ) + " - " +
125 e.getCause( ), e );
126 }
127 }
128
129
130
131
132
133
134 @Override
135 public String getName( )
136 {
137 return CONTENT_SERVICE_NAME;
138 }
139
140
141
142
143
144
145
146 @Override
147 public boolean isInvoked( HttpServletRequest request )
148 {
149 String strXPage = request.getParameter( PARAM_XPAGE_APP );
150
151 if ( ( strXPage != null ) && ( strXPage.length( ) > 0 ) )
152 {
153 return true;
154 }
155
156 return false;
157 }
158
159
160
161
162
163
164 public void setCache( boolean bCache )
165 {
166 }
167
168
169
170
171
172
173 @Override
174 public boolean isCacheEnable( )
175 {
176 return false;
177 }
178
179
180
181
182 @Override
183 public void resetCache( )
184 {
185 }
186
187
188
189
190
191
192 @Override
193 public int getCacheSize( )
194 {
195 return 0;
196 }
197
198
199
200
201
202
203
204
205
206
207 @Override
208 public String getPage( HttpServletRequest request, int nMode )
209 throws UserNotSignedException, SiteMessageException
210 {
211
212 String strName = request.getParameter( PARAM_XPAGE_APP );
213
214 XPageApplicationEntry entry = getApplicationEntry( strName );
215
216
217 if ( ( entry != null ) && ( entry.isEnable( ) ) )
218 {
219 XPage page = null;
220 List<String> listRoles = entry.getRoles( );
221
222 if ( SecurityService.isAuthenticationEnable( ) && ( listRoles.size( ) > 0 ) )
223 {
224 LuteceUser user = SecurityService.getInstance( ).getRegisteredUser( request );
225
226 if ( user != null )
227 {
228 boolean bAutorized = false;
229
230 for ( String strRole : listRoles )
231 {
232 if ( SecurityService.getInstance( ).isUserInRole( request, strRole ) )
233 {
234 bAutorized = true;
235 }
236 }
237
238 if ( bAutorized )
239 {
240 XPageApplication application = getXPageSessionInstance( request, entry );
241 page = application.getPage( request, nMode, entry.getPlugin( ) );
242 }
243 else
244 {
245
246 String strAccessDeniedTemplate = SecurityService.getInstance( ).getAccessDeniedTemplate( );
247 HtmlTemplate tAccessDenied = AppTemplateService.getTemplate( strAccessDeniedTemplate );
248 page = new XPage( );
249 page.setContent( tAccessDenied.getHtml( ) );
250 }
251 }
252 else
253 {
254 throw new UserNotSignedException( );
255 }
256 }
257 else
258 {
259 XPageApplication application = getXPageSessionInstance( request, entry );
260 page = application.getPage( request, nMode, entry.getPlugin( ) );
261 }
262
263 if ( page.isStandalone( ) )
264 {
265 return page.getContent( );
266 }
267
268 PageData data = new PageData( );
269
270 data.setContent( page.getContent( ) );
271 data.setName( page.getTitle( ) );
272
273
274 String strXml = page.getXmlExtendedPathLabel( );
275
276 if ( strXml == null )
277 {
278 data.setPagePath( PortalService.getXPagePathContent( page.getPathLabel( ), 0, request ) );
279 }
280 else
281 {
282 data.setPagePath( PortalService.getXPagePathContent( page.getPathLabel( ), 0, strXml, request ) );
283 }
284
285 return PortalService.buildPageContent( data, nMode, request );
286 }
287 else
288 {
289 AppLogService.error( "The specified Xpage '" + strName +
290 "' cannot be retrieved. Check installation of your Xpage application." );
291 SiteMessageService.setMessage( request, MESSAGE_ERROR_APP_BODY, SiteMessage.TYPE_ERROR );
292
293 return null;
294 }
295 }
296
297
298
299
300
301
302 public static XPageApplicationEntry getApplicationEntry( String strName )
303 {
304 return _mapApplications.get( strName );
305 }
306
307
308
309
310
311 public static Collection<XPageApplicationEntry> getXPageApplicationsList( )
312 {
313 return _mapApplications.values( );
314 }
315
316
317
318
319
320
321
322 private static XPageApplication getXPageSessionInstance( HttpServletRequest request, XPageApplicationEntry entry )
323 {
324 HttpSession session = request.getSession( true );
325 String strAttribute = ATTRIBUTE_XPAGE + entry.getId( );
326 XPageApplication application = (XPageApplication) session.getAttribute( strAttribute );
327
328 if ( application == null )
329 {
330 application = getApplicationInstance( entry );
331 session.setAttribute( strAttribute, application );
332 AppLogService.debug( "New XPage instance of " + entry.getClassName( ) +
333 " created and attached to session " + session );
334 }
335
336 return application;
337 }
338
339
340
341
342
343
344 public static XPageApplication getApplicationInstance( XPageApplicationEntry entry )
345 {
346 XPageApplication application = null;
347
348 try
349 {
350 if ( entry.getClassName( ) == null )
351 {
352 application = SpringContextService.getBean( entry.getPluginName( ) + ".xpage." + entry.getId( ) );
353 }
354 else
355 {
356 application = (XPageApplication) Class.forName( entry.getClassName( ) ).newInstance( );
357 }
358 }
359 catch ( Exception e )
360 {
361 throw new AppException( "Error instantiating XPageApplication : " + entry.getId( ) + " - " +
362 e.getCause( ), e );
363 }
364
365 return application;
366 }
367 }