View Javadoc
1   /*
2    * Copyright (c) 2002-2017, Mairie de 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.plugins.mylutece.modules.notification.rs;
35  
36  import fr.paris.lutece.plugins.mylutece.modules.notification.service.NotificationPlugin;
37  import fr.paris.lutece.plugins.mylutece.modules.notification.service.NotificationService;
38  import fr.paris.lutece.plugins.rest.service.RestConstants;
39  import fr.paris.lutece.portal.service.template.AppTemplateService;
40  import fr.paris.lutece.portal.service.util.AppException;
41  import fr.paris.lutece.portal.service.util.AppLogService;
42  import fr.paris.lutece.portal.service.util.AppPathService;
43  import fr.paris.lutece.util.html.HtmlTemplate;
44  
45  import org.apache.commons.lang.StringUtils;
46  
47  import java.util.HashMap;
48  import java.util.Map;
49  
50  import javax.servlet.http.HttpServletRequest;
51  
52  import javax.ws.rs.Consumes;
53  import javax.ws.rs.FormParam;
54  import javax.ws.rs.GET;
55  import javax.ws.rs.POST;
56  import javax.ws.rs.Path;
57  import javax.ws.rs.Produces;
58  import javax.ws.rs.core.Context;
59  import javax.ws.rs.core.MediaType;
60  
61  
62  /**
63   *
64   * NotificationRest
65   *
66   */
67  @Path( RestConstants.BASE_PATH + NotificationPlugin.PLUGIN_NAME )
68  public class NotificationRest
69  {
70      // CONSTANTS
71      private static final String SLASH = "/";
72  
73      // PARAMETERS
74      private static final String PARAMETER_NOTIFICATION_OBJECT = "notification_object";
75      private static final String PARAMETER_NOTIFICATION_MESSAGE = "notification_message";
76      private static final String PARAMETER_NOTIFICATION_SENDER = "notification_sender";
77      private static final String PARAMETER_NOTIFICATION_RECEIVER = "notification_receiver";
78  
79      // MARKS
80      private static final String MARK_BASE_URL = "base_url";
81  
82      // TEMPLATES
83      private static final String TEMPLATE_WADL = "admin/plugins/mylutece/modules/notification/wadl.xml";
84  
85      // PATHS
86      private static final String PATH_WADL = "wadl";
87      private static final String PATH_NOTIFY = "notify";
88  
89      // MESSAGES
90      private static final String MESSAGE_NOTIFICATION_REST = "MyLutce Notification Rest - ";
91      private static final String MESSAGE_MANDATORY_FIELDS = "Every mandatory fields are not filled.";
92      private static final String MESSAGE_NOTIFICATION_SUCCESSFUL = "Notification successful.";
93  
94      /**
95       * Get the wadl.xml content
96       * @param request {@link HttpServletRequest}
97       * @return the content of wadl.xml
98       */
99      @GET
100     @Path( PATH_WADL )
101     @Produces( MediaType.APPLICATION_XML )
102     public String getWADL( @Context
103     HttpServletRequest request )
104     {
105         StringBuilder sbBase = new StringBuilder( AppPathService.getBaseUrl( request ) );
106 
107         if ( sbBase.toString(  ).endsWith( SLASH ) )
108         {
109             sbBase.deleteCharAt( sbBase.length(  ) - 1 );
110         }
111 
112         sbBase.append( RestConstants.BASE_PATH + NotificationPlugin.PLUGIN_NAME );
113 
114         Map<String, Object> model = new HashMap<String, Object>(  );
115         model.put( MARK_BASE_URL, sbBase.toString(  ) );
116 
117         HtmlTemplate t = AppTemplateService.getTemplate( TEMPLATE_WADL, request.getLocale(  ), model );
118 
119         return t.getHtml(  );
120     }
121 
122     /**
123      * Notify
124      * @param strNotificationObject the notification object
125      * @param strNotificationMessage the notification message
126      * @param strNotificationSender the sender
127      * @param strNotificationReceiver the receiver
128      * @return a message
129      */
130     @POST
131     @Path( PATH_NOTIFY )
132     @Produces( MediaType.TEXT_HTML )
133     @Consumes( MediaType.APPLICATION_FORM_URLENCODED )
134     public String doNotify( @FormParam( PARAMETER_NOTIFICATION_OBJECT )
135     String strNotificationObject, @FormParam( PARAMETER_NOTIFICATION_MESSAGE )
136     String strNotificationMessage, @FormParam( PARAMETER_NOTIFICATION_SENDER )
137     String strNotificationSender, @FormParam( PARAMETER_NOTIFICATION_RECEIVER )
138     String strNotificationReceiver )
139     {
140         String strMessage = StringUtils.EMPTY;
141 
142         if ( StringUtils.isNotBlank( strNotificationObject ) )
143         {
144             try
145             {
146                 if ( StringUtils.isNotBlank( strNotificationReceiver ) )
147                 {
148                     NotificationService.getService(  )
149                                        .notify( strNotificationSender, strNotificationReceiver, strNotificationObject,
150                         strNotificationMessage );
151                 }
152                 else
153                 {
154                     NotificationService.getService(  )
155                                        .notifyAll( strNotificationSender, strNotificationObject, strNotificationMessage );
156                 }
157 
158                 strMessage = MESSAGE_NOTIFICATION_REST + MESSAGE_NOTIFICATION_SUCCESSFUL;
159             }
160             catch ( AppException e )
161             {
162                 strMessage = e.getMessage(  );
163                 AppLogService.error( strMessage );
164             }
165         }
166         else
167         {
168             strMessage = MESSAGE_NOTIFICATION_REST + MESSAGE_MANDATORY_FIELDS;
169             AppLogService.error( strMessage );
170         }
171 
172         return strMessage;
173     }
174 }