Project

General

Profile

« Previous | Next » 

Revision 58338

Create functions for common code:
Authorization: hasBasicAuthorization
Claims Service: getInfoAndBuildClaim that gets jsonObjects and extracts the information needed for "buildAndInsertClaim"

View differences:

modules/uoa-claims-api/trunk/src/main/java/eu/dnetlib/openaire/rest/Authorization.java
146 146
        return false;
147 147
    }
148 148

  
149
    public boolean hasBasicAuthorization(String token, String origin, String  cookie){
150
        if(token == null || token.isEmpty() || cookie == null || cookie.isEmpty() || !cookie.equals(token)|| !hasValidOrigin(origin)){
151
            return false;
152
        }
153
        return true;
154
    }
155

  
149 156
    public List<String> getRegisteredRoles() {
150 157
        return registeredRoles;
151 158
    }
modules/uoa-claims-api/trunk/src/main/java/eu/dnetlib/openaire/rest/ClaimsService.java
10 10
import eu.dnetlib.data.emailSender.EmailSender;
11 11
import org.apache.commons.validator.EmailValidator;
12 12
import org.apache.log4j.Logger;
13
import org.json.JSONObject;
13 14
import org.json.XML;
14 15
import org.springframework.beans.factory.annotation.Autowired;
15 16
import org.springframework.stereotype.Component;
17
import org.springframework.web.bind.annotation.CrossOrigin;
16 18

  
17 19
import javax.servlet.http.HttpServletRequest;
18 20
import javax.ws.rs.*;
......
28 30
 * Created by kiatrop on 15/4/2016.
29 31
 */
30 32
@Component
33
@CrossOrigin(origins = "*")
31 34
@Path("/claimsService")
32 35
public class ClaimsService {
33 36

  
......
514 517
               .build();
515 518
    }
516 519

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

  
564 520
    @DELETE
565
    @Path("/claims/{claimId}")
566
    public Response deleteClaim(@PathParam("claimId") String claimId) {
567

  
568
        if (claimId == null || claimId.isEmpty()) {
569
            return Response.status(Response.Status.NOT_FOUND).entity(compose404Message("Claim id cannot be empty.")).header("Access-Control-Allow-Origin", "*")
570
                    .header("Access-Control-Allow-Methods", "DELETE")
571
                    .type(MediaType.APPLICATION_JSON).build();
572
        }
573

  
574
        try {
575
            if(claimHandler.deleteClaim(claimId)) {
576
                return Response.status(204).entity(compose204Message()).header("Access-Control-Allow-Origin", "*")
577
                        .header("Access-Control-Allow-Methods", "DELETE").type(MediaType.APPLICATION_JSON).build();
578
            }
579

  
580
        } catch (Exception e) {return Response.status(Response.Status.UNAUTHORIZED).entity(compose404Message("Not valid Token"))
581
                    .type(MediaType.APPLICATION_JSON)
582
                    .build();
583
            logger.error("Fail to delete claim with id " + claimId + ".", e);
584
            return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(compose500Message("Fail to delete claim with id " + claimId +".", e))
585
                    .header("Access-Control-Allow-Origin", "*")
586
                    .header("Access-Control-Allow-Methods", "DELETE")
587
                    .type(MediaType.APPLICATION_JSON).build();
588
        }
589

  
590
        return Response.status(Response.Status.NOT_FOUND).entity(compose404Message("Claim id cannot be empty.")).header("Access-Control-Allow-Origin", "*")
591
                .header("Access-Control-Allow-Methods", "DELETE")
592
                .type(MediaType.APPLICATION_JSON).build();
593
    }
594
 */
595
    @DELETE
596 521
    @Path("/claims/bulk")
597 522
    @Produces(MediaType.APPLICATION_JSON)
598 523
    public Response deleteBulkClaims(@QueryParam("claimId") List<String> claimIds,
......
602 527

  
603 528

  
604 529

  
605
         if(token == null || token.isEmpty() || cookie == null || cookie.isEmpty() || !cookie.equals(token)|| !authorization.hasValidOrigin(origin)){
530
        if(!authorization.hasBasicAuthorization(token, origin, cookie)){
606 531
             return Response.status(Response.Status.FORBIDDEN).entity(compose403Message("Forbidden: You don't have permission to access. Maybe you are not registered."))
607 532
                     .type(MediaType.APPLICATION_JSON)
608 533
                     .build();
......
665 590
                             @CookieParam("AccessToken") String  cookie) {
666 591

  
667 592

  
668
        if(token == null || token.isEmpty() || cookie == null || cookie.isEmpty() || !cookie.equals(token) || !authorization.hasValidOrigin(origin)){
593
        if(!authorization.hasBasicAuthorization(token, origin, cookie)){
669 594
            return Response.status(Response.Status.FORBIDDEN).entity(compose403Message("Forbidden: You don't have permission to access. Maybe you are not registered."))
670 595
                    .type(MediaType.APPLICATION_JSON)
671 596
                    .build();
......
678 603
            String claimedBy = userInfo.getEmail();
679 604
            logger.info("claimedBy " + claimedBy);
680 605

  
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 606
            EmailValidator emailValidator = EmailValidator.getInstance();
706 607
            if (!emailValidator.isValid(claimedBy)) {
707 608
                return Response.status(Response.Status.BAD_REQUEST).entity(compose400Message("User e-mail is invalid."))
......
710 611

  
711 612

  
712 613
            try {
713
                String claimId = claimHandler.buildAndInsertClaim(claimedBy, sourceType, sourceId, sourceCollectedFrom, sourceAccessRights, sourceEmbargoEndDate, targetType, targetId, targetCollectedFrom, targetAccessRights, targetEmbargoEndDate);
614
                String claimId = this.getInfoAndBuildClaim(jsonObject,claimedBy);
714 615
                return Response.status(200).entity(compose201PostMessage(request, claimId)).type(MediaType.APPLICATION_JSON).build();
715 616

  
716 617
            } catch (ClaimValidationException ve) {
......
738 639
                                  @CookieParam("AccessToken") String  cookie) {
739 640

  
740 641

  
741
        if(token == null || token.isEmpty() || cookie == null || cookie.isEmpty() || !cookie.equals(token)|| !authorization.hasValidOrigin(origin)){
642
        if(!authorization.hasBasicAuthorization(token, origin, cookie)){
742 643
            return Response.status(Response.Status.FORBIDDEN).entity(compose403Message("Forbidden: You don't have permission to access. Maybe you are not registered."))
743 644
                    .type(MediaType.APPLICATION_JSON)
744 645
                    .build();
......
757 658
            for (JsonElement je : jsonArray) {
758 659
                JsonObject jsonObject = je.getAsJsonObject();
759 660

  
760
                logger.info("targetId " + jsonObject.toString());
761 661
                String claimedBy = userInfo.getEmail();
762 662
                logger.info("claimedBy " + claimedBy);
663
                logger.debug(jsonObject);
763 664

  
764
                String sourceId = jsonObject.get("sourceId").getAsString();
765
                logger.info("sourceId " + sourceId);
766
                String sourceType = jsonObject.get("sourceType").getAsString();
767
                logger.info("sourceType " + sourceType);
768
                String sourceCollectedFrom = jsonObject.get("sourceCollectedFrom").getAsString();
769
                logger.info("sourceCollectedFrom " + sourceCollectedFrom);
770
                String sourceAccessRights = jsonObject.get("sourceAccessRights").getAsString();
771
                logger.info("sourceAccessRights " + sourceAccessRights);
772
                String sourceEmbargoEndDate = jsonObject.get("sourceEmbargoEndDate").getAsString();
773
                sourceEmbargoEndDate = (sourceEmbargoEndDate != null && sourceEmbargoEndDate.equals("")) ? null : sourceEmbargoEndDate;
774
                logger.info("sourceEmbargoEndDate " + sourceEmbargoEndDate);
775 665

  
776
                String targetId = jsonObject.get("targetId").getAsString();
777
                logger.info("targetId " + targetId);
778
                String targetType = jsonObject.get("targetType").getAsString();
779
                logger.info("targetType " + targetType);
780
                String targetCollectedFrom = jsonObject.get("targetCollectedFrom").getAsString();
781
                logger.info("targetCollectedFrom " + targetCollectedFrom);
782
                String targetAccessRights = jsonObject.get("targetAccessRights").getAsString();
783
                logger.info("targetAccessRights " + targetAccessRights);
784
                String targetEmbargoEndDate = jsonObject.get("targetEmbargoEndDate").getAsString();
785
                targetEmbargoEndDate = (targetEmbargoEndDate != null && targetEmbargoEndDate.equals("")) ? null : targetEmbargoEndDate;
786
                logger.info("targetEmbargoEndDate " + targetEmbargoEndDate);
787

  
788 666
                EmailValidator emailValidator = EmailValidator.getInstance();
789 667
                if (!emailValidator.isValid(claimedBy)) {
790 668
                    jsonObject.addProperty("error", "user");
669
                    logger.error("no valid user");
791 670
                    //                return Response.status(Response.Status.BAD_REQUEST).entity(compose400Message("User e-mail is invalid."))
792 671
                    //                        .type(MediaType.APPLICATION_JSON).build();
793 672
                    code400++;
......
796 675

  
797 676

  
798 677
                try {
799
                    String claimId = claimHandler.buildAndInsertClaim(claimedBy, sourceType, sourceId, sourceCollectedFrom, sourceAccessRights, sourceEmbargoEndDate, targetType, targetId, targetCollectedFrom, targetAccessRights, targetEmbargoEndDate);
678
                    String claimId = this.getInfoAndBuildClaim(jsonObject,claimedBy);
800 679
                    insertedIds.add(claimId);
801 680
                    code200++;
802 681
                    //                return Response.status(200).entity(compose201PostMessage(request, claimId)).type(MediaType.APPLICATION_JSON).build();
803 682

  
804 683
                } catch (ClaimValidationException ve) {
684
//                    logger.error("Validation Failed fo claim "+jsonObject);
805 685
                    //                return Response.status(Response.Status.BAD_REQUEST).entity(compose400Message("The given ids are wrong.", ve))
806 686
                    //                        .type(MediaType.APPLICATION_JSON).build();
807 687
                    jsonObject.addProperty("error", "validation");
......
809 689
                    code400++;
810 690

  
811 691
                } catch (SQLStoreException|Exception e) {
812
                    //                logger.error("Fail to add new claim.", e);
692
                                    logger.error("Fail to add new claim.", e);
813 693
                    //                return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(compose500Message("Fail to add new claim.", e))
814 694
                    //                        .type(MediaType.APPLICATION_JSON).build();
815 695
                    jsonObject.addProperty("error", "insertion");
......
842 722
                                     @CookieParam("AccessToken") String  cookie) {
843 723

  
844 724

  
845
        if(token == null || token.isEmpty() || cookie == null || cookie.isEmpty() || !cookie.equals(token)|| !authorization.hasValidOrigin(origin)){
725
        if(!authorization.hasBasicAuthorization(token, origin, cookie)){
846 726
            return Response.status(Response.Status.FORBIDDEN).entity(compose403Message("Forbidden: You don't have permission to access. Maybe you are not registered."))
847 727
                    .type(MediaType.APPLICATION_JSON)
848 728
                    .build();
......
910 790
                                    @CookieParam("AccessToken") String  cookie) {
911 791

  
912 792

  
913
        if(token == null || token.isEmpty() || cookie == null || cookie.isEmpty() || !cookie.equals(token)|| !authorization.hasValidOrigin(origin)){
793
        if(!authorization.hasBasicAuthorization(token, origin, cookie)){
914 794

  
915 795
            return Response.status(Response.Status.FORBIDDEN).entity(compose403Message("Forbidden: You don't have permission to access. Maybe you are not registered."))
916 796
                    .type(MediaType.APPLICATION_JSON)
......
1057 937
                                     @CookieParam("AccessToken") String  cookie) {
1058 938

  
1059 939

  
1060
        if(token == null || token.isEmpty() || cookie == null || cookie.isEmpty() || !cookie.equals(token)|| !authorization.hasValidOrigin(origin)){
940
        if(!authorization.hasBasicAuthorization(token, origin, cookie)){
1061 941
            return Response.status(Response.Status.FORBIDDEN).entity(compose403Message("Forbidden: You don't have permission to access. Maybe you are not registered."))
1062 942
                    .type(MediaType.APPLICATION_JSON)
1063 943
                    .build();
......
1334 1214
        list.add("argirok@di.uoa.gr");
1335 1215
        emailSender.send("openaire_id_test", "openaire_name_test", "community", list);
1336 1216
    }
1217

  
1218
    private String getvalueOf(JsonObject jsonObject, String field){
1219
        String value = (jsonObject.get(field) != null && !jsonObject.get(field).isJsonNull())?jsonObject.get(field).getAsString():null;
1220
        logger.info(field + ": " + value);
1221
        return value;
1222

  
1223

  
1224
    }
1225
    private String getvalueOfDefault(JsonObject jsonObject, String field, String defaultValue){
1226
        String value = this.getvalueOf(jsonObject, field);
1227
        logger.debug("Field:"+field+"->"+value+"<-");
1228
        return (value!=null && !value.equals("")?value:defaultValue);
1229
    }
1230

  
1231
    private String getInfoAndBuildClaim(JsonObject jsonObject, String claimedBy ) throws SQLStoreException, Exception {
1232
        String claimedInDashboard = getvalueOfDefault(jsonObject, "claimedInDashboard", null);
1233

  
1234
        String sourceId = getvalueOf(jsonObject, "sourceId");
1235
        String sourceType = getvalueOf(jsonObject, "sourceType");
1236
        String sourceCollectedFrom = getvalueOf(jsonObject, "sourceCollectedFrom");
1237
        String sourceAccessRights = getvalueOf(jsonObject, "sourceAccessRights");
1238
        String sourceEmbargoEndDate = getvalueOfDefault(jsonObject, "sourceEmbargoEndDate", null);
1239

  
1240
        String targetId = getvalueOf(jsonObject, "targetId");
1241
        String targetType = getvalueOf(jsonObject, "targetType");
1242
        String targetCollectedFrom = getvalueOf(jsonObject, "targetCollectedFrom");
1243
        String targetAccessRights = getvalueOf(jsonObject, "targetAccessRights");
1244
        String targetEmbargoEndDate = getvalueOfDefault(jsonObject, "targetEmbargoEndDate", null);
1245

  
1246

  
1247
        logger.debug("Claimed in"+claimedInDashboard);
1248
        return claimHandler.buildAndInsertClaim(claimedBy, sourceType, sourceId, sourceCollectedFrom, sourceAccessRights, sourceEmbargoEndDate, targetType, targetId, targetCollectedFrom, targetAccessRights, targetEmbargoEndDate,claimedInDashboard);
1249
    }
1250
    
1251
  
1337 1252
}

Also available in: Unified diff