Changeset 5022


Ignore:
Timestamp:
04/22/14 17:23:11 (10 years ago)
Author:
olhsha@mpi.nl
Message:

Refactoring principal: adding higher-order exception handling wrapper that serves a few "main actions" (calling DB). Adding updating principal from the form.

Location:
DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main
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/rest/PrincipalResource.java

    r5014 r5022  
    2222import eu.dasish.annotation.backend.PrincipalExists;
    2323import eu.dasish.annotation.backend.Resource;
     24import eu.dasish.annotation.backend.dao.ILambda;
    2425import eu.dasish.annotation.schema.CurrentPrincipalInfo;
    2526import eu.dasish.annotation.schema.ObjectFactory;
     
    2728import java.io.IOException;
    2829import java.sql.SQLException;
     30import java.util.HashMap;
     31import java.util.Map;
    2932import java.util.UUID;
    3033import javax.servlet.http.HttpServletRequest;
     
    6972    @Transactional(readOnly = true)
    7073    public JAXBElement<Principal> getPrincipal(@PathParam("principalid") String externalIdentifier) throws IOException {
    71         Number remotePrincipalID = this.getPrincipalID();
    72         if (remotePrincipalID == null) {
    73             return new ObjectFactory().createPrincipal(new Principal());
    74         }
    75         try {
    76             final Number principalID = dbIntegrityService.getResourceInternalIdentifier(UUID.fromString(externalIdentifier), Resource.PRINCIPAL);
    77             final Principal principal = dbIntegrityService.getPrincipal(principalID);
     74        Map params = new HashMap<String, String>();
     75        params.put("externalId", externalIdentifier);
     76        Principal principal = (Principal) this.wrapGetResource(params, new GetPrincipal());
     77        if (principal != null) {
    7878            return new ObjectFactory().createPrincipal(principal);
    79         } catch (NotInDataBaseException e) {
    80             loggerServer.debug(e.toString());;
    81             httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e.toString());
    82             return new ObjectFactory().createPrincipal(new Principal());
    83         }
    84 
     79        } else {
     80            return new ObjectFactory().createPrincipal(new Principal());
     81        }
    8582    }
    8683
     
    10299    @Transactional(readOnly = true)
    103100    public JAXBElement<Principal> getPrincipalByInfo(@QueryParam("email") String email) throws IOException {
    104         Number remotePrincipalID = this.getPrincipalID();
    105         if (remotePrincipalID == null) {
    106             return new ObjectFactory().createPrincipal(new Principal());
    107         }
    108         try {
    109             final Principal principal = dbIntegrityService.getPrincipalByInfo(email);
     101        Map params = new HashMap<String, String>();
     102        params.put("email", email);
     103        Principal principal = (Principal) this.wrapGetResource(params, new GetPrincipalByInfo());
     104        if (principal != null) {
    110105            return new ObjectFactory().createPrincipal(principal);
    111         } catch (NotInDataBaseException e) {
    112             loggerServer.debug(e.toString());;
    113             httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e.toString());
     106        } else {
    114107            return new ObjectFactory().createPrincipal(new Principal());
    115108        }
     
    122115    @Transactional(readOnly = true)
    123116    public JAXBElement<CurrentPrincipalInfo> getCurrentPrincipalInfo(@PathParam("principalid") String externalIdentifier) throws IOException {
    124         Number remotePrincipalID = this.getPrincipalID();
    125         if (remotePrincipalID == null) {
     117        Map params = new HashMap<String, String>();
     118        params.put("externalId", externalIdentifier);
     119        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 {
    126124            return new ObjectFactory().createCurrentPrincipalInfo(new CurrentPrincipalInfo());
    127125        }
    128         try {
    129             final Number principalID = dbIntegrityService.getResourceInternalIdentifier(UUID.fromString(externalIdentifier), Resource.PRINCIPAL);
    130             final CurrentPrincipalInfo principalInfo = new CurrentPrincipalInfo();
    131             principalInfo.setRef(dbIntegrityService.getResourceURI(principalID, Resource.PRINCIPAL));
    132             principalInfo.setCurrentPrincipal(this.ifLoggedIn(principalID));
    133             return new ObjectFactory().createCurrentPrincipalInfo(principalInfo);
    134         } catch (NotInDataBaseException e) {
    135             loggerServer.debug(e.toString());;
    136             httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e.toString());
    137             return new ObjectFactory().createCurrentPrincipalInfo(new CurrentPrincipalInfo());
    138         }
    139 
    140126    }
    141127
     
    165151    @Path("{remoteId}")
    166152    public JAXBElement<Principal> addPrincipal(@PathParam("remoteId") String remoteId, Principal principal) throws IOException {
    167         Number remotePrincipalID = this.getPrincipalID();
    168         if (remotePrincipalID == null) {
    169             return new ObjectFactory().createPrincipal(new Principal());
    170         }
     153
     154        Number remotePrincipalID = this.getPrincipalID();
     155        if (remotePrincipalID == null) {
     156            return new ObjectFactory().createPrincipal(new Principal());
     157        }
     158
     159        Map params = new HashMap<String, Object>();
     160        params.put("remoteId", remoteId);
     161        params.put("newPrincipal", principal);
     162
    171163        if (dbIntegrityService.getTypeOfPrincipalAccount(remotePrincipalID).equals(admin)) {
    172             try {
    173                 try {
    174                     final Number principalID = dbIntegrityService.addPrincipal(principal, remoteId);
    175                     final Principal addedPrincipal = dbIntegrityService.getPrincipal(principalID);
    176                     return new ObjectFactory().createPrincipal(addedPrincipal);
    177                 } catch (NotInDataBaseException e1) {
    178                     loggerServer.debug(e1.toString());
    179                     httpServletResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e1.toString());
    180                     return new ObjectFactory().createPrincipal(new Principal());
    181                 }
    182             } catch (PrincipalExists e) {
    183                 loggerServer.debug(e.toString());
    184                 httpServletResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.toString());
    185                 return new ObjectFactory().createPrincipal(new Principal());
    186             }
     164            return this.wrapAddOrUpdatePrincipal(params, new AddPrincipal());
    187165        } else {
    188166            verboseOutput.ADMIN_RIGHTS_EXPECTED();
     
    198176    @Path("register/nonshibboleth")
    199177    public JAXBElement<Principal> registerNonShibbolizedPrincipal(@FormParam("name") String name,
    200             @FormParam("remoteId") String remoteId, @FormParam("password") String passowrd, @FormParam("email") String email)
     178            @FormParam("remoteId") String remoteId, @FormParam("password") String password, @FormParam("email") String email)
    201179            throws IOException {
    202         dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString());
    203180        Principal newPrincipal = new Principal();
    204181        newPrincipal.setDisplayName(name);
    205182        newPrincipal.setEMail(email);
    206         try {
    207             try {
    208                 final int updatedSpringTables = dbIntegrityService.addSpringUser(remoteId, passowrd, shaStrength, remoteId);
    209                 final Number principalID = dbIntegrityService.addPrincipal(newPrincipal, remoteId);
    210                 final Principal addedPrincipal = dbIntegrityService.getPrincipal(principalID);
    211                 return new ObjectFactory().createPrincipal(addedPrincipal);
    212             } catch (NotInDataBaseException e1) {
    213                 loggerServer.debug(e1.toString());
    214                 httpServletResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e1.toString());
    215                 return new ObjectFactory().createPrincipal(new Principal());
    216             }
    217         } catch (PrincipalExists e) {
    218             loggerServer.debug(e.toString());
    219             httpServletResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.toString());
    220             return new ObjectFactory().createPrincipal(new Principal());
    221         }
     183
     184        Map params = new HashMap<String, Object>();
     185        params.put("remoteId", remoteId);
     186        params.put("password", password);
     187        params.put("shaStrength", shaStrength);
     188        params.put("newPrincipal", newPrincipal);
     189
     190        dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString());
     191
     192        return this.wrapAddOrUpdatePrincipal(params, new RegisterNonShibbolizedPrincipal());
    222193
    223194    }
     
    234205        newPrincipal.setDisplayName(name);
    235206        newPrincipal.setEMail(email);
    236         try {
    237             try {
    238                 final Number principalID = dbIntegrityService.addPrincipal(newPrincipal, remoteId);
    239                 final Principal addedPrincipal = dbIntegrityService.getPrincipal(principalID);
    240                 return new ObjectFactory().createPrincipal(addedPrincipal);
    241             } catch (NotInDataBaseException e1) {
    242                 loggerServer.debug(e1.toString());
    243                 httpServletResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e1.toString());
    244                 return new ObjectFactory().createPrincipal(new Principal());
    245             }
    246         } catch (PrincipalExists e) {
    247             loggerServer.debug(e.toString());
    248             httpServletResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.toString());
    249             return new ObjectFactory().createPrincipal(new Principal());
    250         }
     207        Map params = new HashMap<String, Object>();
     208        params.put("remoteId", remoteId);
     209        params.put("newPrincipal", newPrincipal);
     210
     211        return this.wrapAddOrUpdatePrincipal(params, new AddPrincipal());
    251212    }
    252213
     
    271232            return new ObjectFactory().createPrincipal(new Principal());
    272233        }
    273         try {
    274             final Number principalID = dbIntegrityService.getResourceInternalIdentifierFromURI(principal.getURI(), Resource.PRINCIPAL);
    275             if (dbIntegrityService.getTypeOfPrincipalAccount(remotePrincipalID).equals(admin)
    276                     || remotePrincipalID.equals(principalID)) {
    277                 try {
    278                     final int updatedRows = dbIntegrityService.updatePrincipal(principal);
    279                     final Principal addedPrincipal = dbIntegrityService.getPrincipal(principalID);
    280                     return new ObjectFactory().createPrincipal(addedPrincipal);
    281                 } catch (NotInDataBaseException e) {
    282                     loggerServer.debug(e.toString());
    283                     httpServletResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.toString());
    284                     return new ObjectFactory().createPrincipal(new Principal());
    285                 }
    286             } else {
    287                 verboseOutput.ADMIN_RIGHTS_EXPECTED();
    288                 httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN);
    289                 return new ObjectFactory().createPrincipal(new Principal());
    290             }
    291         } catch (NotInDataBaseException e1) {
    292             loggerServer.debug(e1.toString());
    293             httpServletResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e1.toString());
    294             return new ObjectFactory().createPrincipal(new Principal());
    295         }
     234        Map params = new HashMap<String, Object>();
     235        params.put("principal", principal);
     236
     237        if (dbIntegrityService.getTypeOfPrincipalAccount(remotePrincipalID).equals(admin)) {
     238            return this.wrapAddOrUpdatePrincipal(params, new UpdatePrincipal());
     239        } else {
     240            verboseOutput.ADMIN_RIGHTS_EXPECTED();
     241            httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN);
     242            return new ObjectFactory().createPrincipal(new Principal());
     243        }
     244    }
     245
     246    @POST
     247    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
     248    @Produces(MediaType.APPLICATION_XML)
     249    @Path("updateme")
     250    public JAXBElement<Principal> updatePrincipalFromForm(@FormParam("name") String name, @FormParam("email") String email)
     251            throws IOException {
     252        dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString());
     253        Number remotePrincipalID = this.getPrincipalID();
     254        if (remotePrincipalID == null) {
     255            return new ObjectFactory().createPrincipal(new Principal());
     256        }
     257
     258        String principalURI = dbIntegrityService.getResourceURI(remotePrincipalID, Resource.PRINCIPAL);
     259        Principal newPrincipal = new Principal();
     260        newPrincipal.setURI(principalURI);
     261        newPrincipal.setDisplayName(name);
     262        newPrincipal.setEMail(email);
     263        Map params = new HashMap<String, Object>();
     264        params.put("newPrincipal", newPrincipal);
     265
     266        return this.wrapAddOrUpdatePrincipal(params, new UpdatePrincipal());
    296267    }
    297268
     
    362333        return (httpServletRequest.getRemoteUser()).equals(dbIntegrityService.getPrincipalRemoteID(principalID));
    363334    }
     335
     336    /////////////// main working methods that call db-services /////
     337    private class RegisterNonShibbolizedPrincipal implements ILambda<Map, Principal> {
     338
     339        @Override
     340        public Principal apply(Map params) throws NotInDataBaseException, PrincipalExists {
     341            final int updatedSpringTables = dbIntegrityService.addSpringUser((String) params.get("remoteId"), (String) params.get("password"), (Integer) params.get("shaStrength"), (String) params.get("remoteId"));
     342            final Number principalID = dbIntegrityService.addPrincipal((Principal) params.get("newPrincipal"), (String) params.get("remoteId"));
     343            return dbIntegrityService.getPrincipal(principalID);
     344        }
     345    }
     346
     347    private class AddPrincipal implements ILambda<Map, Principal> {
     348
     349        @Override
     350        public Principal apply(Map params) throws NotInDataBaseException, PrincipalExists {
     351            final Number principalID = dbIntegrityService.addPrincipal((Principal) params.get("newPrincipal"), (String) params.get("remoteId"));
     352            return dbIntegrityService.getPrincipal(principalID);
     353        }
     354    }
     355
     356    private class UpdatePrincipal implements ILambda<Map, Principal> {
     357
     358        @Override
     359        public Principal apply(Map params) throws NotInDataBaseException, PrincipalExists {
     360            Principal principal = (Principal) params.get("newPrincipal");
     361            Number principalID = dbIntegrityService.getResourceInternalIdentifierFromURI(principal.getURI(), Resource.PRINCIPAL);
     362            Number principalIDupd = dbIntegrityService.updatePrincipal(principal);
     363            return dbIntegrityService.getPrincipal(principalID);
     364        }
     365    }
     366
     367   
     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);
     373            return dbIntegrityService.getPrincipal(principalID);
     374        }
     375    }
     376
     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"));
     382        }
     383    }
     384
     385    private class GetCurrentPrincipalInfo implements ILambda<Map, CurrentPrincipalInfo> {
     386
     387        @Override
     388        public CurrentPrincipalInfo apply(Map params) throws NotInDataBaseException, PrincipalExists {
     389            final Number principalID = dbIntegrityService.getResourceInternalIdentifier(UUID.fromString((String) params.get("externalId")), Resource.PRINCIPAL);
     390            final CurrentPrincipalInfo principalInfo = new CurrentPrincipalInfo();
     391            principalInfo.setRef(dbIntegrityService.getResourceURI(principalID, Resource.PRINCIPAL));
     392            principalInfo.setCurrentPrincipal(((PrincipalResource) params.get("resource")).ifLoggedIn(principalID));
     393            return principalInfo;
     394        }
     395    }
     396
     397    //// wrappers that map main working methods into exception handling ////
     398    private JAXBElement<Principal> wrapAddOrUpdatePrincipal(Map params, ILambda<Map, Principal> changer) throws IOException {
     399        Number remotePrincipalID = this.getPrincipalID();
     400        if (remotePrincipalID == null) {
     401            return new ObjectFactory().createPrincipal(new Principal());
     402        }
     403        try {
     404            try {
     405                return new ObjectFactory().createPrincipal(changer.apply(params));
     406            } catch (NotInDataBaseException e1) {
     407                loggerServer.debug(e1.toString());
     408                httpServletResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e1.toString());
     409                return new ObjectFactory().createPrincipal(new Principal());
     410            }
     411        } catch (PrincipalExists e) {
     412            loggerServer.debug(e.toString());
     413            httpServletResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.toString());
     414            return new ObjectFactory().createPrincipal(new Principal());
     415        }
     416
     417    }
    364418}
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/rest/ResourceResource.java

    r4995 r5022  
    2222import eu.dasish.annotation.backend.PrincipalExists;
    2323import eu.dasish.annotation.backend.dao.DBIntegrityService;
     24import eu.dasish.annotation.backend.dao.ILambda;
     25import eu.dasish.annotation.schema.ObjectFactory;
    2426import eu.dasish.annotation.schema.Principal;
    2527import java.io.IOException;
     28import java.util.Map;
    2629import javax.servlet.ServletContext;
    2730import javax.servlet.http.HttpServletRequest;
     
    3033import javax.ws.rs.core.UriInfo;
    3134import javax.ws.rs.ext.Providers;
     35import javax.xml.bind.JAXBElement;
    3236import org.slf4j.Logger;
    3337import org.slf4j.LoggerFactory;
     
    3842 * @author olhsha
    3943 */
    40 public class ResourceResource {
     44public class ResourceResource<T> {
    4145
    4246    @Autowired
     
    9599            return null;
    96100        }
     101    }
     102   
     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        }
    97121
    98122    }
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/webapp/index.jsp

    r5014 r5022  
    2323        <p><a href="api/myresource">Jersey resource</a>
    2424        <p><a href="https://trac.clarin.eu/wiki/DASISH/SpecificationDocument#RESTAPI">https://trac.clarin.eu/wiki/DASISH/SpecificationDocument#RESTAPI</a></p>
    25      
     25        <br>
     26        <h3>You are not necessarily logged in.</h3>
    2627        <br>
    2728        <a href="registerNonShibbolethPrincipal.html"> Register a non-shibboleth user</a> <br>
     
    3435        <a href="api/authentication/login"> login  </a> <br>
    3536         <br>
     37         
     38        <br>
     39        <h3>You are logged in. </h3>
     40        <br>
    3641        <a href="api/authentication/logout"> logout</a> <br>
    3742        <br>
     
    3944        <b>Test URI-s</b><br>
    4045        <br>
    41         <b>All output xml-s are valid w.r.t. the schema</b><br>
     46        <a href="updatePrincipal.html"> Update logged-in user.</a> <br>
     47        <br>
    4248        GET <a href="api/authentication/principal">api/authentication/principal</a> <br>
    4349        GET <a href="api/principals/admin">api/principals/admin</a><br>
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/webapp/registerShibbolethPrincipal.html

    r5014 r5022  
    2121        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    2222    </head>
    23     <body onload='document.registerShibForm.username.focus();'>
     23    <body onload='document.registerShibForm.name.focus();'>
    2424
    2525        <h3>User registration</h3>
Note: See TracChangeset for help on using the changeset viewer.