Project

General

Profile

1
package eu.dnetlib.openaire.usermanagement.api;
2

    
3
import com.google.gson.JsonArray;
4
import com.google.gson.JsonElement;
5
import com.google.gson.JsonObject;
6
import com.google.gson.JsonParser;
7
import eu.dnetlib.openaire.user.pojos.RoleVerification;
8
import eu.dnetlib.openaire.user.utils.EmailSender;
9
import eu.dnetlib.openaire.usermanagement.dto.Role;
10
import eu.dnetlib.openaire.usermanagement.utils.AuthorizationService;
11
import eu.dnetlib.openaire.usermanagement.utils.JsonUtils;
12
import eu.dnetlib.openaire.usermanagement.utils.RoleManagement;
13
import eu.dnetlib.openaire.usermanagement.utils.VerificationUtils;
14
import org.apache.log4j.Logger;
15
import org.springframework.beans.factory.annotation.Autowired;
16
import org.springframework.http.HttpStatus;
17
import org.springframework.security.access.prepost.PreAuthorize;
18
import org.springframework.stereotype.Component;
19
import org.springframework.web.bind.annotation.*;
20
import org.springframework.web.client.HttpClientErrorException;
21

    
22
import javax.mail.MessagingException;
23
import javax.servlet.http.HttpServletRequest;
24
import javax.ws.rs.*;
25
import javax.ws.rs.core.Context;
26
import javax.ws.rs.core.MediaType;
27
import javax.ws.rs.core.Response;
28

    
29
@Component(value = "RegistryService")
30
@CrossOrigin("*")
31
@Path("/registry")
32
public class RegistryService {
33

    
34
    private static final Logger logger = Logger.getLogger(RegistryService.class);
35

    
36
    @Autowired
37
    private RoleManagement calls;
38

    
39
    @Autowired
40
    private JsonUtils jsonUtils;
41

    
42
    @Autowired
43
    private VerificationUtils verificationUtils;
44

    
45
    @Autowired
46
    private EmailSender emailSender;
47

    
48
    @Autowired
49
    private AuthorizationService authorizationService;
50

    
51
    /**
52
     * Subscribe to a type(Community, etc.) with id(ee, egi, etc.)
53
     */
54
    @Path("/subscribe/{type}/{id}")
55
    @POST
56
    @Produces(MediaType.APPLICATION_JSON)
57
    @PreAuthorize("isAuthenticated() and @AuthorizationService.isCommunity(#type)")
58
    public Response subscribe(@PathParam("type") String type, @PathParam("id") String id, @Context final HttpServletRequest request) {
59
        try {
60
            JsonElement response = calls.assignMemberRole(type, id, request);
61
            return Response.status(HttpStatus.OK.value()).entity(response.toString()).type(MediaType.APPLICATION_JSON).build();
62
        } catch (HttpClientErrorException e) {
63
            String message = new JsonParser().parse(e.getResponseBodyAsString()).getAsJsonObject().get("message").getAsString();
64
            return Response.status(e.getStatusCode().value()).entity(jsonUtils.createResponse(message).toString()).type(MediaType.APPLICATION_JSON).build();
65
        }
66
    }
67

    
68
    /**
69
     * Unsubscribe from type(Community, etc.) with id(ee, egi, etc.).
70
     * If user has manager role for this entity, it will be removed too.
71
     */
72
    @Path("/unsubscribe/{type}/{id}")
73
    @POST
74
    @Produces(MediaType.APPLICATION_JSON)
75
    @PreAuthorize("isAuthenticated() and @AuthorizationService.isCommunity(#type)")
76
    public Response unsubscribe(@PathParam("type") String type, @PathParam("id") String id, @Context final HttpServletRequest request) {
77
        try {
78
            JsonElement response = calls.removeMemberRole(type, id, request);
79
            return Response.status(HttpStatus.OK.value()).entity(response.toString()).type(MediaType.APPLICATION_JSON).build();
80
        } catch (HttpClientErrorException e) {
81
            String message = new JsonParser().parse(e.getResponseBodyAsString()).getAsJsonObject().get("message").getAsString();
82
            return Response.status(e.getStatusCode().value()).entity(jsonUtils.createResponse(message).toString()).type(MediaType.APPLICATION_JSON).build();
83
        }
84
    }
85

    
86
    /**
87
     * Create a new curator role with the given type(Community, etc.).
88
     **/
89
    @Path("/create/{type}")
90
    @POST
91
    @Produces(MediaType.APPLICATION_JSON)
92
    @PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN)")
93
    public Response createCuratorRole(@PathParam("type") String type) {
94
        try {
95
            JsonElement response = calls.createCuratorRole(type);
96
            return Response.status(HttpStatus.CREATED.value()).entity(response.toString()).type(MediaType.APPLICATION_JSON).build();
97
        } catch (HttpClientErrorException e) {
98
            String message = new JsonParser().parse(e.getResponseBodyAsString()).getAsJsonObject().get("message").getAsString();
99
            return Response.status(e.getStatusCode().value()).entity(jsonUtils.createResponse(message).toString()).type(MediaType.APPLICATION_JSON).build();
100
        }
101
    }
102

    
103
    /**
104
     * Create a new role with the given type(Community, etc.) with id(ee, egi, etc.).
105
     **/
106
    @Path("/create/{type}/{id}")
107
    @POST
108
    @Produces(MediaType.APPLICATION_JSON)
109
    @PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN)")
110
    public Response createMemberRole(@PathParam("type") String type, @PathParam("id") String id) {
111
        try {
112
            JsonElement response = calls.createMemberRole(type, id);
113
            return Response.status(HttpStatus.CREATED.value()).entity(response.toString()).type(MediaType.APPLICATION_JSON).build();
114
        } catch (HttpClientErrorException e) {
115
            String message = new JsonParser().parse(e.getResponseBodyAsString()).getAsJsonObject().get("message").getAsString();
116
            return Response.status(e.getStatusCode().value()).entity(jsonUtils.createResponse(message).toString()).type(MediaType.APPLICATION_JSON).build();
117
        }
118
    }
119

    
120
    /**
121
     * @deprecated
122
     *
123
     * Create a new role with the given name and description.
124
     **/
125
    @Path("/createRole")
126
    @POST
127
    @Produces(MediaType.APPLICATION_JSON)
128
    @Consumes(MediaType.APPLICATION_JSON)
129
    @PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN)")
130
    public Response createRole(@RequestBody Role role) {
131
        try {
132
            JsonElement response = calls.createRole(role.getName(), role.getDescription());
133
            return Response.status(HttpStatus.CREATED.value()).entity(response.toString()).type(MediaType.APPLICATION_JSON).build();
134
        } catch (HttpClientErrorException e) {
135
            String message = new JsonParser().parse(e.getResponseBodyAsString()).getAsJsonObject().get("message").getAsString();
136
            return Response.status(e.getStatusCode().value()).entity(jsonUtils.createResponse(message).toString()).type(MediaType.APPLICATION_JSON).build();
137
        }
138
    }
139

    
140
    /**
141
     * Invite user with email to manage a type(Community, etc.) with id(ee, egi, etc.)
142
     * Auto generated link and code will be sent as response.
143
     */
144
    @Path("/invite/{type}/{id}/manager")
145
    @POST
146
    @Produces(MediaType.APPLICATION_JSON)
147
    @PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN, " +
148
            "@AuthorizationService.curator(#type), @AuthorizationService.manager(#type, #id))")
149
    public Response inviteManager(@PathParam("type") String type, @PathParam("id") String id, @RequestBody String body) {
150
        try {
151
            JsonObject details = new JsonParser().parse(body).getAsJsonObject();
152
            JsonObject email = details.get("email").getAsJsonObject();
153
            String recipient = email.get("recipient").getAsString();
154
            if (!calls.isManager(type, id, recipient)) {
155
                JsonObject invitation = verificationUtils.createManagerInvitation(recipient, type, id);
156
                return sendEmail(details, email, invitation);
157
            } else {
158
                return Response.status(HttpStatus.CONFLICT.value()).entity(jsonUtils.createResponse("User has been already manager of this " + type).toString()).type(MediaType.APPLICATION_JSON).build();
159
            }
160
        } catch (HttpClientErrorException e) {
161
            String message = new JsonParser().parse(e.getResponseBodyAsString()).getAsJsonObject().get("message").getAsString();
162
            return Response.status(e.getStatusCode().value()).entity(jsonUtils.createResponse(message).toString()).type(MediaType.APPLICATION_JSON).build();
163
        }
164
    }
165

    
166
    /**
167
     * Invite user with email to be a member of a type(Community, etc.) with id(ee, egi, etc.)
168
     * Auto generated link and code will be sent as response.
169
     */
170
    @Path("/invite/{type}/{id}/member")
171
    @POST
172
    @Produces(MediaType.APPLICATION_JSON)
173
    @PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN, " +
174
            "@AuthorizationService.curator(#type), @AuthorizationService.manager(#type, #id))")
175
    public Response inviteMember(@PathParam("type") String type, @PathParam("id") String id, @RequestBody String body) {
176
        try {
177
            JsonObject details = new JsonParser().parse(body).getAsJsonObject();
178
            JsonObject email = details.get("email").getAsJsonObject();
179
            String recipient = email.get("recipient").getAsString();
180
            if (!calls.isMember(type, id, recipient)) {
181
                JsonObject invitation = verificationUtils.createMemberInvitation(recipient, type, id);
182
                return sendEmail(details, email, invitation);
183
            } else {
184
                return Response.status(HttpStatus.CONFLICT.value()).entity(jsonUtils.createResponse("User has been already member of this " + type).toString()).type(MediaType.APPLICATION_JSON).build();
185
            }
186
        } catch (HttpClientErrorException e) {
187
            String message = new JsonParser().parse(e.getResponseBodyAsString()).getAsJsonObject().get("message").getAsString();
188
            return Response.status(e.getStatusCode().value()).entity(jsonUtils.createResponse(message).toString()).type(MediaType.APPLICATION_JSON).build();
189
        }
190
    }
191

    
192
    private Response sendEmail(JsonObject details, JsonObject email, JsonObject invitation) {
193
        String link = details.get("link").getAsString() + invitation.get("link").getAsString();
194
        String subject = email.get("subject").getAsString();
195
        String message = email.get("body").getAsString().
196
                replace("((__user__))", "User").
197
                replace("((__link__))", link).
198
                replace("((__code__))", invitation.get("code").getAsString());
199
        try {
200
            emailSender.sendEmail(email.get("recipient").getAsString(), subject, message);
201
            return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(invitation).toString()).type(MediaType.APPLICATION_JSON).build();
202
        } catch (MessagingException e) {
203
            verificationUtils.deleteVerification(invitation.get("link").getAsString());
204
            return Response.status(HttpStatus.BAD_REQUEST.value()).entity(jsonUtils.createResponse("Email sent failed").toString()).type(MediaType.APPLICATION_JSON).build();
205
        }
206
    }
207

    
208
    /**
209
     * Cancel invitation to user with email for managing a type(Community, etc.) with id(ee, egi, etc.)
210
     */
211
    @Path("/invite/{type}/{id}/manager/{email}")
212
    @DELETE
213
    @Produces(MediaType.APPLICATION_JSON)
214
    @PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN, " +
215
            "@AuthorizationService.curator(#type), @AuthorizationService.manager(#type, #id))")
216
    public Response cancelManagerInvitations(@PathParam("type") String type, @PathParam("id") String id, @PathParam("email") String email) {
217
        verificationUtils.deleteManagerVerifications(email, type, id);
218
        return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse("Invitations have been deleted").toString()).type(MediaType.APPLICATION_JSON).build();
219
    }
220

    
221
    /**
222
     * Cancel invitation to user with email for being member of a type(Community, etc.) with id(ee, egi, etc.)
223
     */
224
    @Path("/invite/{type}/{id}/member/{email}")
225
    @DELETE
226
    @Produces(MediaType.APPLICATION_JSON)
227
    @PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN, " +
228
            "@AuthorizationService.curator(#type), @AuthorizationService.manager(#type, #id))")
229
    public Response cancelMemberInvitations(@PathParam("type") String type, @PathParam("id") String id, @PathParam("email") String email) {
230
        verificationUtils.deleteMemberVerifications(email, type, id);
231
        return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse("Invitations have been deleted").toString()).type(MediaType.APPLICATION_JSON).build();
232
    }
233

    
234
    /**
235
     * Get the invited managers for a type(Community, etc.) with id(ee, egi, etc.)
236
     */
237
    @Path("/invite/{type}/{id}/managers/")
238
    @GET
239
    @Produces(MediaType.APPLICATION_JSON)
240
    @PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN, " +
241
            "@AuthorizationService.curator(#type), @AuthorizationService.manager(#type, #id))")
242
    public Response getInvitedManagers(@PathParam("type") String type, @PathParam("id") String id) {
243
        JsonArray invited = verificationUtils.getInvitedManagers(type, id);
244
        return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(invited).toString()).type(MediaType.APPLICATION_JSON).build();
245
    }
246

    
247
    /**
248
     * Get the invited members for a type(Community, etc.) with id(ee, egi, etc.)
249
     */
250
    @Path("/invite/{type}/{id}/members/")
251
    @GET
252
    @Produces(MediaType.APPLICATION_JSON)
253
    @PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN, " +
254
            "@AuthorizationService.curator(#type), @AuthorizationService.manager(#type, #id))")
255
    public Response getInviteMembers(@PathParam("type") String type, @PathParam("id") String id) {
256
        JsonArray invited = verificationUtils.getInvitedMembers(type, id);
257
        return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(invited).toString()).type(MediaType.APPLICATION_JSON).build();
258
    }
259

    
260
    /**
261
     * Get the verification with a specific id only if it refers to the logged in user
262
     */
263
    @Path("/verification/{id}")
264
    @GET
265
    @Produces(MediaType.APPLICATION_JSON)
266
    @PreAuthorize("isAuthenticated()")
267
    public Response getVerification(@PathParam("id") String id) {
268
        RoleVerification verification = verificationUtils.getVerification(id);
269
        if (verification != null) {
270
            if (verification.getEmail().equalsIgnoreCase(authorizationService.getEmail())) {
271
                return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(jsonUtils.createVerification(verification)).toString()).type(MediaType.APPLICATION_JSON).build();
272
            } else {
273
                return Response.status(HttpStatus.FORBIDDEN.value()).entity(jsonUtils.createResponse("Forbidden verification").toString()).type(MediaType.APPLICATION_JSON).build();
274
            }
275
        } else {
276
            return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("Verification has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
277
        }
278
    }
279

    
280
    /**
281
     * Delete the verification with a specific id.
282
     */
283
    @Path("/verification/{id}")
284
    @DELETE
285
    @Produces(MediaType.APPLICATION_JSON)
286
    @PreAuthorize("isAuthenticated() && @VerificationUtils.ownedVerification(#id)")
287
    public Response deleteVerification(@PathParam("id") String id) {
288
        if (verificationUtils.getVerification(id) != null) {
289
            verificationUtils.deleteVerification(id);
290
            return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(jsonUtils.createResponse("Verification deleted")).toString()).type(MediaType.APPLICATION_JSON).build();
291
        } else {
292
            return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse(jsonUtils.createResponse("Verification has not been found")).toString()).type(MediaType.APPLICATION_JSON).build();
293
        }
294
    }
295

    
296
    /**
297
     * Verify the verification with the specific id, if the code is correct and it refers to the logged in user.
298
     * Manager role is assigned to this user, along with the member role.
299
     */
300
    @Path("/verification/manager/{id}")
301
    @POST
302
    @Produces(MediaType.APPLICATION_JSON)
303
    @PreAuthorize("isAuthenticated()")
304
    public Response verifyManager(@PathParam("id") String id, @RequestBody String code, @Context final HttpServletRequest request) {
305
        RoleVerification verification = verificationUtils.getVerification(id);
306
        if (verification != null && verification.getVerificationType().equals("manager")) {
307
            if (verification.getEmail().equalsIgnoreCase(authorizationService.getEmail())) {
308
                if (verification.getVerificationCode().equals(code)) {
309
                    try {
310
                        calls.assignManagerRole(verification.getType(), verification.getEntity(), request);
311
                        if (verification.getType().equals("community") || verification.getType().equals("ri")) {
312
                            calls.assignMemberRole("ri", verification.getEntity(), request);
313
                            verificationUtils.deleteMemberVerifications(verification.getEmail(), "community", verification.getEntity());
314
                            verificationUtils.deleteMemberVerifications(verification.getEmail(), "ri", verification.getEntity());
315
                            verificationUtils.deleteManagerVerifications(verification.getEmail(), "community", verification.getEntity());
316
                            verificationUtils.deleteManagerVerifications(verification.getEmail(), "ri", verification.getEntity());
317
                        } else {
318
                            verificationUtils.deleteMemberVerifications(verification.getEmail(), verification.getType(), verification.getEntity());
319
                            verificationUtils.deleteManagerVerifications(verification.getEmail(), verification.getType(), verification.getEntity());
320
                        }
321
                        return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse("Admin role has been assigned").toString()).type(MediaType.APPLICATION_JSON).build();
322
                    } catch (HttpClientErrorException e) {
323
                        String message = new JsonParser().parse(e.getResponseBodyAsString()).getAsJsonObject().get("message").getAsString();
324
                        return Response.status(e.getStatusCode().value()).entity(jsonUtils.createResponse(message).toString()).type(MediaType.APPLICATION_JSON).build();
325
                    }
326
                } else {
327
                    return Response.status(HttpStatus.BAD_REQUEST.value()).entity(jsonUtils.createResponse("Verification code is wrong").toString()).type(MediaType.APPLICATION_JSON).build();
328
                }
329
            } else {
330
                return Response.status(HttpStatus.FORBIDDEN.value()).entity(jsonUtils.createResponse("Forbidden verification").toString()).type(MediaType.APPLICATION_JSON).build();
331
            }
332
        } else {
333
            return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("Verification has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
334
        }
335
    }
336

    
337
    /**
338
     * Verify the verification with the specific id, if the code is correct and it refers to the logged in user.
339
     * Member role is assigned to this user, along with the member role.
340
     */
341
    @Path("/verification/member/{id}")
342
    @POST
343
    @Produces(MediaType.APPLICATION_JSON)
344
    @PreAuthorize("isAuthenticated()")
345
    public Response verifyMember(@PathParam("id") String id, @RequestBody String code, @Context final HttpServletRequest request) {
346
        RoleVerification verification = verificationUtils.getVerification(id);
347
        if (verification != null && verification.getVerificationType().equals("member")) {
348
            if (verification.getEmail().equalsIgnoreCase(authorizationService.getEmail())) {
349
                if (verification.getVerificationCode().equals(code)) {
350
                    try {
351
                        calls.assignMemberRole(verification.getType(), verification.getEntity(), request);
352
                        verificationUtils.deleteMemberVerifications(verification.getEmail(), verification.getType(), verification.getEntity());
353
                        return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse("Member role has been assigned").toString()).type(MediaType.APPLICATION_JSON).build();
354
                    } catch (HttpClientErrorException e) {
355
                        String message = new JsonParser().parse(e.getResponseBodyAsString()).getAsJsonObject().get("message").getAsString();
356
                        return Response.status(e.getStatusCode().value()).entity(jsonUtils.createResponse(message).toString()).type(MediaType.APPLICATION_JSON).build();
357
                    }
358
                } else {
359
                    return Response.status(HttpStatus.BAD_REQUEST.value()).entity(jsonUtils.createResponse("Verification code is wrong").toString()).type(MediaType.APPLICATION_JSON).build();
360
                }
361
            } else {
362
                return Response.status(HttpStatus.FORBIDDEN.value()).entity(jsonUtils.createResponse("Forbidden verification").toString()).type(MediaType.APPLICATION_JSON).build();
363
            }
364
        } else {
365
            return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("Verification has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
366
        }
367
    }
368

    
369
    /**
370
     * Remove the manager role from user with email for a type(Community, etc.) with id(ee, egi, etc.)
371
     */
372
    @Path("/{type}/{id}/manager/{email}")
373
    @DELETE
374
    @Produces(MediaType.APPLICATION_JSON)
375
    @Consumes(MediaType.APPLICATION_JSON)
376
    @PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN, @AuthorizationService.curator(#type), @AuthorizationService.manager(#type, #id))")
377
    public Response removeManagerRole(@PathParam("type") String type, @PathParam("id") String
378
            id, @PathParam("email") String email) {
379
        try {
380
            JsonElement response = calls.removeManagerRole(type, id, email);
381
            return Response.status(HttpStatus.OK.value()).entity(response.toString()).type(MediaType.APPLICATION_JSON).build();
382
        } catch (HttpClientErrorException e) {
383
            String message = new JsonParser().parse(e.getResponseBodyAsString()).getAsJsonObject().get("message").getAsString();
384
            return Response.status(e.getStatusCode().value()).entity(jsonUtils.createResponse(message).toString()).type(MediaType.APPLICATION_JSON).build();
385
        }
386
    }
387

    
388
    /**
389
     * Remove the member role from user with email for a type(Community, etc.) with id(ee, egi, etc.)
390
     */
391
    @Path("/{type}/{id}/member/{email}")
392
    @DELETE
393
    @Produces(MediaType.APPLICATION_JSON)
394
    @Consumes(MediaType.APPLICATION_JSON)
395
    @PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN, @AuthorizationService.curator(#type), @AuthorizationService.manager(#type, #id))")
396
    public Response removeMemberRole(@PathParam("type") String type, @PathParam("id") String
397
            id, @PathParam("email") String email) {
398
        try {
399
            JsonElement response = calls.removeMemberRole(type, id, email);
400
            return Response.status(HttpStatus.OK.value()).entity(response.toString()).type(MediaType.APPLICATION_JSON).build();
401
        } catch (HttpClientErrorException e) {
402
            String message = new JsonParser().parse(e.getResponseBodyAsString()).getAsJsonObject().get("message").getAsString();
403
            return Response.status(e.getStatusCode().value()).entity(jsonUtils.createResponse(message).toString()).type(MediaType.APPLICATION_JSON).build();
404
        }
405
    }
406

    
407
    /**
408
     * Get the number of the members of a type(Community, etc.) with id(ee, egi, etc.)
409
     */
410
    @Path("/{type}/{id}/members/count")
411
    @GET
412
    @Produces(MediaType.APPLICATION_JSON)
413
    public Response getMembersCount(@PathParam("type") String type, @PathParam("id") String id) {
414
        try {
415
            int response = calls.getAllMembersCount(type, id);
416
            return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(response).toString()).type(MediaType.APPLICATION_JSON).build();
417
        } catch (HttpClientErrorException e) {
418
            String message = new JsonParser().parse(e.getResponseBodyAsString()).getAsJsonObject().get("message").getAsString();
419
            return Response.status(e.getStatusCode().value()).entity(jsonUtils.createResponse(message).toString()).type(MediaType.APPLICATION_JSON).build();
420
        }
421
    }
422

    
423
    /**
424
     * Get infos of the members of a type(Community, etc.) with id(ee, egi, etc.)
425
     */
426
    @Path("/{type}/{id}/members{var:.*}")
427
    @GET
428
    @Produces(MediaType.APPLICATION_JSON)
429
    @PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN," +
430
            "@AuthorizationService.curator(#type), @AuthorizationService.manager(#type, #id))")
431
    public Response getMembers(@PathParam("type") String type, @PathParam("id") String id) {
432
        try {
433
            JsonElement response = calls.getAllMembers(type, id);
434
            return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(response).toString()).type(MediaType.APPLICATION_JSON).build();
435
        } catch (HttpClientErrorException e) {
436
            String message = new JsonParser().parse(e.getResponseBodyAsString()).getAsJsonObject().get("message").getAsString();
437
            return Response.status(e.getStatusCode().value()).entity(jsonUtils.createResponse(message).toString()).type(MediaType.APPLICATION_JSON).build();
438
        }
439
    }
440

    
441
    /**
442
     * Get infos of the managers of a type(Community, etc.) with id(ee, egi, etc.)
443
     */
444
    @Path("/{type}/{id}/managers{var:.*}")
445
    @GET
446
    @Produces(MediaType.APPLICATION_JSON)
447
    public Response getManagers(@PathParam("type") String type, @PathParam("id") String id) {
448
        try {
449
            JsonElement response = calls.getAllManagers(type, id);
450
            return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(response).toString()).type(MediaType.APPLICATION_JSON).build();
451
        } catch (HttpClientErrorException e) {
452
            String message = new JsonParser().parse(e.getResponseBodyAsString()).getAsJsonObject().get("message").getAsString();
453
            return Response.status(e.getStatusCode().value()).entity(jsonUtils.createResponse(message).toString()).type(MediaType.APPLICATION_JSON).build();
454
        }
455
    }
456

    
457
    /**
458
     * Get infos of the curators of a type(Community, etc.)
459
     */
460
    @Path("/{type}/curators{var:.*}")
461
    @GET
462
    @Produces(MediaType.APPLICATION_JSON)
463
    @PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN, @AuthorizationService.curator(#type))")
464
    public Response getCurators(@PathParam("type") String type) {
465
        try {
466
            JsonElement response = calls.getAllCurators(type);
467
            return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(response.toString())).type(MediaType.APPLICATION_JSON).build();
468
        } catch (HttpClientErrorException e) {
469
            String message = new JsonParser().parse(e.getResponseBodyAsString()).getAsJsonObject().get("message").getAsString();
470
            return Response.status(e.getStatusCode().value()).entity(jsonUtils.createResponse(message).toString()).type(MediaType.APPLICATION_JSON).build();
471
        }
472
    }
473
}
(1-1/2)