Changeset 5053


Ignore:
Timestamp:
04/24/14 16:34:51 (10 years ago)
Author:
olhsha@mpi.nl
Message:

Making a separate class for wrappers (wrapping exceptions around actual DB requests)

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

Legend:

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

    r5022 r5053  
    1919
    2020import eu.dasish.annotation.backend.NotInDataBaseException;
    21 import eu.dasish.annotation.backend.PrincipalExists;
    2221
    2322/**
     
    2726public interface ILambda<Map, R> {
    2827   
    29     public R apply(Map params)   throws NotInDataBaseException, PrincipalExists;
     28    public R apply(Map params)   throws NotInDataBaseException;
    3029   
    3130}
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/rest/AnnotationResource.java

    r4969 r5053  
    2020import eu.dasish.annotation.backend.BackendConstants;
    2121import eu.dasish.annotation.backend.NotInDataBaseException;
     22import eu.dasish.annotation.backend.PrincipalExists;
    2223import eu.dasish.annotation.backend.Resource;
     24import eu.dasish.annotation.backend.dao.ILambda;
    2325import eu.dasish.annotation.schema.Annotation;
    2426import eu.dasish.annotation.schema.AnnotationBody;
     
    3133import java.io.IOException;
    3234import java.util.Arrays;
     35import java.util.Map;
    3336import java.util.UUID;
    3437import javax.servlet.http.HttpServletRequest;
     
    414417        }
    415418    }
     419   
     420   ////////////////////////////////////////////////
     421   
     422   
     423   
    416424}
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/rest/PrincipalResource.java

    r5030 r5053  
    2323import eu.dasish.annotation.backend.Resource;
    2424import eu.dasish.annotation.backend.dao.ILambda;
     25import eu.dasish.annotation.backend.dao.ILambdaPrincipal;
    2526import eu.dasish.annotation.schema.CurrentPrincipalInfo;
    2627import eu.dasish.annotation.schema.ObjectFactory;
     
    7475        Map params = new HashMap<String, String>();
    7576        params.put("externalId", externalIdentifier);
    76         return this.wrapPrincipalRequest(params, new GetPrincipal());
     77        Principal result = (Principal) (new RequestWrappers(loggerServer, httpServletResponse, this, dbIntegrityService)).wrapRequestResource(params, new GetPrincipal());
     78        return (result != null) ? (new ObjectFactory().createPrincipal(result)) : (new ObjectFactory().createPrincipal(new Principal()));
    7779    }
    7880
     
    9698        Map params = new HashMap<String, String>();
    9799        params.put("email", email);
    98         return this.wrapPrincipalRequest(params, new GetPrincipalByInfo());
     100        Principal result = (Principal) (new RequestWrappers(loggerServer, httpServletResponse, this, dbIntegrityService)).wrapRequestResource(params, new GetPrincipalByInfo());
     101        return (result != null) ? (new ObjectFactory().createPrincipal(result)) : (new ObjectFactory().createPrincipal(new Principal()));
    99102    }
    100103
     
    107110        params.put("externalId", externalIdentifier);
    108111        params.put("resource", this);
    109         return this.wrapCurrentPrincipalInfoRequest(params, new GetCurrentPrincipalInfo());
    110        
     112        CurrentPrincipalInfo result = (CurrentPrincipalInfo) (new RequestWrappers(loggerServer, httpServletResponse, this, dbIntegrityService)).wrapRequestResource(params, new GetCurrentPrincipalInfo());
     113        return (result != null) ? (new ObjectFactory().createCurrentPrincipalInfo(result)) : (new ObjectFactory().createCurrentPrincipalInfo(new CurrentPrincipalInfo()));
    111114    }
    112115
     
    147150
    148151        if (dbIntegrityService.getTypeOfPrincipalAccount(remotePrincipalID).equals(admin)) {
    149             return this.wrapPrincipalRequest(params, new AddPrincipal());
     152            return (new RequestWrappers(loggerServer, httpServletResponse, this, dbIntegrityService)).wrapAddPrincipalRequest(params, new AddPrincipal());
    150153        } else {
    151154            verboseOutput.ADMIN_RIGHTS_EXPECTED();
     
    174177
    175178        dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString());
    176 
    177         return this.wrapPrincipalRequest(params, new RegisterNonShibbolizedPrincipal());
    178 
     179        return (new RequestWrappers(loggerServer, httpServletResponse, this, dbIntegrityService)).wrapAddPrincipalRequest(params, new RegisterNonShibbolizedPrincipal());
    179180    }
    180181
     
    194195        params.put("newPrincipal", newPrincipal);
    195196
    196         return this.wrapPrincipalRequest(params, new AddPrincipal());
     197        dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString());
     198        return (new RequestWrappers(loggerServer, httpServletResponse, this, dbIntegrityService)).wrapAddPrincipalRequest(params, new AddPrincipal());
    197199    }
    198200
     
    221223
    222224        if (dbIntegrityService.getTypeOfPrincipalAccount(remotePrincipalID).equals(admin)) {
    223             return this.wrapPrincipalRequest(params, new UpdatePrincipal());
     225            Principal result = (Principal) (new RequestWrappers(loggerServer, httpServletResponse, this, dbIntegrityService)).wrapRequestResource(params, new UpdatePrincipal());
     226            return (result != null) ? (new ObjectFactory().createPrincipal(result)) : (new ObjectFactory().createPrincipal(new Principal()));
     227
    224228        } else {
    225229            verboseOutput.ADMIN_RIGHTS_EXPECTED();
     
    248252        Map params = new HashMap<String, Object>();
    249253        params.put("newPrincipal", newPrincipal);
    250 
    251         return this.wrapPrincipalRequest(params, new UpdatePrincipal());
     254        Principal result = (Principal) (new RequestWrappers(loggerServer, httpServletResponse, this, dbIntegrityService)).wrapRequestResource(params, new UpdatePrincipal());
     255        return (result != null) ? (new ObjectFactory().createPrincipal(result)) : (new ObjectFactory().createPrincipal(new Principal()));
    252256    }
    253257
     
    319323    }
    320324
    321     /////////////// main working methods that call db-services /////
    322     private class RegisterNonShibbolizedPrincipal implements ILambda<Map, Principal> {
     325    /////////////// main working methods that call db-services and sent as aparameters to correponding wrappers/////
     326    private class RegisterNonShibbolizedPrincipal implements ILambdaPrincipal<Map, Principal> {
    323327
    324328        @Override
     
    330334    }
    331335
    332     private class AddPrincipal implements ILambda<Map, Principal> {
     336    private class AddPrincipal implements ILambdaPrincipal<Map, Principal> {
    333337
    334338        @Override
     
    342346
    343347        @Override
    344         public Principal apply(Map params) throws NotInDataBaseException, PrincipalExists {
     348        public Principal apply(Map params) throws NotInDataBaseException {
    345349            Principal principal = (Principal) params.get("newPrincipal");
    346350            Number principalID = dbIntegrityService.getResourceInternalIdentifierFromURI(principal.getURI(), Resource.PRINCIPAL);
     
    350354    }
    351355
    352    
    353356    private class GetPrincipal implements ILambda<Map, Principal> {
    354357
    355358        @Override
    356         public Principal apply(Map params) throws NotInDataBaseException, PrincipalExists {
     359        public Principal apply(Map params) throws NotInDataBaseException {
    357360            final Number principalID = dbIntegrityService.getResourceInternalIdentifier(UUID.fromString((String) params.get("externalId")), Resource.PRINCIPAL);
    358361            return dbIntegrityService.getPrincipal(principalID);
     
    363366
    364367        @Override
    365         public Principal apply(Map params) throws NotInDataBaseException, PrincipalExists {
     368        public Principal apply(Map params) throws NotInDataBaseException {
    366369            return dbIntegrityService.getPrincipalByInfo((String) params.get("email"));
    367370        }
     
    371374
    372375        @Override
    373         public CurrentPrincipalInfo apply(Map params) throws NotInDataBaseException, PrincipalExists {
     376        public CurrentPrincipalInfo apply(Map params) throws NotInDataBaseException {
    374377            final Number principalID = dbIntegrityService.getResourceInternalIdentifier(UUID.fromString((String) params.get("externalId")), Resource.PRINCIPAL);
    375378            final CurrentPrincipalInfo principalInfo = new CurrentPrincipalInfo();
     
    379382        }
    380383    }
    381 
    382     //// wrappers that map main working methods into exception handling ////
    383    
    384    
    385    
    386    
    387     private JAXBElement<Principal> wrapPrincipalRequest(Map params, ILambda<Map, Principal> dbRequestor) throws IOException {
    388         Number remotePrincipalID = this.getPrincipalID();
    389         if (remotePrincipalID == null) {
    390             return new ObjectFactory().createPrincipal(new Principal());
    391         }
    392         try {
    393             try {
    394                 return new ObjectFactory().createPrincipal(dbRequestor.apply(params));
    395             } catch (NotInDataBaseException e1) {
    396                 loggerServer.debug(e1.toString());
    397                 httpServletResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e1.toString());
    398                 return new ObjectFactory().createPrincipal(new Principal());
    399             }
    400         } catch (PrincipalExists e) {
    401             loggerServer.debug(e.toString());
    402             httpServletResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.toString());
    403             return new ObjectFactory().createPrincipal(new Principal());
    404         }
    405 
    406     }
    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     }
    428384}
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/rest/VerboseOutput.java

    r4969 r5053  
    3535 
    3636
    37     private String _FORBIDDEN_RESOURCE_ACTION(String identifier, String resource, String action, String ownerName, String ownerEMail) {
     37    public String _FORBIDDEN_RESOURCE_ACTION(String identifier, String resource, String action, String ownerName, String ownerEMail) {
    3838        return " The logged-in principal cannot " + action + " the " + resource + " with the identifier " + identifier + ". Contact the resource's owner " + ownerName + " via the e-mail " + ownerEMail;
    3939    }
Note: See TracChangeset for help on using the changeset viewer.