Changeset 5030


Ignore:
Timestamp:
04/23/14 15:20:54 (10 years ago)
Author:
olhsha@mpi.nl
Message:

Refactoring principal: making one wrapper out of two.

Location:
DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/Helpers.java

    r4997 r5030  
    4545        String result = (new StringBuilder(text)).toString();
    4646        for (String tempTarget : pairs.keySet()) {
    47             result = result.replaceAll(tempTarget, pairs.get(tempTarget));
     47            if (tempTarget != null) {
     48                if (!tempTarget.trim().equals("")) {
     49                    result = result.replaceAll(tempTarget, pairs.get(tempTarget));
     50                }
     51            }
    4852        }
    4953        return result;
     
    6569        return result;
    6670    }
    67    
     71
    6872    public static String welcomeString(String baseUri) {
    6973        String welcome = "<!DOCTYPE html><body>"
     
    7478        return welcome;
    7579    }
    76    
     80
    7781    public static Principal createPrincipalElement(String name, String e_mail) {
    7882        Principal result = new Principal();
     
    8185        return result;
    8286    }
    83    
     87
    8488    public static String hashPswd(String pswd, int strength, String salt) {
    85     ShaPasswordEncoder encoder = new ShaPasswordEncoder(strength);
    86     return encoder.encodePassword(pswd, salt);
     89        ShaPasswordEncoder encoder = new ShaPasswordEncoder(strength);
     90        return encoder.encodePassword(pswd, salt);
    8791    }
    8892}
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/rest/PrincipalResource.java

    r5022 r5030  
    7474        Map params = new HashMap<String, String>();
    7575        params.put("externalId", externalIdentifier);
    76         Principal principal = (Principal) this.wrapGetResource(params, new GetPrincipal());
    77         if (principal != null) {
    78             return new ObjectFactory().createPrincipal(principal);
    79         } else {
    80             return new ObjectFactory().createPrincipal(new Principal());
    81         }
     76        return this.wrapPrincipalRequest(params, new GetPrincipal());
    8277    }
    8378
     
    10196        Map params = new HashMap<String, String>();
    10297        params.put("email", email);
    103         Principal principal = (Principal) this.wrapGetResource(params, new GetPrincipalByInfo());
    104         if (principal != null) {
    105             return new ObjectFactory().createPrincipal(principal);
    106         } else {
    107             return new ObjectFactory().createPrincipal(new Principal());
    108         }
    109 
     98        return this.wrapPrincipalRequest(params, new GetPrincipalByInfo());
    11099    }
    111100
     
    118107        params.put("externalId", externalIdentifier);
    119108        params.put("resource", this);
    120         CurrentPrincipalInfo result = (CurrentPrincipalInfo) this.wrapGetResource(params, new GetCurrentPrincipalInfo());
    121         if (result != null) {
    122             return new ObjectFactory().createCurrentPrincipalInfo(result);
    123         } else {
    124             return new ObjectFactory().createCurrentPrincipalInfo(new CurrentPrincipalInfo());
    125         }
     109        return this.wrapCurrentPrincipalInfoRequest(params, new GetCurrentPrincipalInfo());
     110       
    126111    }
    127112
     
    162147
    163148        if (dbIntegrityService.getTypeOfPrincipalAccount(remotePrincipalID).equals(admin)) {
    164             return this.wrapAddOrUpdatePrincipal(params, new AddPrincipal());
     149            return this.wrapPrincipalRequest(params, new AddPrincipal());
    165150        } else {
    166151            verboseOutput.ADMIN_RIGHTS_EXPECTED();
     
    190175        dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString());
    191176
    192         return this.wrapAddOrUpdatePrincipal(params, new RegisterNonShibbolizedPrincipal());
     177        return this.wrapPrincipalRequest(params, new RegisterNonShibbolizedPrincipal());
    193178
    194179    }
     
    209194        params.put("newPrincipal", newPrincipal);
    210195
    211         return this.wrapAddOrUpdatePrincipal(params, new AddPrincipal());
     196        return this.wrapPrincipalRequest(params, new AddPrincipal());
    212197    }
    213198
     
    236221
    237222        if (dbIntegrityService.getTypeOfPrincipalAccount(remotePrincipalID).equals(admin)) {
    238             return this.wrapAddOrUpdatePrincipal(params, new UpdatePrincipal());
     223            return this.wrapPrincipalRequest(params, new UpdatePrincipal());
    239224        } else {
    240225            verboseOutput.ADMIN_RIGHTS_EXPECTED();
     
    264249        params.put("newPrincipal", newPrincipal);
    265250
    266         return this.wrapAddOrUpdatePrincipal(params, new UpdatePrincipal());
     251        return this.wrapPrincipalRequest(params, new UpdatePrincipal());
    267252    }
    268253
     
    366351
    367352   
    368     private class GetPrincipal implements ILambda<Map<String, String>, Principal> {
    369 
    370         @Override
    371         public Principal apply(Map<String, String> params) throws NotInDataBaseException, PrincipalExists {
    372             final Number principalID = dbIntegrityService.getResourceInternalIdentifier(UUID.fromString(params.get("externalId")), Resource.PRINCIPAL);
     353    private class GetPrincipal implements ILambda<Map, Principal> {
     354
     355        @Override
     356        public Principal apply(Map params) throws NotInDataBaseException, PrincipalExists {
     357            final Number principalID = dbIntegrityService.getResourceInternalIdentifier(UUID.fromString((String) params.get("externalId")), Resource.PRINCIPAL);
    373358            return dbIntegrityService.getPrincipal(principalID);
    374359        }
    375360    }
    376361
    377     private class GetPrincipalByInfo implements ILambda<Map<String, String>, Principal> {
    378 
    379         @Override
    380         public Principal apply(Map<String, String> params) throws NotInDataBaseException, PrincipalExists {
    381             return dbIntegrityService.getPrincipalByInfo(params.get("email"));
     362    private class GetPrincipalByInfo implements ILambda<Map, Principal> {
     363
     364        @Override
     365        public Principal apply(Map params) throws NotInDataBaseException, PrincipalExists {
     366            return dbIntegrityService.getPrincipalByInfo((String) params.get("email"));
    382367        }
    383368    }
     
    396381
    397382    //// wrappers that map main working methods into exception handling ////
    398     private JAXBElement<Principal> wrapAddOrUpdatePrincipal(Map params, ILambda<Map, Principal> changer) throws IOException {
     383   
     384   
     385   
     386   
     387    private JAXBElement<Principal> wrapPrincipalRequest(Map params, ILambda<Map, Principal> dbRequestor) throws IOException {
    399388        Number remotePrincipalID = this.getPrincipalID();
    400389        if (remotePrincipalID == null) {
     
    403392        try {
    404393            try {
    405                 return new ObjectFactory().createPrincipal(changer.apply(params));
     394                return new ObjectFactory().createPrincipal(dbRequestor.apply(params));
    406395            } catch (NotInDataBaseException e1) {
    407396                loggerServer.debug(e1.toString());
     
    416405
    417406    }
     407   
     408     private JAXBElement<CurrentPrincipalInfo> wrapCurrentPrincipalInfoRequest(Map params, ILambda<Map, CurrentPrincipalInfo> dbRequestor) throws IOException {
     409        Number remotePrincipalID = this.getPrincipalID();
     410        if (remotePrincipalID == null) {
     411            return new ObjectFactory().createCurrentPrincipalInfo(new CurrentPrincipalInfo());
     412        }
     413        try {
     414            try {
     415                return new ObjectFactory().createCurrentPrincipalInfo(dbRequestor.apply(params));
     416            } catch (NotInDataBaseException e1) {
     417                loggerServer.debug(e1.toString());
     418                httpServletResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e1.toString());
     419                return new ObjectFactory().createCurrentPrincipalInfo(new CurrentPrincipalInfo());
     420            }
     421        } catch (PrincipalExists e) {
     422            loggerServer.debug(e.toString());
     423            httpServletResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.toString());
     424            return new ObjectFactory().createCurrentPrincipalInfo(new CurrentPrincipalInfo());
     425        }
     426
     427    }
    418428}
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/rest/ResourceResource.java

    r5022 r5030  
    101101    }
    102102   
    103     protected T wrapGetResource(Map params, ILambda<Map, T> getter) throws IOException {
    104         Number remotePrincipalID = this.getPrincipalID();
    105         if (remotePrincipalID == null) {
    106             return null;
    107         }
    108         try {
    109             try {
    110                 return getter.apply(params);
    111             } catch (NotInDataBaseException e1) {
    112                 loggerServer.debug(e1.toString());
    113                 httpServletResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e1.toString());
    114                 return null;
    115             }
    116         } catch (PrincipalExists e) {
    117             loggerServer.debug(e.toString());
    118             httpServletResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.toString());
    119             return null;
    120         }
    121 
    122     }
     103   
    123104}
Note: See TracChangeset for help on using the changeset viewer.