View Javadoc
1   /*
2    * Copyright (c) 2002-2022, 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.i18n.I18nService;
37  import fr.paris.lutece.util.url.UrlItem;
38  
39  import java.util.Map;
40  
41  import javax.servlet.http.HttpServletRequest;
42  import javax.servlet.http.HttpSession;
43  
44  /**
45   * This class provides a service that build messages
46   */
47  public final class SiteMessageService
48  {
49      private static final String ATTRIBUTE_MESSAGE = "LUTECE_PORTAL_MESSAGE";
50      private static final String PROPERTY_TITLE_DEFAULT = "portal.util.message.titleDefault";
51      private static final String PROPERTY_TITLE_QUESTION = "portal.util.message.titleQuestion";
52      private static final String PROPERTY_TITLE_ERROR = "portal.util.message.titleError";
53      private static final String PROPERTY_TITLE_WARNING = "portal.util.message.titleWarning";
54      private static final String PROPERTY_TITLE_CONFIRMATION = "portal.util.message.titleConfirmation";
55      private static final String PROPERTY_TITLE_STOP = "portal.util.message.titleStop";
56      private static final String PARAMETER_SITE_MESSAGE = "sitemessage";
57      private static final String PARAMETER_SITE_MESSAGE_VALUE = "true";
58  
59      /**
60       * Private constructor
61       */
62      private SiteMessageService( )
63      {
64      }
65  
66      /**
67       * Set the INFO message, store it in session and throw a LuteceSiteMessageException
68       *
69       * @param request
70       *            The HttpRequest
71       * @param strMessageKey
72       *            The message key
73       * @throws SiteMessageException
74       *             occurs when a site message need to be displayed
75       */
76      public static void setMessage( HttpServletRequest request, String strMessageKey ) throws SiteMessageException
77      {
78          setMessage( request, strMessageKey, null, null, null, null, SiteMessage.TYPE_INFO );
79      }
80  
81      /**
82       * Set the message, store it in session and throw a LuteceSiteMessageException
83       *
84       * @param request
85       *            The HttpRequest
86       * @param strMessageKey
87       *            The message key
88       * @param nMessageType
89       *            The message type
90       * @throws SiteMessageException
91       *             occurs when a site message need to be displayed
92       */
93      public static void setMessage( HttpServletRequest request, String strMessageKey, int nMessageType ) throws SiteMessageException
94      {
95          setMessage( request, strMessageKey, null, null, null, null, nMessageType );
96      }
97  
98      /**
99       * Set the message, store it in session and throw a LuteceSiteMessageException
100      *
101      * @param request
102      *            The HttpRequest
103      * @param strMessageKey
104      *            The message key
105      * @param nMessageType
106      *            The message type
107      * @param strUrl
108      *            The url of the Ok button
109      * @throws SiteMessageException
110      *             occurs when a site message need to be displayed
111      */
112     public static void setMessage( HttpServletRequest request, String strMessageKey, int nMessageType, String strUrl ) throws SiteMessageException
113     {
114         setMessage( request, strMessageKey, null, null, strUrl, null, nMessageType );
115     }
116 
117     /**
118      * Set the message, store it in session and throw a LuteceSiteMessageException
119      *
120      * @param request
121      *            The HttpRequest
122      * @param strMessageKey
123      *            The message key
124      * @param nMessageType
125      *            The message type
126      * @param strUrl
127      *            The url of the Ok button
128      * @param requestParameters
129      *            The request parameters as a map
130      * @throws SiteMessageException
131      *             occurs when a site message need to be displayed
132      */
133     public static void setMessage( HttpServletRequest request, String strMessageKey, int nMessageType, String strUrl, Map<String, Object> requestParameters )
134             throws SiteMessageException
135     {
136         setMessage( request, strMessageKey, null, null, strUrl, null, nMessageType, requestParameters );
137     }
138 
139     /**
140      * Set the message, store it in session and throw a LuteceSiteMessageException
141      *
142      * @param request
143      *            The HttpRequest
144      * @param strMessageKey
145      *            The message key
146      * @param strTitleKey
147      *            The title key
148      * @param nMessageType
149      *            The message type
150      * @throws SiteMessageException
151      *             occurs when a site message need to be displayed
152      */
153     public static void setMessage( HttpServletRequest request, String strMessageKey, String strTitleKey, int nMessageType ) throws SiteMessageException
154     {
155         setMessage( request, strMessageKey, null, strTitleKey, null, null, nMessageType );
156     }
157 
158     /**
159      * Set the message, store it in session and throw a LuteceSiteMessageException
160      *
161      * @param request
162      *            The HttpRequest
163      * @param strMessageKey
164      *            The message key
165      * @param messageArgs
166      *            Message Arguments
167      * @param strTitleKey
168      *            The title key
169      * @param nMessageType
170      *            The message type
171      * @throws SiteMessageException
172      *             occurs when a site message need to be displayed
173      */
174     public static void setMessage( HttpServletRequest request, String strMessageKey, Object [ ] messageArgs, String strTitleKey, int nMessageType )
175             throws SiteMessageException
176     {
177         setMessage( request, strMessageKey, messageArgs, strTitleKey, null, null, nMessageType );
178     }
179 
180     /**
181      * Set the message, store it in session and throw a LuteceSiteMessageException
182      *
183      * @param request
184      *            The HttpRequest
185      * @param strMessageKey
186      *            The message key
187      * @param messageArgs
188      *            Message Arguments
189      * @param nMessageType
190      *            The message type
191      * @throws SiteMessageException
192      *             occurs when a site message need to be displayed
193      */
194     public static void setMessage( HttpServletRequest request, String strMessageKey, Object [ ] messageArgs, int nMessageType ) throws SiteMessageException
195     {
196         setMessage( request, strMessageKey, messageArgs, null, null, null, nMessageType );
197     }
198 
199     /**
200      * Set the message, store it in session and throw a LuteceSiteMessageException
201      *
202      * @param request
203      *            The HttpRequest
204      * @param strMessageKey
205      *            The message key
206      * @param messageArgs
207      *            Message arguments
208      * @param strTitleKey
209      *            The title key
210      * @param nMessageType
211      *            The message type
212      * @param strUrl
213      *            The URL
214      * @throws SiteMessageException
215      *             occurs when a site message need to be displayed
216      */
217     public static void setMessage( HttpServletRequest request, String strMessageKey, Object [ ] messageArgs, int nMessageType, String strUrl,
218             String strTitleKey ) throws SiteMessageException
219     {
220         setMessage( request, strMessageKey, messageArgs, strTitleKey, strUrl, null, nMessageType );
221     }
222 
223     /**
224      * Set the message, store it in session and throw a LuteceSiteMessageException
225      *
226      * @param request
227      *            The HttpRequest
228      * @param strMessageKey
229      *            The message key
230      * @param messageArgs
231      *            Message Arguments
232      * @param strUrl
233      *            The forward URL
234      * @param strTitleKey
235      *            The title key
236      * @param nMessageType
237      *            The message type
238      * @throws SiteMessageException
239      *             occurs when a site message need to be displayed
240      */
241     public static void setMessage( HttpServletRequest request, String strMessageKey, int nMessageType, String strUrl, String strTitleKey,
242             Object [ ] messageArgs ) throws SiteMessageException
243     {
244         setMessage( request, strMessageKey, messageArgs, strTitleKey, strUrl, null, nMessageType );
245     }
246 
247     /**
248      * Set the message, store it in session and throw a LuteceSiteMessageException
249      *
250      * @param request
251      *            The HttpRequest
252      * @param strMessageKey
253      *            The message key
254      * @param messageArgs
255      *            Message Arguments
256      * @param strTitleKey
257      *            The title key
258      * @param strUrl
259      *            The Url of the Ok button
260      * @param strTarget
261      *            The url target if not "_self"
262      * @param nMessageType
263      *            The message type
264      * @throws SiteMessageException
265      *             occurs when a site message need to be displayed
266      */
267     public static void setMessage( HttpServletRequest request, String strMessageKey, Object [ ] messageArgs, String strTitleKey, String strUrl,
268             String strTarget, int nMessageType ) throws SiteMessageException
269     {
270         setMessage( request, strMessageKey, messageArgs, strTitleKey, strUrl, strTarget, nMessageType, null );
271     }
272 
273     /**
274      * Set the message, store it in session and throw a LuteceSiteMessageException
275      *
276      * @param request
277      *            The HttpRequest
278      * @param strMessageKey
279      *            The message key
280      * @param messageArgs
281      *            Message Arguments
282      * @param strTitleKey
283      *            The title key
284      * @param strUrl
285      *            The Url of the Ok button
286      * @param strTarget
287      *            The url target if not "_self"
288      * @param nMessageType
289      *            The message type
290      * @param requestParameters
291      *            The request parameters
292      * @throws SiteMessageException
293      *             occurs when a site message need to be displayed
294      */
295     public static void setMessage( HttpServletRequest request, String strMessageKey, Object [ ] messageArgs, String strTitleKey, String strUrl,
296             String strTarget, int nMessageType, Map<String, Object> requestParameters ) throws SiteMessageException
297     {
298         setMessage( request, strMessageKey, messageArgs, strTitleKey, strUrl, strTarget, nMessageType, requestParameters, null );
299     }
300 
301     /**
302      * Set the message, store it in session and throw a LuteceSiteMessageException
303      *
304      * @param request
305      *            The HttpRequest
306      * @param strMessageKey
307      *            The message key
308      * @param messageArgs
309      *            Message Arguments
310      * @param strTitleKey
311      *            The title key
312      * @param strUrl
313      *            The Url of the Ok button
314      * @param strTarget
315      *            The url target if not "_self"
316      * @param nMessageType
317      *            The message type
318      * @param requestParameters
319      *            The request parameters
320      * @param strBackUrl
321      *            The Url of back button
322      * @throws SiteMessageException
323      *             occurs when a site message need to be displayed
324      */
325     public static void setMessage( HttpServletRequest request, String strMessageKey, Object [ ] messageArgs, String strTitleKey, String strUrl,
326             String strTarget, int nMessageType, Map<String, Object> requestParameters, String strBackUrl ) throws SiteMessageException
327     {
328         String strTitle = ( strTitleKey != null ) ? strTitleKey : getDefaultTitle( nMessageType );
329         SiteMessagece/message/SiteMessage.html#SiteMessage">SiteMessage message = new SiteMessage( strMessageKey, messageArgs, strTitle, strUrl, strTarget, nMessageType, getTypeButton( nMessageType, strUrl ),
330                 requestParameters, strBackUrl );
331         setMessage( request, message );
332 
333         throw new SiteMessageException( );
334     }
335 
336     /**
337      * Set the custom message, store it in session and throw a LuteceSiteMessageException
338      * 
339      * @param request
340      *            The HttpRequest
341      * @param title
342      *            The title
343      * @param strUrl
344      *            The Url of the Ok button
345      * @param text
346      *            The message
347      * @param nMessageType
348      *            The message type
349      * @param strBackUrl
350      *            The Url of back button
351      * @throws SiteMessageException
352      */
353     public static void setCustomMessage( HttpServletRequest request, String title, String text, String strUrl, int nMessageType, String strBackUrl )
354             throws SiteMessageException
355     {
356         String strTitle = title != null ? title : I18nService.getLocalizedString( getDefaultTitle( nMessageType ), request.getLocale( ) );
357         SiteMessage message = new CustomSiteMessage( strTitle, text, strUrl, nMessageType, getTypeButton( nMessageType, strBackUrl ), strBackUrl );
358 
359         setMessage( request, message );
360 
361         throw new SiteMessageException( );
362     }
363 
364     /**
365      * Set the custom message, store it in session and throw a LuteceSiteMessageException
366      * 
367      * @param request
368      *            The HttpRequest
369      * @param strText
370      *            The message
371      * @param nMessageType
372      *            The message type
373      * @throws SiteMessageException
374      */
375     public static void setCustomMessage( HttpServletRequest request, String strText, int nMessageType ) throws SiteMessageException
376     {
377         setCustomMessage( request, null, strText, null, nMessageType, null );
378     }
379 
380     /**
381      * Set the custom message, store it in session and throw a LuteceSiteMessageException
382      * 
383      * @param request
384      *            The HttpRequest
385      * @param strText
386      *            The message
387      * @param strTitle
388      *            The title
389      * @param nMessageType
390      *            The message type
391      * @throws SiteMessageException
392      */
393     public static void setCustomMessage( HttpServletRequest request, String strTitle, String strText, int nMessageType ) throws SiteMessageException
394     {
395         setCustomMessage( request, strTitle, strText, null, nMessageType, null );
396     }
397 
398     /**
399      * Returns the message associated to the current request
400      * 
401      * @param request
402      *            The HttpRequest
403      * @return The message associated to the current request
404      */
405     public static SiteMessage getMessage( HttpServletRequest request )
406     {
407         HttpSession session = request.getSession( true );
408         return (SiteMessage) session.getAttribute( ATTRIBUTE_MESSAGE );
409     }
410 
411     /**
412      * Store a message into the current session
413      * 
414      * @param request
415      *            The HTTP request
416      * @param message
417      *            The message to store
418      */
419     private static void setMessage( HttpServletRequest request, SiteMessage message )
420     {
421         HttpSession session = request.getSession( true );
422         session.setAttribute( ATTRIBUTE_MESSAGE, message );
423     }
424 
425     /**
426      * Delete the message in session
427      *
428      * @param request
429      *            The HTTP request
430      */
431     public static void cleanMessageSession( HttpServletRequest request )
432     {
433         HttpSession session = request.getSession( true );
434         session.removeAttribute( ATTRIBUTE_MESSAGE );
435     }
436 
437     /**
438      * Set the site message url with parameters if necessary
439      *
440      * @param strRequestUrl
441      *            The Request url
442      * @return The message url
443      */
444     public static String setSiteMessageUrl( String strRequestUrl )
445     {
446         UrlItem/UrlItem.html#UrlItem">UrlItem urlItem = new UrlItem( strRequestUrl );
447         urlItem.addParameter( PARAMETER_SITE_MESSAGE, PARAMETER_SITE_MESSAGE_VALUE );
448 
449         return urlItem.getUrl( );
450     }
451 
452     /**
453      * Returns a default title for the message box
454      * 
455      * @param nMessageType
456      *            The message type
457      * @return The default title
458      */
459     private static String getDefaultTitle( int nMessageType )
460     {
461         String strTitleKey;
462 
463         switch( nMessageType )
464         {
465             case SiteMessage.TYPE_QUESTION:
466                 strTitleKey = PROPERTY_TITLE_QUESTION;
467 
468                 break;
469 
470             case SiteMessage.TYPE_ERROR:
471                 strTitleKey = PROPERTY_TITLE_ERROR;
472 
473                 break;
474 
475             case SiteMessage.TYPE_WARNING:
476                 strTitleKey = PROPERTY_TITLE_WARNING;
477 
478                 break;
479 
480             case SiteMessage.TYPE_CONFIRMATION:
481                 strTitleKey = PROPERTY_TITLE_CONFIRMATION;
482 
483                 break;
484 
485             case SiteMessage.TYPE_STOP:
486                 strTitleKey = PROPERTY_TITLE_STOP;
487 
488                 break;
489 
490             default:
491                 strTitleKey = PROPERTY_TITLE_DEFAULT;
492 
493                 break;
494         }
495 
496         return strTitleKey;
497     }
498 
499     /**
500      * Returns if the cancel button should be displayed or not according the message type
501      * 
502      * @param nMessageType
503      *            The message type
504      * @param strUrl
505      *            The url of the Ok button
506      * @return the type of button
507      */
508     private static int getTypeButton( int nMessageType, String strUrl )
509     {
510         /*
511          * ------------------------------------- * Type url defined no url * TYPE_INFO none back * TYPE_QUESTION cancel back(?) * TYPE_ERROR none back *
512          * TYPE_WARNING none back * TYPE_CONFIRMATION cancel back * TYPE_STOP none back * -------------------------------------
513          */
514         int nTypeButton;
515 
516         if ( ( strUrl != null ) && !strUrl.equals( "" ) )
517         {
518             switch( nMessageType )
519             {
520                 case SiteMessage.TYPE_QUESTION:
521                 case SiteMessage.TYPE_CONFIRMATION:
522                     nTypeButton = SiteMessage.TYPE_BUTTON_CANCEL;
523 
524                     break;
525 
526                 default:
527                     nTypeButton = SiteMessage.TYPE_BUTTON_HIDDEN;
528 
529                     break;
530             }
531         }
532         else
533         {
534             nTypeButton = SiteMessage.TYPE_BUTTON_BACK;
535         }
536 
537         return nTypeButton;
538     }
539 }