Project

General

Profile

1
package eu.dnetlib.openaire.rest;
2

    
3
import com.google.gson.*;
4
import eu.dnetlib.api.enabling.ISLookUpService;
5
import eu.dnetlib.api.enabling.ISLookUpServiceException;
6
import eu.dnetlib.data.claims.migration.ClaimValidationException;
7
import eu.dnetlib.data.claims.migration.entity.Claim;
8
import eu.dnetlib.data.claims.migration.entity.Notification;
9
import eu.dnetlib.data.claims.migration.entity.Project;
10
import eu.dnetlib.data.claims.migration.handler.*;
11
import eu.dnetlib.data.claimsDemo.CommunityUtils;
12
import eu.dnetlib.data.claimsDemo.SQLStoreException;
13
import eu.dnetlib.data.emailSender.EmailSender;
14
import gr.uoa.di.driver.util.ServiceLocator;
15
import net.sf.ehcache.search.expression.Not;
16
import org.apache.commons.validator.EmailValidator;
17
import org.apache.log4j.Logger;
18
import org.json.XML;
19
import org.springframework.beans.factory.annotation.Autowired;
20
import org.springframework.stereotype.Component;
21

    
22
import javax.annotation.Resource;
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
import java.util.ArrayList;
29
import java.util.HashMap;
30
import java.util.List;
31
import java.util.Map;
32

    
33
/**
34
 * Created by kiatrop on 15/4/2016.
35
 */
36
@Component
37
@Path("/claimsService")
38
public class HelloWorldService {
39

    
40
    private static final Logger logger = Logger.getLogger(HelloWorldService.class);
41

    
42
    @Autowired
43
    private FetchClaimHandler fetchClaimHandler = null;
44

    
45
    @Autowired
46
    private FetchProjectHandler fetchProjectHandler= null;
47

    
48
    @Autowired
49
    private FetchNotificationHandler fetchNotificationHandler = null;
50

    
51
    @Autowired
52
    private NotificationHandler notificationHandler = null;
53

    
54
    @Resource
55
    private ServiceLocator<ISLookUpService> lookupServiceLocator = null;
56

    
57
    @Autowired
58
    private ClaimHandler claimHandler = null;
59

    
60
    @Autowired
61
    private DirectIndexHandler directIndexHandler = null;
62

    
63
    @Autowired
64
    public Authorization authorization = null;
65

    
66
    @Autowired
67
    private String defaultFrequencyInHours;
68

    
69
    @Autowired
70
    private EmailSender emailSender;
71

    
72
    @GET
73
    @Path("projects/{projectId}/claims")
74
    @Produces(MediaType.APPLICATION_JSON)
75
    public Response getProjectClaims(@PathParam("projectId") String projectId,
76
                           @DefaultValue("0") @QueryParam("offset") int offset,
77
                           @DefaultValue("20") @QueryParam("limit") int limit,
78
                           @DefaultValue("") @QueryParam("keyword") String keyword,
79
                           @DefaultValue("") @QueryParam("sortby") String orderby,
80
                           @DefaultValue("true") @QueryParam("descending") boolean descending,
81
                           @DefaultValue("") @QueryParam("types") List<String> types,
82
                                     @HeaderParam("X-XSRF-TOKEN") String token,
83
                                     @CookieParam("AccessToken") String  cookie,
84
                           @Context HttpServletRequest request) {
85

    
86
        if(token == null || token.isEmpty() || cookie == null || cookie.isEmpty() || !cookie.equals(token)){
87
            authorization.logStatus(token,cookie);
88
            return Response.status(Response.Status.FORBIDDEN).entity(compose403Message("Forbidden: You don't have permission to access. Maybe you are not registered."))
89
                    .type(MediaType.APPLICATION_JSON)
90
                    .build();
91
        }
92

    
93
        if(authorization.isClaimCurator(token)) {
94

    
95
            int total = -1;
96

    
97
            if (projectId == null || projectId.isEmpty()) {
98
                return Response.status(Response.Status.BAD_REQUEST).entity(compose400Message("Project id cannot be empty."))
99
                        .type(MediaType.APPLICATION_JSON).build();
100
            }
101

    
102
            List<Claim> claims = null;
103
            try {
104
                claims = fetchClaimHandler.fetchClaimsByProject(projectId, limit, offset, keyword, orderby, descending, types,false);
105
                total = fetchClaimHandler.countClaimsByProject(projectId, keyword, types);
106

    
107
            } catch (SQLStoreException|Exception e) {  //TODO check this with exception
108
                logger.error("Could not fetch claims for project with id " + projectId, e);
109
                return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(compose500Message("Fail to fetch claims" +
110
                        " for projects with id " + projectId + ".", e)).type(MediaType.APPLICATION_JSON).build();
111

    
112
            }
113

    
114
            return Response.status(200).entity(composeDataResponse(request, claims, total, offset, limit)).build();
115
        }
116

    
117
        return Response.status(Response.Status.FORBIDDEN).entity(compose403Message("Forbidden: You don't have permission to access"))
118
                .type(MediaType.APPLICATION_JSON)
119
                .build();
120
    }
121

    
122
    @GET
123
    @Path("projects/{projectId}/all_claims")
124
    @Produces(MediaType.APPLICATION_JSON)
125
    public Response getAllProjectClaims(@PathParam("projectId") String projectId,
126
                                     @DefaultValue("-1") @QueryParam("offset") int offset,
127
                                     @DefaultValue("-1") @QueryParam("limit") int limit,
128
                                     @DefaultValue("") @QueryParam("keyword") String keyword,
129
                                     @DefaultValue("") @QueryParam("sortby") String orderby,
130
                                     @DefaultValue("true") @QueryParam("descending") boolean descending,
131
                                     @DefaultValue("") @QueryParam("types") List<String> types,
132
                                     @HeaderParam("X-XSRF-TOKEN") String token,
133
                                     @CookieParam("AccessToken") String  cookie,
134
                                     @Context HttpServletRequest request) {
135

    
136
        if(token == null || token.isEmpty() || cookie == null || cookie.isEmpty() || !cookie.equals(token)){
137
            authorization.logStatus(token,cookie);
138
            return Response.status(Response.Status.FORBIDDEN).entity(compose403Message("Forbidden: You don't have permission to access. Maybe you are not registered."))
139
                    .type(MediaType.APPLICATION_JSON)
140
                    .build();
141
        }
142

    
143
        UserInfo userInfo = authorization.getUserHandler().getUserInfo(token);
144
//        if(authorization.isProjectCurator(userInfo)) {
145
        String userMail = userInfo.getEmail();
146

    
147
        int total = -1;
148

    
149
        if (projectId == null || projectId.isEmpty()) {
150
            return Response.status(Response.Status.BAD_REQUEST).entity(compose400Message("Project id cannot be empty."))
151
                    .type(MediaType.APPLICATION_JSON).build();
152
        }
153

    
154
        List<Claim> claims = null;
155
        try {
156
            boolean forbidden = true;
157
            if(authorization.isProjectCurator(userInfo)) {
158
                forbidden = false;
159
            } else {
160
                List<String> contact_emails = fetchProjectHandler.fetchContactEmailsByProjectId(projectId);
161
                logger.debug(contact_emails);
162
                if(contact_emails != null && contact_emails.contains(userMail)) {
163
                    forbidden = false;
164
                }
165
            }
166

    
167
            if(forbidden){
168
                return Response.status(Response.Status.FORBIDDEN).entity(compose403Message("Forbidden: You don't have permission to access"))
169
                        .type(MediaType.APPLICATION_JSON)
170
                        .build();
171
            }else{
172
                if(offset == -1 && limit == -1) {
173
                    // when offset and limit are -1 fetch claims with null values, to ignore paging and limit clause
174
                    claims = fetchClaimHandler.fetchClaimsByProject(projectId, null, null, keyword, orderby, descending, types, true);
175
                } else {
176
                    claims = fetchClaimHandler.fetchClaimsByProject(projectId, limit, offset, keyword, orderby, descending, types, true);
177
                }
178
                total = fetchClaimHandler.countClaimsByProject(projectId, keyword, types);
179

    
180
                return Response.status(200).entity(composeDataResponse(request, claims, total, offset, limit)).build();
181
            }
182

    
183
        } catch (SQLStoreException|Exception e) {
184
            logger.error("Could not fetch claims for project with id " + projectId, e);
185
            return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(compose500Message("Fail to fetch claims" +
186
                    " for projects with id " + projectId + ".", e)).type(MediaType.APPLICATION_JSON).build();
187
        }
188
    }
189
/*
190
    @GET
191
    @Path("project/claims")
192
    @Produces(MediaType.APPLICATION_JSON)
193
    public Response getProjectClaimsByToken(@QueryParam("projectToken") String projectToken,
194
                                     @DefaultValue("-1") @QueryParam("offset") int offset,
195
                                     @DefaultValue("-1") @QueryParam("limit") int limit,
196
                                     @DefaultValue("") @QueryParam("keyword") String keyword,
197
                                     @DefaultValue("") @QueryParam("sortby") String orderby,
198
                                     @DefaultValue("true") @QueryParam("descending") boolean descending,
199
                                     @DefaultValue("") @QueryParam("types") List<String> types,
200
                                     @HeaderParam("X-XSRF-TOKEN") String token,
201
                                     @CookieParam("AccessToken") String  cookie,
202
                                     @Context HttpServletRequest request) {
203

    
204

    
205
        if(token == null || token.isEmpty() || cookie == null || cookie.isEmpty() || !cookie.equals(token)){
206
            authorization.logStatus(token,cookie);
207
            return Response.status(Response.Status.FORBIDDEN).entity(compose403Message("Forbidden: You don't have permission to access. Maybe you are not registered."))
208
                    .type(MediaType.APPLICATION_JSON)
209
                    .build();
210
        }
211

    
212
        UserInfo userInfo = authorization.getUserHandler().getUserInfo(token);
213
//        if(authorization.isProjectCurator(userInfo)) {
214
            String userMail = userInfo.getEmail();
215

    
216
            int total = -1;
217

    
218
            if (projectToken == null || projectToken.isEmpty()) {
219
                return Response.status(Response.Status.BAD_REQUEST).entity(compose400Message("Project token cannot be empty."))
220
                        .type(MediaType.APPLICATION_JSON).build();
221
            }
222

    
223
            List<Claim> claims = null;
224
            try {
225
                String projectId = null;
226
                if(authorization.isProjectCurator(userInfo)) {
227
                    projectId = fetchProjectHandler.fetchProjectIdByToken(projectToken);
228
                } else {
229
                    projectId = fetchProjectHandler.fetchProjectIdByTokenAndEmail(projectToken, userMail);
230
                }
231
                //String projectId = fetchProjectHandler.fetchProjectIdByToken(projectToken,userMail);
232
                if(projectId == null){
233
                    return Response.status(Response.Status.FORBIDDEN).entity(compose403Message("Forbidden: You don't have permission to access"))
234
                            .type(MediaType.APPLICATION_JSON)
235
                            .build();
236
                }else{
237
                    if(offset == -1 && limit == -1) {
238
                        // when offset and limit are -1 fetch claims with null values, to ignore paging and limit clause
239
                        claims = fetchClaimHandler.fetchClaimsByProject(projectId, null, null, keyword, orderby, descending, types, true);
240
                    } else {
241
                        claims = fetchClaimHandler.fetchClaimsByProject(projectId, limit, offset, keyword, orderby, descending, types, true);
242
                    }
243
                    total = fetchClaimHandler.countClaimsByProject(projectId, keyword, types);
244

    
245
                    return Response.status(200).entity(composeDataResponse(request, claims, total, offset, limit)).build();
246
                }
247

    
248
            } catch (SQLStoreException|Exception e) {
249
                logger.error("Could not fetch claims for project token " + projectToken, e);
250
                return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(compose500Message("Fail to fetch claims" +
251
                        " for projects with token " + projectToken + ".", e)).type(MediaType.APPLICATION_JSON).build();
252
            }
253
//        }
254

    
255
//        return Response.status(Response.Status.FORBIDDEN).entity(compose403Message("Forbidden: You don't have permission to access"))
256
//                .type(MediaType.APPLICATION_JSON)
257
//                .build();
258
    }
259
*/
260

    
261
    @GET
262
    @Path("/contexts/{contextId}/claims")
263
    @Produces(MediaType.APPLICATION_JSON)
264
    public Response getContextClaims(@PathParam("contextId") String contextId,
265
                           @DefaultValue("0") @QueryParam("offset") int offset,
266
                           @DefaultValue("20") @QueryParam("limit") int limit,
267
                           @DefaultValue("") @QueryParam("keyword") String keyword,
268
                           @DefaultValue("") @QueryParam("sortby") String orderby,
269
                           @DefaultValue("true") @QueryParam("descending") boolean descending,
270
                           @DefaultValue("") @QueryParam("types") List<String> types,
271
                                     @HeaderParam("X-XSRF-TOKEN") String token,
272
                                     @CookieParam("AccessToken") String  cookie,
273
                                     @Context HttpServletRequest request) {
274
//        @RequestParam(value="includeStates[]", defaultValue="1,2,3") String[] includeStates
275

    
276
        if(token == null || token.isEmpty() || cookie == null || cookie.isEmpty() || !cookie.equals(token)){
277
            authorization.logStatus(token,cookie);
278
            return Response.status(Response.Status.FORBIDDEN).entity(compose403Message("Forbidden: You don't have permission to access. Maybe you are not registered."))
279
                    .type(MediaType.APPLICATION_JSON)
280
                    .build();
281
        }
282
//        logger.debug("Calling API for context with token " + token);
283

    
284
        if(authorization.isCommunityCurator(token) || authorization.isClaimCurator(token)) {
285

    
286
            int total = -1;
287
            if (contextId == null || contextId.isEmpty()) {
288
                return Response.status(Response.Status.BAD_REQUEST).entity(compose400Message("Context id cannot be empty."))
289
                        .type(MediaType.APPLICATION_JSON).build();
290
            }
291
            logger.debug("Types are " + types);
292
            List<Claim> claims = null;
293

    
294
            try {
295
                claims = fetchClaimHandler.fetchClaimsByContext(contextId, limit, offset, keyword, orderby, descending, types,false);
296
                total = fetchClaimHandler.countClaimsByContext(contextId, keyword, types);
297

    
298
            } catch (SQLStoreException|Exception e) {
299
                logger.error("Could not fetch claims for context with id " + contextId, e);
300
                return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(compose500Message("Fail to fetch claims" +
301
                        " for context with id " + contextId + ".", e)).type(MediaType.APPLICATION_JSON).build();
302
            }
303

    
304
            return Response.status(200).entity(composeDataResponse(request, claims, total, offset, limit)).build();
305
        }
306

    
307
        return Response.status(Response.Status.FORBIDDEN).entity(compose403Message("Forbidden: You don't have permission to access"))
308
                .type(MediaType.APPLICATION_JSON)
309
                .build();
310
    }
311

    
312

    
313
    @GET
314
    @Path("/results/{resultId}/claims")
315
    @Produces(MediaType.APPLICATION_JSON)
316
    public Response getResultClaims(@PathParam("resultId") String resultId,
317
                                    @DefaultValue("0") @QueryParam("offset") int offset,
318
                                    @DefaultValue("20") @QueryParam("limit") int limit,
319
                                    @DefaultValue("") @QueryParam("keyword") String keyword,
320
                                    @DefaultValue("") @QueryParam("sortby") String orderby,
321
                                    @DefaultValue("true") @QueryParam("descending") boolean descending,
322
                                    @DefaultValue("") @QueryParam("types") List<String> types,
323
                                    @HeaderParam("X-XSRF-TOKEN") String token,
324
                                    @CookieParam("AccessToken") String  cookie,
325
                                    @Context HttpServletRequest request) {
326

    
327

    
328
        if(token == null || token.isEmpty() || cookie == null || cookie.isEmpty() || !cookie.equals(token)){
329
            authorization.logStatus(token,cookie);
330
            return Response.status(Response.Status.FORBIDDEN).entity(compose403Message("Forbidden: You don't have permission to access. Maybe you are not registered."))
331
                    .type(MediaType.APPLICATION_JSON)
332
                    .build();
333
        }
334

    
335
        if(authorization.isClaimCurator(token)) {
336

    
337
            int total = -1;
338
            if (resultId == null || resultId.isEmpty()) {
339
                return Response.status(Response.Status.BAD_REQUEST).entity(compose400Message("Context id cannot be empty."))
340
                        .type(MediaType.APPLICATION_JSON).build();
341
            }
342

    
343
            List<Claim> claims = null;
344
            try {
345
                claims = fetchClaimHandler.fetchClaimsByResult(resultId, limit, offset, keyword, orderby, descending, types,false);
346
                total = fetchClaimHandler.countClaimsByResult(resultId, keyword, types);
347

    
348
            } catch (SQLStoreException|Exception e) {
349
                logger.error("Could not fetch claims for result with id " + resultId, e);
350
                return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(compose500Message("Fail to fetch claims" +
351
                        " for result with id " + resultId + ".", e)).type(MediaType.APPLICATION_JSON).build();
352
            }
353

    
354
            return Response.status(200).entity(composeDataResponse(request, claims, total, offset, limit)).build();
355
        }
356

    
357
        return Response.status(Response.Status.FORBIDDEN).entity(compose403Message("Forbidden: You don't have permission to access"))
358
                .type(MediaType.APPLICATION_JSON)
359
                .build();
360
    }
361

    
362

    
363
    @GET
364
    @Path("/users/claims")
365
    @Produces(MediaType.APPLICATION_JSON)
366
    public Response getUserClaims(@DefaultValue("0") @QueryParam("offset") int offset,
367
                                  @DefaultValue("20") @QueryParam("limit") int limit,
368
                                  @DefaultValue("") @QueryParam("keyword") String keyword,
369
                                  @DefaultValue("") @QueryParam("sortby") String orderby,
370
                                  @DefaultValue("true") @QueryParam("descending") boolean descending,
371
                                  @DefaultValue("") @QueryParam("types") List<String> types,
372
                                  @HeaderParam("X-XSRF-TOKEN") String token,
373
                                  @CookieParam("AccessToken") String  cookie,
374
                                  @Context HttpServletRequest request) {
375

    
376

    
377
        if(token == null || token.isEmpty() || cookie == null || cookie.isEmpty() || !cookie.equals(token)){
378
            return Response.status(Response.Status.FORBIDDEN).entity(compose403Message("Forbidden: You don't have permission to access. Maybe you are not registered."))
379
                    .type(MediaType.APPLICATION_JSON)
380
                    .build();
381
        }
382

    
383
        UserInfo userInfo = authorization.getUserHandler().getUserInfo(token);
384
        if(authorization.isRegistered(userInfo)) {
385
            String userMail = userInfo.getEmail();
386
            logger.debug("User is registerd "  );
387
            int total = -1;
388
            EmailValidator emailValidator = EmailValidator.getInstance();
389

    
390
            if (userMail == null || userMail.isEmpty()) {
391
                return Response.status(Response.Status.BAD_REQUEST).entity(compose400Message("User e-mail cannot be empty."))
392
                        .type(MediaType.APPLICATION_JSON).build();
393
            }
394

    
395
            if (!emailValidator.isValid(userMail)) {
396
                return Response.status(Response.Status.BAD_REQUEST).entity(compose400Message("User e-mail is not valid."))
397
                        .type(MediaType.APPLICATION_JSON).build();
398
            }
399

    
400
            List<Claim> claims = null;
401
            try {
402
                logger.debug("About to fetch claims"  );
403
                claims = fetchClaimHandler.fetchClaimsByUser(userMail, limit, offset, keyword, orderby, descending, types,false);
404
                total = fetchClaimHandler.countClaimsByUser(userMail, keyword, types);
405

    
406
            } catch (SQLStoreException|Exception e) {
407
                logger.error("Could not fetch claims for user with mail " + userMail, e);
408
                return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(compose500Message("Fail to fetch claims" +
409
                        " for user with e-mail " + userMail + ".", e)).type(MediaType.APPLICATION_JSON).build();
410
            }
411

    
412
            return Response.status(200).entity(composeDataResponse(request, claims, total, offset, limit)).build();
413
        }
414
        logger.debug("User is *NOT* registerd "  );
415
        return Response.status(Response.Status.FORBIDDEN).entity(compose403Message("Forbidden: You don't have permission to access. You are not registered."))
416
                .type(MediaType.APPLICATION_JSON)
417
                .build();
418
    }
419

    
420
    @GET
421
    @Path("/claims/{claimId}")
422
    @Produces(MediaType.APPLICATION_JSON)
423
    public Response getClaimsById(@PathParam("claimId") String claimId,
424
                                  @DefaultValue("0") @QueryParam("offset") int offset,
425
                                  @DefaultValue("20") @QueryParam("limit") int limit,
426
                                  @HeaderParam("X-XSRF-TOKEN") String token,
427
                                  @CookieParam("AccessToken") String  cookie,
428
                                  @Context HttpServletRequest request) {
429

    
430

    
431
        if(token == null || token.isEmpty() || cookie == null || cookie.isEmpty() || !cookie.equals(token)){
432
            return Response.status(Response.Status.FORBIDDEN).entity(compose403Message("Forbidden: You don't have permission to access. Maybe you are not registered."))
433
                    .type(MediaType.APPLICATION_JSON)
434
                    .build();
435
        }
436

    
437
        if(authorization.isRegistered(token)) {
438

    
439
            List<Claim> claims = null;
440

    
441
            int total = -1;
442
            if (claimId == null || claimId.isEmpty()) {
443
                try {
444
                    claims = fetchClaimHandler.fetchAllClaims(limit, offset,false);
445
                    total = fetchClaimHandler.countAllClaims("", new ArrayList<String>());
446

    
447
                    return Response.status(200).entity(composeDataResponse(request, claims, total, offset, limit)).type(MediaType.APPLICATION_JSON).build();
448

    
449
                } catch (SQLStoreException|Exception e) {
450
                    logger.error("Could not fetch claims.", e);
451
                    return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(compose500Message("Fail to fetch claims.", e))
452
                            .type(MediaType.APPLICATION_JSON).build();
453
                }
454
            }
455

    
456
            try {
457
                Claim claim = fetchClaimHandler.fetchClaimById(claimId,false);
458
                if (claim == null) {
459
                    return Response.status(Response.Status.NOT_FOUND).entity(compose404Message("Cannot find claim with id " + claimId + "."))
460
                            .type(MediaType.APPLICATION_JSON).build();
461
                }
462

    
463
                return Response.status(200).entity(composeDataResponse(claim)).build();
464

    
465
            } catch (SQLStoreException|Exception e) {
466
                return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(compose500Message("Fail to fetch claim " +
467
                        "with id " + claimId + " id.", e)).type(MediaType.APPLICATION_JSON).build();
468
            }
469
        }
470
        return Response.status(Response.Status.FORBIDDEN).entity(compose403Message("Forbidden: You don't have permission to access. You are not registered."))
471
                .type(MediaType.APPLICATION_JSON)
472
                .build();
473
    }
474

    
475
    @GET
476
    @Path("/claims")
477
    @Produces(MediaType.APPLICATION_JSON)
478
    public Response getAllClaims(@DefaultValue("0") @QueryParam("offset") int offset,
479
                                  @DefaultValue("20") @QueryParam("limit") int limit,
480
                                  @DefaultValue("") @QueryParam("keyword") String keyword,
481
                                  @DefaultValue("date") @QueryParam("sortby") String orderby,
482
                                  @DefaultValue("true") @QueryParam("descending") boolean descending,
483
                                  @DefaultValue("") @QueryParam("types") List<String> types,
484
                                 @HeaderParam("X-XSRF-TOKEN") String token,
485
                                 @HeaderParam("Origin") String origin,
486
                                 @CookieParam("AccessToken") String  cookie,
487
                                  @Context HttpServletRequest request) {
488

    
489

    
490
        logger.debug("Header  \"Origin\" has value  " + origin);
491

    
492

    
493

    
494
        if(token == null || token.isEmpty() || cookie == null || cookie.isEmpty() || !cookie.equals(token)){
495
            logger.debug("User is not  authorized - Eroor 403");
496

    
497
            return Response.status(Response.Status.FORBIDDEN).entity(compose403Message("Forbidden: You don't have permission to access. Maybe you are not registered."))
498
                    .type(MediaType.APPLICATION_JSON)
499
                    .build();
500
        }
501

    
502
        if(authorization.isClaimCurator(token)) {
503
            logger.debug("User is authorized ! !");
504
            List<Claim> claims = null;
505

    
506
            int total = -1;
507
            try {
508
                claims = fetchClaimHandler.fetchAllClaims(limit, offset, keyword, orderby, descending, types,false);
509
                total = fetchClaimHandler.countAllClaims(keyword, types);
510

    
511
                return Response.status(200).entity(composeDataResponse(request, claims, total, offset, limit)).build();
512

    
513
            } catch (SQLStoreException|Exception e) {
514
                logger.error("Could not fetch claims.", e);
515
                return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(compose500Message("Fail to fetch claims.", e))
516
                        .type(MediaType.APPLICATION_JSON).build();
517
            }
518
        }
519

    
520
        return Response.status(Response.Status.FORBIDDEN).entity(compose403Message("Forbidden: You don't have permission to access"))
521
               .type(MediaType.APPLICATION_JSON)
522
               .build();
523
    }
524

    
525
    //ARGIRO TODO: Na thn tsekarw
526
//    @POST
527
//    @Path("/claims/{claimId}")
528
//    @Produces(MediaType.APPLICATION_JSON)
529
//    public Response deleteClaim(@PathParam("claimId") String claimId,
530
//                                @QueryParam("token") String token) {
531
//
532
//        if(!JWTValidator.isValid(token)) {
533
//            return Response.status(Response.Status.UNAUTHORIZED).entity(compose404Message("Not valid Token"))
534
//                    .type(MediaType.APPLICATION_JSON)
535
//                    .build();
536
//        }
537
//        try {
538
//
539
//            if (authorization.isRegistered(token) && (authorization.getUserHandler().getMail(token).equals(fetchClaimHandler.fetchClaimById(claimId).getUserMail()))) {
540
//                if (claimId == null || claimId.isEmpty()) {
541
//                    return Response.status(Response.Status.NOT_FOUND).entity(compose404Message("Claim id cannot be empty."))
542
//                            .type(MediaType.APPLICATION_JSON).build();
543
//                }
544
//
545
//                try {
546
//                    if (claimHandler.deleteClaim(claimId)) {
547
//                        return Response.status(204).entity(compose204Message()).type(MediaType.APPLICATION_JSON).build();
548
//                    }
549
//
550
//                } catch (Exception e) {
551
//                    logger.error("Fail to delete claim with id " + claimId + ".", e);
552
//                    return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(compose500Message("Fail to delete claim with id " + claimId + ".", e))
553
//                            .type(MediaType.APPLICATION_JSON).build();
554
//                }
555
//
556
//                return Response.status(Response.Status.NOT_FOUND).entity(compose404Message("Claim id cannot be empty."))
557
//                        .type(MediaType.APPLICATION_JSON).build();
558
//            }
559
//
560
//            return Response.status(Response.Status.FORBIDDEN).entity(compose403Message("Forbidden: You don't have permission to access. Maybe you are not registered."))
561
//                    .type(MediaType.APPLICATION_JSON)
562
//                    .build();
563
//
564
//        } catch (Exception e) {
565
//            logger.error("Could not fetch claims.", e);
566
//            return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(compose500Message("Fail to fetch claims.", e))
567
//                    .type(MediaType.APPLICATION_JSON).build();
568
//        }
569
//    }
570
/*
571

    
572
    @DELETE
573
    @Path("/claims/{claimId}")
574
    public Response deleteClaim(@PathParam("claimId") String claimId) {
575

    
576
        if (claimId == null || claimId.isEmpty()) {
577
            return Response.status(Response.Status.NOT_FOUND).entity(compose404Message("Claim id cannot be empty.")).header("Access-Control-Allow-Origin", "*")
578
                    .header("Access-Control-Allow-Methods", "DELETE")
579
                    .type(MediaType.APPLICATION_JSON).build();
580
        }
581

    
582
        try {
583
            if(claimHandler.deleteClaim(claimId)) {
584
                return Response.status(204).entity(compose204Message()).header("Access-Control-Allow-Origin", "*")
585
                        .header("Access-Control-Allow-Methods", "DELETE").type(MediaType.APPLICATION_JSON).build();
586
            }
587

    
588
        } catch (Exception e) {return Response.status(Response.Status.UNAUTHORIZED).entity(compose404Message("Not valid Token"))
589
                    .type(MediaType.APPLICATION_JSON)
590
                    .build();
591
            logger.error("Fail to delete claim with id " + claimId + ".", e);
592
            return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(compose500Message("Fail to delete claim with id " + claimId +".", e))
593
                    .header("Access-Control-Allow-Origin", "*")
594
                    .header("Access-Control-Allow-Methods", "DELETE")
595
                    .type(MediaType.APPLICATION_JSON).build();
596
        }
597

    
598
        return Response.status(Response.Status.NOT_FOUND).entity(compose404Message("Claim id cannot be empty.")).header("Access-Control-Allow-Origin", "*")
599
                .header("Access-Control-Allow-Methods", "DELETE")
600
                .type(MediaType.APPLICATION_JSON).build();
601
    }
602
 */
603
    @DELETE
604
    @Path("/claims/bulk")
605
    @Produces(MediaType.APPLICATION_JSON)
606
    public Response deleteBulkClaims(@QueryParam("claimId") List<String> claimIds,
607
                                     @HeaderParam("X-XSRF-TOKEN") String token,
608
                                     @HeaderParam("Origin") String origin,
609
                                     @CookieParam("AccessToken") String  cookie){
610

    
611

    
612

    
613
         if(token == null || token.isEmpty() || cookie == null || cookie.isEmpty() || !cookie.equals(token)|| !authorization.hasValidOrigin(origin)){
614
             return Response.status(Response.Status.FORBIDDEN).entity(compose403Message("Forbidden: You don't have permission to access. Maybe you are not registered."))
615
                     .type(MediaType.APPLICATION_JSON)
616
                     .build();
617
         }
618

    
619

    
620
        ArrayList<String> deletedIds= new ArrayList<String>();
621
        ArrayList<String> notFoundIds= new ArrayList<String>();
622

    
623
        if (claimIds == null || claimIds.size() == 0) {
624
            return Response.status(Response.Status.NOT_FOUND).entity(compose404BulkDeleteMessage("Claim ids cannot be empty.",deletedIds,notFoundIds))
625
                    .type(MediaType.APPLICATION_JSON).build();
626
        }
627

    
628
        logger.debug("Trying to delete claims with ids: " + claimIds.toString() + ".");
629
        UserInfo userInfo = authorization.getUserHandler().getUserInfo(token);
630
        for (String claimId : claimIds) {
631
            try {
632

    
633
                if (authorization.isRegistered(userInfo)) {
634
                    if (authorization.isClaimCurator(userInfo) || authorization.isCommunityCurator(userInfo) || userInfo.getEmail().equals(fetchClaimHandler.fetchClaimById(claimId,false).getUserMail())) {
635
                        if (claimHandler.deleteClaim(claimId)) {
636
                            deletedIds.add(claimId);
637
                        } else {
638
                            notFoundIds.add(claimId);
639
                        }
640
                    } else {
641
                        return Response.status(Response.Status.FORBIDDEN).entity(compose403Message("Forbidden: You don't have permission to delete."))
642
                                .type(MediaType.APPLICATION_JSON)
643
                                .build();
644
                    }
645
                } else {
646
                    return Response.status(Response.Status.FORBIDDEN).entity(compose403Message("Forbidden: You don't have permission to access. Maybe you are not registered."))
647
                            .type(MediaType.APPLICATION_JSON)
648
                            .build();
649
                }
650
            } catch (SQLStoreException|Exception e) {
651
                logger.error("Fail to delete claim with id " + claimId + ".", e);
652
                notFoundIds.add(claimId);
653
            }
654
        }
655
        logger.debug("Successfully deleted " + deletedIds.size() + " from " + claimIds.size()  +". Deleted claims with ids: " + deletedIds.toString() + ".");
656
        if (claimIds.size() == notFoundIds.size()) {
657
            return Response.status(Response.Status.NOT_FOUND).entity(compose404BulkDeleteMessage("Claim ids cannot be empty.",deletedIds,notFoundIds))
658
                    .type(MediaType.APPLICATION_JSON).build();
659
        } else if (claimIds.size() == notFoundIds.size()) {
660
            return Response.status(204).entity(compose204BulkDeleteMessage(deletedIds,notFoundIds)).type(MediaType.APPLICATION_JSON).build();
661
        } else {
662
            return Response.status(204).entity(compose204BulkDeleteMessage(deletedIds,notFoundIds)).type(MediaType.APPLICATION_JSON).build();
663
        }
664
    }
665

    
666
    @POST
667
    @Path("/claims")
668
    @Produces(MediaType.APPLICATION_JSON)
669
    @Consumes(MediaType.APPLICATION_JSON)
670
    public Response addClaim(String input, @Context HttpServletRequest request,
671
                             @HeaderParam("X-XSRF-TOKEN") String token,
672
                             @HeaderParam("Origin") String origin,
673
                             @CookieParam("AccessToken") String  cookie) {
674

    
675

    
676
        if(token == null || token.isEmpty() || cookie == null || cookie.isEmpty() || !cookie.equals(token) || !authorization.hasValidOrigin(origin)){
677
            return Response.status(Response.Status.FORBIDDEN).entity(compose403Message("Forbidden: You don't have permission to access. Maybe you are not registered."))
678
                    .type(MediaType.APPLICATION_JSON)
679
                    .build();
680
        }
681

    
682
        UserInfo userInfo = authorization.getUserHandler().getUserInfo(token);
683
        if(authorization.isRegistered(userInfo)) {
684
            JsonObject jsonObject = new JsonParser().parse(input).getAsJsonObject();
685

    
686
            String claimedBy = userInfo.getEmail();
687
            logger.info("claimedBy " + claimedBy);
688

    
689
            String sourceId = jsonObject.get("sourceId").getAsString();
690
            logger.info("sourceId " + sourceId);
691
            String sourceType = jsonObject.get("sourceType").getAsString();
692
            logger.info("sourceType " + sourceType);
693
            String sourceCollectedFrom = jsonObject.get("sourceCollectedFrom").getAsString();
694
            logger.info("sourceCollectedFrom " + sourceCollectedFrom);
695
            String sourceAccessRights = jsonObject.get("sourceAccessRights").getAsString();
696
            logger.info("sourceAccessRights " + sourceAccessRights);
697
            String sourceEmbargoEndDate = jsonObject.get("sourceEmbargoEndDate").getAsString();
698
            sourceEmbargoEndDate = (sourceEmbargoEndDate != null && sourceEmbargoEndDate.equals("")) ? null : sourceEmbargoEndDate;
699
            logger.info("sourceEmbargoEndDate " + sourceEmbargoEndDate);
700

    
701
            String targetId = jsonObject.get("targetId").getAsString();
702
            logger.info("targetId " + targetId);
703
            String targetType = jsonObject.get("targetType").getAsString();
704
            logger.info("targetType " + targetType);
705
            String targetCollectedFrom = jsonObject.get("targetCollectedFrom").getAsString();
706
            logger.info("targetCollectedFrom " + targetCollectedFrom);
707
            String targetAccessRights = jsonObject.get("targetAccessRights").getAsString();
708
            logger.info("targetAccessRights " + targetAccessRights);
709
            String targetEmbargoEndDate = jsonObject.get("targetEmbargoEndDate").getAsString();
710
            targetEmbargoEndDate = (targetEmbargoEndDate != null && targetEmbargoEndDate.equals("")) ? null : targetEmbargoEndDate;
711
            logger.info("targetEmbargoEndDate " + targetEmbargoEndDate);
712

    
713
            EmailValidator emailValidator = EmailValidator.getInstance();
714
            if (!emailValidator.isValid(claimedBy)) {
715
                return Response.status(Response.Status.BAD_REQUEST).entity(compose400Message("User e-mail is invalid."))
716
                        .type(MediaType.APPLICATION_JSON).build();
717
            }
718

    
719

    
720
            try {
721
                String claimId = claimHandler.buildAndInsertClaim(claimedBy, sourceType, sourceId, sourceCollectedFrom, sourceAccessRights, sourceEmbargoEndDate, targetType, targetId, targetCollectedFrom, targetAccessRights, targetEmbargoEndDate);
722
                return Response.status(200).entity(compose201PostMessage(request, claimId)).type(MediaType.APPLICATION_JSON).build();
723

    
724
            } catch (ClaimValidationException ve) {
725
                return Response.status(Response.Status.BAD_REQUEST).entity(compose400Message("The given ids are wrong.", ve))
726
                        .type(MediaType.APPLICATION_JSON).build();
727

    
728
            } catch (SQLStoreException|Exception e) {
729
                logger.error("Fail to add new claim.", e);
730
                return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(compose500Message("Fail to add new claim.", e))
731
                        .type(MediaType.APPLICATION_JSON).build();
732
            }
733
        }
734
        return Response.status(Response.Status.FORBIDDEN).entity(compose403Message("Forbidden: You don't have permission to access."))
735
                .type(MediaType.APPLICATION_JSON)
736
                .build();
737
    }
738

    
739
    @POST
740
    @Path("/claims/bulk")
741
    @Produces(MediaType.APPLICATION_JSON)
742
    @Consumes(MediaType.APPLICATION_JSON)
743
    public Response addBulkClaims(String input, @Context HttpServletRequest request,
744
                                  @HeaderParam("X-XSRF-TOKEN") String token,
745
                                  @HeaderParam("Origin") String origin,
746
                                  @CookieParam("AccessToken") String  cookie) {
747

    
748

    
749
        if(token == null || token.isEmpty() || cookie == null || cookie.isEmpty() || !cookie.equals(token)|| !authorization.hasValidOrigin(origin)){
750
            return Response.status(Response.Status.FORBIDDEN).entity(compose403Message("Forbidden: You don't have permission to access. Maybe you are not registered."))
751
                    .type(MediaType.APPLICATION_JSON)
752
                    .build();
753
        }
754

    
755
        UserInfo userInfo = authorization.getUserHandler().getUserInfo(token);
756
        if(authorization.isRegistered(userInfo)) {
757
            ArrayList<String> insertedIds = new ArrayList<String>();
758
            JsonArray errorInClaims = new JsonArray();
759

    
760
            int code200 = 0;
761
            int code400 = 0;
762
            int code500 = 0;
763

    
764
            JsonArray jsonArray = new JsonParser().parse(input).getAsJsonArray();
765
            for (JsonElement je : jsonArray) {
766
                JsonObject jsonObject = je.getAsJsonObject();
767

    
768
                logger.info("targetId " + jsonObject.toString());
769
                String claimedBy = userInfo.getEmail();
770
                logger.info("claimedBy " + claimedBy);
771

    
772
                String sourceId = jsonObject.get("sourceId").getAsString();
773
                logger.info("sourceId " + sourceId);
774
                String sourceType = jsonObject.get("sourceType").getAsString();
775
                logger.info("sourceType " + sourceType);
776
                String sourceCollectedFrom = jsonObject.get("sourceCollectedFrom").getAsString();
777
                logger.info("sourceCollectedFrom " + sourceCollectedFrom);
778
                String sourceAccessRights = jsonObject.get("sourceAccessRights").getAsString();
779
                logger.info("sourceAccessRights " + sourceAccessRights);
780
                String sourceEmbargoEndDate = jsonObject.get("sourceEmbargoEndDate").getAsString();
781
                sourceEmbargoEndDate = (sourceEmbargoEndDate != null && sourceEmbargoEndDate.equals("")) ? null : sourceEmbargoEndDate;
782
                logger.info("sourceEmbargoEndDate " + sourceEmbargoEndDate);
783

    
784
                String targetId = jsonObject.get("targetId").getAsString();
785
                logger.info("targetId " + targetId);
786
                String targetType = jsonObject.get("targetType").getAsString();
787
                logger.info("targetType " + targetType);
788
                String targetCollectedFrom = jsonObject.get("targetCollectedFrom").getAsString();
789
                logger.info("targetCollectedFrom " + targetCollectedFrom);
790
                String targetAccessRights = jsonObject.get("targetAccessRights").getAsString();
791
                logger.info("targetAccessRights " + targetAccessRights);
792
                String targetEmbargoEndDate = jsonObject.get("targetEmbargoEndDate").getAsString();
793
                targetEmbargoEndDate = (targetEmbargoEndDate != null && targetEmbargoEndDate.equals("")) ? null : targetEmbargoEndDate;
794
                logger.info("targetEmbargoEndDate " + targetEmbargoEndDate);
795

    
796
                EmailValidator emailValidator = EmailValidator.getInstance();
797
                if (!emailValidator.isValid(claimedBy)) {
798
                    jsonObject.addProperty("error", "user");
799
                    //                return Response.status(Response.Status.BAD_REQUEST).entity(compose400Message("User e-mail is invalid."))
800
                    //                        .type(MediaType.APPLICATION_JSON).build();
801
                    code400++;
802
                    errorInClaims.add(jsonObject);
803
                }
804

    
805

    
806
                try {
807
                    String claimId = claimHandler.buildAndInsertClaim(claimedBy, sourceType, sourceId, sourceCollectedFrom, sourceAccessRights, sourceEmbargoEndDate, targetType, targetId, targetCollectedFrom, targetAccessRights, targetEmbargoEndDate);
808
                    insertedIds.add(claimId);
809
                    code200++;
810
                    //                return Response.status(200).entity(compose201PostMessage(request, claimId)).type(MediaType.APPLICATION_JSON).build();
811

    
812
                } catch (ClaimValidationException ve) {
813
                    //                return Response.status(Response.Status.BAD_REQUEST).entity(compose400Message("The given ids are wrong.", ve))
814
                    //                        .type(MediaType.APPLICATION_JSON).build();
815
                    jsonObject.addProperty("error", "validation");
816
                    errorInClaims.add(jsonObject);
817
                    code400++;
818

    
819
                } catch (SQLStoreException|Exception e) {
820
                    //                logger.error("Fail to add new claim.", e);
821
                    //                return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(compose500Message("Fail to add new claim.", e))
822
                    //                        .type(MediaType.APPLICATION_JSON).build();
823
                    jsonObject.addProperty("error", "insertion");
824
                    errorInClaims.add(jsonObject);
825
                    code500++;
826
                }
827
            }
828
            if (jsonArray.size() == code500) {
829
                return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(compose500BulkInsertMessage("Fail to add new claim.", insertedIds, errorInClaims))
830
                        .type(MediaType.APPLICATION_JSON).build();
831
            } else if (code200 > 0) {
832
                return Response.status(200).entity(compose201BulkInsertMessage(insertedIds, errorInClaims)).type(MediaType.APPLICATION_JSON).build();
833
            } else {
834
                return Response.status(Response.Status.BAD_REQUEST).entity(compose400BulkInsertMessage("The given ids are wrong.", insertedIds, errorInClaims))
835
                        .type(MediaType.APPLICATION_JSON).build();
836
            }
837
        }
838
        return Response.status(Response.Status.FORBIDDEN).entity(compose403Message("Forbidden: You don't have permission to access. You are not registered."))
839
                .type(MediaType.APPLICATION_JSON)
840
                .build();
841

    
842
    }
843
    @POST
844
    @Path("/curate/bulk")
845
    @Produces(MediaType.APPLICATION_JSON)
846
    @Consumes(MediaType.APPLICATION_JSON)
847
    public Response curateBulkClaims(String input, @Context HttpServletRequest request,
848
                                     @HeaderParam("X-XSRF-TOKEN") String token,
849
                                     @HeaderParam("Origin") String origin,
850
                                     @CookieParam("AccessToken") String  cookie) {
851

    
852

    
853
        if(token == null || token.isEmpty() || cookie == null || cookie.isEmpty() || !cookie.equals(token)|| !authorization.hasValidOrigin(origin)){
854
            return Response.status(Response.Status.FORBIDDEN).entity(compose403Message("Forbidden: You don't have permission to access. Maybe you are not registered."))
855
                    .type(MediaType.APPLICATION_JSON)
856
                    .build();
857
        }
858
        UserInfo userInfo = authorization.getUserHandler().getUserInfo(token);
859
        if (authorization.isRegistered(userInfo)) {
860
            ArrayList<String> insertedIds = new ArrayList<String>();
861
            JsonArray errorInClaims = new JsonArray();
862
            int code200 = 0;
863
            int code400 = 0;
864
            int code500 = 0;
865
            JsonArray jsonArray = new JsonParser().parse(input).getAsJsonArray();
866
            String curatedBy =userInfo.getEmail();
867

    
868
            for (JsonElement je : jsonArray) {
869
                JsonObject jsonObject = je.getAsJsonObject();
870

    
871
                 String id = jsonObject.get("id").getAsString();
872
                logger.info("id " + id);
873

    
874
                Boolean approved = jsonObject.get("approved").getAsBoolean();
875
                logger.info("approved " + approved);
876
                EmailValidator emailValidator = EmailValidator.getInstance();
877
                if (!emailValidator.isValid(curatedBy)) {
878
                    jsonObject.addProperty("error", "user");
879
                    //                return Response.status(Response.Status.BAD_REQUEST).entity(compose400Message("User e-mail is invalid."))
880
                    //                        .type(MediaType.APPLICATION_JSON).build();
881
                    code400++;
882
                    errorInClaims.add(jsonObject);
883
                }
884

    
885
                try {
886
                    claimHandler.updateClaimCurationInfo(curatedBy,id, approved);
887
                    insertedIds.add(id);
888
                    code200++;
889

    
890
                } catch (SQLStoreException|Exception e) {
891
                    jsonObject.addProperty("error", "insertion");
892
                    errorInClaims.add(jsonObject);
893
                    code500++;
894
                }
895
            }
896
            if (jsonArray.size() == code500) {
897
                return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(compose500BulkInsertMessage("Fail to update claims.", insertedIds, errorInClaims))
898
                        .type(MediaType.APPLICATION_JSON).build();
899
            } else if (code200 > 0) {
900
                return Response.status(200).entity(compose201BulkInsertMessage(insertedIds, errorInClaims)).type(MediaType.APPLICATION_JSON).build();
901
            } else {
902
                return Response.status(Response.Status.BAD_REQUEST).entity(compose400BulkInsertMessage("The given ids are wrong.", insertedIds, errorInClaims))
903
                        .type(MediaType.APPLICATION_JSON).build();
904
            }
905
        }
906
        return Response.status(Response.Status.FORBIDDEN).entity(compose403Message("Forbidden: You don't have permission to access. You are not registered."))
907
                .type(MediaType.APPLICATION_JSON)
908
                .build();
909

    
910
    }
911
    @POST
912
    @Path("/feed/bulk")
913
    @Produces(MediaType.APPLICATION_JSON)
914
    @Consumes(MediaType.APPLICATION_JSON)
915
    public Response feedBulkRecords(String input, @Context HttpServletRequest request,
916
                                    @HeaderParam("X-XSRF-TOKEN") String token,
917
                                    @HeaderParam("Origin") String origin,
918
                                    @CookieParam("AccessToken") String  cookie) {
919

    
920

    
921
        if(token == null || token.isEmpty() || cookie == null || cookie.isEmpty() || !cookie.equals(token)|| !authorization.hasValidOrigin(origin)){
922

    
923
            return Response.status(Response.Status.FORBIDDEN).entity(compose403Message("Forbidden: You don't have permission to access. Maybe you are not registered."))
924
                    .type(MediaType.APPLICATION_JSON)
925
                    .build();
926
        }
927

    
928
        if (authorization.isRegistered(token)) {
929
            ArrayList<String> insertedIds = new ArrayList<String>();
930
            JsonArray errorInClaims = new JsonArray();
931
            int code200 = 0;
932
            int code400 = 0;
933
            int code500 = 0;
934
            JsonArray jsonArray = new JsonParser().parse(input).getAsJsonArray();
935
            for (JsonElement je : jsonArray) {
936
                JsonObject jsonObject = je.getAsJsonObject();
937
                Boolean inserted = directIndexHandler.insertRecord(new Gson().toJson(jsonObject.get("record")));
938
                if (inserted) {
939
                    insertedIds.add(jsonObject.get("id").getAsString());
940
                    code200++;
941
                } else {
942
                    errorInClaims.add(jsonObject.get("id").getAsString());
943
                    code400++;
944
                }
945

    
946

    
947
            }
948
            if (jsonArray.size() == code500) {
949
                return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(compose500BulkInsertMessage("Fail to add new claim.", insertedIds, errorInClaims))
950
                        .type(MediaType.APPLICATION_JSON).build();
951
            } else if (code200 > 0) {
952
                return Response.status(200).entity(compose201BulkInsertMessage(insertedIds, errorInClaims)).type(MediaType.APPLICATION_JSON).build();
953
            } else {
954
                return Response.status(Response.Status.BAD_REQUEST).entity(compose400BulkInsertMessage("The given ids are wrong.", insertedIds, errorInClaims))
955
                        .type(MediaType.APPLICATION_JSON).build();
956
            }
957
        }
958
        return Response.status(Response.Status.FORBIDDEN).entity(compose403Message("Forbidden: You don't have permission to access. You are not registered."))
959
                .type(MediaType.APPLICATION_JSON)
960
                .build();
961

    
962
    }
963

    
964
    @Path("/communities")
965
    @GET
966
    @Produces(MediaType.APPLICATION_JSON)
967
    public Response fetchCommunities(@HeaderParam("X-XSRF-TOKEN") String token,
968
                                     @CookieParam("AccessToken") String  cookie) throws ISLookUpServiceException {
969

    
970

    
971
        if(token == null || token.isEmpty() || cookie == null || cookie.isEmpty() || !cookie.equals(token)){
972
            return Response.status(Response.Status.FORBIDDEN).entity(compose403Message("Forbidden: You don't have permission to access. Maybe you are not registered."))
973
                    .type(MediaType.APPLICATION_JSON)
974
                    .build();
975
        }
976

    
977
        if (authorization.isRegistered(token)) {
978
            //it needs to have at lease one category with @claim=true
979
            List<String> communities = lookupServiceLocator.getService().quickSearchProfile
980
                    ("for $x in collection('/db/DRIVER/ContextDSResources/ContextDSResourceType')  \n" +
981
                            "                where $x//context[@type='community']//category[@claim='true'] \n" +
982
                            "            return <communities>{$x//context/@id}{$x//context/@label}</communities>");
983

    
984
            return Response.status(200).entity(composeDataResponse(xml2Json(communities))).type(MediaType.APPLICATION_JSON).build();
985
        }
986
        return Response.status(Response.Status.FORBIDDEN).entity(compose403Message("Forbidden: You don't have permission to access. You are not registered."))
987
                .type(MediaType.APPLICATION_JSON)
988
                .build();
989
    }
990

    
991
    @Path("/communities/{communityid}/categories")
992
    @GET
993
    @Produces(MediaType.APPLICATION_JSON)
994
    public Response fetchCommunityCategories(@PathParam("communityid") String communityid,
995
                                             @HeaderParam("X-XSRF-TOKEN") String token,
996
                                             @CookieParam("AccessToken") String  cookie) throws ISLookUpServiceException {
997

    
998

    
999
        if(token == null || token.isEmpty() || cookie == null || cookie.isEmpty() || !cookie.equals(token)){
1000
            return Response.status(Response.Status.FORBIDDEN).entity(compose403Message("Forbidden: You don't have permission to access. Maybe you are not registered."))
1001
                    .type(MediaType.APPLICATION_JSON)
1002
                    .build();
1003
        }
1004

    
1005
        if (authorization.isRegistered(token)) {
1006
            List<String> categories = lookupServiceLocator.getService().quickSearchProfile
1007
                    ("for $x in collection('/db/DRIVER/ContextDSResources/ContextDSResourceType')//context[@id='" + communityid + "']//category[@claim=\'true\']\n" +
1008
                            "return <category>{$x/@id}{$x/@label}</category>");
1009

    
1010
            logger.info("for $x in collection('/db/DRIVER/ContextDSResources/ContextDSResourceType')//context[@id='" + communityid + "']//category[@claim=\'true\']\n" +
1011
                    "return <category>{$x/@id}{$x/@label}</category>");
1012

    
1013
            if (categories == null || categories.isEmpty()) {
1014
                return Response.status(404).entity(compose404Message("There are no categories for community with id " + communityid)).type(MediaType.APPLICATION_JSON).build();
1015
            }
1016

    
1017
            xml2Json(categories);
1018

    
1019
            return Response.status(200).entity(composeDataResponse(xml2Json(categories))).type(MediaType.APPLICATION_JSON).build();
1020
        }
1021
        return Response.status(Response.Status.FORBIDDEN).entity(compose403Message("Forbidden: You don't have permission to access. You are not registered."))
1022
                .type(MediaType.APPLICATION_JSON)
1023
                .build();
1024
    }
1025

    
1026
    @Path("/categories/{categoryid}/concepts")
1027
    @GET
1028
    @Produces(MediaType.APPLICATION_JSON)
1029
    public Response fetchCategoryConcepts(@PathParam("categoryid") String categoryid,
1030
                                          @HeaderParam("X-XSRF-TOKEN") String token,
1031
                                          @CookieParam("AccessToken") String  cookie) throws ISLookUpServiceException {
1032

    
1033

    
1034
        if(token == null || token.isEmpty() || cookie == null || cookie.isEmpty() || !cookie.equals(token)){
1035
            return Response.status(Response.Status.FORBIDDEN).entity(compose403Message("Forbidden: You don't have permission to access. Maybe you are not registered."))
1036
                    .type(MediaType.APPLICATION_JSON)
1037
                    .build();
1038
        }
1039

    
1040
        if (authorization.isRegistered(token)) {
1041
            List<String> concepts = lookupServiceLocator.getService().quickSearchProfile
1042
                    ("for $x in collection('/db/DRIVER/ContextDSResources/ContextDSResourceType') return $x//category[@id='" + categoryid + "']//concept");
1043

    
1044
            if (concepts == null || concepts.isEmpty()) {
1045
                return Response.status(404).entity(compose404Message("There are no concepts for category with id " + categoryid)).type(MediaType.APPLICATION_JSON).build();
1046
            }
1047

    
1048
            return Response.status(200).entity(composeDataResponse(xml2Json(concepts))).type(MediaType.APPLICATION_JSON).build();
1049
        }
1050
        return Response.status(Response.Status.FORBIDDEN).entity(compose403Message("Forbidden: You don't have permission to access. You are not registered."))
1051
                .type(MediaType.APPLICATION_JSON)
1052
                .build();
1053
    }
1054

    
1055
    @GET
1056
    @Path("/users/notification")
1057
    @Produces(MediaType.APPLICATION_JSON)
1058
    public Response getUserEmailNotificationPreferences(@QueryParam("communityId") String openaireId,
1059
                                  @HeaderParam("X-XSRF-TOKEN") String token,
1060
                                  @CookieParam("AccessToken") String  cookie,
1061
                                  @Context HttpServletRequest request) {
1062
        
1063
        if(token == null || token.isEmpty() || cookie == null || cookie.isEmpty() || !cookie.equals(token)){
1064
            return Response.status(Response.Status.FORBIDDEN).entity(compose403Message("Forbidden: You don't have permission to access. Maybe you are not registered."))
1065
                    .type(MediaType.APPLICATION_JSON)
1066
                    .build();
1067
        }
1068

    
1069
        UserInfo userInfo = authorization.getUserHandler().getUserInfo(token);
1070
        if(authorization.isRegistered(userInfo)) {
1071
            String userMail = userInfo.getEmail();
1072
            logger.debug("User is registerd "  );
1073

    
1074
            EmailValidator emailValidator = EmailValidator.getInstance();
1075

    
1076
            if (userMail == null || userMail.isEmpty()) {
1077
                return Response.status(Response.Status.BAD_REQUEST).entity(compose400Message("User e-mail cannot be empty."))
1078
                        .type(MediaType.APPLICATION_JSON).build();
1079
            }
1080

    
1081
            if (!emailValidator.isValid(userMail)) {
1082
                return Response.status(Response.Status.BAD_REQUEST).entity(compose400Message("User e-mail is not valid."))
1083
                        .type(MediaType.APPLICATION_JSON).build();
1084
            }
1085

    
1086
            List<Notification> notifications = null;
1087
            try {
1088
                if(openaireId != null) {
1089
                    CommunityUtils communityInfo = CommunityUtils.getCommunityInfo(openaireId);
1090
                    if(communityInfo.getManagers().contains(userMail)) {
1091

    
1092
                        Notification notification = null;
1093
                        logger.debug("About to fetch notification");
1094
                        notification = fetchNotificationHandler.fetchNotification(openaireId, userMail);
1095

    
1096
                        if (notifications == null) {
1097
                            notifications = new ArrayList<Notification>();
1098
                        }
1099
                        if (notification == null) {
1100
                            notification = new Notification(openaireId, communityInfo.getName(), userMail, Integer.parseInt(defaultFrequencyInHours), true);
1101
                        } else {
1102
                            notification.setOpenaireName(communityInfo.getName());
1103
                        }
1104
                        notifications.add(notification);
1105
                    }
1106
                } else {
1107
                    Map<String, String> projectIdsAndNames = fetchProjectHandler.fetchProjectIdsAndNamesByProjectManagerMail(userMail);
1108
                    if(projectIdsAndNames != null) {
1109
                        for (Map.Entry<String, String> projectIdAndName : projectIdsAndNames.entrySet()) {
1110
                            Notification notification = null;
1111
                            logger.debug("About to fetch notification");
1112
                            notification = fetchNotificationHandler.fetchNotification(projectIdAndName.getKey(), userMail);
1113

    
1114
                            if (notifications == null) {
1115
                                notifications = new ArrayList<Notification>();
1116
                            }
1117
                            if(notification == null) {
1118
                                notification = new Notification(projectIdAndName.getKey(), projectIdAndName.getValue(), userMail, Integer.parseInt(defaultFrequencyInHours), true);
1119
                            } else {
1120
                                notification.setOpenaireName(projectIdAndName.getValue());
1121
                            }
1122
                            notifications.add(notification);
1123
                            logger.debug(notification);
1124
                            logger.debug("notification openaireId:"+notification.getOpenaireId());
1125
                            logger.debug(notifications.size());
1126
                        }
1127
                    }
1128
                }
1129

    
1130
            } catch (SQLStoreException|Exception e) {
1131
                logger.error("Could not fetch notification preferences for user with mail " + userMail, e);
1132
                return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(compose500Message("Fail to fetch notification preferences" +
1133
                        " for user with e-mail " + userMail + ".", e)).type(MediaType.APPLICATION_JSON).build();
1134
            }
1135

    
1136
            if (notifications == null || notifications.isEmpty()) {
1137
                return Response.status(204).entity(compose204Message("There are no notifications for user with mail " + userMail)).type(MediaType.APPLICATION_JSON).build();
1138
            }
1139

    
1140
            return Response.status(200).entity(composeDataResponse(notifications)).build();
1141

    
1142
        }
1143
        logger.debug("User is *NOT* registerd "  );
1144
        return Response.status(Response.Status.FORBIDDEN).entity(compose403Message("Forbidden: You don't have permission to access. You are not registered."))
1145
                .type(MediaType.APPLICATION_JSON)
1146
                .build();
1147
    }
1148

    
1149
    @POST
1150
    @Path("/users/notification/save")
1151
    @Produces(MediaType.APPLICATION_JSON)
1152
    @Consumes(MediaType.APPLICATION_JSON)
1153
    public Response saveOrUpdateUserEmailNotificationPreferences(String input, @Context HttpServletRequest request,
1154
                                     @HeaderParam("X-XSRF-TOKEN") String token,
1155
                                     @HeaderParam("Origin") String origin,
1156
                                     @CookieParam("AccessToken") String  cookie) {
1157

    
1158

    
1159
        if(token == null || token.isEmpty() || cookie == null || cookie.isEmpty() || !cookie.equals(token)|| !authorization.hasValidOrigin(origin)){
1160
            return Response.status(Response.Status.FORBIDDEN).entity(compose403Message("Forbidden: You don't have permission to access. Maybe you are not registered."))
1161
                    .type(MediaType.APPLICATION_JSON)
1162
                    .build();
1163
        }
1164
        UserInfo userInfo = authorization.getUserHandler().getUserInfo(token);
1165
        if (authorization.isRegistered(userInfo)) {
1166
            ArrayList<String> insertedIds = new ArrayList<String>();
1167
            JsonArray errorInClaims = new JsonArray();
1168
            int code200 = 0;
1169
            int code400 = 0;
1170
            int code500 = 0;
1171
            JsonObject jsonObject = new JsonParser().parse(input).getAsJsonObject();
1172

    
1173
            String userMail =userInfo.getEmail();
1174

    
1175
            String openaireId = jsonObject.get("openaireId").getAsString();
1176
            logger.info("openaireId " + openaireId);
1177

    
1178
            boolean notify = jsonObject.get("notify").getAsBoolean();
1179
            logger.info("notify "+notify);
1180

    
1181
            int frequency = jsonObject.get("frequency").getAsInt();
1182
            logger.info("frequency " + frequency);
1183

    
1184
            EmailValidator emailValidator = EmailValidator.getInstance();
1185
            if (!emailValidator.isValid(userMail)) {
1186
                jsonObject.addProperty("error", "user");
1187
                                return Response.status(Response.Status.BAD_REQUEST).entity(compose400Message("User e-mail is invalid."))
1188
                                        .type(MediaType.APPLICATION_JSON).build();
1189
            }
1190

    
1191
            try {
1192
                boolean continueProcedure = false;
1193
                List<String> managers = null;
1194
                try {
1195
                    managers = fetchProjectHandler.fetchContactEmailsByProjectId(openaireId);
1196
                } catch (Exception e) {
1197
                    e.printStackTrace();
1198
                } catch (SQLStoreException e) {
1199
                    e.printStackTrace();
1200
                }
1201
                if(managers != null && managers.contains(userMail)) {
1202
                    continueProcedure = true;
1203
                } else {
1204
                    CommunityUtils communityInfo = CommunityUtils.getCommunityInfo(openaireId);
1205
                    if(communityInfo.getManagers().contains(userMail)) {
1206
                        continueProcedure = true;
1207
                    }
1208
                }
1209

    
1210
                if(continueProcedure) {
1211
                    Notification notification = null;
1212
                    logger.debug("About to fetch notification");
1213
                    notification = fetchNotificationHandler.fetchNotification(openaireId, userMail);
1214

    
1215
                    if (notification == null) {
1216
                        logger.debug("About to insert notification");
1217
                        notificationHandler.buildAndInsertNotification(openaireId, userMail, frequency, notify);
1218
                    } else {
1219
                        logger.debug("About to update notification");
1220
                        notificationHandler.updateNotificationPreferences(openaireId, userMail, frequency, notify);
1221
                    }
1222
                } else {
1223
                    Response.status(Response.Status.FORBIDDEN).entity(compose403Message("Forbidden: You don't have permission to access. You are not registered."))
1224
                            .type(MediaType.APPLICATION_JSON)
1225
                            .build();
1226
                }
1227
            } catch (SQLStoreException|Exception e) {
1228
                logger.error("Could not save or update notification preferences for user with mail " + userMail, e);
1229
                return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(compose500Message("Fail to fetch notification preferences" +
1230
                        " for user with e-mail " + userMail + ".", e)).type(MediaType.APPLICATION_JSON).build();
1231
            }
1232

    
1233
            return Response.status(200).entity(compose204Message("Save or Update for notification successful")).type(MediaType.APPLICATION_JSON).build();
1234
        }
1235
        return Response.status(Response.Status.FORBIDDEN).entity(compose403Message("Forbidden: You don't have permission to access. You are not registered."))
1236
                .type(MediaType.APPLICATION_JSON)
1237
                .build();
1238
    }
1239

    
1240
    private String xml2Json(List<String> input) {
1241
        StringBuilder builder = new StringBuilder();
1242
        for(String category: input) {
1243
            builder.append(category);
1244
        }
1245
        return builder.toString();
1246
    }
1247

    
1248
    private String compose204Message(String message) {
1249
        return  "{ \"status\" : \"error\", \"code\" : \"204\", \"message\" : \"  " + message +" \" }";
1250
    }
1251

    
1252
    private String compose400Message(String message) {
1253
        return  "{ \"status\" : \"error\", \"code\" : \"400\", \"message\" : \"  " + message +" \" }";
1254
    }
1255

    
1256
    private String compose400Message(String message, Exception exception) {
1257
        return  "{ \"status\" : \"error\", \"code\" : \"400\", \"message\" : \"  " + message +"\", " +
1258
                "\"description\" : \""+  exception.getMessage() +"\" }";
1259
    }
1260

    
1261
    private String compose403Message(String message) {
1262
        return  "{ \"status\" : \"error\", \"code\" : \"403\", \"message\" : \"  " + message +"\", " +
1263
                "\"description\" : \"\" }";
1264
    }
1265

    
1266
    private String compose404BulkDeleteMessage(String message, List<String> deletedIds, List<String> notFoundIds) {
1267
        return  "{ \"status\" : \"error\", \"code\" : \"404\", \"message\" : \"  " + message +" \","+  "\"deletedIds\" : " + new Gson().toJson(deletedIds) +","+  "\"notFoundIds\" : " + new Gson().toJson(notFoundIds) + " }";
1268
    }
1269
    private String compose404Message(String message) {
1270
        return  "{ \"status\" : \"error\", \"code\" : \"404\", \"message\" : \"  " + message +" \" }";
1271
    }
1272

    
1273
    private String compose400BulkInsertMessage(String message, List<String> insertedIds,JsonArray errorInClaims) {
1274
        return  "{ \"status\" : \"error\", \"code\" : \"400\", \"message\" : \"  " + message +" \""+  ", \"insertedIds\" : " + new Gson().toJson(insertedIds) +","+  "\"errorInClaims\" : " + new Gson().toJson(errorInClaims) + " }";
1275
    }
1276

    
1277
    private String compose500Message(String message, Throwable throwable) {
1278
        return  "{ \"status\" : \"fail\", \"code\" : \"500\", \"message\" : \"  " + message + "\", " +
1279
                "\"description\" : \""+  throwable.getMessage() +"\" }";
1280
    }
1281

    
1282
    private String compose500BulkInsertMessage(String message,  List<String> insertedIds, JsonArray errorInClaims) {
1283
        return  "{ \"status\" : \"fail\", \"code\" : \"500\", \"message\" : \"  " + message + "\", " +
1284
                "\"description\" : \""+   "\" , " +  "\"insertedIds\" : " + new Gson().toJson(insertedIds) +","+  "\"errorInClaims\" : " + new Gson().toJson(errorInClaims) + " }";
1285
    }
1286

    
1287
    /*
1288
    TODO check if needed
1289
    private String compose500BulkDeleteMessage(String message,  List<String> deletedIds, List<String> notFoundIds, Exception exception) {
1290
        return  "{ \"status\" : \"fail\", \"code\" : \"500\", \"message\" : \"  " + message + "\", " +
1291
                "\"description\" : \""+  exception.getMessage() +"\" ," +  "\"deletedIds\" : " + new Gson().toJson(deletedIds) +","+  "\"notFoundIds\" : " + new Gson().toJson(notFoundIds) + " }";
1292
    }
1293

    
1294
    TODO check if needed
1295
    private  String compose200Message(){
1296
        return " { \"status\" : \"success\", \"code\": \"200\" }";
1297
    }
1298

    
1299
    TODO check if needed
1300
    private  String compose204Message(){
1301
        return " { \"status\" : \"success\", \"code\": \"204\" }";
1302
    } */
1303

    
1304
    private  String compose204BulkDeleteMessage(List<String> deletedIds, List<String> notFoundIds){
1305
        return " { \"status\" : \"success\", \"code\": \"204\", "+ "\"deletedIds\" : " + new Gson().toJson(deletedIds) +","+  "\"notFoundIds\" : " + new Gson().toJson(notFoundIds) + "}";
1306
    }
1307

    
1308

    
1309
    private  String compose201PostMessage(@Context HttpServletRequest request, String claimId){
1310
        String url = request.getRequestURL().toString();
1311
        //String query = request.getQueryString();
1312

    
1313
        return " { \"status\" : \"success\", \"code\": \"201\", \"link\": \"" + url  +"/"+ claimId +"\" }";
1314
    }
1315
    private  String compose201BulkInsertMessage(List<String> insertedIds, JsonArray errorInClaims){
1316
        //String url = request.getRequestURL().toString();
1317
        //String query = request.getQueryString();
1318

    
1319
        return " { \"status\" : \"success\", \"code\": \"201\","+ "\"insertedIds\" : " + new Gson().toJson(insertedIds) +","+  "\"errorInClaims\" : " + new Gson().toJson(errorInClaims)+ "}";
1320
    }
1321
    private String composeDataResponse(HttpServletRequest request, List<Claim> claims, int total, int offset, int limit) {
1322
        if(offset != -1 && limit != -1) {   // if offset and limit are -1, no paging in the response
1323
            return " { \"status\" : \"success\", \"code\": \"200\",  "+composeTotalResults(total)+", " + composePaging(request, total, offset, limit) + ", "
1324
                    + "\"data\" : " + new Gson().toJson(claims) + " }";
1325
        } else {
1326
            return " { \"status\" : \"success\", \"code\": \"200\",  "+composeTotalResults(total)+", "
1327
                    + "\"data\" : " + new Gson().toJson(claims) + " }";
1328
        }
1329
    }
1330

    
1331
    private String composeDataResponse(String xml) {
1332
        return " { \"status\" : \"success\", \"code\": \"200\", "
1333
                + "\"data\" : " + XML.toJSONObject(xml).toString() + " }";
1334
    }
1335

    
1336
    private String composeDataResponse(Claim claim) {
1337
        return " { \"status\" : \"success\", \"code\": \"200\", " + "\"data\" : " + new Gson().toJson(claim) + " }";
1338
    }
1339

    
1340
    private String composeDataResponse(List<Notification> notifications) {
1341
        return " { \"status\" : \"success\", \"code\": \"200\", " + "\"data\" : " + new Gson().toJson(notifications) + " }";
1342
    }
1343

    
1344
    private static String composePaging(HttpServletRequest request, int total, int currentOffset, int limit) {
1345
        logger.info("total " + total);
1346
        logger.info("currentOffset " + currentOffset);
1347
        logger.info("limit " + limit);
1348

    
1349
        String url = request.getRequestURL().toString();
1350
        //String query = request.getQueryString();
1351

    
1352
        String first = url+"?offset=0&limit=20";
1353

    
1354
        int previousPage;
1355
        int nextPage;
1356
        int lastPage;
1357

    
1358
        if (total <= limit) {
1359
            lastPage = 0;
1360
        } else {
1361
            if(total%limit == 0) {
1362
                lastPage = total/limit-1;
1363
            } else {
1364
                lastPage = total/limit ;
1365
            }
1366
        }
1367
        String last = url+"?offset=" + lastPage + "&limit=20";
1368

    
1369
        if (currentOffset-1 <= 0) {
1370
            previousPage = 0;
1371
        } else {
1372
            previousPage = currentOffset-1;
1373
        }
1374
        String previous = url+"?offset=" + previousPage + "&limit=20";
1375

    
1376
        if (currentOffset+1 >= lastPage) {
1377
            nextPage = lastPage;
1378

    
1379
        } else {
1380
            nextPage = currentOffset + 1;
1381
        }
1382
        String next = url+"?offset=" + nextPage + "&limit=20";
1383

    
1384
        return "\"paging\": [" +  "{\"rel\":\"first\", \"href\":\"" + first +"\"}, {\"rel\":\"last\", \"href\":\"" + last + "\"}, {\"rel\":\"previous\", \"href\": \"" + previous + "\"}, {\"rel\":\"next\", \"href\":\"" +  next +"\"}"+ "]";
1385
    }
1386
    private String composeTotalResults(int total) {
1387
        return "\"total\": \""+total+"\"";
1388
    }
1389

    
1390
     public static void main(String[] args) {
1391

    
1392
/*
1393
         EmailValidator emailValidator = EmailValidator.getInstance();
1394
         System.out.println(emailValidator.isValid("jate@gdddd"));
1395

    
1396
         InternetAddress emailAddr = null;
1397
         try {
1398
             emailAddr = new InternetAddress("jate@gdddd");
1399
             emailAddr.validate();
1400
         } catch (AddressException e) {
1401
             System.out.println("false");
1402
         }
1403
         System.out.println("true");
1404

    
1405
         ArrayList<String> insertedIds= new ArrayList<String>();
1406
         insertedIds.add("1");
1407
         insertedIds.add("2");
1408
         insertedIds.add("3");
1409
         System.out.println(helloWorldService.compose403Message("hello"));
1410

    
1411
*/
1412
//         BasicConfigurator.configure();
1413
//         ApplicationContext context = new ClassPathXmlApplicationContext("eu/dnetlib/openaire/rest/springContext-claims-authorization.xml");
1414
//
1415
//         Authorization authorization =  context.getBean(Authorization.class);
1416
//         UserHandler userHandler = context.getBean(UserHandler.class);
1417
//         System.out.println(authorization.getAdminRoles());
1418
//         authorization.isClaimCurator("eyJraWQiOiJvaWRjIiwiYWxnIjoiUlMyNTYifQ.eyJzdWIiOiIwOTMxNzMwMTMyODMzNjMyQG9wZW5taW50ZWQuZXUiLCJhenAiOiIyNGU4MzE3Ni0xMzEyLTRiYTMtYmMwYi1mZmVlYmVhMTYwM2UiLCJpc3MiOiJodHRwczpcL1wvYWFpLm9wZW5taW50ZWQuZXVcL29pZGNcLyIsImV4cCI6MTQ5ODQ4NTk3NiwiaWF0IjoxNDk4NDcxNTc2LCJqdGkiOiJkMWRlZjc1Yi00MTEyLTRiZDktYTIyNi0wZThhOWI2M2Y3MWQifQ.WVYOb_yO8OaxIIt2jRYEDQBhGGFRDTBw3DgtVV_smuN5yx1ScCj6aehLu3JKPSArme4m2SGF4TEGhpwNJkwhM2WapGtxmtuCmCzYIo_QlC1Yki9hr2OT2rXMcQsJCiKaBSf6pLue6Sn78GMB5yaUTvOQHRgidXGiZXH5lsuZUx15Q6Equ_wzond_rgP9mRheRkTyIFuvvg4PuzmudBc11Ty863vIIQtoWF7_p98zTbHxiNF9lLPwzPZKxDoQ8JeayQEC-jsWVLgxmp-h0jG_Ko5jFVVJeeosqMMucOrs2FT_NKHVYVqB6VVh0C6nOufeiLrNDeMUlDT4dAvKD2zE9w");
1419

    
1420
    }
1421

    
1422
    @GET
1423
    @Path("email-notifications")
1424
    //@Produces(MediaType.APPLICATION_JSON)
1425
    public void forceSendEmailNotifications() {
1426
        emailSender.run();
1427
    }
1428

    
1429
}
(2-2/3)