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