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.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
65
66
67 @Path( RestConstants.BASE_PATH + NotificationPlugin.PLUGIN_NAME )
68 public class NotificationRest
69 {
70
71 private static final String SLASH = "/";
72
73
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
80 private static final String MARK_BASE_URL = "base_url";
81
82
83 private static final String TEMPLATE_WADL = "admin/plugins/mylutece/modules/notification/wadl.xml";
84
85
86 private static final String PATH_WADL = "wadl";
87 private static final String PATH_NOTIFY = "notify";
88
89
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
96
97
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
124
125
126
127
128
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 }