Changeset 2348
- Timestamp:
- 11/13/12 09:30:49 (12 years ago)
- Location:
- ComponentRegistry/branches/ComponentRegistry-1.13.0-olha/ComponentRegistry
- Files:
-
- 9 edited
Legend:
- Unmodified
- Added
- Removed
-
ComponentRegistry/branches/ComponentRegistry-1.13.0-olha/ComponentRegistry
- Property svn:mergeinfo changed
/ComponentRegistry/trunk/ComponentRegistry merged: 2146,2189,2261,2347
- Property svn:mergeinfo changed
-
ComponentRegistry/branches/ComponentRegistry-1.13.0-olha/ComponentRegistry/src/main/binding
- Property svn:mergeinfo changed (with no actual effect on merging)
-
ComponentRegistry/branches/ComponentRegistry-1.13.0-olha/ComponentRegistry/src/main/java
- Property svn:mergeinfo changed
/ComponentRegistry/trunk/ComponentRegistry/src/main/java merged: 2347
- Property svn:mergeinfo changed
-
ComponentRegistry/branches/ComponentRegistry-1.13.0-olha/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/CMDComponentSpecExpander.java
r1753 r2348 26 26 public void expandNestedComponent(List<CMDComponentType> cmdComponents, String id) throws ComponentRegistryException { 27 27 expandNestedComponent(cmdComponents, new HashSet<String>(Collections.singleton(id))); 28 }29 30 public void expandNestedComponent(List<CMDComponentType> cmdComponents) throws ComponentRegistryException {31 expandNestedComponent(cmdComponents, new HashSet<String>());32 28 } 33 29 -
ComponentRegistry/branches/ComponentRegistry-1.13.0-olha/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/rest/ComponentRegistryRestService.java
r2296 r2348 85 85 * ComponentStatus 86 86 * 87 * 87 88 * @param userSpace 88 89 * @return … … 809 810 810 811 try { 811 // Expand to check for recursion (todo: run on copy or do a dry-run so that the actual spec does not get expanded) 812 registry.getExpander().expandNestedComponent(spec.getCMDComponent(), desc.getId()); 812 checkForRecursion(validator, registry, desc); 813 813 814 814 // Add profile … … 838 838 LOG.error("Error when closing inputstream: ", e); 839 839 } 840 } 841 } 842 843 /** 844 * 845 * @param validator 846 * @param registry 847 * @param desc 848 * @throws ComponentRegistryException if recursion is detected or something goes wrong while trying to detect recursion 849 */ 850 private void checkForRecursion(MDValidator validator, ComponentRegistry registry, AbstractDescription desc) throws ComponentRegistryException { 851 try { 852 // Expand to check for recursion. Operate on copy so that original does not get expanded. 853 final CMDComponentSpec specCopy = validator.getCopyOfCMDComponentSpec(); 854 // In case of recursion, the following will throw a ComponentRegistryException 855 registry.getExpander().expandNestedComponent(specCopy.getCMDComponent(), desc.getId()); 856 } catch (JAXBException ex) { 857 throw new ComponentRegistryException("Unmarshalling failed while preparing recursion detection", ex); 840 858 } 841 859 } -
ComponentRegistry/branches/ComponentRegistry-1.13.0-olha/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/rest/MDValidator.java
r1860 r2348 29 29 30 30 public class MDValidator implements Validator { 31 31 32 32 private final static Logger LOG = LoggerFactory.getLogger(MDValidator.class); 33 33 static final String MISMATCH_ERROR = "Cannot register component as a profile or vica versa."; … … 43 43 private List<String> errorMessages = new ArrayList<String>(); 44 44 private CMDComponentSpec spec = null; 45 private byte[] originalSpecBytes; 45 46 private final InputStream input; 46 47 private final AbstractDescription description; … … 65 66 this.publicRegistry = publicRegistry; 66 67 } 67 68 68 69 @Override 69 70 public List<String> getErrorMessages() { 70 71 return errorMessages; 71 72 } 72 73 73 74 @Override 74 75 public boolean validate() { … … 77 78 validator.setResourceResolver(new ComponentRegistryResourceResolver()); 78 79 // We may need to reuse the input stream, so save it to a byte array first 79 byte[] inputBytes = getBytesFromInputStream();80 StreamSource source = new StreamSource(new ByteArrayInputStream( inputBytes));80 originalSpecBytes = getBytesFromInputStream(); 81 StreamSource source = new StreamSource(new ByteArrayInputStream(originalSpecBytes)); 81 82 if (!validator.validateProfile(source)) { 82 83 final List<Message> validatorMessages = validator.getMessages(); … … 89 90 } 90 91 } else { 91 spec = MDMarshaller.unmarshal(CMDComponentSpec.class, new ByteArrayInputStream(inputBytes), null);92 spec = unmarshalSpec(originalSpecBytes); 92 93 if (spec.isIsProfile() != description.isProfile()) { 93 94 errorMessages.add(MISMATCH_ERROR); … … 113 114 return errorMessages.isEmpty(); 114 115 } 115 116 116 117 private byte[] getBytesFromInputStream() throws IOException { 117 118 int len; 118 119 byte[] b = new byte[4096]; 119 120 final ByteArrayOutputStream bOS = new ByteArrayOutputStream(); 120 121 121 122 while ((len = input.read(b)) > 0) { 122 123 bOS.write(b, 0, len); 123 124 } 124 125 125 126 return bOS.toByteArray(); 126 127 } 127 128 128 129 private void validateComponents(List<CMDComponentType> cmdComponents) throws ComponentRegistryException { 129 130 for (CMDComponentType cmdComponentType : cmdComponents) { … … 132 133 } 133 134 } 134 135 135 136 private void validateDescribedComponents(CMDComponentType cmdComponentType) throws ComponentRegistryException { 136 137 checkPublicComponents(cmdComponentType); 137 138 } 138 139 139 140 private void checkPublicComponents(CMDComponentType cmdComponentType) throws ComponentRegistryException { 140 141 if (isDefinedInSeparateFile(cmdComponentType)) { … … 161 162 } 162 163 } 163 164 164 165 } 165 166 } 166 167 } 167 168 168 169 private boolean isDefinedInSeparateFile(CMDComponentType cmdComponentType) { 169 170 return cmdComponentType.getName() == null; 170 171 } 171 172 173 /** 174 * Do not call before having called {@link #validate() }! 175 * 176 * @return the spec unmarshalled during {@link #validate() }. If this has not been called, returns null. 177 */ 172 178 public CMDComponentSpec getCMDComponentSpec() { 173 179 return spec; 174 180 } 181 182 /** 183 * Creates a fresh (re-unmarshalled) copy of the specification this instance has validated. If you are not going to alter this copy, 184 * you can re-use and share the copy used during validation by getting it from {@link #getCMDComponentSpec() }. 185 * <em>Do not call before having called {@link #validate() }!</em> 186 * 187 * @return a freshly unmarshalled copy of the spec based on the bytes collected from the input stream passed to {@link #validate() }. 188 * If this has not been called, returns null. 189 * @throws JAXBException exception occurred while marshalling from the input bytes 190 * @see #validate() 191 * @see #getCMDComponentSpec() 192 */ 193 public CMDComponentSpec getCopyOfCMDComponentSpec() throws JAXBException { 194 // Re-unmarshall original bytes 195 return unmarshalSpec(originalSpecBytes); 196 } 197 198 private static CMDComponentSpec unmarshalSpec(byte[] inputBytes) throws JAXBException { 199 return MDMarshaller.unmarshal(CMDComponentSpec.class, new ByteArrayInputStream(inputBytes), null); 200 } 175 201 } -
ComponentRegistry/branches/ComponentRegistry-1.13.0-olha/ComponentRegistry/src/main/webresources/WEB-INF/spring-config/component-registry.xml
r1947 r2348 17 17 <property name="component2SchemaXsl" value="http://www.clarin.eu/cmd/xslt/comp2schema-v2/comp2schema.xsl"/> 18 18 <property name="componentSpecSchemaLocation" value="http://www.clarin.eu/cmd http://www.clarin.eu/cmd/general-component-schema.xsd" /> 19 <property name="isocatRestUrl" value="http://www.isocat.org/rest/" /> 19 <!-- TODO: Read the following from a context parameter --> 20 <property name="isocatRestUrl" value="https://catalog.clarin.eu/isocat/rest/" /> 20 21 <!-- Extra attribute keys that are passed in a shibboleth authenticated request principal. 21 22 You need to configure the shhaa.xml file to enable the extra attributes. -
ComponentRegistry/branches/ComponentRegistry-1.13.0-olha/ComponentRegistry/src/test/java
- Property svn:mergeinfo changed
/ComponentRegistry/trunk/ComponentRegistry/src/test/java merged: 2146,2189,2347
- Property svn:mergeinfo changed
-
ComponentRegistry/branches/ComponentRegistry-1.13.0-olha/ComponentRegistry/src/test/java/clarin/cmdi/componentregistry/rest/MDValidatorTest.java
r1993 r2348 4 4 import clarin.cmdi.componentregistry.ComponentRegistryFactory; 5 5 import clarin.cmdi.componentregistry.ComponentStatus; 6 import clarin.cmdi.componentregistry.components.CMDComponentSpec; 6 7 import clarin.cmdi.componentregistry.impl.database.ComponentRegistryTestDatabase; 7 8 import clarin.cmdi.componentregistry.model.ComponentDescription; … … 37 38 @Test 38 39 public void testValidateSucces() { 39 String profileContent = ""; 40 profileContent += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; 41 profileContent += "<CMD_ComponentSpec isProfile=\"true\" xmlns:xml=\"http://www.w3.org/XML/1998/namespace\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"; 42 profileContent += " xsi:noNamespaceSchemaLocation=\"general-component-schema.xsd\">\n"; 43 profileContent += " <Header />\n"; 44 profileContent += " <CMD_Component name=\"Actor\" CardinalityMin=\"0\" CardinalityMax=\"unbounded\">\n"; 45 profileContent += " <CMD_Element name=\"Age\">\n"; 46 profileContent += " <ValueScheme>\n"; 47 profileContent += " <pattern>[23][0-9]</pattern>\n"; 48 profileContent += " </ValueScheme>\n"; 49 profileContent += " </CMD_Element>\n"; 50 profileContent += " </CMD_Component>\n"; 51 profileContent += "</CMD_ComponentSpec>\n"; 52 InputStream input = new ByteArrayInputStream(profileContent.getBytes()); 53 54 ProfileDescription desc = ProfileDescription.createNewDescription(); 55 MDValidator validator = new MDValidator(input, desc, publicRegistry, null, publicRegistry); 40 MDValidator validator = getValidProfileValidator(); 56 41 assertTrue(validator.validate()); 57 42 } … … 222 207 assertTrue(validator.validate()); 223 208 assertEquals(0, validator.getErrorMessages().size()); 224 209 } 210 211 /** 212 * Test of getCMDComponentSpec method, of class MDValidator. 213 */ 214 @Test 215 public void testGetCMDComponentSpec() throws Exception { 216 String profileContent = getValidProfileString(); 217 InputStream input = new ByteArrayInputStream(profileContent.getBytes()); 218 219 ProfileDescription desc = ProfileDescription.createNewDescription(); 220 MDValidator validator = new MDValidator(input, desc, publicRegistry, null, publicRegistry); 221 222 // Spec is created during validation, before it should be null 223 assertNull(validator.getCMDComponentSpec()); 224 validator.validate(); 225 226 // Get spec created during validation 227 final CMDComponentSpec cmdComponentSpec = validator.getCMDComponentSpec(); 228 assertNotNull(cmdComponentSpec); 229 230 // Spec content should match XML 231 assertTrue(cmdComponentSpec.isIsProfile()); 232 assertEquals("Actor", cmdComponentSpec.getCMDComponent().get(0).getName()); 233 234 // Spec copy should be a freshly unmarshalled copy 235 final CMDComponentSpec specCopy = validator.getCopyOfCMDComponentSpec(); 236 assertNotSame(cmdComponentSpec, specCopy); 237 238 // Content should still match XML 239 assertTrue(specCopy.isIsProfile()); 240 assertEquals("Actor", specCopy.getCMDComponent().get(0).getName()); 241 } 242 243 private MDValidator getValidProfileValidator() { 244 final String profileContent = getValidProfileString(); 245 InputStream input = new ByteArrayInputStream(profileContent.getBytes()); 246 ProfileDescription desc = ProfileDescription.createNewDescription(); 247 MDValidator validator = new MDValidator(input, desc, publicRegistry, null, publicRegistry); 248 return validator; 249 } 250 251 private String getValidProfileString() { 252 String profileContent = ""; 253 profileContent += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; 254 profileContent += "<CMD_ComponentSpec isProfile=\"true\" xmlns:xml=\"http://www.w3.org/XML/1998/namespace\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"; 255 profileContent += " xsi:noNamespaceSchemaLocation=\"general-component-schema.xsd\">\n"; 256 profileContent += " <Header />\n"; 257 profileContent += " <CMD_Component name=\"Actor\" CardinalityMin=\"0\" CardinalityMax=\"unbounded\">\n"; 258 profileContent += " <CMD_Element name=\"Age\">\n"; 259 profileContent += " <ValueScheme>\n"; 260 profileContent += " <pattern>[23][0-9]</pattern>\n"; 261 profileContent += " </ValueScheme>\n"; 262 profileContent += " </CMD_Element>\n"; 263 profileContent += " </CMD_Component>\n"; 264 profileContent += "</CMD_ComponentSpec>\n"; 265 return profileContent; 225 266 } 226 267 }
Note: See TracChangeset
for help on using the changeset viewer.