Changeset 1631 for ComponentRegistry/branches/jeaferversion/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/rest/ComponentRegistryRestService.java
- Timestamp:
- 11/28/11 15:46:02 (13 years ago)
- Location:
- ComponentRegistry/branches/jeaferversion
- Files:
-
- 2 edited
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
ComponentRegistry/branches/jeaferversion/ComponentRegistry
- Property svn:ignore
-
old new 7 7 profiles.xml 8 8 nb-configuration.xml 9 nbactions-Run without tests.xml
-
- Property svn:ignore
-
ComponentRegistry/branches/jeaferversion/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/rest/ComponentRegistryRestService.java
r1604 r1631 41 41 import clarin.cmdi.componentregistry.components.CMDComponentSpec; 42 42 import clarin.cmdi.componentregistry.model.AbstractDescription; 43 import clarin.cmdi.componentregistry.model.CommentMapping.Comment; 43 44 import clarin.cmdi.componentregistry.model.ComponentDescription; 44 45 import clarin.cmdi.componentregistry.model.ProfileDescription; … … 69 70 70 71 private ComponentRegistry getRegistry(boolean userspace) { 71 72 73 72 Principal userPrincipal = security.getUserPrincipal(); 73 UserCredentials userCredentials = getUserCredentials(userPrincipal); 74 return getRegistry(userspace, userCredentials); 74 75 } 75 76 76 77 private ComponentRegistry getRegistry(boolean userspace, UserCredentials userCredentials) { 77 78 return componentRegistryFactory.getComponentRegistry(userspace, userCredentials); 78 79 } 79 80 80 81 private Principal checkAndGetUserPrincipal() { 81 82 83 84 85 82 Principal principal = security.getUserPrincipal(); 83 if (principal == null) { 84 throw new IllegalArgumentException("no user principal found."); 85 } 86 return principal; 86 87 } 87 88 88 89 private UserCredentials getUserCredentials(Principal userPrincipal) { 89 90 91 92 93 90 UserCredentials userCredentials = null; 91 if (userPrincipal != null) { 92 userCredentials = new UserCredentials(userPrincipal); 93 } 94 return userCredentials; 94 95 } 95 96 … … 98 99 @Produces({MediaType.TEXT_XML, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) 99 100 public List<ComponentDescription> getRegisteredComponents(@QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace) throws ComponentRegistryException { 100 101 102 103 104 101 long start = System.currentTimeMillis(); 102 List<ComponentDescription> components = getRegistry(userspace).getComponentDescriptions(); 103 LOG.info("Releasing " + components.size() + " registered components into the world (" + (System.currentTimeMillis() - start) 104 + " millisecs)"); 105 return components; 105 106 } 106 107 … … 109 110 @Produces({MediaType.TEXT_XML, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) 110 111 public List<ProfileDescription> getRegisteredProfiles(@QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace, 111 112 113 114 115 116 117 118 119 120 121 122 123 112 @QueryParam(METADATA_EDITOR_PARAM) @DefaultValue("false") boolean metadataEditor) throws ComponentRegistryException { 113 long start = System.currentTimeMillis(); 114 115 List<ProfileDescription> profiles; 116 if (metadataEditor) { 117 profiles = getRegistry(userspace).getProfileDescriptionsForMetadaEditor(); 118 } else { 119 profiles = getRegistry(userspace).getProfileDescriptions(); 120 } 121 122 LOG.info("Releasing " + profiles.size() + " registered profiles into the world (" + (System.currentTimeMillis() - start) 123 + " millisecs)"); 124 return profiles; 124 125 } 125 126 … … 128 129 @Produces({MediaType.TEXT_XML, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) 129 130 public CMDComponentSpec getRegisteredComponent(@PathParam("componentId") String componentId, 130 131 132 131 @QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace) throws ComponentRegistryException { 132 LOG.info("Component with id: " + componentId + " is requested."); 133 return getRegistry(userspace).getMDComponent(componentId); 133 134 } 134 135 … … 137 138 @Produces({MediaType.TEXT_XML, MediaType.APPLICATION_XML}) 138 139 public Response getRegisteredComponentRawType(@PathParam("componentId") final String componentId, @PathParam("rawType") String rawType) { 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 140 LOG.info("Component with id: " + componentId + " and rawType:" + rawType + " is requested."); 141 StreamingOutput result = null; 142 try { 143 final ComponentRegistry registry = findRegistry(componentId, new ComponentClosure()); 144 if (registry == null) { 145 return Response.status(Status.NOT_FOUND).entity("Id: " + componentId + " is not registered, cannot create data.").build(); 146 } 147 ComponentDescription desc = registry.getComponentDescription(componentId); 148 checkAndThrowDescription(desc, componentId); 149 String fileName = desc.getName() + "." + rawType; 150 if ("xml".equalsIgnoreCase(rawType)) { 151 result = new StreamingOutput() { 152 153 @Override 154 public void write(OutputStream output) throws IOException, WebApplicationException { 155 try { 156 registry.getMDComponentAsXml(componentId, output); 157 } catch (ComponentRegistryException e) { 158 LOG.info("Could not retrieve component", e); 159 throw new WebApplicationException(e, Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build()); 160 } 161 } 162 }; 163 } else if ("xsd".equalsIgnoreCase(rawType)) { 164 result = new StreamingOutput() { 165 166 @Override 167 public void write(OutputStream output) throws IOException, WebApplicationException { 168 try { 169 registry.getMDComponentAsXsd(componentId, output); 170 } catch (ComponentRegistryException e) { 171 LOG.info("Could not retrieve component", e); 172 throw new WebApplicationException(e, Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build()); 173 } 174 175 } 176 }; 177 } else { 178 throw new WebApplicationException(Response.serverError().entity( 179 "unsupported rawType: " + rawType + " (only xml or xsd are supported)").build()); 180 } 181 return createDownloadResponse(result, fileName); 182 } catch (ComponentRegistryException e) { 183 LOG.info("Could not retrieve component", e); 184 return Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build(); 185 } 185 186 } 186 187 187 188 public ComponentRegistry findRegistry(String id, RegistryClosure<? extends AbstractDescription> clos) throws ComponentRegistryException { 188 189 190 191 192 193 194 195 196 197 198 199 200 201 189 AbstractDescription desc = null; 190 ComponentRegistry result = getRegistry(false); 191 desc = clos.getDescription(result, id); 192 if (desc == null) { 193 List<ComponentRegistry> userRegs = componentRegistryFactory.getAllUserRegistries(); 194 for (ComponentRegistry reg : userRegs) { 195 desc = clos.getDescription(reg, id); 196 if (desc != null) { 197 result = reg; 198 break; 199 } 200 } 201 } 202 return result; 202 203 } 203 204 … … 206 207 @Produces({MediaType.TEXT_XML, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) 207 208 public CMDComponentSpec getRegisteredProfile(@PathParam("profileId") String profileId, 208 209 210 209 @QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace) throws ComponentRegistryException { 210 LOG.info("Profile with id: " + profileId + " is requested."); 211 return getRegistry(userspace).getMDProfile(profileId); 211 212 } 212 213 … … 215 216 @Produces({MediaType.TEXT_XML, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) 216 217 public List<AbstractDescription> getComponentUsage(@PathParam("componentId") String componentId, @QueryParam(USERSPACE_PARAM) @DefaultValue("true") boolean userspace) throws ComponentRegistryException { 217 try { 218 final long start = System.currentTimeMillis(); 219 ComponentRegistry registry = getRegistry(userspace); 220 List<ComponentDescription> components = registry.getUsageInComponents(componentId); 221 List<ProfileDescription> profiles = registry.getUsageInProfiles(componentId); 222 223 LOG.info("Found " + components.size() + " components and " + profiles.size() + " profiles that use component " + componentId 224 + " (" + (System.currentTimeMillis() - start) + " millisecs)"); 225 226 List<AbstractDescription> usages = new ArrayList<AbstractDescription>(components.size() + profiles.size()); 227 usages.addAll(components); 228 usages.addAll(profiles); 229 230 return usages; 231 } catch (ComponentRegistryException e) { 232 LOG.info("Could not retrieve profile usage", e); 233 throw e; 234 } 218 try { 219 final long start = System.currentTimeMillis(); 220 ComponentRegistry registry = getRegistry(userspace); 221 List<ComponentDescription> components = registry.getUsageInComponents(componentId); 222 List<ProfileDescription> profiles = registry.getUsageInProfiles(componentId); 223 224 LOG.info("Found " + components.size() + " components and " + profiles.size() + " profiles that use component " + componentId 225 + " (" + (System.currentTimeMillis() - start) + " millisecs)"); 226 227 List<AbstractDescription> usages = new ArrayList<AbstractDescription>(components.size() + profiles.size()); 228 usages.addAll(components); 229 usages.addAll(profiles); 230 231 return usages; 232 } catch (ComponentRegistryException e) { 233 LOG.info("Could not retrieve profile usage", e); 234 throw e; 235 } 236 } 237 238 @GET 239 @Path("/profile/{profileId}/comments") 240 @Produces({MediaType.TEXT_XML, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) 241 public List<Comment> getRegisteredCommentsFromProfile(@PathParam("profileId") String profileId, @QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace) throws ComponentRegistryException { 242 long start = System.currentTimeMillis(); 243 List<Comment> comments = getRegistry(userspace).getCommentsInProfile(profileId); 244 LOG.info("Releasing " + comments.size() + " registered comments in Profile into the world (" + (System.currentTimeMillis() - start) 245 + " millisecs)"); 246 return comments; 247 } 248 249 @GET 250 @Path("/component/{componentId}/comments") 251 @Produces({MediaType.TEXT_XML, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) 252 public List<Comment> getRegisteredCommentsFromComponent(@PathParam("componentId") String componentId, @QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace) throws ComponentRegistryException { 253 long start = System.currentTimeMillis(); 254 List<Comment> comments = getRegistry(userspace).getCommentsInComponent(componentId); 255 LOG.info("Releasing " + comments.size() + " registered comments in Component into the world (" + (System.currentTimeMillis() - start) 256 + " millisecs)"); 257 return comments; 258 } 259 260 @GET 261 @Path("/profiles/{profileId}/comments/{commentsId}") 262 @Produces({MediaType.TEXT_XML, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) 263 public Comment getSpecifiedCommentFromProfile(@PathParam("commentId") String commentId, @QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace) throws ComponentRegistryException { 264 long start = System.currentTimeMillis(); 265 LOG.info(" Comments of component with id" + commentId + " are requested."); 266 return getRegistry(userspace).getSpecifiedCommentInProfile(commentId); 267 } 268 269 @GET 270 @Path("/component/{componentId}/comments/{commentsId}") 271 @Produces({MediaType.TEXT_XML, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) 272 public Comment getRegisteredCommentFromComponent(@PathParam("commentId") String commentId, @QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace) throws ComponentRegistryException { 273 LOG.info(" Comments of component with id" + commentId + " are requested."); 274 return getRegistry(userspace).getSpecifiedCommentInComponent(commentId); 235 275 } 236 276 … … 246 286 @Path("/profiles/{profileId}") 247 287 public Response manipulateRegisteredProfile(@PathParam("profileId") String profileId, @FormParam("method") String method, 248 @QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace) { 249 if ("delete".equalsIgnoreCase(method)) { 250 return deleteRegisteredProfile(profileId, userspace); 251 } else { 252 return Response.ok().build(); 253 } 254 } 288 @QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace) { 289 if ("delete".equalsIgnoreCase(method)) { 290 return deleteRegisteredProfile(profileId, userspace); 291 } else { 292 return Response.ok().build(); 293 } 294 } 295 296 // @POST 297 // @Path("/profiles/{profileId}/comments/{commentId}") 298 // public Response manipulateRegisteredCommentFromProfile(@PathParam("profileId") String commentId, @FormParam("method") String method, 299 // @QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace) { 300 // if ("delete".equalsIgnoreCase(method)) { 301 // return deleteRegisteredProfile(commentId, userspace); 302 // } else { 303 // return Response.ok().build(); 304 // } 305 // } 306 // 307 // @POST 308 // @Path("/components/{componentId}/comments/{commentId}") 309 // public Response manipulateRegisteredCommentFromComponent(@PathParam("profileId") String commentId, @FormParam("method") String method, 310 // @QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace) { 311 // if ("delete".equalsIgnoreCase(method)) { 312 // return deleteRegisteredProfile(commentId, userspace); 313 // } else { 314 // return Response.ok().build(); 315 // } 316 // } 255 317 256 318 @POST … … 258 320 @Consumes("multipart/form-data") 259 321 public Response publishRegisteredProfile(@PathParam("profileId") String profileId, @FormDataParam(DATA_FORM_FIELD) InputStream input, 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 322 @FormDataParam(NAME_FORM_FIELD) String name, @FormDataParam(DESCRIPTION_FORM_FIELD) String description, 323 @FormDataParam(GROUP_FORM_FIELD) String group, @FormDataParam(DOMAIN_FORM_FIELD) String domainName) { 324 Principal principal = checkAndGetUserPrincipal(); 325 try { 326 ProfileDescription desc = getRegistry(true).getProfileDescription(profileId); 327 if (desc != null) { 328 updateDescription(desc, name, description, domainName, group); 329 return register(input, desc, getUserCredentials(principal), true, new PublishAction(principal)); 330 } else { 331 LOG.error("Update of nonexistent id (" + profileId + ") failed."); 332 return Response.serverError().entity("Invalid id, cannot update nonexistent profile").build(); 333 } 334 } catch (ComponentRegistryException e) { 335 LOG.info("Could not retrieve component", e); 336 return Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build(); 337 } 276 338 } 277 339 … … 280 342 @Consumes("multipart/form-data") 281 343 public Response updateRegisteredProfile(@PathParam("profileId") String profileId, 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 344 @QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace, @FormDataParam(DATA_FORM_FIELD) InputStream input, 345 @FormDataParam(NAME_FORM_FIELD) String name, @FormDataParam(DESCRIPTION_FORM_FIELD) String description, 346 @FormDataParam(GROUP_FORM_FIELD) String group, @FormDataParam(DOMAIN_FORM_FIELD) String domainName) { 347 Principal principal = checkAndGetUserPrincipal(); 348 UserCredentials userCredentials = getUserCredentials(principal); 349 try { 350 ProfileDescription desc = getRegistry(userspace).getProfileDescription(profileId); 351 if (desc != null) { 352 updateDescription(desc, name, description, domainName, group); 353 return register(input, desc, userCredentials, userspace, new UpdateAction(principal)); 354 } else { 355 LOG.error("Update of nonexistent id (" + profileId + ") failed."); 356 return Response.serverError().entity("Invalid id, cannot update nonexistent profile").build(); 357 } 358 } catch (ComponentRegistryException e) { 359 LOG.info("Could not retrieve component", e); 360 return Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build(); 361 } 300 362 } 301 363 … … 311 373 @Path("/components/{componentId}") 312 374 public Response manipulateRegisteredComponent(@PathParam("componentId") String componentId, @FormParam("method") String method, 313 314 315 316 317 318 375 @QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace) { 376 if ("delete".equalsIgnoreCase(method)) { 377 return deleteRegisteredComponent(componentId, userspace); 378 } else { 379 return Response.ok().build(); 380 } 319 381 } 320 382 … … 323 385 @Consumes("multipart/form-data") 324 386 public Response publishRegisteredComponent(@PathParam("componentId") String componentId, 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 387 @FormDataParam(DATA_FORM_FIELD) InputStream input, @FormDataParam(NAME_FORM_FIELD) String name, 388 @FormDataParam(DESCRIPTION_FORM_FIELD) String description, @FormDataParam(GROUP_FORM_FIELD) String group, 389 @FormDataParam(DOMAIN_FORM_FIELD) String domainName) { 390 Principal principal = checkAndGetUserPrincipal(); 391 try { 392 ComponentDescription desc = getRegistry(true).getComponentDescription(componentId); 393 if (desc != null) { 394 updateDescription(desc, name, description, domainName, group); 395 return register(input, desc, getUserCredentials(principal), true, new PublishAction(principal)); 396 } else { 397 LOG.error("Update of nonexistent id (" + componentId + ") failed."); 398 return Response.serverError().entity("Invalid id, cannot update nonexistent profile").build(); 399 } 400 } catch (ComponentRegistryException e) { 401 LOG.info("Could not retrieve component", e); 402 return Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build(); 403 } 342 404 } 343 405 … … 346 408 @Consumes("multipart/form-data") 347 409 public Response updateRegisteredComponent(@PathParam("componentId") String componentId, 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 410 @QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace, @FormDataParam(DATA_FORM_FIELD) InputStream input, 411 @FormDataParam(NAME_FORM_FIELD) String name, @FormDataParam(DESCRIPTION_FORM_FIELD) String description, 412 @FormDataParam(GROUP_FORM_FIELD) String group, @FormDataParam(DOMAIN_FORM_FIELD) String domainName) { 413 Principal principal = checkAndGetUserPrincipal(); 414 try { 415 ComponentDescription desc = getRegistry(userspace).getComponentDescription(componentId); 416 if (desc != null) { 417 updateDescription(desc, name, description, domainName, group); 418 return register(input, desc, getUserCredentials(principal), userspace, new UpdateAction(principal)); 419 } else { 420 LOG.error("Update of nonexistent id (" + componentId + ") failed."); 421 return Response.serverError().entity("Invalid id, cannot update nonexistent component").build(); 422 } 423 } catch (ComponentRegistryException e) { 424 LOG.info("Could not retrieve component", e); 425 return Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build(); 426 } 365 427 } 366 428 367 429 private void updateDescription(AbstractDescription desc, String name, String description, String domainName, String group) { 368 369 370 371 372 430 desc.setName(name); 431 desc.setDescription(description); 432 desc.setDomainName(domainName); 433 desc.setGroupName(group); 434 desc.setRegistrationDate(AbstractDescription.createNewDate()); 373 435 } 374 436 … … 376 438 @Path("/components/{componentId}") 377 439 public Response deleteRegisteredComponent(@PathParam("componentId") String componentId, 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 440 @QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace) { 441 Principal principal = checkAndGetUserPrincipal(); 442 ComponentRegistry registry = getRegistry(userspace); 443 LOG.info("Component with id: " + componentId + " set for deletion."); 444 try { 445 registry.deleteMDComponent(componentId, principal, false); 446 } catch (DeleteFailedException e) { 447 LOG.info("Component with id: " + componentId + " deletion failed.", e); 448 return Response.status(Status.FORBIDDEN).entity("" + e.getMessage()).build(); 449 } catch (ComponentRegistryException e) { 450 LOG.info("Component with id: " + componentId + " deletion failed.", e); 451 return Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build(); 452 } catch (IOException e) { 453 LOG.info("Component with id: " + componentId + " deletion failed.", e); 454 return Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build(); 455 } catch (UserUnauthorizedException e) { 456 LOG.info("Component with id: " + componentId + " deletion failed: " + e.getMessage()); 457 return Response.serverError().status(Status.UNAUTHORIZED).entity("" + e.getMessage()).build(); 458 } 459 LOG.info("Component with id: " + componentId + " deleted."); 460 return Response.ok().build(); 399 461 } 400 462 … … 402 464 @Path("/profiles/{profileId}") 403 465 public Response deleteRegisteredProfile(@PathParam("profileId") String profileId, 404 @QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace) { 405 Principal principal = checkAndGetUserPrincipal(); 406 LOG.info("Profile with id: " + profileId + " set for deletion."); 407 try { 408 getRegistry(userspace).deleteMDProfile(profileId, principal); 409 } catch (DeleteFailedException e) { 410 LOG.info("Profile with id: " + profileId + " deletion failed: " + e.getMessage()); 411 return Response.serverError().status(Status.FORBIDDEN).entity("" + e.getMessage()).build(); 412 } catch (ComponentRegistryException e) { 413 LOG.info("Could not retrieve component", e); 414 return Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build(); 415 } catch (IOException e) { 416 LOG.info("Profile with id: " + profileId + " deletion failed.", e); 417 return Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build(); 418 } catch (UserUnauthorizedException e) { 419 LOG.info("Profile with id: " + profileId + " deletion failed: " + e.getMessage()); 420 return Response.serverError().status(Status.UNAUTHORIZED).entity("" + e.getMessage()).build(); 421 } 422 LOG.info("Profile with id: " + profileId + " deleted."); 423 return Response.ok().build(); 424 } 466 @QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace) { 467 Principal principal = checkAndGetUserPrincipal(); 468 LOG.info("Profile with id: " + profileId + " set for deletion."); 469 try { 470 getRegistry(userspace).deleteMDProfile(profileId, principal); 471 } catch (DeleteFailedException e) { 472 LOG.info("Profile with id: " + profileId + " deletion failed: " + e.getMessage()); 473 return Response.serverError().status(Status.FORBIDDEN).entity("" + e.getMessage()).build(); 474 } catch (ComponentRegistryException e) { 475 LOG.info("Could not retrieve component", e); 476 return Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build(); 477 } catch (IOException e) { 478 LOG.info("Profile with id: " + profileId + " deletion failed.", e); 479 return Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build(); 480 } catch (UserUnauthorizedException e) { 481 LOG.info("Profile with id: " + profileId + " deletion failed: " + e.getMessage()); 482 return Response.serverError().status(Status.UNAUTHORIZED).entity("" + e.getMessage()).build(); 483 } 484 LOG.info("Profile with id: " + profileId + " deleted."); 485 return Response.ok().build(); 486 } 487 488 // @DELETE 489 // @Path("/profiles/{profileId}/comments/{commentId}") 490 // public Response deleteRegisteredCommentFromProfile(@PathParam("commentId") String commentId, 491 // @QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace) { 492 // Principal principal = checkAndGetUserPrincipal(); 493 // LOG.info("Comment with id: " + commentId + " set for deletion."); 494 // try { 495 // getRegistry(userspace).deleteMDComment(commentId, principal); 496 // } catch (DeleteFailedException e) { 497 // LOG.info("Comment with id: " + commentId + " deletion failed: " + e.getMessage()); 498 // return Response.serverError().status(Status.FORBIDDEN).entity("" + e.getMessage()).build(); 499 // } catch (ComponentRegistryException e) { 500 // LOG.info("Could not retrieve component", e); 501 // return Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build(); 502 // } catch (IOException e) { 503 // LOG.info("Comment with id: " + commentId + " deletion failed.", e); 504 // return Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build(); 505 // } catch (UserUnauthorizedException e) { 506 // LOG.info("Comment with id: " + commentId + " deletion failed: " + e.getMessage()); 507 // return Response.serverError().status(Status.UNAUTHORIZED).entity("" + e.getMessage()).build(); 508 // } 509 // LOG.info("Profile with id: " + commentId + " deleted."); 510 // return Response.ok().build(); 511 // } 512 // 513 // @DELETE 514 // @Path("/components/{componentId}/comments/{commentId}") 515 // public Response deleteRegisteredCommentFromComponent(@PathParam("commentId") String commentId, 516 // @QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace) { 517 // Principal principal = checkAndGetUserPrincipal(); 518 // LOG.info("Comment with id: " + commentId + " set for deletion."); 519 // try { 520 // getRegistry(userspace).deleteMDComment(commentId, principal); 521 // } catch (DeleteFailedException e) { 522 // LOG.info("Comment with id: " + commentId + " deletion failed: " + e.getMessage()); 523 // return Response.serverError().status(Status.FORBIDDEN).entity("" + e.getMessage()).build(); 524 // } catch (ComponentRegistryException e) { 525 // LOG.info("Could not retrieve component", e); 526 // return Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build(); 527 // } catch (IOException e) { 528 // LOG.info("Comment with id: " + commentId + " deletion failed.", e); 529 // return Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build(); 530 // } catch (UserUnauthorizedException e) { 531 // LOG.info("Comment with id: " + commentId + " deletion failed: " + e.getMessage()); 532 // return Response.serverError().status(Status.UNAUTHORIZED).entity("" + e.getMessage()).build(); 533 // } 534 // LOG.info("Profile with id: " + commentId + " deleted."); 535 // return Response.ok().build(); 536 // } 425 537 426 538 @GET … … 428 540 @Produces({MediaType.TEXT_XML, MediaType.APPLICATION_XML}) 429 541 public Response getRegisteredProfileRawType(@PathParam("profileId") final String profileId, @PathParam("rawType") String rawType) { 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 542 LOG.info("Profile with id: " + profileId + " and rawType:" + rawType + " is requested."); 543 StreamingOutput result = null; 544 try { 545 final ComponentRegistry registry = findRegistry(profileId, new ProfileClosure()); 546 if (registry == null) { 547 return Response.status(Status.NOT_FOUND).entity("Id: " + profileId + " is not registered, cannot create data.").build(); 548 } 549 ProfileDescription desc = registry.getProfileDescription(profileId); 550 checkAndThrowDescription(desc, profileId); 551 String fileName = desc.getName() + "." + rawType; 552 553 if ("xml".equalsIgnoreCase(rawType)) { 554 result = new StreamingOutput() { 555 556 @Override 557 public void write(OutputStream output) throws IOException, WebApplicationException { 558 try { 559 registry.getMDProfileAsXml(profileId, output); 560 } catch (ComponentRegistryException e) { 561 LOG.warn("Could not retrieve component", e); 562 throw new WebApplicationException(e, Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build()); 563 } 564 } 565 }; 566 } else if ("xsd".equalsIgnoreCase(rawType)) { 567 result = new StreamingOutput() { 568 569 @Override 570 public void write(OutputStream output) throws IOException, WebApplicationException { 571 try { 572 registry.getMDProfileAsXsd(profileId, output); 573 } catch (ComponentRegistryException e) { 574 LOG.warn("Could not retrieve component", e); 575 throw new WebApplicationException(e, Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build()); 576 } 577 } 578 }; 579 } else { 580 throw new WebApplicationException(Response.serverError().entity( 581 "unsupported rawType: " + rawType + " (only xml or xsd are supported)").build()); 582 } 583 return createDownloadResponse(result, fileName); 584 } catch (ComponentRegistryException e) { 585 LOG.info("Could not retrieve component", e); 586 return Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build(); 587 } 476 588 } 477 589 478 590 private void checkAndThrowDescription(AbstractDescription desc, String id) { 479 480 481 591 if (desc == null) { 592 throw new WebApplicationException(Response.serverError().entity("Incorrect id:" + id + "cannot handle request").build()); 593 } 482 594 } 483 595 484 596 private Response createDownloadResponse(StreamingOutput result, String fileName) { 485 486 487 488 597 //Making response so it triggers browsers native save as dialog. 598 Response response = Response.ok().type("application/x-download").header("Content-Disposition", 599 "attachment; filename=\"" + fileName + "\"").entity(result).build(); 600 return response; 489 601 490 602 } … … 495 607 @Consumes("multipart/form-data") 496 608 public Response registerProfile(@FormDataParam(DATA_FORM_FIELD) InputStream input, @FormDataParam(NAME_FORM_FIELD) String name, 497 498 499 500 501 502 503 504 505 506 507 508 509 609 @FormDataParam(DESCRIPTION_FORM_FIELD) String description, @FormDataParam(GROUP_FORM_FIELD) String group, @FormDataParam(DOMAIN_FORM_FIELD) String domainName, 610 @QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace) { 611 Principal principal = checkAndGetUserPrincipal(); 612 UserCredentials userCredentials = getUserCredentials(principal); 613 ProfileDescription desc = createNewProfileDescription(); 614 desc.setCreatorName(userCredentials.getDisplayName()); 615 desc.setUserId(userCredentials.getPrincipalName()); // Hash used to be created here, now Id is constructed by impl 616 desc.setName(name); 617 desc.setDescription(description); 618 desc.setGroupName(group); 619 desc.setDomainName(domainName); 620 LOG.info("Trying to register Profile: " + desc); 621 return register(input, desc, userCredentials, userspace, new NewAction()); 510 622 } 511 623 … … 515 627 @Consumes("multipart/form-data") 516 628 public Response registerComponent(@FormDataParam(DATA_FORM_FIELD) InputStream input, @FormDataParam(NAME_FORM_FIELD) String name, 517 518 519 520 521 522 523 524 525 526 527 528 529 629 @FormDataParam(DESCRIPTION_FORM_FIELD) String description, @FormDataParam(GROUP_FORM_FIELD) String group, 630 @FormDataParam(DOMAIN_FORM_FIELD) String domainName, @QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace) { 631 Principal principal = checkAndGetUserPrincipal(); 632 UserCredentials userCredentials = getUserCredentials(principal); 633 ComponentDescription desc = createNewComponentDescription(); 634 desc.setCreatorName(userCredentials.getDisplayName()); 635 desc.setUserId(userCredentials.getPrincipalName()); // Hash used to be created here, now Id is constructed by impl 636 desc.setName(name); 637 desc.setDescription(description); 638 desc.setGroupName(group); 639 desc.setDomainName(domainName); 640 LOG.info("Trying to register Component: " + desc); 641 return register(input, desc, userCredentials, userspace, new NewAction()); 530 642 } 531 643 … … 534 646 @Produces({MediaType.TEXT_XML, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) 535 647 public Response pingSession() { 536 537 538 539 540 541 542 543 544 648 boolean stillActive = false; 649 Principal userPrincipal = security.getUserPrincipal(); 650 LOG.info("ping by user: " + (userPrincipal == null ? "null" : userPrincipal.getName())); 651 if (request != null) { 652 if (userPrincipal != null && !ComponentRegistryFactory.ANONYMOUS_USER.equals(userPrincipal.getName())) { 653 stillActive = !((HttpServletRequest) request).getSession().isNew(); 654 } 655 } 656 return Response.ok().entity("<session stillActive=\"" + stillActive + "\"/>").build(); 545 657 } 546 658 547 659 private Response register(InputStream input, AbstractDescription desc, UserCredentials userCredentials, boolean userspace, 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 660 RegisterAction action) { 661 try { 662 ComponentRegistry registry = getRegistry(userspace, userCredentials); 663 DescriptionValidator descriptionValidator = new DescriptionValidator(desc); 664 MDValidator validator = new MDValidator(input, desc, registry, getRegistry(true), componentRegistryFactory.getPublicRegistry()); 665 RegisterResponse response = new RegisterResponse(); 666 response.setIsInUserSpace(userspace); 667 validate(response, descriptionValidator, validator); 668 if (response.getErrors().isEmpty()) { 669 CMDComponentSpec spec = validator.getCMDComponentSpec(); 670 int returnCode = action.execute(desc, spec, response, registry); 671 if (returnCode == 0) { 672 response.setRegistered(true); 673 response.setDescription(desc); 674 } else { 675 response.setRegistered(false); 676 response.addError("Unable to register at this moment. Internal server error."); 677 } 678 } else { 679 LOG.info("Registration failed with validation errors:" + Arrays.toString(response.getErrors().toArray())); 680 response.setRegistered(false); 681 } 682 response.setIsProfile(desc.isProfile()); 683 return Response.ok(response).build(); 684 } finally { 685 try { 686 input.close();//either we read the input or there was an exception, we need to close it. 687 } catch (IOException e) { 688 LOG.error("Error when closing inputstream: ", e); 689 } 690 } 579 691 } 580 692 581 693 private ComponentDescription createNewComponentDescription() { 582 583 584 694 ComponentDescription desc = ComponentDescription.createNewDescription(); 695 desc.setHref(createXlink(desc.getId())); 696 return desc; 585 697 } 586 698 587 699 private ProfileDescription createNewProfileDescription() { 588 589 590 700 ProfileDescription desc = ProfileDescription.createNewDescription(); 701 desc.setHref(createXlink(desc.getId())); 702 return desc; 591 703 } 592 704 593 705 private String createXlink(String id) { 594 595 706 URI uri = uriInfo.getRequestUriBuilder().path(id).build(); 707 return uri.toString(); 596 708 } 597 709 598 710 private void validate(RegisterResponse response, Validator... validators) { 599 600 601 602 603 604 605 711 for (Validator validator : validators) { 712 if (!validator.validate()) { 713 for (String error : validator.getErrorMessages()) { 714 response.addError(error); 715 } 716 } 717 } 606 718 } 607 719 … … 610 722 */ 611 723 public void setComponentRegistryFactory(ComponentRegistryFactory componentRegistryFactory) { 612 724 this.componentRegistryFactory = componentRegistryFactory; 613 725 } 614 726 }
Note: See TracChangeset
for help on using the changeset viewer.