Changeset 5022
- Timestamp:
- 04/22/14 17:23:11 (10 years ago)
- 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 22 22 import eu.dasish.annotation.backend.PrincipalExists; 23 23 import eu.dasish.annotation.backend.Resource; 24 import eu.dasish.annotation.backend.dao.ILambda; 24 25 import eu.dasish.annotation.schema.CurrentPrincipalInfo; 25 26 import eu.dasish.annotation.schema.ObjectFactory; … … 27 28 import java.io.IOException; 28 29 import java.sql.SQLException; 30 import java.util.HashMap; 31 import java.util.Map; 29 32 import java.util.UUID; 30 33 import javax.servlet.http.HttpServletRequest; … … 69 72 @Transactional(readOnly = true) 70 73 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) { 78 78 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 } 85 82 } 86 83 … … 102 99 @Transactional(readOnly = true) 103 100 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) { 110 105 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 { 114 107 return new ObjectFactory().createPrincipal(new Principal()); 115 108 } … … 122 115 @Transactional(readOnly = true) 123 116 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 { 126 124 return new ObjectFactory().createCurrentPrincipalInfo(new CurrentPrincipalInfo()); 127 125 } 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 140 126 } 141 127 … … 165 151 @Path("{remoteId}") 166 152 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 171 163 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()); 187 165 } else { 188 166 verboseOutput.ADMIN_RIGHTS_EXPECTED(); … … 198 176 @Path("register/nonshibboleth") 199 177 public JAXBElement<Principal> registerNonShibbolizedPrincipal(@FormParam("name") String name, 200 @FormParam("remoteId") String remoteId, @FormParam("password") String pass owrd, @FormParam("email") String email)178 @FormParam("remoteId") String remoteId, @FormParam("password") String password, @FormParam("email") String email) 201 179 throws IOException { 202 dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString());203 180 Principal newPrincipal = new Principal(); 204 181 newPrincipal.setDisplayName(name); 205 182 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()); 222 193 223 194 } … … 234 205 newPrincipal.setDisplayName(name); 235 206 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()); 251 212 } 252 213 … … 271 232 return new ObjectFactory().createPrincipal(new Principal()); 272 233 } 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()); 296 267 } 297 268 … … 362 333 return (httpServletRequest.getRemoteUser()).equals(dbIntegrityService.getPrincipalRemoteID(principalID)); 363 334 } 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 } 364 418 } -
DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/rest/ResourceResource.java
r4995 r5022 22 22 import eu.dasish.annotation.backend.PrincipalExists; 23 23 import eu.dasish.annotation.backend.dao.DBIntegrityService; 24 import eu.dasish.annotation.backend.dao.ILambda; 25 import eu.dasish.annotation.schema.ObjectFactory; 24 26 import eu.dasish.annotation.schema.Principal; 25 27 import java.io.IOException; 28 import java.util.Map; 26 29 import javax.servlet.ServletContext; 27 30 import javax.servlet.http.HttpServletRequest; … … 30 33 import javax.ws.rs.core.UriInfo; 31 34 import javax.ws.rs.ext.Providers; 35 import javax.xml.bind.JAXBElement; 32 36 import org.slf4j.Logger; 33 37 import org.slf4j.LoggerFactory; … … 38 42 * @author olhsha 39 43 */ 40 public class ResourceResource {44 public class ResourceResource<T> { 41 45 42 46 @Autowired … … 95 99 return null; 96 100 } 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 } 97 121 98 122 } -
DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/webapp/index.jsp
r5014 r5022 23 23 <p><a href="api/myresource">Jersey resource</a> 24 24 <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> 26 27 <br> 27 28 <a href="registerNonShibbolethPrincipal.html"> Register a non-shibboleth user</a> <br> … … 34 35 <a href="api/authentication/login"> login </a> <br> 35 36 <br> 37 38 <br> 39 <h3>You are logged in. </h3> 40 <br> 36 41 <a href="api/authentication/logout"> logout</a> <br> 37 42 <br> … … 39 44 <b>Test URI-s</b><br> 40 45 <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> 42 48 GET <a href="api/authentication/principal">api/authentication/principal</a> <br> 43 49 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 21 21 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 22 22 </head> 23 <body onload='document.registerShibForm. username.focus();'>23 <body onload='document.registerShibForm.name.focus();'> 24 24 25 25 <h3>User registration</h3>
Note: See TracChangeset
for help on using the changeset viewer.