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