Changeset 1860
- Timestamp:
- 04/05/12 14:13:08 (12 years ago)
- Location:
- ComponentRegistry/trunk/ComponentRegistry
- Files:
-
- 1 added
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
ComponentRegistry/trunk/ComponentRegistry/pom.xml
r1816 r1860 185 185 <groupId>clarin.cmdi</groupId> 186 186 <artifactId>cmd-validate</artifactId> 187 <version>1. 0</version>187 <version>1.1</version> 188 188 </dependency> 189 189 </dependencies> -
ComponentRegistry/trunk/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/MDMarshaller.java
r1816 r1860 41 41 private final static Logger LOG = LoggerFactory.getLogger(MDMarshaller.class); 42 42 /** 43 * I define W3C_XML_SCHEMA_NS_URI here cannot get it from @see XMLConstants there is a conflict between stax-api and java5. 43 * I define W3C_XML_SCHEMA_NS_URI here cannot get it from 44 * 45 * @see XMLConstants there is a conflict between stax-api and java5. 44 46 */ 45 47 private static final String W3C_XML_SCHEMA_NS_URI = "http://www.w3.org/2001/XMLSchema"; … … 65 67 66 68 /** 67 * 69 * 68 70 * @param docClass 69 71 * @param inputStream … … 102 104 } 103 105 104 public static Schema getCMDComponentSchema() {106 public static synchronized Schema getCMDComponentSchema() { 105 107 if (generalComponentSchema == null) { 106 108 try { 107 109 SchemaFactory schemaFactory = SchemaFactory.newInstance(W3C_XML_SCHEMA_NS_URI); 108 schemaFactory.setResourceResolver(new LSResourceResolver() { 109 private CatalogResolver catRes = new CatalogResolver(); 110 111 @Override 112 public LSInput resolveResource(String type, String namespaceURI, String publicId, String systemId, String baseURI) { 113 InputSource resolveEntity = catRes.resolveEntity(publicId, systemId); 114 resolveEntity.setEncoding("UTF-8"); 115 DOMImplementationLS domImplementation; 116 try { 117 domImplementation = (DOMImplementationLS) DOMImplementationRegistry.newInstance().getDOMImplementation("LS"); 118 } catch (ClassCastException e) { 119 throw new RuntimeException(e); 120 } catch (ClassNotFoundException e) { 121 throw new RuntimeException(e); 122 } catch (InstantiationException e) { 123 throw new RuntimeException(e); 124 } catch (IllegalAccessException e) { 125 throw new RuntimeException(e); 126 } 127 LSInput lsInput = domImplementation.createLSInput(); 128 lsInput.setEncoding("UTF-8"); 129 lsInput.setByteStream(resolveEntity.getByteStream()); 130 lsInput.setCharacterStream(resolveEntity.getCharacterStream()); 131 return lsInput; 132 } 133 }); 110 schemaFactory.setResourceResolver(new ComponentRegistryResourceResolver()); 134 111 generalComponentSchema = schemaFactory.newSchema(new URL(Configuration.getInstance().getGeneralComponentSchema())); 135 112 } catch (MalformedURLException e) { … … 169 146 } 170 147 171 172 173 148 private static String getSpecId(CMDComponentSpec spec) { 174 149 String result = ""; -
ComponentRegistry/trunk/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/rest/ComponentRegistryRestService.java
r1753 r1860 52 52 @Path("/registry") 53 53 public class ComponentRegistryRestService { 54 54 55 55 @Context 56 56 private UriInfo uriInfo; … … 69 69 @Inject(value = "componentRegistryFactory") 70 70 private ComponentRegistryFactory componentRegistryFactory; 71 71 72 72 private ComponentRegistry getRegistry(boolean userspace) { 73 73 Principal userPrincipal = security.getUserPrincipal(); … … 75 75 return getRegistry(userspace, userCredentials); 76 76 } 77 77 78 78 private ComponentRegistry getRegistry(boolean userspace, UserCredentials userCredentials) { 79 79 return componentRegistryFactory.getComponentRegistry(userspace, userCredentials); … … 81 81 82 82 /** 83 * 83 * 84 84 * @return Principal of current request 85 85 * @throws IllegalArgumentException If no user principal found … … 92 92 return principal; 93 93 } 94 94 95 95 private UserCredentials getUserCredentials(Principal userPrincipal) { 96 96 UserCredentials userCredentials = null; … … 100 100 return userCredentials; 101 101 } 102 102 103 103 @GET 104 104 @Path("/components") … … 111 111 return components; 112 112 } 113 113 114 114 @GET 115 115 @Path("/profiles") … … 118 118 @QueryParam(METADATA_EDITOR_PARAM) @DefaultValue("false") boolean metadataEditor) throws ComponentRegistryException { 119 119 long start = System.currentTimeMillis(); 120 120 121 121 List<ProfileDescription> profiles; 122 122 if (metadataEditor) { … … 125 125 profiles = getRegistry(userspace).getProfileDescriptions(); 126 126 } 127 127 128 128 LOG.info("Releasing " + profiles.size() + " registered profiles into the world (" + (System.currentTimeMillis() - start) 129 129 + " millisecs)"); 130 130 return profiles; 131 131 } 132 132 133 133 @GET 134 134 @Path("/components/{componentId}") … … 139 139 return getRegistry(userspace).getMDComponent(componentId); 140 140 } 141 141 142 142 @GET 143 143 @Path("/components/{componentId}/{rawType}") … … 156 156 if ("xml".equalsIgnoreCase(rawType)) { 157 157 result = new StreamingOutput() { 158 158 159 159 @Override 160 160 public void write(OutputStream output) throws IOException, WebApplicationException { … … 169 169 } else if ("xsd".equalsIgnoreCase(rawType)) { 170 170 result = new StreamingOutput() { 171 171 172 172 @Override 173 173 public void write(OutputStream output) throws IOException, WebApplicationException { … … 178 178 throw new WebApplicationException(e, Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build()); 179 179 } 180 180 181 181 } 182 182 }; … … 191 191 } 192 192 } 193 193 194 194 public ComponentRegistry findRegistry(String id, RegistryClosure<? extends AbstractDescription> clos) throws ComponentRegistryException { 195 195 AbstractDescription desc = null; … … 208 208 return result; 209 209 } 210 210 211 211 @GET 212 212 @Path("/profiles/{profileId}") … … 217 217 return getRegistry(userspace).getMDProfile(profileId); 218 218 } 219 219 220 220 @GET 221 221 @Path("/components/usage/{componentId}") … … 227 227 List<ComponentDescription> components = registry.getUsageInComponents(componentId); 228 228 List<ProfileDescription> profiles = registry.getUsageInProfiles(componentId); 229 229 230 230 LOG.info("Found " + components.size() + " components and " + profiles.size() + " profiles that use component " + componentId 231 231 + " (" + (System.currentTimeMillis() - start) + " millisecs)"); 232 232 233 233 List<AbstractDescription> usages = new ArrayList<AbstractDescription>(components.size() + profiles.size()); 234 234 usages.addAll(components); 235 235 usages.addAll(profiles); 236 236 237 237 return usages; 238 238 } catch (ComponentRegistryException e) { … … 241 241 } 242 242 } 243 243 244 244 @GET 245 245 @Path("/profiles/{profileId}/comments") … … 252 252 return comments; 253 253 } 254 254 255 255 @GET 256 256 @Path("/components/{componentId}/comments") … … 263 263 return comments; 264 264 } 265 265 266 266 @GET 267 267 @Path("/profiles/{profileId}/comments/{commentId}") … … 271 271 return getRegistry(userspace).getSpecifiedCommentInProfile(profileId, commentId); 272 272 } 273 273 274 274 @GET 275 275 @Path("/components/{componentId}/comments/{commentId}") … … 281 281 282 282 /** 283 * 283 * 284 284 * Purely helper method for my front-end (FLEX) which only does post/get requests. The query param is checked and the "proper" method is 285 285 * called. 286 * 286 287 * @param profileId 287 288 * @param method … … 298 299 } 299 300 } 300 301 301 302 @POST 302 303 @Path("/profiles/{profileId}/comments/{commentId}") … … 309 310 } 310 311 } 311 312 312 313 @POST 313 314 @Path("/components/{componentId}/comments/{commentId}") … … 320 321 } 321 322 } 322 323 323 324 @POST 324 325 @Path("/profiles/{profileId}/publish") … … 344 345 } 345 346 } 346 347 347 348 @POST 348 349 @Path("/profiles/{profileId}/update") … … 369 370 return Response.status(Status.UNAUTHORIZED).entity(ex.getMessage()).build(); 370 371 } 371 372 372 373 } 373 374 374 375 /** 375 * 376 * 376 377 * Purely helper method for my front-end (FLEX) which van only do post/get requests. The query param is checked and the "proper" method 377 378 * is called. 379 * 378 380 * @param componentId 379 381 * @param method … … 390 392 } 391 393 } 392 394 393 395 @POST 394 396 @Path("/components/{componentId}/publish") … … 415 417 } 416 418 } 417 419 418 420 @POST 419 421 @Path("/components/{componentId}/update") … … 440 442 } 441 443 } 442 444 443 445 private void updateDescription(AbstractDescription desc, String name, String description, String domainName, String group) { 444 446 desc.setName(name); … … 448 450 desc.setRegistrationDate(AbstractDescription.createNewDate()); 449 451 } 450 452 451 453 @DELETE 452 454 @Path("/components/{componentId}") … … 474 476 return Response.ok().build(); 475 477 } 476 478 477 479 @DELETE 478 480 @Path("/profiles/{profileId}") … … 499 501 return Response.ok().build(); 500 502 } 501 503 502 504 @DELETE 503 505 @Path("/profiles/{profileId}/comments/{commentId}") … … 530 532 return Response.ok().build(); 531 533 } 532 534 533 535 @DELETE 534 536 @Path("/components/{componentId}/comments/{commentId}") … … 561 563 return Response.ok().build(); 562 564 } 563 565 564 566 @GET 565 567 @Path("/profiles/{profileId}/{rawType}") … … 576 578 checkAndThrowDescription(desc, profileId); 577 579 String fileName = desc.getName() + "." + rawType; 578 580 579 581 if ("xml".equalsIgnoreCase(rawType)) { 580 582 result = new StreamingOutput() { 581 583 582 584 @Override 583 585 public void write(OutputStream output) throws IOException, WebApplicationException { … … 592 594 } else if ("xsd".equalsIgnoreCase(rawType)) { 593 595 result = new StreamingOutput() { 594 596 595 597 @Override 596 598 public void write(OutputStream output) throws IOException, WebApplicationException { … … 613 615 } 614 616 } 615 617 616 618 private void checkAndThrowDescription(AbstractDescription desc, String id) { 617 619 if (desc == null) { … … 619 621 } 620 622 } 621 623 622 624 private Response createDownloadResponse(StreamingOutput result, String fileName) { 623 625 //Making response so it triggers browsers native save as dialog. … … 625 627 "attachment; filename=\"" + fileName + "\"").entity(result).build(); 626 628 return response; 627 628 } 629 629 630 } 631 630 632 @POST 631 633 @Path("/profiles") … … 651 653 } 652 654 } 653 655 654 656 @POST 655 657 @Path("/components") … … 675 677 } 676 678 } 677 679 678 680 @POST 679 681 @Path("/components/{componentId}/comments") … … 701 703 } 702 704 } 703 705 704 706 @POST 705 707 @Path("/profiles/{profileId}/comments") … … 727 729 } 728 730 } 729 731 730 732 @GET 731 733 @Path("/pingSession") … … 742 744 return Response.ok().entity("<session stillActive=\"" + stillActive + "\"/>").build(); 743 745 } 744 746 745 747 private Response register(InputStream input, AbstractDescription desc, UserCredentials userCredentials, boolean userspace, 746 748 RegisterAction action) { … … 777 779 } 778 780 response.setIsProfile(desc.isProfile()); 781 LOG.info("Registration of {} successful: {}", response.getDescription().getId(), response.isRegistered()); 779 782 return Response.ok(response).build(); 780 783 } finally { … … 786 789 } 787 790 } 788 791 789 792 private Response registerComment(InputStream input, ComponentRegistry registry, boolean userspace, 790 793 AbstractDescription description, Principal principal, UserCredentials userCredentials) { … … 806 809 } 807 810 } 808 811 809 812 int returnCode = registry.registerComment(com, principal.getName()); 810 813 if (returnCode == 0) { … … 832 835 } 833 836 } 834 837 835 838 private ComponentDescription createNewComponentDescription() { 836 839 ComponentDescription desc = ComponentDescription.createNewDescription(); … … 838 841 return desc; 839 842 } 840 843 841 844 private ProfileDescription createNewProfileDescription() { 842 845 ProfileDescription desc = ProfileDescription.createNewDescription(); … … 844 847 return desc; 845 848 } 846 849 847 850 private Comment createNewComment() { 848 851 Comment com = Comment.createANewComment(); 849 852 return com; 850 853 } 851 854 852 855 private String createXlink(String id) { 853 856 URI uri = uriInfo.getRequestUriBuilder().path(id).build(); 854 857 return uri.toString(); 855 858 } 856 859 857 860 private void validate(RegisterResponse response, Validator... validators) { 858 861 for (Validator validator : validators) { … … 864 867 } 865 868 } 866 869 867 870 private void validateComment(CommentResponse response, Validator... validators) { 868 871 for (Validator validator : validators) { -
ComponentRegistry/trunk/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/rest/MDValidator.java
r1816 r1860 3 3 import clarin.cmdi.componentregistry.ComponentRegistry; 4 4 import clarin.cmdi.componentregistry.ComponentRegistryException; 5 import clarin.cmdi.componentregistry.ComponentRegistryResourceResolver; 5 6 import clarin.cmdi.componentregistry.Configuration; 6 7 import clarin.cmdi.componentregistry.MDMarshaller; … … 28 29 29 30 public class MDValidator implements Validator { 30 31 31 32 private final static Logger LOG = LoggerFactory.getLogger(MDValidator.class); 32 33 static final String MISMATCH_ERROR = "Cannot register component as a profile or vica versa."; … … 49 50 50 51 /** 51 * 52 * @param input In order to validate the input is consumed. So use @see getCMDComponentSpec to get the parsed CMDComponentSpec. 52 * 53 * @param input In order to validate the input is consumed. So use 54 * @see getCMDComponentSpec to get the parsed CMDComponentSpec. 53 55 * @param desc 54 * @param registry (registry you currently used) 55 * @param userRegistry can be null, We get user registry as well so we can give nice error messages if needed. Can be the same as @param registry 56 * @param registry (registry you currently used) 57 * @param userRegistry can be null, We get user registry as well so we can give nice error messages if needed. Can be the same as 58 * @param registry 56 59 */ 57 60 public MDValidator(InputStream input, AbstractDescription description, ComponentRegistry registry, ComponentRegistry userRegistry, ComponentRegistry publicRegistry) { … … 62 65 this.publicRegistry = publicRegistry; 63 66 } 64 67 65 68 @Override 66 69 public List<String> getErrorMessages() { 67 70 return errorMessages; 68 71 } 69 72 70 73 @Override 71 74 public boolean validate() { 72 75 try { 73 76 clarin.cmdi.schema.cmd.Validator validator = new clarin.cmdi.schema.cmd.Validator(new URL(Configuration.getInstance().getGeneralComponentSchema())); 77 validator.setResourceResolver(new ComponentRegistryResourceResolver()); 74 78 // We may need to reuse the input stream, so save it to a byte array first 75 79 byte[] inputBytes = getBytesFromInputStream(); … … 109 113 return errorMessages.isEmpty(); 110 114 } 111 115 112 116 private byte[] getBytesFromInputStream() throws IOException { 113 117 int len; 114 118 byte[] b = new byte[4096]; 115 119 final ByteArrayOutputStream bOS = new ByteArrayOutputStream(); 116 120 117 121 while ((len = input.read(b)) > 0) { 118 122 bOS.write(b, 0, len); 119 123 } 120 124 121 125 return bOS.toByteArray(); 122 126 } 123 127 124 128 private void validateComponents(List<CMDComponentType> cmdComponents) throws ComponentRegistryException { 125 129 for (CMDComponentType cmdComponentType : cmdComponents) { … … 128 132 } 129 133 } 130 134 131 135 private void validateDescribedComponents(CMDComponentType cmdComponentType) throws ComponentRegistryException { 132 136 checkPublicComponents(cmdComponentType); 133 137 } 134 138 135 139 private void checkPublicComponents(CMDComponentType cmdComponentType) throws ComponentRegistryException { 136 140 if (isDefinedInSeparateFile(cmdComponentType)) { … … 157 161 } 158 162 } 159 163 160 164 } 161 165 } 162 166 } 163 167 164 168 private boolean isDefinedInSeparateFile(CMDComponentType cmdComponentType) { 165 169 return cmdComponentType.getName() == null; 166 170 } 167 171 168 172 public CMDComponentSpec getCMDComponentSpec() { 169 173 return spec;
Note: See TracChangeset
for help on using the changeset viewer.