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 gr.uoa.di.driver.util.ServiceLocator;
|
14
|
import net.sf.ehcache.search.expression.Not;
|
15
|
import org.apache.commons.validator.EmailValidator;
|
16
|
import org.apache.log4j.Logger;
|
17
|
import org.json.XML;
|
18
|
import org.springframework.beans.factory.annotation.Autowired;
|
19
|
import org.springframework.stereotype.Component;
|
20
|
|
21
|
import javax.annotation.Resource;
|
22
|
import javax.servlet.http.HttpServletRequest;
|
23
|
import javax.ws.rs.*;
|
24
|
import javax.ws.rs.core.Context;
|
25
|
import javax.ws.rs.core.MediaType;
|
26
|
import javax.ws.rs.core.Response;
|
27
|
import java.util.ArrayList;
|
28
|
import java.util.HashMap;
|
29
|
import java.util.List;
|
30
|
import java.util.Map;
|
31
|
|
32
|
/**
|
33
|
* Created by kiatrop on 15/4/2016.
|
34
|
*/
|
35
|
@Component
|
36
|
@Path("/claimsService")
|
37
|
public class HelloWorldService {
|
38
|
|
39
|
private static final Logger logger = Logger.getLogger(HelloWorldService.class);
|
40
|
|
41
|
@Autowired
|
42
|
private FetchClaimHandler fetchClaimHandler = null;
|
43
|
|
44
|
@Autowired
|
45
|
private FetchProjectHandler fetchProjectHandler= null;
|
46
|
|
47
|
@Autowired
|
48
|
private FetchNotificationHandler fetchNotificationHandler = null;
|
49
|
|
50
|
@Autowired
|
51
|
private NotificationHandler notificationHandler = null;
|
52
|
|
53
|
@Resource
|
54
|
private ServiceLocator<ISLookUpService> lookupServiceLocator = null;
|
55
|
|
56
|
@Autowired
|
57
|
private ClaimHandler claimHandler = null;
|
58
|
|
59
|
@Autowired
|
60
|
private DirectIndexHandler directIndexHandler = null;
|
61
|
|
62
|
@Autowired
|
63
|
public Authorization authorization = null;
|
64
|
|
65
|
@Autowired
|
66
|
private String defaultFrequencyInHours;
|
67
|
|
68
|
@GET
|
69
|
@Path("projects/{projectId}/claims")
|
70
|
@Produces(MediaType.APPLICATION_JSON)
|
71
|
public Response getProjectClaims(@PathParam("projectId") String projectId,
|
72
|
@DefaultValue("0") @QueryParam("offset") int offset,
|
73
|
@DefaultValue("20") @QueryParam("limit") int limit,
|
74
|
@DefaultValue("") @QueryParam("keyword") String keyword,
|
75
|
@DefaultValue("") @QueryParam("sortby") String orderby,
|
76
|
@DefaultValue("true") @QueryParam("descending") boolean descending,
|
77
|
@DefaultValue("") @QueryParam("types") List<String> types,
|
78
|
@HeaderParam("X-XSRF-TOKEN") String token,
|
79
|
@CookieParam("AccessToken") String cookie,
|
80
|
@Context HttpServletRequest request) {
|
81
|
|
82
|
if(token == null || token.isEmpty() || cookie == null || cookie.isEmpty() || !cookie.equals(token)){
|
83
|
authorization.logStatus(token,cookie);
|
84
|
return Response.status(Response.Status.FORBIDDEN).entity(compose403Message("Forbidden: You don't have permission to access. Maybe you are not registered."))
|
85
|
.type(MediaType.APPLICATION_JSON)
|
86
|
.build();
|
87
|
}
|
88
|
|
89
|
if(authorization.isClaimCurator(token)) {
|
90
|
|
91
|
int total = -1;
|
92
|
|
93
|
if (projectId == null || projectId.isEmpty()) {
|
94
|
return Response.status(Response.Status.BAD_REQUEST).entity(compose400Message("Project id cannot be empty."))
|
95
|
.type(MediaType.APPLICATION_JSON).build();
|
96
|
}
|
97
|
|
98
|
List<Claim> claims = null;
|
99
|
try {
|
100
|
claims = fetchClaimHandler.fetchClaimsByProject(projectId, limit, offset, keyword, orderby, descending, types,false);
|
101
|
total = fetchClaimHandler.countClaimsByProject(projectId, keyword, types);
|
102
|
|
103
|
} catch (SQLStoreException|Exception e) { //TODO check this with exception
|
104
|
logger.error("Could not fetch claims for project with id " + projectId, e);
|
105
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(compose500Message("Fail to fetch claims" +
|
106
|
" for projects with id " + projectId + ".", e)).type(MediaType.APPLICATION_JSON).build();
|
107
|
|
108
|
}
|
109
|
|
110
|
return Response.status(200).entity(composeDataResponse(request, claims, total, offset, limit)).build();
|
111
|
}
|
112
|
|
113
|
return Response.status(Response.Status.FORBIDDEN).entity(compose403Message("Forbidden: You don't have permission to access"))
|
114
|
.type(MediaType.APPLICATION_JSON)
|
115
|
.build();
|
116
|
}
|
117
|
|
118
|
@GET
|
119
|
@Path("projects/{projectId}/all_claims")
|
120
|
@Produces(MediaType.APPLICATION_JSON)
|
121
|
public Response getAllProjectClaims(@PathParam("projectId") String projectId,
|
122
|
@DefaultValue("-1") @QueryParam("offset") int offset,
|
123
|
@DefaultValue("-1") @QueryParam("limit") int limit,
|
124
|
@DefaultValue("") @QueryParam("keyword") String keyword,
|
125
|
@DefaultValue("") @QueryParam("sortby") String orderby,
|
126
|
@DefaultValue("true") @QueryParam("descending") boolean descending,
|
127
|
@DefaultValue("") @QueryParam("types") List<String> types,
|
128
|
@HeaderParam("X-XSRF-TOKEN") String token,
|
129
|
@CookieParam("AccessToken") String cookie,
|
130
|
@Context HttpServletRequest request) {
|
131
|
|
132
|
if(token == null || token.isEmpty() || cookie == null || cookie.isEmpty() || !cookie.equals(token)){
|
133
|
authorization.logStatus(token,cookie);
|
134
|
return Response.status(Response.Status.FORBIDDEN).entity(compose403Message("Forbidden: You don't have permission to access. Maybe you are not registered."))
|
135
|
.type(MediaType.APPLICATION_JSON)
|
136
|
.build();
|
137
|
}
|
138
|
|
139
|
UserInfo userInfo = authorization.getUserHandler().getUserInfo(token);
|
140
|
// if(authorization.isProjectCurator(userInfo)) {
|
141
|
String userMail = userInfo.getEmail();
|
142
|
|
143
|
int total = -1;
|
144
|
|
145
|
if (projectId == null || projectId.isEmpty()) {
|
146
|
return Response.status(Response.Status.BAD_REQUEST).entity(compose400Message("Project id cannot be empty."))
|
147
|
.type(MediaType.APPLICATION_JSON).build();
|
148
|
}
|
149
|
|
150
|
List<Claim> claims = null;
|
151
|
try {
|
152
|
boolean forbidden = true;
|
153
|
if(authorization.isProjectCurator(userInfo)) {
|
154
|
forbidden = false;
|
155
|
} else {
|
156
|
List<String> contact_emails = fetchProjectHandler.fetchContactEmailsByProjectId(projectId);
|
157
|
logger.debug(contact_emails);
|
158
|
if(contact_emails != null && contact_emails.contains(userMail)) {
|
159
|
forbidden = false;
|
160
|
}
|
161
|
}
|
162
|
|
163
|
if(forbidden){
|
164
|
return Response.status(Response.Status.FORBIDDEN).entity(compose403Message("Forbidden: You don't have permission to access"))
|
165
|
.type(MediaType.APPLICATION_JSON)
|
166
|
.build();
|
167
|
}else{
|
168
|
if(offset == -1 && limit == -1) {
|
169
|
// when offset and limit are -1 fetch claims with null values, to ignore paging and limit clause
|
170
|
claims = fetchClaimHandler.fetchClaimsByProject(projectId, null, null, keyword, orderby, descending, types, true);
|
171
|
} else {
|
172
|
claims = fetchClaimHandler.fetchClaimsByProject(projectId, limit, offset, keyword, orderby, descending, types, true);
|
173
|
}
|
174
|
total = fetchClaimHandler.countClaimsByProject(projectId, keyword, types);
|
175
|
|
176
|
return Response.status(200).entity(composeDataResponse(request, claims, total, offset, limit)).build();
|
177
|
}
|
178
|
|
179
|
} catch (SQLStoreException|Exception e) {
|
180
|
logger.error("Could not fetch claims for project with id " + projectId, e);
|
181
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(compose500Message("Fail to fetch claims" +
|
182
|
" for projects with id " + projectId + ".", e)).type(MediaType.APPLICATION_JSON).build();
|
183
|
}
|
184
|
}
|
185
|
/*
|
186
|
@GET
|
187
|
@Path("project/claims")
|
188
|
@Produces(MediaType.APPLICATION_JSON)
|
189
|
public Response getProjectClaimsByToken(@QueryParam("projectToken") String projectToken,
|
190
|
@DefaultValue("-1") @QueryParam("offset") int offset,
|
191
|
@DefaultValue("-1") @QueryParam("limit") int limit,
|
192
|
@DefaultValue("") @QueryParam("keyword") String keyword,
|
193
|
@DefaultValue("") @QueryParam("sortby") String orderby,
|
194
|
@DefaultValue("true") @QueryParam("descending") boolean descending,
|
195
|
@DefaultValue("") @QueryParam("types") List<String> types,
|
196
|
@HeaderParam("X-XSRF-TOKEN") String token,
|
197
|
@CookieParam("AccessToken") String cookie,
|
198
|
@Context HttpServletRequest request) {
|
199
|
|
200
|
|
201
|
if(token == null || token.isEmpty() || cookie == null || cookie.isEmpty() || !cookie.equals(token)){
|
202
|
authorization.logStatus(token,cookie);
|
203
|
return Response.status(Response.Status.FORBIDDEN).entity(compose403Message("Forbidden: You don't have permission to access. Maybe you are not registered."))
|
204
|
.type(MediaType.APPLICATION_JSON)
|
205
|
.build();
|
206
|
}
|
207
|
|
208
|
UserInfo userInfo = authorization.getUserHandler().getUserInfo(token);
|
209
|
// if(authorization.isProjectCurator(userInfo)) {
|
210
|
String userMail = userInfo.getEmail();
|
211
|
|
212
|
int total = -1;
|
213
|
|
214
|
if (projectToken == null || projectToken.isEmpty()) {
|
215
|
return Response.status(Response.Status.BAD_REQUEST).entity(compose400Message("Project token cannot be empty."))
|
216
|
.type(MediaType.APPLICATION_JSON).build();
|
217
|
}
|
218
|
|
219
|
List<Claim> claims = null;
|
220
|
try {
|
221
|
String projectId = null;
|
222
|
if(authorization.isProjectCurator(userInfo)) {
|
223
|
projectId = fetchProjectHandler.fetchProjectIdByToken(projectToken);
|
224
|
} else {
|
225
|
projectId = fetchProjectHandler.fetchProjectIdByTokenAndEmail(projectToken, userMail);
|
226
|
}
|
227
|
//String projectId = fetchProjectHandler.fetchProjectIdByToken(projectToken,userMail);
|
228
|
if(projectId == null){
|
229
|
return Response.status(Response.Status.FORBIDDEN).entity(compose403Message("Forbidden: You don't have permission to access"))
|
230
|
.type(MediaType.APPLICATION_JSON)
|
231
|
.build();
|
232
|
}else{
|
233
|
if(offset == -1 && limit == -1) {
|
234
|
// when offset and limit are -1 fetch claims with null values, to ignore paging and limit clause
|
235
|
claims = fetchClaimHandler.fetchClaimsByProject(projectId, null, null, keyword, orderby, descending, types, true);
|
236
|
} else {
|
237
|
claims = fetchClaimHandler.fetchClaimsByProject(projectId, limit, offset, keyword, orderby, descending, types, true);
|
238
|
}
|
239
|
total = fetchClaimHandler.countClaimsByProject(projectId, keyword, types);
|
240
|
|
241
|
return Response.status(200).entity(composeDataResponse(request, claims, total, offset, limit)).build();
|
242
|
}
|
243
|
|
244
|
} catch (SQLStoreException|Exception e) {
|
245
|
logger.error("Could not fetch claims for project token " + projectToken, e);
|
246
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(compose500Message("Fail to fetch claims" +
|
247
|
" for projects with token " + projectToken + ".", e)).type(MediaType.APPLICATION_JSON).build();
|
248
|
}
|
249
|
// }
|
250
|
|
251
|
// return Response.status(Response.Status.FORBIDDEN).entity(compose403Message("Forbidden: You don't have permission to access"))
|
252
|
// .type(MediaType.APPLICATION_JSON)
|
253
|
// .build();
|
254
|
}
|
255
|
*/
|
256
|
|
257
|
@GET
|
258
|
@Path("/contexts/{contextId}/claims")
|
259
|
@Produces(MediaType.APPLICATION_JSON)
|
260
|
public Response getContextClaims(@PathParam("contextId") String contextId,
|
261
|
@DefaultValue("0") @QueryParam("offset") int offset,
|
262
|
@DefaultValue("20") @QueryParam("limit") int limit,
|
263
|
@DefaultValue("") @QueryParam("keyword") String keyword,
|
264
|
@DefaultValue("") @QueryParam("sortby") String orderby,
|
265
|
@DefaultValue("true") @QueryParam("descending") boolean descending,
|
266
|
@DefaultValue("") @QueryParam("types") List<String> types,
|
267
|
@HeaderParam("X-XSRF-TOKEN") String token,
|
268
|
@CookieParam("AccessToken") String cookie,
|
269
|
@Context HttpServletRequest request) {
|
270
|
// @RequestParam(value="includeStates[]", defaultValue="1,2,3") String[] includeStates
|
271
|
|
272
|
if(token == null || token.isEmpty() || cookie == null || cookie.isEmpty() || !cookie.equals(token)){
|
273
|
authorization.logStatus(token,cookie);
|
274
|
return Response.status(Response.Status.FORBIDDEN).entity(compose403Message("Forbidden: You don't have permission to access. Maybe you are not registered."))
|
275
|
.type(MediaType.APPLICATION_JSON)
|
276
|
.build();
|
277
|
}
|
278
|
// logger.debug("Calling API for context with token " + token);
|
279
|
|
280
|
if(authorization.isCommunityCurator(token) || authorization.isClaimCurator(token)) {
|
281
|
|
282
|
int total = -1;
|
283
|
if (contextId == null || contextId.isEmpty()) {
|
284
|
return Response.status(Response.Status.BAD_REQUEST).entity(compose400Message("Context id cannot be empty."))
|
285
|
.type(MediaType.APPLICATION_JSON).build();
|
286
|
}
|
287
|
logger.debug("Types are " + types);
|
288
|
List<Claim> claims = null;
|
289
|
|
290
|
try {
|
291
|
claims = fetchClaimHandler.fetchClaimsByContext(contextId, limit, offset, keyword, orderby, descending, types,false);
|
292
|
total = fetchClaimHandler.countClaimsByContext(contextId, keyword, types);
|
293
|
|
294
|
} catch (SQLStoreException|Exception e) {
|
295
|
logger.error("Could not fetch claims for context with id " + contextId, e);
|
296
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(compose500Message("Fail to fetch claims" +
|
297
|
" for context with id " + contextId + ".", e)).type(MediaType.APPLICATION_JSON).build();
|
298
|
}
|
299
|
|
300
|
return Response.status(200).entity(composeDataResponse(request, claims, total, offset, limit)).build();
|
301
|
}
|
302
|
|
303
|
return Response.status(Response.Status.FORBIDDEN).entity(compose403Message("Forbidden: You don't have permission to access"))
|
304
|
.type(MediaType.APPLICATION_JSON)
|
305
|
.build();
|
306
|
}
|
307
|
|
308
|
|
309
|
@GET
|
310
|
@Path("/results/{resultId}/claims")
|
311
|
@Produces(MediaType.APPLICATION_JSON)
|
312
|
public Response getResultClaims(@PathParam("resultId") String resultId,
|
313
|
@DefaultValue("0") @QueryParam("offset") int offset,
|
314
|
@DefaultValue("20") @QueryParam("limit") int limit,
|
315
|
@DefaultValue("") @QueryParam("keyword") String keyword,
|
316
|
@DefaultValue("") @QueryParam("sortby") String orderby,
|
317
|
@DefaultValue("true") @QueryParam("descending") boolean descending,
|
318
|
@DefaultValue("") @QueryParam("types") List<String> types,
|
319
|
@HeaderParam("X-XSRF-TOKEN") String token,
|
320
|
@CookieParam("AccessToken") String cookie,
|
321
|
@Context HttpServletRequest request) {
|
322
|
|
323
|
|
324
|
if(token == null || token.isEmpty() || cookie == null || cookie.isEmpty() || !cookie.equals(token)){
|
325
|
authorization.logStatus(token,cookie);
|
326
|
return Response.status(Response.Status.FORBIDDEN).entity(compose403Message("Forbidden: You don't have permission to access. Maybe you are not registered."))
|
327
|
.type(MediaType.APPLICATION_JSON)
|
328
|
.build();
|
329
|
}
|
330
|
|
331
|
if(authorization.isClaimCurator(token)) {
|
332
|
|
333
|
int total = -1;
|
334
|
if (resultId == null || resultId.isEmpty()) {
|
335
|
return Response.status(Response.Status.BAD_REQUEST).entity(compose400Message("Context id cannot be empty."))
|
336
|
.type(MediaType.APPLICATION_JSON).build();
|
337
|
}
|
338
|
|
339
|
List<Claim> claims = null;
|
340
|
try {
|
341
|
claims = fetchClaimHandler.fetchClaimsByResult(resultId, limit, offset, keyword, orderby, descending, types,false);
|
342
|
total = fetchClaimHandler.countClaimsByResult(resultId, keyword, types);
|
343
|
|
344
|
} catch (SQLStoreException|Exception e) {
|
345
|
logger.error("Could not fetch claims for result with id " + resultId, e);
|
346
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(compose500Message("Fail to fetch claims" +
|
347
|
" for result with id " + resultId + ".", e)).type(MediaType.APPLICATION_JSON).build();
|
348
|
}
|
349
|
|
350
|
return Response.status(200).entity(composeDataResponse(request, claims, total, offset, limit)).build();
|
351
|
}
|
352
|
|
353
|
return Response.status(Response.Status.FORBIDDEN).entity(compose403Message("Forbidden: You don't have permission to access"))
|
354
|
.type(MediaType.APPLICATION_JSON)
|
355
|
.build();
|
356
|
}
|
357
|
|
358
|
|
359
|
@GET
|
360
|
@Path("/users/claims")
|
361
|
@Produces(MediaType.APPLICATION_JSON)
|
362
|
public Response getUserClaims(@DefaultValue("0") @QueryParam("offset") int offset,
|
363
|
@DefaultValue("20") @QueryParam("limit") int limit,
|
364
|
@DefaultValue("") @QueryParam("keyword") String keyword,
|
365
|
@DefaultValue("") @QueryParam("sortby") String orderby,
|
366
|
@DefaultValue("true") @QueryParam("descending") boolean descending,
|
367
|
@DefaultValue("") @QueryParam("types") List<String> types,
|
368
|
@HeaderParam("X-XSRF-TOKEN") String token,
|
369
|
@CookieParam("AccessToken") String cookie,
|
370
|
@Context HttpServletRequest request) {
|
371
|
|
372
|
|
373
|
if(token == null || token.isEmpty() || cookie == null || cookie.isEmpty() || !cookie.equals(token)){
|
374
|
return Response.status(Response.Status.FORBIDDEN).entity(compose403Message("Forbidden: You don't have permission to access. Maybe you are not registered."))
|
375
|
.type(MediaType.APPLICATION_JSON)
|
376
|
.build();
|
377
|
}
|
378
|
|
379
|
UserInfo userInfo = authorization.getUserHandler().getUserInfo(token);
|
380
|
if(authorization.isRegistered(userInfo)) {
|
381
|
String userMail = userInfo.getEmail();
|
382
|
logger.debug("User is registerd " );
|
383
|
int total = -1;
|
384
|
EmailValidator emailValidator = EmailValidator.getInstance();
|
385
|
|
386
|
if (userMail == null || userMail.isEmpty()) {
|
387
|
return Response.status(Response.Status.BAD_REQUEST).entity(compose400Message("User e-mail cannot be empty."))
|
388
|
.type(MediaType.APPLICATION_JSON).build();
|
389
|
}
|
390
|
|
391
|
if (!emailValidator.isValid(userMail)) {
|
392
|
return Response.status(Response.Status.BAD_REQUEST).entity(compose400Message("User e-mail is not valid."))
|
393
|
.type(MediaType.APPLICATION_JSON).build();
|
394
|
}
|
395
|
|
396
|
List<Claim> claims = null;
|
397
|
try {
|
398
|
logger.debug("About to fetch claims" );
|
399
|
claims = fetchClaimHandler.fetchClaimsByUser(userMail, limit, offset, keyword, orderby, descending, types,false);
|
400
|
total = fetchClaimHandler.countClaimsByUser(userMail, keyword, types);
|
401
|
|
402
|
} catch (SQLStoreException|Exception e) {
|
403
|
logger.error("Could not fetch claims for user with mail " + userMail, e);
|
404
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(compose500Message("Fail to fetch claims" +
|
405
|
" for user with e-mail " + userMail + ".", e)).type(MediaType.APPLICATION_JSON).build();
|
406
|
}
|
407
|
|
408
|
return Response.status(200).entity(composeDataResponse(request, claims, total, offset, limit)).build();
|
409
|
}
|
410
|
logger.debug("User is *NOT* registerd " );
|
411
|
return Response.status(Response.Status.FORBIDDEN).entity(compose403Message("Forbidden: You don't have permission to access. You are not registered."))
|
412
|
.type(MediaType.APPLICATION_JSON)
|
413
|
.build();
|
414
|
}
|
415
|
|
416
|
@GET
|
417
|
@Path("/claims/{claimId}")
|
418
|
@Produces(MediaType.APPLICATION_JSON)
|
419
|
public Response getClaimsById(@PathParam("claimId") String claimId,
|
420
|
@DefaultValue("0") @QueryParam("offset") int offset,
|
421
|
@DefaultValue("20") @QueryParam("limit") int limit,
|
422
|
@HeaderParam("X-XSRF-TOKEN") String token,
|
423
|
@CookieParam("AccessToken") String cookie,
|
424
|
@Context HttpServletRequest request) {
|
425
|
|
426
|
|
427
|
if(token == null || token.isEmpty() || cookie == null || cookie.isEmpty() || !cookie.equals(token)){
|
428
|
return Response.status(Response.Status.FORBIDDEN).entity(compose403Message("Forbidden: You don't have permission to access. Maybe you are not registered."))
|
429
|
.type(MediaType.APPLICATION_JSON)
|
430
|
.build();
|
431
|
}
|
432
|
|
433
|
if(authorization.isRegistered(token)) {
|
434
|
|
435
|
List<Claim> claims = null;
|
436
|
|
437
|
int total = -1;
|
438
|
if (claimId == null || claimId.isEmpty()) {
|
439
|
try {
|
440
|
claims = fetchClaimHandler.fetchAllClaims(limit, offset,false);
|
441
|
total = fetchClaimHandler.countAllClaims("", new ArrayList<String>());
|
442
|
|
443
|
return Response.status(200).entity(composeDataResponse(request, claims, total, offset, limit)).type(MediaType.APPLICATION_JSON).build();
|
444
|
|
445
|
} catch (SQLStoreException|Exception e) {
|
446
|
logger.error("Could not fetch claims.", e);
|
447
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(compose500Message("Fail to fetch claims.", e))
|
448
|
.type(MediaType.APPLICATION_JSON).build();
|
449
|
}
|
450
|
}
|
451
|
|
452
|
try {
|
453
|
Claim claim = fetchClaimHandler.fetchClaimById(claimId,false);
|
454
|
if (claim == null) {
|
455
|
return Response.status(Response.Status.NOT_FOUND).entity(compose404Message("Cannot find claim with id " + claimId + "."))
|
456
|
.type(MediaType.APPLICATION_JSON).build();
|
457
|
}
|
458
|
|
459
|
return Response.status(200).entity(composeDataResponse(claim)).build();
|
460
|
|
461
|
} catch (SQLStoreException|Exception e) {
|
462
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(compose500Message("Fail to fetch claim " +
|
463
|
"with id " + claimId + " id.", e)).type(MediaType.APPLICATION_JSON).build();
|
464
|
}
|
465
|
}
|
466
|
return Response.status(Response.Status.FORBIDDEN).entity(compose403Message("Forbidden: You don't have permission to access. You are not registered."))
|
467
|
.type(MediaType.APPLICATION_JSON)
|
468
|
.build();
|
469
|
}
|
470
|
|
471
|
@GET
|
472
|
@Path("/claims")
|
473
|
@Produces(MediaType.APPLICATION_JSON)
|
474
|
public Response getAllClaims(@DefaultValue("0") @QueryParam("offset") int offset,
|
475
|
@DefaultValue("20") @QueryParam("limit") int limit,
|
476
|
@DefaultValue("") @QueryParam("keyword") String keyword,
|
477
|
@DefaultValue("date") @QueryParam("sortby") String orderby,
|
478
|
@DefaultValue("true") @QueryParam("descending") boolean descending,
|
479
|
@DefaultValue("") @QueryParam("types") List<String> types,
|
480
|
@HeaderParam("X-XSRF-TOKEN") String token,
|
481
|
@HeaderParam("Origin") String origin,
|
482
|
@CookieParam("AccessToken") String cookie,
|
483
|
@Context HttpServletRequest request) {
|
484
|
|
485
|
|
486
|
logger.debug("Header \"Origin\" has value " + origin);
|
487
|
|
488
|
|
489
|
|
490
|
if(token == null || token.isEmpty() || cookie == null || cookie.isEmpty() || !cookie.equals(token)){
|
491
|
logger.debug("User is not authorized - Eroor 403");
|
492
|
|
493
|
return Response.status(Response.Status.FORBIDDEN).entity(compose403Message("Forbidden: You don't have permission to access. Maybe you are not registered."))
|
494
|
.type(MediaType.APPLICATION_JSON)
|
495
|
.build();
|
496
|
}
|
497
|
|
498
|
if(authorization.isClaimCurator(token)) {
|
499
|
logger.debug("User is authorized ! !");
|
500
|
List<Claim> claims = null;
|
501
|
|
502
|
int total = -1;
|
503
|
try {
|
504
|
claims = fetchClaimHandler.fetchAllClaims(limit, offset, keyword, orderby, descending, types,false);
|
505
|
total = fetchClaimHandler.countAllClaims(keyword, types);
|
506
|
|
507
|
return Response.status(200).entity(composeDataResponse(request, claims, total, offset, limit)).build();
|
508
|
|
509
|
} catch (SQLStoreException|Exception e) {
|
510
|
logger.error("Could not fetch claims.", e);
|
511
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(compose500Message("Fail to fetch claims.", e))
|
512
|
.type(MediaType.APPLICATION_JSON).build();
|
513
|
}
|
514
|
}
|
515
|
|
516
|
return Response.status(Response.Status.FORBIDDEN).entity(compose403Message("Forbidden: You don't have permission to access"))
|
517
|
.type(MediaType.APPLICATION_JSON)
|
518
|
.build();
|
519
|
}
|
520
|
|
521
|
//ARGIRO TODO: Na thn tsekarw
|
522
|
// @POST
|
523
|
// @Path("/claims/{claimId}")
|
524
|
// @Produces(MediaType.APPLICATION_JSON)
|
525
|
// public Response deleteClaim(@PathParam("claimId") String claimId,
|
526
|
// @QueryParam("token") String token) {
|
527
|
//
|
528
|
// if(!JWTValidator.isValid(token)) {
|
529
|
// return Response.status(Response.Status.UNAUTHORIZED).entity(compose404Message("Not valid Token"))
|
530
|
// .type(MediaType.APPLICATION_JSON)
|
531
|
// .build();
|
532
|
// }
|
533
|
// try {
|
534
|
//
|
535
|
// if (authorization.isRegistered(token) && (authorization.getUserHandler().getMail(token).equals(fetchClaimHandler.fetchClaimById(claimId).getUserMail()))) {
|
536
|
// if (claimId == null || claimId.isEmpty()) {
|
537
|
// return Response.status(Response.Status.NOT_FOUND).entity(compose404Message("Claim id cannot be empty."))
|
538
|
// .type(MediaType.APPLICATION_JSON).build();
|
539
|
// }
|
540
|
//
|
541
|
// try {
|
542
|
// if (claimHandler.deleteClaim(claimId)) {
|
543
|
// return Response.status(204).entity(compose204Message()).type(MediaType.APPLICATION_JSON).build();
|
544
|
// }
|
545
|
//
|
546
|
// } catch (Exception e) {
|
547
|
// logger.error("Fail to delete claim with id " + claimId + ".", e);
|
548
|
// return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(compose500Message("Fail to delete claim with id " + claimId + ".", e))
|
549
|
// .type(MediaType.APPLICATION_JSON).build();
|
550
|
// }
|
551
|
//
|
552
|
// return Response.status(Response.Status.NOT_FOUND).entity(compose404Message("Claim id cannot be empty."))
|
553
|
// .type(MediaType.APPLICATION_JSON).build();
|
554
|
// }
|
555
|
//
|
556
|
// return Response.status(Response.Status.FORBIDDEN).entity(compose403Message("Forbidden: You don't have permission to access. Maybe you are not registered."))
|
557
|
// .type(MediaType.APPLICATION_JSON)
|
558
|
// .build();
|
559
|
//
|
560
|
// } catch (Exception e) {
|
561
|
// logger.error("Could not fetch claims.", e);
|
562
|
// return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(compose500Message("Fail to fetch claims.", e))
|
563
|
// .type(MediaType.APPLICATION_JSON).build();
|
564
|
// }
|
565
|
// }
|
566
|
/*
|
567
|
|
568
|
@DELETE
|
569
|
@Path("/claims/{claimId}")
|
570
|
public Response deleteClaim(@PathParam("claimId") String claimId) {
|
571
|
|
572
|
if (claimId == null || claimId.isEmpty()) {
|
573
|
return Response.status(Response.Status.NOT_FOUND).entity(compose404Message("Claim id cannot be empty.")).header("Access-Control-Allow-Origin", "*")
|
574
|
.header("Access-Control-Allow-Methods", "DELETE")
|
575
|
.type(MediaType.APPLICATION_JSON).build();
|
576
|
}
|
577
|
|
578
|
try {
|
579
|
if(claimHandler.deleteClaim(claimId)) {
|
580
|
return Response.status(204).entity(compose204Message()).header("Access-Control-Allow-Origin", "*")
|
581
|
.header("Access-Control-Allow-Methods", "DELETE").type(MediaType.APPLICATION_JSON).build();
|
582
|
}
|
583
|
|
584
|
} catch (Exception e) {return Response.status(Response.Status.UNAUTHORIZED).entity(compose404Message("Not valid Token"))
|
585
|
.type(MediaType.APPLICATION_JSON)
|
586
|
.build();
|
587
|
logger.error("Fail to delete claim with id " + claimId + ".", e);
|
588
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(compose500Message("Fail to delete claim with id " + claimId +".", e))
|
589
|
.header("Access-Control-Allow-Origin", "*")
|
590
|
.header("Access-Control-Allow-Methods", "DELETE")
|
591
|
.type(MediaType.APPLICATION_JSON).build();
|
592
|
}
|
593
|
|
594
|
return Response.status(Response.Status.NOT_FOUND).entity(compose404Message("Claim id cannot be empty.")).header("Access-Control-Allow-Origin", "*")
|
595
|
.header("Access-Control-Allow-Methods", "DELETE")
|
596
|
.type(MediaType.APPLICATION_JSON).build();
|
597
|
}
|
598
|
*/
|
599
|
@DELETE
|
600
|
@Path("/claims/bulk")
|
601
|
@Produces(MediaType.APPLICATION_JSON)
|
602
|
public Response deleteBulkClaims(@QueryParam("claimId") List<String> claimIds,
|
603
|
@HeaderParam("X-XSRF-TOKEN") String token,
|
604
|
@HeaderParam("Origin") String origin,
|
605
|
@CookieParam("AccessToken") String cookie){
|
606
|
|
607
|
|
608
|
|
609
|
if(token == null || token.isEmpty() || cookie == null || cookie.isEmpty() || !cookie.equals(token)|| !authorization.hasValidOrigin(origin)){
|
610
|
return Response.status(Response.Status.FORBIDDEN).entity(compose403Message("Forbidden: You don't have permission to access. Maybe you are not registered."))
|
611
|
.type(MediaType.APPLICATION_JSON)
|
612
|
.build();
|
613
|
}
|
614
|
|
615
|
|
616
|
ArrayList<String> deletedIds= new ArrayList<String>();
|
617
|
ArrayList<String> notFoundIds= new ArrayList<String>();
|
618
|
|
619
|
if (claimIds == null || claimIds.size() == 0) {
|
620
|
return Response.status(Response.Status.NOT_FOUND).entity(compose404BulkDeleteMessage("Claim ids cannot be empty.",deletedIds,notFoundIds))
|
621
|
.type(MediaType.APPLICATION_JSON).build();
|
622
|
}
|
623
|
|
624
|
logger.debug("Trying to delete claims with ids: " + claimIds.toString() + ".");
|
625
|
UserInfo userInfo = authorization.getUserHandler().getUserInfo(token);
|
626
|
for (String claimId : claimIds) {
|
627
|
try {
|
628
|
|
629
|
if (authorization.isRegistered(userInfo)) {
|
630
|
if (authorization.isClaimCurator(userInfo) || authorization.isCommunityCurator(userInfo) || userInfo.getEmail().equals(fetchClaimHandler.fetchClaimById(claimId,false).getUserMail())) {
|
631
|
if (claimHandler.deleteClaim(claimId)) {
|
632
|
deletedIds.add(claimId);
|
633
|
} else {
|
634
|
notFoundIds.add(claimId);
|
635
|
}
|
636
|
} else {
|
637
|
return Response.status(Response.Status.FORBIDDEN).entity(compose403Message("Forbidden: You don't have permission to delete."))
|
638
|
.type(MediaType.APPLICATION_JSON)
|
639
|
.build();
|
640
|
}
|
641
|
} else {
|
642
|
return Response.status(Response.Status.FORBIDDEN).entity(compose403Message("Forbidden: You don't have permission to access. Maybe you are not registered."))
|
643
|
.type(MediaType.APPLICATION_JSON)
|
644
|
.build();
|
645
|
}
|
646
|
} catch (SQLStoreException|Exception e) {
|
647
|
logger.error("Fail to delete claim with id " + claimId + ".", e);
|
648
|
notFoundIds.add(claimId);
|
649
|
}
|
650
|
}
|
651
|
logger.debug("Successfully deleted " + deletedIds.size() + " from " + claimIds.size() +". Deleted claims with ids: " + deletedIds.toString() + ".");
|
652
|
if (claimIds.size() == notFoundIds.size()) {
|
653
|
return Response.status(Response.Status.NOT_FOUND).entity(compose404BulkDeleteMessage("Claim ids cannot be empty.",deletedIds,notFoundIds))
|
654
|
.type(MediaType.APPLICATION_JSON).build();
|
655
|
} else if (claimIds.size() == notFoundIds.size()) {
|
656
|
return Response.status(204).entity(compose204BulkDeleteMessage(deletedIds,notFoundIds)).type(MediaType.APPLICATION_JSON).build();
|
657
|
} else {
|
658
|
return Response.status(204).entity(compose204BulkDeleteMessage(deletedIds,notFoundIds)).type(MediaType.APPLICATION_JSON).build();
|
659
|
}
|
660
|
}
|
661
|
|
662
|
@POST
|
663
|
@Path("/claims")
|
664
|
@Produces(MediaType.APPLICATION_JSON)
|
665
|
@Consumes(MediaType.APPLICATION_JSON)
|
666
|
public Response addClaim(String input, @Context HttpServletRequest request,
|
667
|
@HeaderParam("X-XSRF-TOKEN") String token,
|
668
|
@HeaderParam("Origin") String origin,
|
669
|
@CookieParam("AccessToken") String cookie) {
|
670
|
|
671
|
|
672
|
if(token == null || token.isEmpty() || cookie == null || cookie.isEmpty() || !cookie.equals(token) || !authorization.hasValidOrigin(origin)){
|
673
|
return Response.status(Response.Status.FORBIDDEN).entity(compose403Message("Forbidden: You don't have permission to access. Maybe you are not registered."))
|
674
|
.type(MediaType.APPLICATION_JSON)
|
675
|
.build();
|
676
|
}
|
677
|
|
678
|
UserInfo userInfo = authorization.getUserHandler().getUserInfo(token);
|
679
|
if(authorization.isRegistered(userInfo)) {
|
680
|
JsonObject jsonObject = new JsonParser().parse(input).getAsJsonObject();
|
681
|
|
682
|
String claimedBy = userInfo.getEmail();
|
683
|
logger.info("claimedBy " + claimedBy);
|
684
|
|
685
|
String sourceId = jsonObject.get("sourceId").getAsString();
|
686
|
logger.info("sourceId " + sourceId);
|
687
|
String sourceType = jsonObject.get("sourceType").getAsString();
|
688
|
logger.info("sourceType " + sourceType);
|
689
|
String sourceCollectedFrom = jsonObject.get("sourceCollectedFrom").getAsString();
|
690
|
logger.info("sourceCollectedFrom " + sourceCollectedFrom);
|
691
|
String sourceAccessRights = jsonObject.get("sourceAccessRights").getAsString();
|
692
|
logger.info("sourceAccessRights " + sourceAccessRights);
|
693
|
String sourceEmbargoEndDate = jsonObject.get("sourceEmbargoEndDate").getAsString();
|
694
|
sourceEmbargoEndDate = (sourceEmbargoEndDate != null && sourceEmbargoEndDate.equals("")) ? null : sourceEmbargoEndDate;
|
695
|
logger.info("sourceEmbargoEndDate " + sourceEmbargoEndDate);
|
696
|
|
697
|
String targetId = jsonObject.get("targetId").getAsString();
|
698
|
logger.info("targetId " + targetId);
|
699
|
String targetType = jsonObject.get("targetType").getAsString();
|
700
|
logger.info("targetType " + targetType);
|
701
|
String targetCollectedFrom = jsonObject.get("targetCollectedFrom").getAsString();
|
702
|
logger.info("targetCollectedFrom " + targetCollectedFrom);
|
703
|
String targetAccessRights = jsonObject.get("targetAccessRights").getAsString();
|
704
|
logger.info("targetAccessRights " + targetAccessRights);
|
705
|
String targetEmbargoEndDate = jsonObject.get("targetEmbargoEndDate").getAsString();
|
706
|
targetEmbargoEndDate = (targetEmbargoEndDate != null && targetEmbargoEndDate.equals("")) ? null : targetEmbargoEndDate;
|
707
|
logger.info("targetEmbargoEndDate " + targetEmbargoEndDate);
|
708
|
|
709
|
EmailValidator emailValidator = EmailValidator.getInstance();
|
710
|
if (!emailValidator.isValid(claimedBy)) {
|
711
|
return Response.status(Response.Status.BAD_REQUEST).entity(compose400Message("User e-mail is invalid."))
|
712
|
.type(MediaType.APPLICATION_JSON).build();
|
713
|
}
|
714
|
|
715
|
|
716
|
try {
|
717
|
String claimId = claimHandler.buildAndInsertClaim(claimedBy, sourceType, sourceId, sourceCollectedFrom, sourceAccessRights, sourceEmbargoEndDate, targetType, targetId, targetCollectedFrom, targetAccessRights, targetEmbargoEndDate);
|
718
|
return Response.status(200).entity(compose201PostMessage(request, claimId)).type(MediaType.APPLICATION_JSON).build();
|
719
|
|
720
|
} catch (ClaimValidationException ve) {
|
721
|
return Response.status(Response.Status.BAD_REQUEST).entity(compose400Message("The given ids are wrong.", ve))
|
722
|
.type(MediaType.APPLICATION_JSON).build();
|
723
|
|
724
|
} catch (SQLStoreException|Exception e) {
|
725
|
logger.error("Fail to add new claim.", e);
|
726
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(compose500Message("Fail to add new claim.", e))
|
727
|
.type(MediaType.APPLICATION_JSON).build();
|
728
|
}
|
729
|
}
|
730
|
return Response.status(Response.Status.FORBIDDEN).entity(compose403Message("Forbidden: You don't have permission to access."))
|
731
|
.type(MediaType.APPLICATION_JSON)
|
732
|
.build();
|
733
|
}
|
734
|
|
735
|
@POST
|
736
|
@Path("/claims/bulk")
|
737
|
@Produces(MediaType.APPLICATION_JSON)
|
738
|
@Consumes(MediaType.APPLICATION_JSON)
|
739
|
public Response addBulkClaims(String input, @Context HttpServletRequest request,
|
740
|
@HeaderParam("X-XSRF-TOKEN") String token,
|
741
|
@HeaderParam("Origin") String origin,
|
742
|
@CookieParam("AccessToken") String cookie) {
|
743
|
|
744
|
|
745
|
if(token == null || token.isEmpty() || cookie == null || cookie.isEmpty() || !cookie.equals(token)|| !authorization.hasValidOrigin(origin)){
|
746
|
return Response.status(Response.Status.FORBIDDEN).entity(compose403Message("Forbidden: You don't have permission to access. Maybe you are not registered."))
|
747
|
.type(MediaType.APPLICATION_JSON)
|
748
|
.build();
|
749
|
}
|
750
|
|
751
|
UserInfo userInfo = authorization.getUserHandler().getUserInfo(token);
|
752
|
if(authorization.isRegistered(userInfo)) {
|
753
|
ArrayList<String> insertedIds = new ArrayList<String>();
|
754
|
JsonArray errorInClaims = new JsonArray();
|
755
|
|
756
|
int code200 = 0;
|
757
|
int code400 = 0;
|
758
|
int code500 = 0;
|
759
|
|
760
|
JsonArray jsonArray = new JsonParser().parse(input).getAsJsonArray();
|
761
|
for (JsonElement je : jsonArray) {
|
762
|
JsonObject jsonObject = je.getAsJsonObject();
|
763
|
|
764
|
logger.info("targetId " + jsonObject.toString());
|
765
|
String claimedBy = userInfo.getEmail();
|
766
|
logger.info("claimedBy " + claimedBy);
|
767
|
|
768
|
String sourceId = jsonObject.get("sourceId").getAsString();
|
769
|
logger.info("sourceId " + sourceId);
|
770
|
String sourceType = jsonObject.get("sourceType").getAsString();
|
771
|
logger.info("sourceType " + sourceType);
|
772
|
String sourceCollectedFrom = jsonObject.get("sourceCollectedFrom").getAsString();
|
773
|
logger.info("sourceCollectedFrom " + sourceCollectedFrom);
|
774
|
String sourceAccessRights = jsonObject.get("sourceAccessRights").getAsString();
|
775
|
logger.info("sourceAccessRights " + sourceAccessRights);
|
776
|
String sourceEmbargoEndDate = jsonObject.get("sourceEmbargoEndDate").getAsString();
|
777
|
sourceEmbargoEndDate = (sourceEmbargoEndDate != null && sourceEmbargoEndDate.equals("")) ? null : sourceEmbargoEndDate;
|
778
|
logger.info("sourceEmbargoEndDate " + sourceEmbargoEndDate);
|
779
|
|
780
|
String targetId = jsonObject.get("targetId").getAsString();
|
781
|
logger.info("targetId " + targetId);
|
782
|
String targetType = jsonObject.get("targetType").getAsString();
|
783
|
logger.info("targetType " + targetType);
|
784
|
String targetCollectedFrom = jsonObject.get("targetCollectedFrom").getAsString();
|
785
|
logger.info("targetCollectedFrom " + targetCollectedFrom);
|
786
|
String targetAccessRights = jsonObject.get("targetAccessRights").getAsString();
|
787
|
logger.info("targetAccessRights " + targetAccessRights);
|
788
|
String targetEmbargoEndDate = jsonObject.get("targetEmbargoEndDate").getAsString();
|
789
|
targetEmbargoEndDate = (targetEmbargoEndDate != null && targetEmbargoEndDate.equals("")) ? null : targetEmbargoEndDate;
|
790
|
logger.info("targetEmbargoEndDate " + targetEmbargoEndDate);
|
791
|
|
792
|
EmailValidator emailValidator = EmailValidator.getInstance();
|
793
|
if (!emailValidator.isValid(claimedBy)) {
|
794
|
jsonObject.addProperty("error", "user");
|
795
|
// return Response.status(Response.Status.BAD_REQUEST).entity(compose400Message("User e-mail is invalid."))
|
796
|
// .type(MediaType.APPLICATION_JSON).build();
|
797
|
code400++;
|
798
|
errorInClaims.add(jsonObject);
|
799
|
}
|
800
|
|
801
|
|
802
|
try {
|
803
|
String claimId = claimHandler.buildAndInsertClaim(claimedBy, sourceType, sourceId, sourceCollectedFrom, sourceAccessRights, sourceEmbargoEndDate, targetType, targetId, targetCollectedFrom, targetAccessRights, targetEmbargoEndDate);
|
804
|
insertedIds.add(claimId);
|
805
|
code200++;
|
806
|
// return Response.status(200).entity(compose201PostMessage(request, claimId)).type(MediaType.APPLICATION_JSON).build();
|
807
|
|
808
|
} catch (ClaimValidationException ve) {
|
809
|
// return Response.status(Response.Status.BAD_REQUEST).entity(compose400Message("The given ids are wrong.", ve))
|
810
|
// .type(MediaType.APPLICATION_JSON).build();
|
811
|
jsonObject.addProperty("error", "validation");
|
812
|
errorInClaims.add(jsonObject);
|
813
|
code400++;
|
814
|
|
815
|
} catch (SQLStoreException|Exception e) {
|
816
|
// logger.error("Fail to add new claim.", e);
|
817
|
// return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(compose500Message("Fail to add new claim.", e))
|
818
|
// .type(MediaType.APPLICATION_JSON).build();
|
819
|
jsonObject.addProperty("error", "insertion");
|
820
|
errorInClaims.add(jsonObject);
|
821
|
code500++;
|
822
|
}
|
823
|
}
|
824
|
if (jsonArray.size() == code500) {
|
825
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(compose500BulkInsertMessage("Fail to add new claim.", insertedIds, errorInClaims))
|
826
|
.type(MediaType.APPLICATION_JSON).build();
|
827
|
} else if (code200 > 0) {
|
828
|
return Response.status(200).entity(compose201BulkInsertMessage(insertedIds, errorInClaims)).type(MediaType.APPLICATION_JSON).build();
|
829
|
} else {
|
830
|
return Response.status(Response.Status.BAD_REQUEST).entity(compose400BulkInsertMessage("The given ids are wrong.", insertedIds, errorInClaims))
|
831
|
.type(MediaType.APPLICATION_JSON).build();
|
832
|
}
|
833
|
}
|
834
|
return Response.status(Response.Status.FORBIDDEN).entity(compose403Message("Forbidden: You don't have permission to access. You are not registered."))
|
835
|
.type(MediaType.APPLICATION_JSON)
|
836
|
.build();
|
837
|
|
838
|
}
|
839
|
@POST
|
840
|
@Path("/curate/bulk")
|
841
|
@Produces(MediaType.APPLICATION_JSON)
|
842
|
@Consumes(MediaType.APPLICATION_JSON)
|
843
|
public Response curateBulkClaims(String input, @Context HttpServletRequest request,
|
844
|
@HeaderParam("X-XSRF-TOKEN") String token,
|
845
|
@HeaderParam("Origin") String origin,
|
846
|
@CookieParam("AccessToken") String cookie) {
|
847
|
|
848
|
|
849
|
if(token == null || token.isEmpty() || cookie == null || cookie.isEmpty() || !cookie.equals(token)|| !authorization.hasValidOrigin(origin)){
|
850
|
return Response.status(Response.Status.FORBIDDEN).entity(compose403Message("Forbidden: You don't have permission to access. Maybe you are not registered."))
|
851
|
.type(MediaType.APPLICATION_JSON)
|
852
|
.build();
|
853
|
}
|
854
|
UserInfo userInfo = authorization.getUserHandler().getUserInfo(token);
|
855
|
if (authorization.isRegistered(userInfo)) {
|
856
|
ArrayList<String> insertedIds = new ArrayList<String>();
|
857
|
JsonArray errorInClaims = new JsonArray();
|
858
|
int code200 = 0;
|
859
|
int code400 = 0;
|
860
|
int code500 = 0;
|
861
|
JsonArray jsonArray = new JsonParser().parse(input).getAsJsonArray();
|
862
|
String curatedBy =userInfo.getEmail();
|
863
|
|
864
|
for (JsonElement je : jsonArray) {
|
865
|
JsonObject jsonObject = je.getAsJsonObject();
|
866
|
|
867
|
String id = jsonObject.get("id").getAsString();
|
868
|
logger.info("id " + id);
|
869
|
|
870
|
Boolean approved = jsonObject.get("approved").getAsBoolean();
|
871
|
logger.info("approved " + approved);
|
872
|
EmailValidator emailValidator = EmailValidator.getInstance();
|
873
|
if (!emailValidator.isValid(curatedBy)) {
|
874
|
jsonObject.addProperty("error", "user");
|
875
|
// return Response.status(Response.Status.BAD_REQUEST).entity(compose400Message("User e-mail is invalid."))
|
876
|
// .type(MediaType.APPLICATION_JSON).build();
|
877
|
code400++;
|
878
|
errorInClaims.add(jsonObject);
|
879
|
}
|
880
|
|
881
|
try {
|
882
|
claimHandler.updateClaimCurationInfo(curatedBy,id, approved);
|
883
|
insertedIds.add(id);
|
884
|
code200++;
|
885
|
|
886
|
} catch (SQLStoreException|Exception e) {
|
887
|
jsonObject.addProperty("error", "insertion");
|
888
|
errorInClaims.add(jsonObject);
|
889
|
code500++;
|
890
|
}
|
891
|
}
|
892
|
if (jsonArray.size() == code500) {
|
893
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(compose500BulkInsertMessage("Fail to update claims.", insertedIds, errorInClaims))
|
894
|
.type(MediaType.APPLICATION_JSON).build();
|
895
|
} else if (code200 > 0) {
|
896
|
return Response.status(200).entity(compose201BulkInsertMessage(insertedIds, errorInClaims)).type(MediaType.APPLICATION_JSON).build();
|
897
|
} else {
|
898
|
return Response.status(Response.Status.BAD_REQUEST).entity(compose400BulkInsertMessage("The given ids are wrong.", insertedIds, errorInClaims))
|
899
|
.type(MediaType.APPLICATION_JSON).build();
|
900
|
}
|
901
|
}
|
902
|
return Response.status(Response.Status.FORBIDDEN).entity(compose403Message("Forbidden: You don't have permission to access. You are not registered."))
|
903
|
.type(MediaType.APPLICATION_JSON)
|
904
|
.build();
|
905
|
|
906
|
}
|
907
|
@POST
|
908
|
@Path("/feed/bulk")
|
909
|
@Produces(MediaType.APPLICATION_JSON)
|
910
|
@Consumes(MediaType.APPLICATION_JSON)
|
911
|
public Response feedBulkRecords(String input, @Context HttpServletRequest request,
|
912
|
@HeaderParam("X-XSRF-TOKEN") String token,
|
913
|
@HeaderParam("Origin") String origin,
|
914
|
@CookieParam("AccessToken") String cookie) {
|
915
|
|
916
|
|
917
|
if(token == null || token.isEmpty() || cookie == null || cookie.isEmpty() || !cookie.equals(token)|| !authorization.hasValidOrigin(origin)){
|
918
|
|
919
|
return Response.status(Response.Status.FORBIDDEN).entity(compose403Message("Forbidden: You don't have permission to access. Maybe you are not registered."))
|
920
|
.type(MediaType.APPLICATION_JSON)
|
921
|
.build();
|
922
|
}
|
923
|
|
924
|
if (authorization.isRegistered(token)) {
|
925
|
ArrayList<String> insertedIds = new ArrayList<String>();
|
926
|
JsonArray errorInClaims = new JsonArray();
|
927
|
int code200 = 0;
|
928
|
int code400 = 0;
|
929
|
int code500 = 0;
|
930
|
JsonArray jsonArray = new JsonParser().parse(input).getAsJsonArray();
|
931
|
for (JsonElement je : jsonArray) {
|
932
|
JsonObject jsonObject = je.getAsJsonObject();
|
933
|
Boolean inserted = directIndexHandler.insertRecord(new Gson().toJson(jsonObject.get("record")));
|
934
|
if (inserted) {
|
935
|
insertedIds.add(jsonObject.get("id").getAsString());
|
936
|
code200++;
|
937
|
} else {
|
938
|
errorInClaims.add(jsonObject.get("id").getAsString());
|
939
|
code400++;
|
940
|
}
|
941
|
|
942
|
|
943
|
}
|
944
|
if (jsonArray.size() == code500) {
|
945
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(compose500BulkInsertMessage("Fail to add new claim.", insertedIds, errorInClaims))
|
946
|
.type(MediaType.APPLICATION_JSON).build();
|
947
|
} else if (code200 > 0) {
|
948
|
return Response.status(200).entity(compose201BulkInsertMessage(insertedIds, errorInClaims)).type(MediaType.APPLICATION_JSON).build();
|
949
|
} else {
|
950
|
return Response.status(Response.Status.BAD_REQUEST).entity(compose400BulkInsertMessage("The given ids are wrong.", insertedIds, errorInClaims))
|
951
|
.type(MediaType.APPLICATION_JSON).build();
|
952
|
}
|
953
|
}
|
954
|
return Response.status(Response.Status.FORBIDDEN).entity(compose403Message("Forbidden: You don't have permission to access. You are not registered."))
|
955
|
.type(MediaType.APPLICATION_JSON)
|
956
|
.build();
|
957
|
|
958
|
}
|
959
|
|
960
|
@Path("/communities")
|
961
|
@GET
|
962
|
@Produces(MediaType.APPLICATION_JSON)
|
963
|
public Response fetchCommunities(@HeaderParam("X-XSRF-TOKEN") String token,
|
964
|
@CookieParam("AccessToken") String cookie) throws ISLookUpServiceException {
|
965
|
|
966
|
|
967
|
if(token == null || token.isEmpty() || cookie == null || cookie.isEmpty() || !cookie.equals(token)){
|
968
|
return Response.status(Response.Status.FORBIDDEN).entity(compose403Message("Forbidden: You don't have permission to access. Maybe you are not registered."))
|
969
|
.type(MediaType.APPLICATION_JSON)
|
970
|
.build();
|
971
|
}
|
972
|
|
973
|
if (authorization.isRegistered(token)) {
|
974
|
//it needs to have at lease one category with @claim=true
|
975
|
List<String> communities = lookupServiceLocator.getService().quickSearchProfile
|
976
|
("for $x in collection('/db/DRIVER/ContextDSResources/ContextDSResourceType') \n" +
|
977
|
" where $x//context[@type='community']//category[@claim='true'] \n" +
|
978
|
" return <communities>{$x//context/@id}{$x//context/@label}</communities>");
|
979
|
|
980
|
return Response.status(200).entity(composeDataResponse(xml2Json(communities))).type(MediaType.APPLICATION_JSON).build();
|
981
|
}
|
982
|
return Response.status(Response.Status.FORBIDDEN).entity(compose403Message("Forbidden: You don't have permission to access. You are not registered."))
|
983
|
.type(MediaType.APPLICATION_JSON)
|
984
|
.build();
|
985
|
}
|
986
|
|
987
|
@Path("/communities/{communityid}/categories")
|
988
|
@GET
|
989
|
@Produces(MediaType.APPLICATION_JSON)
|
990
|
public Response fetchCommunityCategories(@PathParam("communityid") String communityid,
|
991
|
@HeaderParam("X-XSRF-TOKEN") String token,
|
992
|
@CookieParam("AccessToken") String cookie) throws ISLookUpServiceException {
|
993
|
|
994
|
|
995
|
if(token == null || token.isEmpty() || cookie == null || cookie.isEmpty() || !cookie.equals(token)){
|
996
|
return Response.status(Response.Status.FORBIDDEN).entity(compose403Message("Forbidden: You don't have permission to access. Maybe you are not registered."))
|
997
|
.type(MediaType.APPLICATION_JSON)
|
998
|
.build();
|
999
|
}
|
1000
|
|
1001
|
if (authorization.isRegistered(token)) {
|
1002
|
List<String> categories = lookupServiceLocator.getService().quickSearchProfile
|
1003
|
("for $x in collection('/db/DRIVER/ContextDSResources/ContextDSResourceType')//context[@id='" + communityid + "']//category[@claim=\'true\']\n" +
|
1004
|
"return <category>{$x/@id}{$x/@label}</category>");
|
1005
|
|
1006
|
logger.info("for $x in collection('/db/DRIVER/ContextDSResources/ContextDSResourceType')//context[@id='" + communityid + "']//category[@claim=\'true\']\n" +
|
1007
|
"return <category>{$x/@id}{$x/@label}</category>");
|
1008
|
|
1009
|
if (categories == null || categories.isEmpty()) {
|
1010
|
return Response.status(404).entity(compose404Message("There are no categories for community with id " + communityid)).type(MediaType.APPLICATION_JSON).build();
|
1011
|
}
|
1012
|
|
1013
|
xml2Json(categories);
|
1014
|
|
1015
|
return Response.status(200).entity(composeDataResponse(xml2Json(categories))).type(MediaType.APPLICATION_JSON).build();
|
1016
|
}
|
1017
|
return Response.status(Response.Status.FORBIDDEN).entity(compose403Message("Forbidden: You don't have permission to access. You are not registered."))
|
1018
|
.type(MediaType.APPLICATION_JSON)
|
1019
|
.build();
|
1020
|
}
|
1021
|
|
1022
|
@Path("/categories/{categoryid}/concepts")
|
1023
|
@GET
|
1024
|
@Produces(MediaType.APPLICATION_JSON)
|
1025
|
public Response fetchCategoryConcepts(@PathParam("categoryid") String categoryid,
|
1026
|
@HeaderParam("X-XSRF-TOKEN") String token,
|
1027
|
@CookieParam("AccessToken") String cookie) throws ISLookUpServiceException {
|
1028
|
|
1029
|
|
1030
|
if(token == null || token.isEmpty() || cookie == null || cookie.isEmpty() || !cookie.equals(token)){
|
1031
|
return Response.status(Response.Status.FORBIDDEN).entity(compose403Message("Forbidden: You don't have permission to access. Maybe you are not registered."))
|
1032
|
.type(MediaType.APPLICATION_JSON)
|
1033
|
.build();
|
1034
|
}
|
1035
|
|
1036
|
if (authorization.isRegistered(token)) {
|
1037
|
List<String> concepts = lookupServiceLocator.getService().quickSearchProfile
|
1038
|
("for $x in collection('/db/DRIVER/ContextDSResources/ContextDSResourceType') return $x//category[@id='" + categoryid + "']//concept");
|
1039
|
|
1040
|
if (concepts == null || concepts.isEmpty()) {
|
1041
|
return Response.status(404).entity(compose404Message("There are no concepts for category with id " + categoryid)).type(MediaType.APPLICATION_JSON).build();
|
1042
|
}
|
1043
|
|
1044
|
return Response.status(200).entity(composeDataResponse(xml2Json(concepts))).type(MediaType.APPLICATION_JSON).build();
|
1045
|
}
|
1046
|
return Response.status(Response.Status.FORBIDDEN).entity(compose403Message("Forbidden: You don't have permission to access. You are not registered."))
|
1047
|
.type(MediaType.APPLICATION_JSON)
|
1048
|
.build();
|
1049
|
}
|
1050
|
|
1051
|
@GET
|
1052
|
@Path("/users/notification")
|
1053
|
@Produces(MediaType.APPLICATION_JSON)
|
1054
|
public Response getUserEmailNotificationPreferences(@QueryParam("communityId") String openaireId,
|
1055
|
@HeaderParam("X-XSRF-TOKEN") String token,
|
1056
|
@CookieParam("AccessToken") String cookie,
|
1057
|
@Context HttpServletRequest request) {
|
1058
|
|
1059
|
if(token == null || token.isEmpty() || cookie == null || cookie.isEmpty() || !cookie.equals(token)){
|
1060
|
return Response.status(Response.Status.FORBIDDEN).entity(compose403Message("Forbidden: You don't have permission to access. Maybe you are not registered."))
|
1061
|
.type(MediaType.APPLICATION_JSON)
|
1062
|
.build();
|
1063
|
}
|
1064
|
|
1065
|
UserInfo userInfo = authorization.getUserHandler().getUserInfo(token);
|
1066
|
if(authorization.isRegistered(userInfo)) {
|
1067
|
String userMail = userInfo.getEmail();
|
1068
|
logger.debug("User is registerd " );
|
1069
|
|
1070
|
EmailValidator emailValidator = EmailValidator.getInstance();
|
1071
|
|
1072
|
if (userMail == null || userMail.isEmpty()) {
|
1073
|
return Response.status(Response.Status.BAD_REQUEST).entity(compose400Message("User e-mail cannot be empty."))
|
1074
|
.type(MediaType.APPLICATION_JSON).build();
|
1075
|
}
|
1076
|
|
1077
|
if (!emailValidator.isValid(userMail)) {
|
1078
|
return Response.status(Response.Status.BAD_REQUEST).entity(compose400Message("User e-mail is not valid."))
|
1079
|
.type(MediaType.APPLICATION_JSON).build();
|
1080
|
}
|
1081
|
|
1082
|
List<Notification> notifications = null;
|
1083
|
try {
|
1084
|
if(openaireId != null) {
|
1085
|
CommunityUtils communityInfo = CommunityUtils.getCommunityInfo(openaireId);
|
1086
|
if(communityInfo.getManagers().contains(userMail)) {
|
1087
|
|
1088
|
Notification notification = null;
|
1089
|
logger.debug("About to fetch notification");
|
1090
|
notification = fetchNotificationHandler.fetchNotification(openaireId, userMail);
|
1091
|
|
1092
|
if (notifications == null) {
|
1093
|
notifications = new ArrayList<Notification>();
|
1094
|
}
|
1095
|
if (notification == null) {
|
1096
|
notification = new Notification(openaireId, communityInfo.getName(), userMail, Integer.parseInt(defaultFrequencyInHours), true);
|
1097
|
} else {
|
1098
|
notification.setOpenaireName(communityInfo.getName());
|
1099
|
}
|
1100
|
notifications.add(notification);
|
1101
|
}
|
1102
|
} else {
|
1103
|
Map<String, String> projectIdsAndNames = fetchProjectHandler.fetchProjectIdsAndNamesByProjectManagerMail(userMail);
|
1104
|
if(projectIdsAndNames != null) {
|
1105
|
for (Map.Entry<String, String> projectIdAndName : projectIdsAndNames.entrySet()) {
|
1106
|
Notification notification = null;
|
1107
|
logger.debug("About to fetch notification");
|
1108
|
notification = fetchNotificationHandler.fetchNotification(projectIdAndName.getKey(), userMail);
|
1109
|
|
1110
|
if (notifications == null) {
|
1111
|
notifications = new ArrayList<Notification>();
|
1112
|
}
|
1113
|
if(notification == null) {
|
1114
|
notification = new Notification(projectIdAndName.getKey(), projectIdAndName.getValue(), userMail, Integer.parseInt(defaultFrequencyInHours), true);
|
1115
|
} else {
|
1116
|
notification.setOpenaireName(projectIdAndName.getValue());
|
1117
|
}
|
1118
|
notifications.add(notification);
|
1119
|
logger.debug(notification);
|
1120
|
logger.debug("notification openaireId:"+notification.getOpenaireId());
|
1121
|
logger.debug(notifications.size());
|
1122
|
}
|
1123
|
}
|
1124
|
}
|
1125
|
|
1126
|
} catch (SQLStoreException|Exception e) {
|
1127
|
logger.error("Could not fetch notification preferences for user with mail " + userMail, e);
|
1128
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(compose500Message("Fail to fetch notification preferences" +
|
1129
|
" for user with e-mail " + userMail + ".", e)).type(MediaType.APPLICATION_JSON).build();
|
1130
|
}
|
1131
|
|
1132
|
if (notifications == null || notifications.isEmpty()) {
|
1133
|
return Response.status(204).entity(compose204Message("There are no notifications for user with mail " + userMail)).type(MediaType.APPLICATION_JSON).build();
|
1134
|
}
|
1135
|
|
1136
|
return Response.status(200).entity(composeDataResponse(notifications)).build();
|
1137
|
|
1138
|
}
|
1139
|
logger.debug("User is *NOT* registerd " );
|
1140
|
return Response.status(Response.Status.FORBIDDEN).entity(compose403Message("Forbidden: You don't have permission to access. You are not registered."))
|
1141
|
.type(MediaType.APPLICATION_JSON)
|
1142
|
.build();
|
1143
|
}
|
1144
|
|
1145
|
@POST
|
1146
|
@Path("/users/notification/save")
|
1147
|
@Produces(MediaType.APPLICATION_JSON)
|
1148
|
@Consumes(MediaType.APPLICATION_JSON)
|
1149
|
public Response saveOrUpdateUserEmailNotificationPreferences(String input, @Context HttpServletRequest request,
|
1150
|
@HeaderParam("X-XSRF-TOKEN") String token,
|
1151
|
@HeaderParam("Origin") String origin,
|
1152
|
@CookieParam("AccessToken") String cookie) {
|
1153
|
|
1154
|
|
1155
|
if(token == null || token.isEmpty() || cookie == null || cookie.isEmpty() || !cookie.equals(token)|| !authorization.hasValidOrigin(origin)){
|
1156
|
return Response.status(Response.Status.FORBIDDEN).entity(compose403Message("Forbidden: You don't have permission to access. Maybe you are not registered."))
|
1157
|
.type(MediaType.APPLICATION_JSON)
|
1158
|
.build();
|
1159
|
}
|
1160
|
UserInfo userInfo = authorization.getUserHandler().getUserInfo(token);
|
1161
|
if (authorization.isRegistered(userInfo)) {
|
1162
|
ArrayList<String> insertedIds = new ArrayList<String>();
|
1163
|
JsonArray errorInClaims = new JsonArray();
|
1164
|
int code200 = 0;
|
1165
|
int code400 = 0;
|
1166
|
int code500 = 0;
|
1167
|
JsonObject jsonObject = new JsonParser().parse(input).getAsJsonObject();
|
1168
|
|
1169
|
String userMail =userInfo.getEmail();
|
1170
|
|
1171
|
String openaireId = jsonObject.get("openaireId").getAsString();
|
1172
|
logger.info("openaireId " + openaireId);
|
1173
|
|
1174
|
boolean notify = jsonObject.get("notify").getAsBoolean();
|
1175
|
logger.info("notify "+notify);
|
1176
|
|
1177
|
int frequency = jsonObject.get("frequency").getAsInt();
|
1178
|
logger.info("frequency " + frequency);
|
1179
|
|
1180
|
EmailValidator emailValidator = EmailValidator.getInstance();
|
1181
|
if (!emailValidator.isValid(userMail)) {
|
1182
|
jsonObject.addProperty("error", "user");
|
1183
|
return Response.status(Response.Status.BAD_REQUEST).entity(compose400Message("User e-mail is invalid."))
|
1184
|
.type(MediaType.APPLICATION_JSON).build();
|
1185
|
}
|
1186
|
|
1187
|
try {
|
1188
|
boolean continueProcedure = false;
|
1189
|
List<String> managers = null;
|
1190
|
try {
|
1191
|
managers = fetchProjectHandler.fetchContactEmailsByProjectId(openaireId);
|
1192
|
} catch (Exception e) {
|
1193
|
e.printStackTrace();
|
1194
|
} catch (SQLStoreException e) {
|
1195
|
e.printStackTrace();
|
1196
|
}
|
1197
|
if(managers != null && managers.contains(userMail)) {
|
1198
|
continueProcedure = true;
|
1199
|
} else {
|
1200
|
CommunityUtils communityInfo = CommunityUtils.getCommunityInfo(openaireId);
|
1201
|
if(communityInfo.getManagers().contains(userMail)) {
|
1202
|
continueProcedure = true;
|
1203
|
}
|
1204
|
}
|
1205
|
|
1206
|
if(continueProcedure) {
|
1207
|
Notification notification = null;
|
1208
|
logger.debug("About to fetch notification");
|
1209
|
notification = fetchNotificationHandler.fetchNotification(openaireId, userMail);
|
1210
|
|
1211
|
if (notification == null) {
|
1212
|
logger.debug("About to insert notification");
|
1213
|
notificationHandler.buildAndInsertNotification(openaireId, userMail, frequency, notify);
|
1214
|
} else {
|
1215
|
logger.debug("About to update notification");
|
1216
|
notificationHandler.updateNotificationPreferences(openaireId, userMail, frequency, notify);
|
1217
|
}
|
1218
|
} else {
|
1219
|
Response.status(Response.Status.FORBIDDEN).entity(compose403Message("Forbidden: You don't have permission to access. You are not registered."))
|
1220
|
.type(MediaType.APPLICATION_JSON)
|
1221
|
.build();
|
1222
|
}
|
1223
|
} catch (SQLStoreException|Exception e) {
|
1224
|
logger.error("Could not save or update notification preferences for user with mail " + userMail, e);
|
1225
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(compose500Message("Fail to fetch notification preferences" +
|
1226
|
" for user with e-mail " + userMail + ".", e)).type(MediaType.APPLICATION_JSON).build();
|
1227
|
}
|
1228
|
|
1229
|
return Response.status(200).entity(compose204Message("Save or Update for notification successful")).type(MediaType.APPLICATION_JSON).build();
|
1230
|
}
|
1231
|
return Response.status(Response.Status.FORBIDDEN).entity(compose403Message("Forbidden: You don't have permission to access. You are not registered."))
|
1232
|
.type(MediaType.APPLICATION_JSON)
|
1233
|
.build();
|
1234
|
}
|
1235
|
|
1236
|
private String xml2Json(List<String> input) {
|
1237
|
StringBuilder builder = new StringBuilder();
|
1238
|
for(String category: input) {
|
1239
|
builder.append(category);
|
1240
|
}
|
1241
|
return builder.toString();
|
1242
|
}
|
1243
|
|
1244
|
private String compose204Message(String message) {
|
1245
|
return "{ \"status\" : \"error\", \"code\" : \"204\", \"message\" : \" " + message +" \" }";
|
1246
|
}
|
1247
|
|
1248
|
private String compose400Message(String message) {
|
1249
|
return "{ \"status\" : \"error\", \"code\" : \"400\", \"message\" : \" " + message +" \" }";
|
1250
|
}
|
1251
|
|
1252
|
private String compose400Message(String message, Exception exception) {
|
1253
|
return "{ \"status\" : \"error\", \"code\" : \"400\", \"message\" : \" " + message +"\", " +
|
1254
|
"\"description\" : \""+ exception.getMessage() +"\" }";
|
1255
|
}
|
1256
|
|
1257
|
private String compose403Message(String message) {
|
1258
|
return "{ \"status\" : \"error\", \"code\" : \"403\", \"message\" : \" " + message +"\", " +
|
1259
|
"\"description\" : \"\" }";
|
1260
|
}
|
1261
|
|
1262
|
private String compose404BulkDeleteMessage(String message, List<String> deletedIds, List<String> notFoundIds) {
|
1263
|
return "{ \"status\" : \"error\", \"code\" : \"404\", \"message\" : \" " + message +" \","+ "\"deletedIds\" : " + new Gson().toJson(deletedIds) +","+ "\"notFoundIds\" : " + new Gson().toJson(notFoundIds) + " }";
|
1264
|
}
|
1265
|
private String compose404Message(String message) {
|
1266
|
return "{ \"status\" : \"error\", \"code\" : \"404\", \"message\" : \" " + message +" \" }";
|
1267
|
}
|
1268
|
|
1269
|
private String compose400BulkInsertMessage(String message, List<String> insertedIds,JsonArray errorInClaims) {
|
1270
|
return "{ \"status\" : \"error\", \"code\" : \"400\", \"message\" : \" " + message +" \""+ ", \"insertedIds\" : " + new Gson().toJson(insertedIds) +","+ "\"errorInClaims\" : " + new Gson().toJson(errorInClaims) + " }";
|
1271
|
}
|
1272
|
|
1273
|
private String compose500Message(String message, Throwable throwable) {
|
1274
|
return "{ \"status\" : \"fail\", \"code\" : \"500\", \"message\" : \" " + message + "\", " +
|
1275
|
"\"description\" : \""+ throwable.getMessage() +"\" }";
|
1276
|
}
|
1277
|
|
1278
|
private String compose500BulkInsertMessage(String message, List<String> insertedIds, JsonArray errorInClaims) {
|
1279
|
return "{ \"status\" : \"fail\", \"code\" : \"500\", \"message\" : \" " + message + "\", " +
|
1280
|
"\"description\" : \""+ "\" , " + "\"insertedIds\" : " + new Gson().toJson(insertedIds) +","+ "\"errorInClaims\" : " + new Gson().toJson(errorInClaims) + " }";
|
1281
|
}
|
1282
|
|
1283
|
/*
|
1284
|
TODO check if needed
|
1285
|
private String compose500BulkDeleteMessage(String message, List<String> deletedIds, List<String> notFoundIds, Exception exception) {
|
1286
|
return "{ \"status\" : \"fail\", \"code\" : \"500\", \"message\" : \" " + message + "\", " +
|
1287
|
"\"description\" : \""+ exception.getMessage() +"\" ," + "\"deletedIds\" : " + new Gson().toJson(deletedIds) +","+ "\"notFoundIds\" : " + new Gson().toJson(notFoundIds) + " }";
|
1288
|
}
|
1289
|
|
1290
|
TODO check if needed
|
1291
|
private String compose200Message(){
|
1292
|
return " { \"status\" : \"success\", \"code\": \"200\" }";
|
1293
|
}
|
1294
|
|
1295
|
TODO check if needed
|
1296
|
private String compose204Message(){
|
1297
|
return " { \"status\" : \"success\", \"code\": \"204\" }";
|
1298
|
} */
|
1299
|
|
1300
|
private String compose204BulkDeleteMessage(List<String> deletedIds, List<String> notFoundIds){
|
1301
|
return " { \"status\" : \"success\", \"code\": \"204\", "+ "\"deletedIds\" : " + new Gson().toJson(deletedIds) +","+ "\"notFoundIds\" : " + new Gson().toJson(notFoundIds) + "}";
|
1302
|
}
|
1303
|
|
1304
|
|
1305
|
private String compose201PostMessage(@Context HttpServletRequest request, String claimId){
|
1306
|
String url = request.getRequestURL().toString();
|
1307
|
//String query = request.getQueryString();
|
1308
|
|
1309
|
return " { \"status\" : \"success\", \"code\": \"201\", \"link\": \"" + url +"/"+ claimId +"\" }";
|
1310
|
}
|
1311
|
private String compose201BulkInsertMessage(List<String> insertedIds, JsonArray errorInClaims){
|
1312
|
//String url = request.getRequestURL().toString();
|
1313
|
//String query = request.getQueryString();
|
1314
|
|
1315
|
return " { \"status\" : \"success\", \"code\": \"201\","+ "\"insertedIds\" : " + new Gson().toJson(insertedIds) +","+ "\"errorInClaims\" : " + new Gson().toJson(errorInClaims)+ "}";
|
1316
|
}
|
1317
|
private String composeDataResponse(HttpServletRequest request, List<Claim> claims, int total, int offset, int limit) {
|
1318
|
if(offset != -1 && limit != -1) { // if offset and limit are -1, no paging in the response
|
1319
|
return " { \"status\" : \"success\", \"code\": \"200\", "+composeTotalResults(total)+", " + composePaging(request, total, offset, limit) + ", "
|
1320
|
+ "\"data\" : " + new Gson().toJson(claims) + " }";
|
1321
|
} else {
|
1322
|
return " { \"status\" : \"success\", \"code\": \"200\", "+composeTotalResults(total)+", "
|
1323
|
+ "\"data\" : " + new Gson().toJson(claims) + " }";
|
1324
|
}
|
1325
|
}
|
1326
|
|
1327
|
private String composeDataResponse(String xml) {
|
1328
|
return " { \"status\" : \"success\", \"code\": \"200\", "
|
1329
|
+ "\"data\" : " + XML.toJSONObject(xml).toString() + " }";
|
1330
|
}
|
1331
|
|
1332
|
private String composeDataResponse(Claim claim) {
|
1333
|
return " { \"status\" : \"success\", \"code\": \"200\", " + "\"data\" : " + new Gson().toJson(claim) + " }";
|
1334
|
}
|
1335
|
|
1336
|
private String composeDataResponse(List<Notification> notifications) {
|
1337
|
return " { \"status\" : \"success\", \"code\": \"200\", " + "\"data\" : " + new Gson().toJson(notifications) + " }";
|
1338
|
}
|
1339
|
|
1340
|
private static String composePaging(HttpServletRequest request, int total, int currentOffset, int limit) {
|
1341
|
logger.info("total " + total);
|
1342
|
logger.info("currentOffset " + currentOffset);
|
1343
|
logger.info("limit " + limit);
|
1344
|
|
1345
|
String url = request.getRequestURL().toString();
|
1346
|
//String query = request.getQueryString();
|
1347
|
|
1348
|
String first = url+"?offset=0&limit=20";
|
1349
|
|
1350
|
int previousPage;
|
1351
|
int nextPage;
|
1352
|
int lastPage;
|
1353
|
|
1354
|
if (total <= limit) {
|
1355
|
lastPage = 0;
|
1356
|
} else {
|
1357
|
if(total%limit == 0) {
|
1358
|
lastPage = total/limit-1;
|
1359
|
} else {
|
1360
|
lastPage = total/limit ;
|
1361
|
}
|
1362
|
}
|
1363
|
String last = url+"?offset=" + lastPage + "&limit=20";
|
1364
|
|
1365
|
if (currentOffset-1 <= 0) {
|
1366
|
previousPage = 0;
|
1367
|
} else {
|
1368
|
previousPage = currentOffset-1;
|
1369
|
}
|
1370
|
String previous = url+"?offset=" + previousPage + "&limit=20";
|
1371
|
|
1372
|
if (currentOffset+1 >= lastPage) {
|
1373
|
nextPage = lastPage;
|
1374
|
|
1375
|
} else {
|
1376
|
nextPage = currentOffset + 1;
|
1377
|
}
|
1378
|
String next = url+"?offset=" + nextPage + "&limit=20";
|
1379
|
|
1380
|
return "\"paging\": [" + "{\"rel\":\"first\", \"href\":\"" + first +"\"}, {\"rel\":\"last\", \"href\":\"" + last + "\"}, {\"rel\":\"previous\", \"href\": \"" + previous + "\"}, {\"rel\":\"next\", \"href\":\"" + next +"\"}"+ "]";
|
1381
|
}
|
1382
|
private String composeTotalResults(int total) {
|
1383
|
return "\"total\": \""+total+"\"";
|
1384
|
}
|
1385
|
|
1386
|
public static void main(String[] args) {
|
1387
|
|
1388
|
/*
|
1389
|
EmailValidator emailValidator = EmailValidator.getInstance();
|
1390
|
System.out.println(emailValidator.isValid("jate@gdddd"));
|
1391
|
|
1392
|
InternetAddress emailAddr = null;
|
1393
|
try {
|
1394
|
emailAddr = new InternetAddress("jate@gdddd");
|
1395
|
emailAddr.validate();
|
1396
|
} catch (AddressException e) {
|
1397
|
System.out.println("false");
|
1398
|
}
|
1399
|
System.out.println("true");
|
1400
|
|
1401
|
ArrayList<String> insertedIds= new ArrayList<String>();
|
1402
|
insertedIds.add("1");
|
1403
|
insertedIds.add("2");
|
1404
|
insertedIds.add("3");
|
1405
|
System.out.println(helloWorldService.compose403Message("hello"));
|
1406
|
|
1407
|
*/
|
1408
|
// BasicConfigurator.configure();
|
1409
|
// ApplicationContext context = new ClassPathXmlApplicationContext("eu/dnetlib/openaire/rest/springContext-claims-authorization.xml");
|
1410
|
//
|
1411
|
// Authorization authorization = context.getBean(Authorization.class);
|
1412
|
// UserHandler userHandler = context.getBean(UserHandler.class);
|
1413
|
// System.out.println(authorization.getAdminRoles());
|
1414
|
// authorization.isClaimCurator("eyJraWQiOiJvaWRjIiwiYWxnIjoiUlMyNTYifQ.eyJzdWIiOiIwOTMxNzMwMTMyODMzNjMyQG9wZW5taW50ZWQuZXUiLCJhenAiOiIyNGU4MzE3Ni0xMzEyLTRiYTMtYmMwYi1mZmVlYmVhMTYwM2UiLCJpc3MiOiJodHRwczpcL1wvYWFpLm9wZW5taW50ZWQuZXVcL29pZGNcLyIsImV4cCI6MTQ5ODQ4NTk3NiwiaWF0IjoxNDk4NDcxNTc2LCJqdGkiOiJkMWRlZjc1Yi00MTEyLTRiZDktYTIyNi0wZThhOWI2M2Y3MWQifQ.WVYOb_yO8OaxIIt2jRYEDQBhGGFRDTBw3DgtVV_smuN5yx1ScCj6aehLu3JKPSArme4m2SGF4TEGhpwNJkwhM2WapGtxmtuCmCzYIo_QlC1Yki9hr2OT2rXMcQsJCiKaBSf6pLue6Sn78GMB5yaUTvOQHRgidXGiZXH5lsuZUx15Q6Equ_wzond_rgP9mRheRkTyIFuvvg4PuzmudBc11Ty863vIIQtoWF7_p98zTbHxiNF9lLPwzPZKxDoQ8JeayQEC-jsWVLgxmp-h0jG_Ko5jFVVJeeosqMMucOrs2FT_NKHVYVqB6VVh0C6nOufeiLrNDeMUlDT4dAvKD2zE9w");
|
1415
|
|
1416
|
}
|
1417
|
|
1418
|
}
|