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.web;
35
36 import fr.paris.lutece.portal.service.admin.AdminUserService;
37 import fr.paris.lutece.portal.service.content.ContentPostProcessorService;
38 import fr.paris.lutece.portal.service.content.ContentService;
39 import fr.paris.lutece.portal.service.datastore.DatastoreService;
40 import fr.paris.lutece.portal.service.editor.EditorBbcodeService;
41 import fr.paris.lutece.portal.service.i18n.I18nService;
42 import fr.paris.lutece.portal.service.init.AppInfo;
43 import fr.paris.lutece.portal.service.init.AppInit;
44 import fr.paris.lutece.portal.service.mail.MailService;
45 import fr.paris.lutece.portal.service.message.ISiteMessageHandler;
46 import fr.paris.lutece.portal.service.message.SiteMessage;
47 import fr.paris.lutece.portal.service.message.SiteMessageException;
48 import fr.paris.lutece.portal.service.message.SiteMessageService;
49 import fr.paris.lutece.portal.service.portal.PortalService;
50 import fr.paris.lutece.portal.service.portal.ThemesService;
51 import fr.paris.lutece.portal.service.resource.IExtendableResource;
52 import fr.paris.lutece.portal.service.resource.IExtendableResourceService;
53 import fr.paris.lutece.portal.service.security.LuteceUser;
54 import fr.paris.lutece.portal.service.security.SecurityService;
55 import fr.paris.lutece.portal.service.security.UserNotSignedException;
56 import fr.paris.lutece.portal.service.spring.SpringContextService;
57 import fr.paris.lutece.portal.service.template.AppTemplateService;
58 import fr.paris.lutece.portal.service.util.AppException;
59 import fr.paris.lutece.portal.service.util.AppLogService;
60 import fr.paris.lutece.portal.service.util.AppPathService;
61 import fr.paris.lutece.portal.service.util.AppPropertiesService;
62 import fr.paris.lutece.portal.web.constants.Markers;
63 import fr.paris.lutece.portal.web.constants.Parameters;
64 import fr.paris.lutece.util.html.HtmlTemplate;
65 import fr.paris.lutece.util.url.UrlItem;
66
67 import org.apache.commons.lang3.StringUtils;
68 import org.apache.commons.lang3.BooleanUtils;
69
70 import java.io.UnsupportedEncodingException;
71
72 import java.net.URLEncoder;
73
74 import java.util.Enumeration;
75 import java.util.HashMap;
76 import java.util.List;
77 import java.util.Locale;
78 import java.util.Map;
79
80 import javax.servlet.http.HttpServletRequest;
81 import javax.servlet.http.HttpSession;
82
83
84
85
86 public class PortalJspBean
87 {
88 public static final int MODE_HTML = 0;
89 public static final int MODE_ADMIN = 1;
90 private static final String TEMPLATE_PAGE_ERROR404 = "skin/site/page_error404.html";
91 private static final String TEMPLATE_PAGE_ERROR500 = "skin/site/page_error500.html";
92 private static final String TEMPLATE_POPUP_CREDITS = "skin/site/popup_credits.html";
93 private static final String TEMPLATE_POPUP_LEGAL_INFO = "skin/site/popup_legal_info.html";
94 private static final String TEMPLATE_STARTUP_FAILURE = "skin/site/startup_failure.html";
95 private static final String TEMPLATE_SEND_RESOURCE = "skin/site/popup_send_resource.html";
96 private static final String TEMPLATE_EMAIL_SEND_RESOURCE = "skin/site/mail_send_resource.html";
97 private static final String PROPERTY_INFOS_CNIL = "lutece.legal.infos";
98 private static final String ATTRIBUTE_LOGIN_NEXT_URL = "luteceLoginNextUrl";
99 private static final String ATTRIBUTE_UPLOAD_FILTER_SITE_NEXT_URL = "uploadFilterSiteNextUrl";
100 private static final String MARK_PORTAL_DOMAIN = "portal_domain";
101 private static final String MARK_ADDRESS_INFOS_CNIL = "confidentiality_info";
102 private static final String MARK_APP_VERSION = "app_version";
103 private static final String MARK_FAILURE_MESSAGE = "failure_message";
104 private static final String MARK_FAILURE_DETAILS = "failure_details";
105 private static final String MARK_RESOURCE_URL = "resource_url";
106 private static final String MARK_RESOURCE = "resource";
107 private static final String MARK_ERROR = "error";
108 private static final String MARK_SUCCESS = "success";
109 private static final String MARK_ERROR_CAUSE = "error_cause";
110 private static final String MARK_PLUGIN_THEME = "plugin_theme";
111 private static final String MARK_THEME = "theme";
112 private static final String BEAN_SITE_MESSAGE_HANDLER = "siteMessageHandler";
113 private static final String PARAMETER_EXTENDABLE_RESOURCE_TYPE = "extendableResourceType";
114 private static final String PARAMETER_ID_EXTENDABLE_RESOURCE = "idExtendableResource";
115 private static final String PARAMETER_SENDER_NAME = "senderName";
116 private static final String PARAMETER_SENDER_FIRST_NAME = "senderFirstname";
117 private static final String PARAMETER_SENDER_EMAIL = "senderEmail";
118 private static final String PARAMETER_CONTENT = "content";
119 private static final String PARAMETER_SEND = "send";
120 private static final String MESSAGE_ERROR_WRONG_SENDER_EMAIL = "portal.site.error.wrongEmailFormat";
121 private static final String MESSAGE_ERROR_MANDATORY_FIELDS = "portal.util.message.mandatoryFields";
122 private static final String MESSAGE_NO_RESOURCE_FOUND = "portal.site.error.noResourceFound";
123 private static final String PROPERTY_PAGE_TITLE_ERROR404 = "portal.util.error404.title";
124 private static final String PROPERTY_PAGE_TITLE_CREDITS = "portal.site.popup_credits.pageTitle";
125 private static final String PROPERTY_PAGE_TITLE_LEGAL_INFO = "portal.site.popup_legal_info.pageTitle";
126 private static final String PROPERTY_PAGE_TITLE_STARTUP_FAILURE = "portal.util.startup.failure.title";
127 private static final String PROPERTY_PAGE_TITLE_ERROR500 = "portal.util.error500.title";
128 private static final String PROPERTY_DEBUG = "error.page.debug";
129 private static final String PROPERTY_DEBUG_DEFAULT = "true";
130 private static final String CONSTANT_SPACE = " ";
131 private static final String KEY_WEBMASTER_EMAIL = "portal.site.site_property.noreply_email";
132
133
134
135
136
137
138
139
140
141
142
143
144 public String getContent( HttpServletRequest request ) throws UserNotSignedException, SiteMessageException
145 {
146 return getContent( request, MODE_HTML );
147 }
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162 public String getContent( HttpServletRequest request, int nMode ) throws UserNotSignedException, SiteMessageException
163 {
164 if ( !AppInit.isWebappSuccessfullyLoaded( ) )
165 {
166 return getStartUpFailurePage( request );
167 }
168
169
170 if ( SecurityService.isAuthenticationEnable( ) )
171 {
172 try
173 {
174 if ( SecurityService.getInstance( ).isExternalAuthentication( ) && !SecurityService.getInstance( ).isMultiAuthenticationSupported( ) )
175 {
176 SecurityService.getInstance( ).getRemoteUser( request );
177 }
178 else
179 {
180 LuteceUser user = SecurityService.getInstance( ).getRegisteredUser( request );
181
182
183
184
185 if ( user == null && SecurityService.getInstance( ).isMultiAuthenticationSupported( ) )
186 {
187
188
189
190
191 SecurityService.getInstance( ).getRemoteUser( request );
192 }
193 }
194 }
195 catch( UserNotSignedException unse )
196 {
197
198
199 }
200 }
201
202
203 ContentService cs = PortalService.getInvokedContentService( request );
204
205 String strContent = ( cs != null ) ? cs.getPage( request, nMode ) : PortalService.getDefaultPage( request, nMode );
206
207 if ( ContentPostProcessorService.hasProcessor( ) )
208 {
209 strContent = ContentPostProcessorService.process( request, strContent );
210 }
211
212 return strContent;
213 }
214
215
216
217
218
219
220
221
222
223 public String getSiteMessageContent( HttpServletRequest request )
224 {
225 return getSiteMessageContent( request, MODE_HTML );
226 }
227
228
229
230
231
232
233
234
235
236
237
238 public String getSiteMessageContent( HttpServletRequest request, int nMode )
239 {
240 String strContent = null;
241
242 if ( !AppInit.isWebappSuccessfullyLoaded( ) )
243 {
244 return getStartUpFailurePage( request );
245 }
246
247 ISiteMessageHandler handler = SpringContextService.getBean( BEAN_SITE_MESSAGE_HANDLER );
248
249 if ( handler.hasMessage( request ) )
250 {
251 strContent = handler.getPage( request, nMode );
252 }
253
254 return strContent;
255 }
256
257
258
259
260
261
262
263
264 public String getStartUpFailurePage( HttpServletRequest request )
265 {
266 HashMap<String, Object> model = new HashMap<>( );
267 fillPageModel( request, model );
268 model.put( MARK_FAILURE_MESSAGE, AppInit.getLoadingFailureCause( ) );
269 model.put( MARK_FAILURE_DETAILS, AppInit.getLoadingFailureDetails( ) );
270 model.put( Markers.PAGE_TITLE, I18nService.getLocalizedString( PROPERTY_PAGE_TITLE_STARTUP_FAILURE, request.getLocale( ) ) );
271
272 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_STARTUP_FAILURE, request.getLocale( ), model );
273
274 return template.getHtml( );
275 }
276
277
278
279
280
281
282
283
284 public String getCredits( HttpServletRequest request )
285 {
286 HashMap<String, Object> model = new HashMap<>( );
287 fillPageModel( request, model );
288 model.put( MARK_APP_VERSION, AppInfo.getVersion( ) );
289 model.put( MARK_PORTAL_DOMAIN, PortalService.getSiteName( ) );
290 model.put( Markers.PAGE_TITLE, I18nService.getLocalizedString( PROPERTY_PAGE_TITLE_CREDITS, request.getLocale( ) ) );
291
292 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_POPUP_CREDITS, request.getLocale( ), model );
293
294 return template.getHtml( );
295 }
296
297
298
299
300
301
302
303
304 public String getLegalInfos( HttpServletRequest request )
305 {
306 HashMap<String, Object> model = new HashMap<>( );
307 fillPageModel( request, model );
308 model.put( MARK_ADDRESS_INFOS_CNIL, AppPropertiesService.getProperty( PROPERTY_INFOS_CNIL ) );
309 model.put( MARK_PORTAL_DOMAIN, PortalService.getSiteName( ) );
310 model.put( Markers.PAGE_TITLE, I18nService.getLocalizedString( PROPERTY_PAGE_TITLE_LEGAL_INFO, request.getLocale( ) ) );
311
312 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_POPUP_LEGAL_INFO, request.getLocale( ), model );
313
314 return template.getHtml( );
315 }
316
317
318
319
320
321
322
323
324 public String getError404Page( HttpServletRequest request )
325 {
326 HashMap<String, Object> model = new HashMap<>( );
327 fillPageModel( request, model );
328 model.put( Markers.PAGE_TITLE, I18nService.getLocalizedString( PROPERTY_PAGE_TITLE_ERROR404, request.getLocale( ) ) );
329
330 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_PAGE_ERROR404, request.getLocale( ), model );
331
332 return template.getHtml( );
333 }
334
335
336
337
338
339
340
341
342
343
344 public String getError500Page( HttpServletRequest request, Throwable exception )
345 {
346 if ( exception instanceof AppException )
347 {
348
349
350
351
352 AppLogService.error( "Error 500 : Caused by previous Critical AppException" );
353 }
354 else
355 {
356 AppLogService.error( "Error 500 : {}", exception.getMessage( ), exception );
357 }
358
359 String strCause = null;
360
361 if ( AppPropertiesService.getProperty( PROPERTY_DEBUG, PROPERTY_DEBUG_DEFAULT ).equalsIgnoreCase( "true" ) )
362 {
363 strCause = exception.getMessage( );
364
365 if ( exception.getCause( ) != null )
366 {
367 strCause += exception.getCause( ).getMessage( );
368 }
369 }
370
371 return getError500Page( request, strCause );
372 }
373
374
375
376
377
378
379
380
381
382
383 public String getError500Page( HttpServletRequest request, String strCause )
384 {
385 HashMap<String, Object> model = new HashMap<>( );
386 fillPageModel( request, model );
387 model.put( Markers.PAGE_TITLE, I18nService.getLocalizedString( PROPERTY_PAGE_TITLE_ERROR500, request.getLocale( ) ) );
388 model.put( MARK_ERROR_CAUSE, strCause );
389
390 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_PAGE_ERROR500, request.getLocale( ), model );
391
392 return template.getHtml( );
393 }
394
395
396
397
398
399
400
401
402
403 private static void fillPageModel( HttpServletRequest request, HashMap<String, Object> model )
404 {
405 model.put( Markers.BASE_URL, AppPathService.getBaseUrl( request ) );
406 model.put( MARK_PLUGIN_THEME, null );
407 model.put( MARK_THEME, ThemesService.getGlobalThemeObject( ) );
408 }
409
410
411
412
413
414
415
416
417
418 public static String redirectLogin( HttpServletRequest request )
419 {
420 String strNextUrl = request.getRequestURI( );
421 UrlIteml/UrlItem.html#UrlItem">UrlItem url = new UrlItem( strNextUrl );
422 Enumeration<String> enumParams = request.getParameterNames( );
423
424 while ( enumParams.hasMoreElements( ) )
425 {
426 String strParamName = enumParams.nextElement( );
427
428 try
429 {
430 url.addParameter( strParamName, URLEncoder.encode( request.getParameter( strParamName ), "UTF-8" ) );
431 }
432 catch( UnsupportedEncodingException ex )
433 {
434 AppLogService.error( "Redirection error while encoding URL : {}", ex.getMessage( ), ex );
435 }
436 }
437
438 HttpSession session = request.getSession( true );
439 session.setAttribute( ATTRIBUTE_LOGIN_NEXT_URL, url.getUrl( ) );
440
441 String strRedirect = SecurityService.getInstance( ).getLoginPageUrl( );
442
443 return AppPathService.getAbsoluteUrl( request, strRedirect );
444 }
445
446
447
448
449
450
451
452
453
454 public static String getLoginNextUrl( HttpServletRequest request )
455 {
456 HttpSession session = request.getSession( );
457 return (String) session.getAttribute( ATTRIBUTE_LOGIN_NEXT_URL );
458 }
459
460
461
462
463
464
465
466 public static void setUploadFilterSiteNextUrl( HttpServletRequest request )
467 {
468 String strNextUrl = request.getRequestURI( );
469 UrlIteml/UrlItem.html#UrlItem">UrlItem url = new UrlItem( strNextUrl );
470 Enumeration<String> enumParams = request.getParameterNames( );
471
472 while ( enumParams.hasMoreElements( ) )
473 {
474 String strParamName = enumParams.nextElement( );
475 url.addParameter( strParamName, request.getParameter( strParamName ) );
476 }
477
478 HttpSession session = request.getSession( true );
479 session.setAttribute( ATTRIBUTE_UPLOAD_FILTER_SITE_NEXT_URL, url.getUrl( ) );
480 }
481
482
483
484
485
486
487
488
489 public static String getUploadFilterSiteNextUrl( HttpServletRequest request )
490 {
491 HttpSession session = request.getSession( );
492 return (String) session.getAttribute( ATTRIBUTE_UPLOAD_FILTER_SITE_NEXT_URL );
493 }
494
495
496
497
498
499
500
501 public static void removeUploadFilterSiteNextUrl( HttpServletRequest request )
502 {
503 HttpSession session = request.getSession( );
504 session.removeAttribute( ATTRIBUTE_UPLOAD_FILTER_SITE_NEXT_URL );
505 }
506
507
508
509
510
511
512
513
514
515
516 public static String sendResource( HttpServletRequest request ) throws SiteMessageException
517 {
518 String strSenderEmail = DatastoreService.getDataValue( KEY_WEBMASTER_EMAIL, "no-reply@mydomain.com" );
519 String strSenderName = request.getParameter( PARAMETER_SENDER_NAME );
520 String strSenderFirstName = request.getParameter( PARAMETER_SENDER_FIRST_NAME );
521 String strReceipientEmail = request.getParameter( Parameters.EMAIL );
522 String strContent = request.getParameter( PARAMETER_CONTENT );
523 String strExtendableResourceType = request.getParameter( PARAMETER_EXTENDABLE_RESOURCE_TYPE );
524 String strIdExtendableResource = request.getParameter( PARAMETER_ID_EXTENDABLE_RESOURCE );
525 String strSend = request.getParameter( PARAMETER_SEND );
526 IExtendableResource resource = null;
527
528 String strError = checkSendParams( strSend, strSenderEmail, strSenderName, strSenderFirstName, strReceipientEmail, strContent, request.getLocale( ) );
529
530
531 IExtendableResourceService resourceService = null;
532 List<IExtendableResourceService> listExtendableResourceService = SpringContextService.getBeansOfType( IExtendableResourceService.class );
533
534 for ( IExtendableResourceService extendableResourceService : listExtendableResourceService )
535 {
536 if ( extendableResourceService.isInvoked( strExtendableResourceType ) )
537 {
538 resourceService = extendableResourceService;
539 resource = extendableResourceService.getResource( strIdExtendableResource, strExtendableResourceType );
540 }
541 }
542
543 if ( ( resourceService == null ) || ( resource == null ) )
544 {
545 SiteMessageService.setMessage( request, MESSAGE_NO_RESOURCE_FOUND, SiteMessage.TYPE_ERROR );
546 throw new SiteMessageException( );
547 }
548
549 String strResourceUrl = resourceService.getResourceUrl( strIdExtendableResource, strExtendableResourceType );
550 Map<String, Object> model = new HashMap<>( );
551 model.put( MARK_RESOURCE, resource );
552 model.put( MARK_RESOURCE_URL, strResourceUrl );
553 model.put( Markers.BASE_URL, AppPathService.getBaseUrl( request ) );
554
555 if ( ( strSend != null ) && ( strError == null ) )
556 {
557 Map<String, Object> mailModel = new HashMap<>( );
558 mailModel.put( Markers.BASE_URL, AppPathService.getBaseUrl( request ) );
559 mailModel.put( MARK_RESOURCE, resource );
560 mailModel.put( PARAMETER_SENDER_EMAIL, strSenderEmail );
561 mailModel.put( PARAMETER_SENDER_NAME, strSenderName );
562 mailModel.put( PARAMETER_SENDER_FIRST_NAME, strSenderFirstName );
563 mailModel.put( Parameters.EMAIL, strReceipientEmail );
564 mailModel.put( PARAMETER_CONTENT, EditorBbcodeService.getInstance( ).parse( strContent ) );
565 mailModel.put( MARK_RESOURCE_URL, resourceService.getResourceUrl( strIdExtendableResource, strExtendableResourceType ) );
566
567 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_EMAIL_SEND_RESOURCE, request.getLocale( ), mailModel );
568 MailService.sendMailHtml( strReceipientEmail, strSenderFirstName + CONSTANT_SPACE + strSenderName, strSenderEmail,
569 resource.getExtendableResourceName( ), template.getHtml( ) );
570 model.put( MARK_SUCCESS, MARK_SUCCESS );
571 }
572 else
573 {
574 model.put( PARAMETER_SENDER_NAME, strSenderName );
575 model.put( PARAMETER_SENDER_FIRST_NAME, strSenderFirstName );
576 model.put( Parameters.EMAIL, strReceipientEmail );
577 model.put( PARAMETER_CONTENT, strContent );
578 model.put( MARK_ERROR, strError );
579 }
580
581 model.put( Markers.PAGE_MAIN_MENU, StringUtils.EMPTY );
582
583 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_SEND_RESOURCE, request.getLocale( ), model );
584
585 return template.getHtml( );
586 }
587
588 private static String checkSendParams( String strSend, String strSenderEmail, String strSenderName, String strSenderFirstName, String strReceipientEmail,
589 String strContent, Locale locale )
590 {
591 String strError = null;
592
593 if ( strSend != null )
594 {
595 boolean [ ] conditions = new boolean [ ] {
596 StringUtils.isBlank( strSenderEmail ), StringUtils.isBlank( strSenderName ), StringUtils.isBlank( strSenderFirstName ),
597 StringUtils.isBlank( strReceipientEmail ), StringUtils.isBlank( strContent )
598 };
599
600 if ( BooleanUtils.or( conditions ) )
601 {
602 strError = I18nService.getLocalizedString( MESSAGE_ERROR_MANDATORY_FIELDS, locale );
603 }
604
605 if ( ( strError != null ) && ( !AdminUserService.checkEmail( strSenderEmail ) || !AdminUserService.checkEmail( strReceipientEmail ) ) )
606 {
607 strError = I18nService.getLocalizedString( MESSAGE_ERROR_WRONG_SENDER_EMAIL, locale );
608 }
609 }
610 return strError;
611 }
612 }