View Javadoc
1   /*
2    * Copyright (c) 2002-2025, City of 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.portal.service.message;
35  
36  import fr.paris.lutece.portal.service.template.AppTemplateService;
37  import fr.paris.lutece.portal.service.util.AppPathService;
38  import fr.paris.lutece.util.ErrorMessage;
39  import fr.paris.lutece.util.html.HtmlTemplate;
40  
41  import java.util.HashMap;
42  import java.util.List;
43  import java.util.Locale;
44  import java.util.Map;
45  import java.util.Set;
46  
47  import javax.servlet.http.HttpServletRequest;
48  import javax.servlet.http.HttpSession;
49  
50  import javax.validation.ConstraintViolation;
51  
52  /**
53   * This class provides a service that build messages and deliver the Url to display them
54   */
55  public final class AdminMessageService
56  {
57      private static final String ATTRIBUTE_MESSAGE = "LUTECE_ADMIN_MESSAGE";
58      private static final String JSP_ADMIN_MESSAGE = "jsp/admin/AdminMessage.jsp";
59      private static final String JSP_BACK = "javascript:history.go(-1)";
60      private static final String TARGET_SELF = "_self";
61      private static final String PROPERTY_TITLE_DEFAULT = "portal.util.message.titleDefault";
62      private static final String PROPERTY_TITLE_QUESTION = "portal.util.message.titleQuestion";
63      private static final String PROPERTY_TITLE_ERROR = "portal.util.message.titleError";
64      private static final String PROPERTY_TITLE_WARNING = "portal.util.message.titleWarning";
65      private static final String PROPERTY_TITLE_CONFIRMATION = "portal.util.message.titleConfirmation";
66      private static final String PROPERTY_TITLE_STOP = "portal.util.message.titleStop";
67      private static final String TEMPLATE_FORMAT_LIST = "admin/util/message_list.html";
68      private static final String MARK_MESSAGES_LIST = "messages_list";
69      private static final String TEMPLATE_ERRORS_LIST = "admin/util/errors_list.html";
70      private static final String MARK_ERRORS_LIST = "errors_list";
71      private static final String LOGOUT_PARAMETER = "logout";
72  
73      /**
74       * Private constructor
75       */
76      private AdminMessageService( )
77      {
78      }
79  
80      /**
81       * Returns the Url that display the given message
82       * 
83       * @param request
84       *            The HttpRequest
85       * @param strMessageKey
86       *            The message key
87       * @return The Url of the JSP that display the message
88       */
89      public static String getMessageUrl( HttpServletRequest request, String strMessageKey )
90      {
91          return getMessageUrl( request, strMessageKey, null, null, JSP_BACK, TARGET_SELF, AdminMessage.TYPE_INFO );
92      }
93  
94      /**
95       * Returns the Url that display the given message
96       * 
97       * @param request
98       *            The HttpRequest
99       * @param strMessageKey
100      *            The message key
101      * @param nMessageType
102      *            The message type
103      * @return The Url of the JSP that display the message
104      */
105     public static String getMessageUrl( HttpServletRequest request, String strMessageKey, int nMessageType )
106     {
107         return getMessageUrl( request, strMessageKey, null, null, JSP_BACK, TARGET_SELF, nMessageType );
108     }
109 
110     /**
111      * Returns the Url that display the given message
112      * 
113      * @return The Url of the JSP that display the message
114      * @param messageArgs
115      *            Message arguments
116      * @param request
117      *            The HttpRequest
118      * @param strMessageKey
119      *            The message key
120      * @param nMessageType
121      *            The message type
122      */
123     public static String getMessageUrl( HttpServletRequest request, String strMessageKey, Object [ ] messageArgs, int nMessageType )
124     {
125         return getMessageUrl( request, strMessageKey, messageArgs, null, JSP_BACK, TARGET_SELF, nMessageType );
126     }
127 
128     /**
129      * Returns the Url that display the given message
130      * 
131      * @return The Url of the JSP that display the message
132      * @param strTarget
133      *            Target
134      * @param request
135      *            The HttpRequest
136      * @param strMessageKey
137      *            The message key
138      * @param strUrl
139      *            The Url of the OK button
140      */
141     public static String getMessageUrl( HttpServletRequest request, String strMessageKey, String strUrl, String strTarget )
142     {
143         return getMessageUrl( request, strMessageKey, null, null, strUrl, strTarget, AdminMessage.TYPE_INFO );
144     }
145 
146     /**
147      * Returns the Url that display the given message
148      * 
149      * @return The Url of the JSP that display the message
150      * @param strTarget
151      *            The url target if not "_self"
152      * @param request
153      *            The HttpRequest
154      * @param strMessageKey
155      *            The message key
156      * @param strUrl
157      *            The Url of the Ok button
158      * @param nMessageType
159      *            The message type
160      */
161     public static String getMessageUrl( HttpServletRequest request, String strMessageKey, String strUrl, String strTarget, int nMessageType )
162     {
163         return getMessageUrl( request, strMessageKey, null, null, strUrl, strTarget, nMessageType );
164     }
165 
166     /**
167      * Returns the Url that display the given message
168      * 
169      * @return The Url of the JSP that display the message
170      * @param request
171      *            The HttpRequest
172      * @param strMessageKey
173      *            The message key
174      * @param strUrl
175      *            The Url of the Ok button
176      * @param nMessageType
177      *            The message type
178      */
179     public static String getMessageUrl( HttpServletRequest request, String strMessageKey, String strUrl, int nMessageType )
180     {
181         return getMessageUrl( request, strMessageKey, null, null, strUrl, "", nMessageType );
182     }
183 
184     /**
185      * Returns the Url that display the given message
186      * 
187      * @return The Url of the JSP that display the message
188      * @param request
189      *            The HttpRequest
190      * @param strMessageKey
191      *            The message key
192      * @param strUrl
193      *            The Url of the Ok button
194      * @param nMessageType
195      *            The message type
196      * @param requestParameters
197      *            a collection of parameters
198      */
199     public static String getMessageUrl( HttpServletRequest request, String strMessageKey, String strUrl, int nMessageType, Map requestParameters )
200     {
201         return getMessageUrl( request, strMessageKey, null, null, strUrl, "", nMessageType, requestParameters );
202     }
203 
204     /**
205      * Returns the Url that display the given message
206      * 
207      * @return The Url of the JSP that display the message
208      * @param messageArgs
209      *            Message Arguments
210      * @param request
211      *            The HttpRequest
212      * @param strMessageKey
213      *            The message key
214      * @param strUrl
215      *            The Url of the Ok button
216      * @param nMessageType
217      *            The message type
218      */
219     public static String getMessageUrl( HttpServletRequest request, String strMessageKey, Object [ ] messageArgs, String strUrl, int nMessageType )
220     {
221         return getMessageUrl( request, strMessageKey, messageArgs, null, strUrl, "", nMessageType );
222     }
223 
224     /**
225      * Returns the Url that display the given message
226      * 
227      * @return The Url of the JSP that display the message
228      * @param messageArgs
229      *            Message Arguments
230      * @param strTarget
231      *            The url target if not "_self"
232      * @param strTitleKey
233      *            The title key
234      * @param nMessageType
235      *            The message type
236      * @param request
237      *            The HttpRequest
238      * @param strMessageKey
239      *            The message key
240      * @param strUrl
241      *            The Url of the Ok button
242      */
243     public static String getMessageUrl( HttpServletRequest request, String strMessageKey, Object [ ] messageArgs, String strTitleKey, String strUrl,
244             String strTarget, int nMessageType )
245     {
246         return getMessageUrl( request, strMessageKey, messageArgs, strTitleKey, strUrl, strTarget, nMessageType, null );
247     }
248 
249     /**
250      * Returns the Url that display the given message
251      * 
252      * @return The Url of the JSP that display the message
253      * @param messageArgs
254      *            Message Arguments
255      * @param strTarget
256      *            The url target if not "_self"
257      * @param strTitleKey
258      *            The title key
259      * @param nMessageType
260      *            The message type
261      * @param request
262      *            The HttpRequest
263      * @param strMessageKey
264      *            The message key
265      * @param strUrl
266      *            The Url of the Ok button
267      * @param requestParameters
268      *            a collection of parameters
269      */
270     public static String getMessageUrl( HttpServletRequest request, String strMessageKey, Object [ ] messageArgs, String strTitleKey, String strUrl,
271             String strTarget, int nMessageType, Map<String, Object> requestParameters )
272     {
273         return getMessageUrl( request, strMessageKey, messageArgs, strTitleKey, strUrl, strTarget, nMessageType, requestParameters, null );
274     }
275 
276     /**
277      * Returns the Url that display the given message
278      * 
279      * @param <T>
280      *            The type of the bean that has been validated
281      * @param request
282      *            The HTTP request
283      * @param strMessageKey
284      *            The message key
285      * @param constraintViolations
286      *            The set of violations
287      * @return The Url of the JSP that display the message
288      */
289     public static <T> String getMessageUrl( HttpServletRequest request, String strMessageKey, Set<ConstraintViolation<T>> constraintViolations )
290     {
291         return getMessageUrl( request, strMessageKey, formatConstraintViolations( request, constraintViolations ), null, JSP_BACK, TARGET_SELF,
292                 AdminMessage.TYPE_ERROR );
293     }
294 
295     /**
296      * Returns the Url that display the given message
297      * 
298      * @param request
299      *            The HTTP request
300      * @param strMessageKey
301      *            The message key
302      * @param errors
303      *            The set of violations
304      * @return The Url of the JSP that display the message
305      */
306     public static String getMessageUrl( HttpServletRequest request, String strMessageKey, List<? extends ErrorMessage> errors )
307     {
308         return getMessageUrl( request, strMessageKey, formatValidationErrors( request, errors ), null, JSP_BACK, TARGET_SELF, AdminMessage.TYPE_ERROR );
309     }
310 
311     /**
312      * Returns the Url that display the given message.
313      *
314      * @param request
315      *            The HttpRequest
316      * @param strMessageKey
317      *            The message key
318      * @param messageArgs
319      *            Message Arguments
320      * @param strTitleKey
321      *            The title key
322      * @param strUrl
323      *            The Url of the Ok button
324      * @param strTarget
325      *            The url target if not "_self"
326      * @param nMessageType
327      *            The message type
328      * @param requestParameters
329      *            a collection of parameters
330      * @param strBackUrl
331      *            the str back url
332      * @return The Url of the JSP that display the message
333      */
334     public static String getMessageUrl( HttpServletRequest request, String strMessageKey, Object [ ] messageArgs, String strTitleKey, String strUrl,
335             String strTarget, int nMessageType, Map<String, Object> requestParameters, String strBackUrl )
336     {
337         String strTitle = ( strTitleKey != null ) ? strTitleKey : getDefaultTitle( nMessageType );
338         boolean bCancelButton = getCancelButton( nMessageType );
339         AdminMessagee/message/AdminMessage.html#AdminMessage">AdminMessage message = new AdminMessage( strMessageKey, messageArgs, strTitle, strUrl, strTarget, nMessageType, bCancelButton, requestParameters,
340                 strBackUrl );
341         setMessage( request, message );
342 
343         return getUrl( request );
344     }
345 
346     /**
347      * Returns the message associated to the current request
348      * 
349      * @param request
350      *            The HttpRequest
351      * @return The message associated to the current request
352      */
353     public static AdminMessage getMessage( HttpServletRequest request )
354     {
355         HttpSession session = request.getSession( true );
356         return (AdminMessage) session.getAttribute( ATTRIBUTE_MESSAGE );
357     }
358 
359     /**
360      * Return relative url for the admin message jsp. This method does not generate admin message.
361      * 
362      * @return The relative url
363      */
364     public static String getMessageRelativeUrl( )
365     {
366         return JSP_ADMIN_MESSAGE;
367     }
368 
369     /**
370      * Store a message into the current session
371      * 
372      * @param request
373      *            The HTTP request
374      * @param message
375      *            The message to store
376      */
377     private static void setMessage( HttpServletRequest request, AdminMessage message )
378     {
379         HttpSession session = request.getSession( true );
380         session.setAttribute( ATTRIBUTE_MESSAGE, message );
381     }
382 
383     /**
384      * Build the message url
385      * 
386      * @param request
387      *            The HTTP request
388      * @return The Url
389      */
390     private static String getUrl( HttpServletRequest request )
391     {
392         return AppPathService.getBaseUrl( request ) + JSP_ADMIN_MESSAGE;
393     }
394 
395     /**
396      * Returns a default title for the message box
397      * 
398      * @param nMessageType
399      *            The message type
400      * @return The default title
401      */
402     private static String getDefaultTitle( int nMessageType )
403     {
404         String strTitleKey;
405 
406         switch( nMessageType )
407         {
408             case AdminMessage.TYPE_QUESTION:
409                 strTitleKey = PROPERTY_TITLE_QUESTION;
410 
411                 break;
412 
413             case AdminMessage.TYPE_ERROR:
414                 strTitleKey = PROPERTY_TITLE_ERROR;
415 
416                 break;
417 
418             case AdminMessage.TYPE_WARNING:
419                 strTitleKey = PROPERTY_TITLE_WARNING;
420 
421                 break;
422 
423             case AdminMessage.TYPE_CONFIRMATION:
424                 strTitleKey = PROPERTY_TITLE_CONFIRMATION;
425 
426                 break;
427 
428             case AdminMessage.TYPE_STOP:
429                 strTitleKey = PROPERTY_TITLE_STOP;
430 
431                 break;
432 
433             default:
434                 strTitleKey = PROPERTY_TITLE_DEFAULT;
435 
436                 break;
437         }
438 
439         return strTitleKey;
440     }
441 
442     /**
443      * Returns if the cancel button should be displayed or not according the message type
444      * 
445      * @param nMessageType
446      *            The message type
447      * @return True if the button should be displayed, otherwise false
448      */
449     private static boolean getCancelButton( int nMessageType )
450     {
451         boolean bCancel;
452 
453         switch( nMessageType )
454         {
455             case AdminMessage.TYPE_QUESTION:
456             case AdminMessage.TYPE_CONFIRMATION:
457                 bCancel = true;
458 
459                 break;
460 
461             default:
462                 bCancel = false;
463 
464                 break;
465         }
466 
467         return bCancel;
468     }
469 
470     /**
471      * Format a list of item to include in a message
472      * 
473      * @param list
474      *            The list of item as string
475      * @param locale
476      *            The current locale
477      * @return The formatted list
478      */
479     public static String getFormattedList( List<String> list, Locale locale )
480     {
481         Map<String, List<String>> model = new HashMap<>( );
482         model.put( MARK_MESSAGES_LIST, list );
483 
484         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_FORMAT_LIST, locale, model );
485 
486         return template.getHtml( );
487     }
488 
489     /**
490      * Format a set of constraints violations as en error list.
491      * 
492      * @param <T>
493      *            The type of the object
494      * @param request
495      *            The HTTP request
496      * @param constraintViolations
497      *            The set of violations
498      * @return The formatted errors list as an object array
499      */
500     private static <T> Object [ ] formatConstraintViolations( HttpServletRequest request, Set<ConstraintViolation<T>> constraintViolations )
501     {
502         Map<String, Object> model = new HashMap<>( );
503         model.put( MARK_ERRORS_LIST, constraintViolations );
504 
505         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_ERRORS_LIST, request.getLocale( ), model );
506         return new String [ ] {
507                 template.getHtml( )
508         };
509     }
510 
511     /**
512      * Format a set of constraints violations as en error list.
513      * 
514      * @param <T>
515      *            The type of the bean that has been validated
516      * @param request
517      *            The HTTP request
518      * @param errors
519      *            The set of violations
520      * @return The formatted errors list as an object array
521      */
522     private static Object [ ] formatValidationErrors( HttpServletRequest request, List<? extends ErrorMessage> errors )
523     {
524         Map<String, Object> model = new HashMap<>( );
525         model.put( MARK_ERRORS_LIST, errors );
526 
527         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_ERRORS_LIST, request.getLocale( ), model );
528         return new String [ ] {
529                 template.getHtml( )
530         };
531     }
532 
533     /**
534      * Build the logout message url
535      * 
536      * @param request
537      *            The HTTP request
538      * @return The Url
539      */
540     public static String getLogoutMessageUrl( HttpServletRequest request )
541     {
542         // Force session creation to prevent already commited response and session creation
543         HttpSession session = request.getSession( true );
544         return getUrl( request ) + "?" + LOGOUT_PARAMETER;
545     }
546 
547     /**
548      * Checks if the message url is the logout message url
549      * 
550      * @param request
551      *            The HTTP request
552      * @return True if the url is the logout message url
553      */
554     public static boolean isLogoutMessageUrl( HttpServletRequest request )
555     {
556         return null != request.getParameter( LOGOUT_PARAMETER );
557     }
558 }