-
Notifications
You must be signed in to change notification settings - Fork 477
/
NotificationController.java
426 lines (406 loc) · 26.8 KB
/
NotificationController.java
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/*
* Copyright 2014-2016 CyberVision, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.kaaproject.kaa.server.admin.controller;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.kaaproject.kaa.common.dto.EndpointNotificationDto;
import org.kaaproject.kaa.common.dto.NotificationDto;
import org.kaaproject.kaa.common.dto.NotificationSchemaDto;
import org.kaaproject.kaa.common.dto.TopicDto;
import org.kaaproject.kaa.common.dto.VersionDto;
import org.kaaproject.kaa.server.admin.shared.services.KaaAdminServiceException;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
@Api(value = "Notifications", description = "Provides function for manage notifications", basePath = "/kaaAdmin/rest")
@Controller
public class NotificationController extends AbstractAdminController {
/**
* Gets the notification schemas by application token.
*
* @param applicationToken the application token
* @return the list notification schema dto
* @throws KaaAdminServiceException the kaa admin service exception
*/
@ApiOperation(value = "Get notification schemas",
notes = "Returns notification schemas for an application. Only users with the TENANT_DEVELOPER or TENANT_USER role are allowed to request this " +
"information. The Tenant ID value of the application must match the Tenant ID of the request submitter.")
@ApiResponses(value = {
@ApiResponse(code = 400, message = "Invalid endpointGroupId supplied"),
@ApiResponse(code = 401, message = "The user is not authenticated or invalid credentials were provided"),
@ApiResponse(code = 403, message = "The authenticated user does not have the required role (TENANT_DEVELOPER or TENANT_USER) or the Tenant ID " +
"of the application does not match the Tenant ID of the authenticated user"),
@ApiResponse(code = 404, message = "An application with the specified applicationToken does not exist"),
@ApiResponse(code = 500, message = "An unexpected error occurred on the server side")})
@RequestMapping(value = "notificationSchemas/{applicationToken}", method = RequestMethod.GET)
@ResponseBody
public List<NotificationSchemaDto> getNotificationSchemasByApplicationToken(
@ApiParam(name = "applicationToken", value = "A unique auto-generated application identifier", required = true)
@PathVariable String applicationToken)
throws KaaAdminServiceException {
return notificationService.getNotificationSchemasByApplicationToken(applicationToken);
}
/**
* Gets the user notification schemas by application token.
*
* @param applicationToken the application token
* @return the list schema dto
* @throws KaaAdminServiceException the kaa admin service exception
*/
@ApiOperation(value = "Get user notification schemas",
notes = "Returns user notification schemas for an application. Only users with the TENANT_DEVELOPER or TENANT_USER role are allowed to request " +
"this information.")
@ApiResponses(value = {
@ApiResponse(code = 401, message = "The user is not authenticated or invalid credentials were provided"),
@ApiResponse(code = 403, message = "The authenticated user does not have the required role (TENANT_DEVELOPER or TENANT_USER) or the Tenant ID " +
"of the application does not match the Tenant ID of the authenticated user"),
@ApiResponse(code = 404, message = "An application with the specified applicationToken does not exist"),
@ApiResponse(code = 500, message = "An unexpected error occurred on the server side")})
@RequestMapping(value = "userNotificationSchemas/{applicationToken}", method = RequestMethod.GET)
@ResponseBody
public List<VersionDto> getUserNotificationSchemasByApplicationToken(
@ApiParam(name = "applicationToken", value = "A unique auto-generated application identifier", required = true)
@PathVariable String applicationToken) throws KaaAdminServiceException {
return notificationService.getUserNotificationSchemasByApplicationToken(applicationToken);
}
/**
* Gets the notification schema by her id.
*
* @param notificationSchemaId the notification schema id
* @return the notification schema dto
* @throws KaaAdminServiceException the kaa admin service exception
*/
@ApiOperation(value = "Get notification schema",
notes = "Returns a notification schema by notification schema ID. Only users with the TENANT_DEVELOPER or TENANT_USER role are allowed to " +
"request this information.")
@ApiResponses(value = {
@ApiResponse(code = 401, message = "The user is not authenticated or invalid credentials were provided"),
@ApiResponse(code = 403, message = "The authenticated user does not have the required role (TENANT_DEVELOPER or TENANT_USER) or the Tenant ID " +
"of the application does not match the Tenant ID of the authenticated user"),
@ApiResponse(code = 404, message = "A notification schema with the specified notificationSchemaId does not exist"),
@ApiResponse(code = 500, message = "An unexpected error occurred on the server side")})
@RequestMapping(value = "notificationSchema/{notificationSchemaId}", method = RequestMethod.GET)
@ResponseBody
public NotificationSchemaDto getNotificationSchema(
@ApiParam(name = "notificationSchemaId", value = "A unique notification schema identifier", required = true)
@PathVariable String notificationSchemaId) throws KaaAdminServiceException {
return notificationService.getNotificationSchema(notificationSchemaId);
}
/**
* Adds notification schema to the list of all notification schemas.
*
* @param notificationSchema the notification schema
* @return the notification schema dto
* @throws KaaAdminServiceException the kaa admin service exception
*/
@RequestMapping(value = "createNotificationSchema", method = RequestMethod.POST)
@ResponseBody
public NotificationSchemaDto createNotificationSchema(@RequestBody NotificationSchemaDto notificationSchema) throws KaaAdminServiceException {
return notificationService.saveNotificationSchema(notificationSchema);
}
/**
* Edits existing notification schema.
*
* @param notificationSchema the notification schema
* @return the notification schema dto
* @throws KaaAdminServiceException the kaa admin service exception
*/
@ApiOperation(value = "Create/Edit notification schema",
notes = "Creates or updates a notification schema. To create notification schema you do not need to specify the notification schema ID, " +
"createUsername field of the schema will be set to the name of the user who has uploaded it, a unique version number will be generated " +
"(incrementally) for this schema. To edit the notification schema specify the notification schema ID. If a notification schema with the " +
"specified ID exists, the configuration will be updated. Only users with the TENANT_DEVELOPER or TENANT_USER role are allowed to perform " +
"this operation.")
@ApiResponses(value = {
@ApiResponse(code = 400, message = "The specified notification schema is not a valid avro schema"),
@ApiResponse(code = 401, message = "The user is not authenticated or invalid credentials were provided"),
@ApiResponse(code = 403, message = "The authenticated user does not have the required role (TENANT_DEVELOPER or TENANT_USER) or the Tenant ID " +
"of the application does not match the Tenant ID of the authenticated user"),
@ApiResponse(code = 500, message = "An unexpected error occurred on the server side")})
@RequestMapping(value = "saveNotificationSchema", method = RequestMethod.POST)
@ResponseBody
public NotificationSchemaDto saveNotificationSchema(
@ApiParam(name = "notificationSchema", value = "NotificationSchemaDto body.", required = true)
@RequestBody NotificationSchemaDto notificationSchema)
throws KaaAdminServiceException {
return notificationService.saveNotificationSchema(notificationSchema);
}
/**
* Gets all topics by application token.
*
* @param applicationToken the application token
* @return the topic dto
* @throws KaaAdminServiceException the kaa admin service exception
*/
@ApiOperation(value = "Get topics",
notes = "Returns all topics for the specified application. Only users with the TENANT_DEVELOPER or TENANT_USER role are allowed to request this " +
"information.")
@ApiResponses(value = {
@ApiResponse(code = 401, message = "The user is not authenticated or invalid credentials were provided"),
@ApiResponse(code = 403, message = "The authenticated user does not have the required role (TENANT_DEVELOPER or TENANT_USER) or the Tenant ID " +
"of the application does not match the Tenant ID of the authenticated user"),
@ApiResponse(code = 404, message = "An application with the specified applicationToken does not exist"),
@ApiResponse(code = 500, message = "An unexpected error occurred on the server side")})
@RequestMapping(value = "topics/{applicationToken}", method = RequestMethod.GET)
@ResponseBody
public List<TopicDto> getTopicsByApplicationToken(
@ApiParam(name = "applicationToken", value = "A unique auto-generated application identifier", required = true)
@PathVariable String applicationToken) throws KaaAdminServiceException {
return notificationService.getTopicsByApplicationToken(applicationToken);
}
/**
* Gets all topics by endpoint group id.
*
* @param endpointGroupId the endpoint group id
* @return the topic dto
* @throws KaaAdminServiceException the kaa admin service exception
*/
@ApiOperation(value = "Get endpoint group topics",
notes = "Returns all topics for the specified endpoint group. Only users with the TENANT_DEVELOPER or TENANT_USER role are allowed to request " +
"this information.")
@ApiResponses(value = {
@ApiResponse(code = 401, message = "The user is not authenticated or invalid credentials were provided"),
@ApiResponse(code = 403, message = "The authenticated user does not have the required role (TENANT_DEVELOPER or TENANT_USER) or the Tenant ID " +
"of the application does not match the Tenant ID of the authenticated user"),
@ApiResponse(code = 404, message = "An endpoint group to with the specified endpointGroupId does not exist"),
@ApiResponse(code = 500, message = "An unexpected error occurred on the server side")})
@RequestMapping(value = "topics", method = RequestMethod.GET)
@ResponseBody
public List<TopicDto> getTopicsByEndpointGroupId(
@ApiParam(name = "endpointGroupId", value = "A unique endpoint group identifier", required = true)
@RequestParam(value = "endpointGroupId") String endpointGroupId)
throws KaaAdminServiceException {
return notificationService.getTopicsByEndpointGroupId(endpointGroupId);
}
/**
* Gets all vacant topics by endpoint group id.
*
* @param endpointGroupId the endpoint group id
* @return the topic dto
* @throws KaaAdminServiceException the kaa admin service exception
*/
@ApiOperation(value = "Get vacant topics",
notes = "Returns all vacant (not present in the endpoint group) topics for the specified endpoint group. Only users with the TENANT_DEVELOPER or " +
"TENANT_USER role are allowed to request this information.")
@ApiResponses(value = {
@ApiResponse(code = 401, message = "The user is not authenticated or invalid credentials were provided"),
@ApiResponse(code = 403, message = "The authenticated user does not have the required role (TENANT_DEVELOPER or TENANT_USER) or the Tenant ID " +
"of the application does not match the Tenant ID of the authenticated user"),
@ApiResponse(code = 404, message = "An endpoint group with the specified endpointGroupId does not exist"),
@ApiResponse(code = 500, message = "An unexpected error occurred on the server side")})
@RequestMapping(value = "vacantTopics/{endpointGroupId}", method = RequestMethod.GET)
@ResponseBody
public List<TopicDto> getVacantTopicsByEndpointGroupId(
@ApiParam(name = "endpointGroupId", value = "A unique endpoint group identifier", required = true)
@PathVariable String endpointGroupId) throws KaaAdminServiceException {
return notificationService.getVacantTopicsByEndpointGroupId(endpointGroupId);
}
/**
* Gets the topic by his id.
*
* @param topicId the topic id
* @return the topic dto
* @throws KaaAdminServiceException the kaa admin service exception
*/
@ApiOperation(value = "Get topic",
notes = "Returns a topic by topic ID. Only users with the TENANT_DEVELOPER or TENANT_USER role are allowed to request this information.")
@ApiResponses(value = {
@ApiResponse(code = 400, message = "Invalid topicId supplied"),
@ApiResponse(code = 401, message = "The user is not authenticated or invalid credentials were provided"),
@ApiResponse(code = 403, message = "The authenticated user does not have the required role (TENANT_DEVELOPER or TENANT_USER) or the Tenant ID " +
"of the application does not match the Tenant ID of the authenticated user"),
@ApiResponse(code = 404, message = "The topic with the specified topicId does not exist"),
@ApiResponse(code = 500, message = "An unexpected error occurred on the server side")})
@RequestMapping(value = "topic/{topicId}", method = RequestMethod.GET)
@ResponseBody
public TopicDto getTopic(
@ApiParam(name = "topicId", value = "A unique topic identifier", required = true)
@PathVariable String topicId) throws KaaAdminServiceException {
return notificationService.getTopic(topicId);
}
/**
* Edits topic to the list of all topics.
*
* @param topic the topic
* @return the topic dto
* @throws KaaAdminServiceException the kaa admin service exception
*/
@ApiOperation(value = "Create/Edit topic",
notes = "Creates or edits a topic. To create topic you do not need to specify the topic ID. To edit the topic specify the topic ID. If a topic " +
"with the specified ID exists, it will be updated. Only users with theTENANT_DEVELOPER or TENANT_USER role are allowed to perform " +
"this operation.")
@ApiResponses(value = {
@ApiResponse(code = 401, message = "The user is not authenticated or invalid credentials were provided"),
@ApiResponse(code = 403, message = "The authenticated user does not have the required role (TENANT_DEVELOPER or TENANT_USER) or the Tenant ID " +
"of the application does not match the Tenant ID of the authenticated user"),
@ApiResponse(code = 404, message = "A topic to be edited with the specified topicId does not exist"),
@ApiResponse(code = 500, message = "An unexpected error occurred on the server side")})
@RequestMapping(value = "topic", method = RequestMethod.POST)
@ResponseBody
public TopicDto editTopic(
@ApiParam(name = "topic", value = "TopicDto body. Mandatory fields: applicationId, name, type", required = true)
@RequestBody TopicDto topic) throws KaaAdminServiceException {
return notificationService.editTopic(topic);
}
/**
* Delete topic by his id.
*
* @param topicId the topic id
* @throws KaaAdminServiceException the kaa admin service exception
*/
@ApiOperation(value = "Delete topic",
notes = "Deletes a topic. Only users with the TENANT_DEVELOPER or TENANT_USER role are allowed to perform this operation.")
@ApiResponses(value = {
@ApiResponse(code = 400, message = "Invalid topicId supplied"),
@ApiResponse(code = 401, message = "The user is not authenticated or invalid credentials were provided"),
@ApiResponse(code = 403, message = "The authenticated user does not have the required role (TENANT_DEVELOPER or TENANT_USER) or the Tenant ID " +
"of the application does not match the Tenant ID of the authenticated user"),
@ApiResponse(code = 404, message = "The topic with the specified topicId does not exist"),
@ApiResponse(code = 500, message = "An unexpected error occurred on the server side")})
@RequestMapping(value = "delTopic", method = RequestMethod.POST)
@ResponseStatus(value = HttpStatus.OK)
public void deleteTopic(
@ApiParam(name = "topicId", value = "A unique topic identifier", required = true)
@RequestParam(value = "topicId") String topicId) throws KaaAdminServiceException {
notificationService.deleteTopic(topicId);
}
/**
* Adds the topic with specific id to endpoint group with specific id.
*
* @param endpointGroupId the endpoint group id
* @param topicId the topic id
* @throws KaaAdminServiceException the kaa admin service exception
*/
@ApiOperation(value = "Add topic to endpoint group",
notes = "Adds the specified topic to the specified endpoint group. Only users with the TENANT_DEVELOPER or TENANT_USER role are allowed to " +
"perform this operation.")
@ApiResponses(value = {
@ApiResponse(code = 400, message = "Invalid endpointGroupId supplied"),
@ApiResponse(code = 401, message = "The user is not authenticated or invalid credentials were provided"),
@ApiResponse(code = 403, message = "The authenticated user does not have the required role (TENANT_DEVELOPER or TENANT_USER) or the Tenant ID " +
"of the application does not match the Tenant ID of the authenticated user"),
@ApiResponse(code = 404, message = "A topic with the specified topicId or an endpoint group with the specified endpointGroupId does not exist"),
@ApiResponse(code = 500, message = "An unexpected error occurred on the server side")})
@RequestMapping(value = "addTopicToEpGroup", method = RequestMethod.POST)
@ResponseStatus(value = HttpStatus.OK)
public void addTopicToEndpointGroup(
@ApiParam(name = "endpointGroupId", value = "A unique endpoint group identifier", required = true)
@RequestParam(value = "endpointGroupId") String endpointGroupId,
@ApiParam(name = "topicId", value = "A unique topic identifier", required = true)
@RequestParam(value = "topicId") String topicId) throws KaaAdminServiceException {
notificationService.addTopicToEndpointGroup(endpointGroupId, topicId);
}
/**
* Removes the topic with specific id to endpoint group with specific id.
*
* @param endpointGroupId the endpoint group id
* @param topicId the topic id
* @throws KaaAdminServiceException the kaa admin service exception
*/
@ApiOperation(value = "Remove topic from endpoint group",
notes = "Removes the specified topic from the specified endpoint group. Only users with the TENANT_DEVELOPER or TENANT_USER role are allowed " +
"to perform this operation.")
@ApiResponses(value = {
@ApiResponse(code = 400, message = "Invalid endpointGroupId supplied"),
@ApiResponse(code = 401, message = "The user is not authenticated or invalid credentials were provided"),
@ApiResponse(code = 403, message = "The authenticated user does not have the required role (TENANT_DEVELOPER or TENANT_USER) or the Tenant ID " +
"of the application does not match the Tenant ID of the authenticated user"),
@ApiResponse(code = 404, message = "A topic with the specified topicId or an endpoint group with the specified endpointGroupId does not exist"),
@ApiResponse(code = 500, message = "An unexpected error occurred on the server side")})
@RequestMapping(value = "removeTopicFromEpGroup", method = RequestMethod.POST)
@ResponseStatus(value = HttpStatus.OK)
public void removeTopicFromEndpointGroup(
@ApiParam(name = "endpointGroupId", value = "A unique endpoint group identifier", required = true)
@RequestParam(value = "endpointGroupId") String endpointGroupId,
@ApiParam(name = "topicId", value = "A unique topic identifier", required = true)
@RequestParam(value = "topicId") String topicId) throws KaaAdminServiceException {
notificationService.removeTopicFromEndpointGroup(endpointGroupId, topicId);
}
/**
* Send notification, with information from specific file, to the client.
*
* @param notification the notification
* @param file the file
* @return the notification dto
* @throws KaaAdminServiceException the kaa admin service exception
*/
@ApiOperation(value = "Send notification",
notes = "Sends a notification with the notification body from the specified file. Only users with the TENANT_DEVELOPER or TENANT_USER role are " +
"allowed to perform this operation.")
@ApiResponses(value = {
@ApiResponse(code = 400, message = "The specified notification is not valid"),
@ApiResponse(code = 401, message = "The user is not authenticated or invalid credentials were provided"),
@ApiResponse(code = 403, message = "The authenticated user does not have the required role (TENANT_DEVELOPER or TENANT_USER) or the Tenant ID " +
"of the application does not match the Tenant ID of the authenticated user"),
@ApiResponse(code = 404, message = "A file with the notification body was not found in the form data or an application with the specified ID " +
"does not exist or a topic with the specified ID does not exist"),
@ApiResponse(code = 500, message = "An unexpected error occurred on the server side")})
@RequestMapping(value = "sendNotification", method = RequestMethod.POST, consumes = {"multipart/mixed", "multipart/form-data"})
@ResponseBody
public NotificationDto sendNotification(
@ApiParam(name = "notification", value = "NotificationDto body. Mandatory fields: applicationId, schemaId, version, topicId, type", required = true)
@RequestPart("notification") NotificationDto notification,
@ApiParam(name = "file", value = "Notification schema represented in json format", required = true)
@RequestPart("file") MultipartFile file) throws KaaAdminServiceException {
byte[] data = getFileContent(file);
return notificationService.sendNotification(notification, data);
}
/**
* Send unicast notification, with information from specific file, to the
* client identified by endpointKeyHash.
*
* @param notification the notification
* @param clientKeyHash the client key hash
* @param file the file
* @return the endpoint notification dto
* @throws KaaAdminServiceException the kaa admin service exception
*/
@ApiOperation(value = "Send unicast notification",
notes = "Sends a unicast notification with the notification body from the specified file to the client identified by endpointKeyHash. Only users " +
"with the TENANT_DEVELOPER or TENANT_USER role are allowed to perform this operation.")
@ApiResponses(value = {
@ApiResponse(code = 400, message = "The specified notification is not valid"),
@ApiResponse(code = 401, message = "The user is not authenticated or invalid credentials were provided"),
@ApiResponse(code = 403, message = "The authenticated user does not have the required role (TENANT_DEVELOPER or TENANT_USER) or the Tenant ID " +
"of the application does not match the Tenant ID of the authenticated user"),
@ApiResponse(code = 404, message = "A file with the notification body was not found in the form data or an application with the specified ID does " +
"not exist or a topic with the specified ID does not exist"),
@ApiResponse(code = 500, message = "An unexpected error occurred on the server side")})
@RequestMapping(value = "sendUnicastNotification", method = RequestMethod.POST, consumes = {"multipart/mixed", "multipart/form-data"})
@ResponseBody
public EndpointNotificationDto sendUnicastNotification(
@ApiParam(name = "notification", value = "NotificationDto body. Mandatory fields: applicationId, schemaId, version, topicId, type", required = true)
@RequestPart("notification") NotificationDto notification,
@ApiParam(name = "endpointKeyHash", value = "The key hash of the endpoint in Base64 URL safe format", required = true)
@RequestPart("endpointKeyHash") String clientKeyHash,
@ApiParam(name = "file", value = "Notification schema represented in json format", required = true)
@RequestPart("file") MultipartFile file) throws KaaAdminServiceException {
byte[] data = getFileContent(file);
return notificationService.sendUnicastNotification(notification, clientKeyHash, data);
}
}