Changeset 5549 for ComponentRegistry/trunk/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/rest/MDValidator.java
- Timestamp:
- 08/11/14 16:07:55 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
ComponentRegistry/trunk/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/rest/MDValidator.java
r4550 r5549 5 5 import clarin.cmdi.componentregistry.ComponentRegistryResourceResolver; 6 6 import clarin.cmdi.componentregistry.Configuration; 7 import clarin.cmdi.componentregistry.ItemNotFoundException; 7 8 import clarin.cmdi.componentregistry.MDMarshaller; 9 import clarin.cmdi.componentregistry.NullIdException; 10 import clarin.cmdi.componentregistry.RegistrySpace; 11 import clarin.cmdi.componentregistry.UserUnauthorizedException; 8 12 import clarin.cmdi.componentregistry.components.CMDComponentSpec; 9 13 import clarin.cmdi.componentregistry.components.CMDComponentType; 10 14 import clarin.cmdi.componentregistry.model.BaseDescription; 11 import clarin.cmdi.componentregistry.model.ComponentDescription;12 15 import clarin.cmdi.schema.cmd.Validator.Message; 13 16 import clarin.cmdi.schema.cmd.ValidatorException; … … 29 32 30 33 public class MDValidator implements Validator { 31 34 32 35 private final static Logger LOG = LoggerFactory.getLogger(MDValidator.class); 33 36 static final String MISMATCH_ERROR = "Cannot register component as a profile or vica versa."; … … 36 39 static final String SCHEMA_ERROR = "Error in reading general component schema: "; 37 40 static final String IO_ERROR = "Error while reading specification or general component schema: "; 38 static final String COMPONENT_NOT_ PUBLICLY_REGISTERED_ERROR = "referenced component cannot be found in the publishedcomponents: ";41 static final String COMPONENT_NOT_REGISTERED_IN_APPROPRIATE_SPACE_ERROR = "referenced component cannot be found in the appropriate registry components: "; 39 42 static final String COMPONENT_REGISTRY_EXCEPTION_ERROR = "An exception occurred while accessing the component registry: "; 40 43 static final String ILLEGAL_ATTRIBUTE_NAME_ERROR = "Illegal attribute name: "; … … 47 50 private final BaseDescription description; 48 51 private final ComponentRegistry registry; 49 private final ComponentRegistry userRegistry;50 private final ComponentRegistry publicRegistry;51 52 private final MDMarshaller marshaller; 52 53 … … 61 62 * @param registry 62 63 */ 63 public MDValidator(InputStream input, BaseDescription description, ComponentRegistry registry, ComponentRegistry userRegistry, ComponentRegistry publicRegistry,MDMarshaller marshaller) {64 public MDValidator(InputStream input, BaseDescription description, ComponentRegistry registry, MDMarshaller marshaller) { 64 65 this.input = input; 65 66 this.description = description; 66 67 this.registry = registry; 67 this.userRegistry = userRegistry;68 this.publicRegistry = publicRegistry;69 68 this.marshaller = marshaller; 70 69 } 71 70 72 71 @Override 73 72 public List<String> getErrorMessages() { 74 73 return errorMessages; 75 74 } 76 75 77 76 @Override 78 public boolean validate() {77 public boolean validate() throws UserUnauthorizedException { 79 78 try { 80 79 clarin.cmdi.schema.cmd.Validator validator = new clarin.cmdi.schema.cmd.Validator(new URL(Configuration.getInstance().getGeneralComponentSchema())); … … 116 115 } catch (ComponentRegistryException e) { 117 116 errorMessages.add(COMPONENT_REGISTRY_EXCEPTION_ERROR + e); 117 } catch (ItemNotFoundException e2) { 118 errorMessages.add(COMPONENT_NOT_REGISTERED_ERROR + e2); 119 } catch (NullIdException e3) { 120 errorMessages.add(COMPONENT_NOT_REGISTERED_ERROR + e3); 118 121 } 119 122 } 120 123 return errorMessages.isEmpty(); 121 124 } 122 125 123 126 private byte[] getBytesFromInputStream() throws IOException { 124 127 int len; 125 128 byte[] b = new byte[4096]; 126 129 final ByteArrayOutputStream bOS = new ByteArrayOutputStream(); 127 130 128 131 while ((len = input.read(b)) > 0) { 129 132 bOS.write(b, 0, len); 130 133 } 131 134 132 135 return bOS.toByteArray(); 133 136 } 134 137 135 private void validateComponents(CMDComponentSpec componentSpec) throws ComponentRegistryException {138 private void validateComponents(CMDComponentSpec componentSpec) throws ComponentRegistryException, UserUnauthorizedException, ItemNotFoundException, NullIdException { 136 139 validateComponents(Collections.singletonList(componentSpec.getCMDComponent())); 137 140 } 138 139 private void validateComponents(List<CMDComponentType> cmdComponents) throws ComponentRegistryException {141 142 private void validateComponents(List<CMDComponentType> cmdComponents) throws ComponentRegistryException, UserUnauthorizedException, ItemNotFoundException, NullIdException { 140 143 for (CMDComponentType cmdComponentType : cmdComponents) { 141 validateDescribedComponents(cmdComponentType);142 validateComponents(cmdComponentType.getCMDComponent());//Recursion143 } 144 } 145 146 private void validateDescribedComponents(CMDComponentType cmdComponentType) throws ComponentRegistryException {147 checkPublicComponents(cmdComponentType);148 } 149 150 private void check PublicComponents(CMDComponentType cmdComponentType) throws ComponentRegistryException {144 this.validateDescribedComponents(cmdComponentType); 145 this.validateComponents(cmdComponentType.getCMDComponent());//Recursion 146 } 147 } 148 149 private void validateDescribedComponents(CMDComponentType cmdComponentType) throws ComponentRegistryException, UserUnauthorizedException, ItemNotFoundException, NullIdException { 150 this.checkComponentInSpace(cmdComponentType); 151 } 152 153 private void checkComponentInSpace(CMDComponentType cmdComponentType) throws ComponentRegistryException, UserUnauthorizedException, ItemNotFoundException, NullIdException { 151 154 if (isDefinedInSeparateFile(cmdComponentType)) { 152 155 String id = cmdComponentType.getComponentId(); 153 CMDComponentSpec registeredComponent; 154 if (registry.isPublic()) { // public registry requires only published components 155 registeredComponent = registry.getMDComponent(id); 156 if (registeredComponent == null) { 157 String error = cmdComponentType.getComponentId(); 158 if (userRegistry != null) { 159 ComponentDescription desc = userRegistry.getComponentDescription(id); 160 if (desc != null) { 161 error = desc.getName() + " (" + cmdComponentType.getComponentId() + ")"; 156 if (id == null) { 157 String name = (cmdComponentType.getName() == null) ? "null" : cmdComponentType.getName(); 158 throw new NullIdException("The component with the name " + name + " has a null id. :("); 159 } 160 CMDComponentSpec registeredComponent = registry.getMDComponent(id); 161 if (registeredComponent != null) { 162 String componentId = cmdComponentType.getComponentId(); 163 Boolean isPublicB = registry.isItemPublic(id);// throws ItemNotFoundException 164 boolean isPublic = isPublicB.booleanValue(); 165 if (isPublic) { // if a component is public, it is available for any registry 166 return; 167 }; 168 // a private component for a private registry is available only if its owner is the owner of the resgitry 169 if (registry.getRegistrySpace().equals(RegistrySpace.PRIVATE)) { 170 Number registryOwnerId = registry.getRegistryOwner().getId(); 171 Number componentOwnerId = registry.getBaseDescriptionOwnerId(cmdComponentType.getComponentId()); 172 if (registryOwnerId.equals(componentOwnerId)) { 173 return; 174 }; 175 errorMessages.add(COMPONENT_NOT_REGISTERED_IN_APPROPRIATE_SPACE_ERROR + componentId + " (private registry)"); 176 return; 177 178 } else { // a private component in a group registry is availabe only if it belongs to the group 179 if (registry.getRegistrySpace().equals(RegistrySpace.GROUP)) { 180 if (registry.getGroupId() == null) { 181 errorMessages.add(COMPONENT_REGISTRY_EXCEPTION_ERROR + "in the group space, the group id is null"); 182 return; 162 183 } 184 List<Number> componentGroupIds = registry.getItemGroups(cmdComponentType.getComponentId()); 185 if (componentGroupIds.contains(registry.getGroupId())) { 186 return; 187 } 188 errorMessages.add(COMPONENT_NOT_REGISTERED_IN_APPROPRIATE_SPACE_ERROR + componentId + " (group registry) " + registry.getGroupId()); 189 return; 163 190 } 164 errorMessages.add(COMPONENT_NOT_PUBLICLY_REGISTERED_ERROR + error); 191 errorMessages.add(COMPONENT_NOT_REGISTERED_IN_APPROPRIATE_SPACE_ERROR + componentId + " (private component in public registry)."); 192 return; 165 193 } 166 } else { //User registry, can link to components from public registry and the user's registry 167 registeredComponent = registry.getMDComponent(id); 168 if (registeredComponent == null) { 169 registeredComponent = publicRegistry.getMDComponent(id); 170 if (registeredComponent == null) { 171 errorMessages.add(COMPONENT_NOT_REGISTERED_ERROR + cmdComponentType.getComponentId()); 172 } 173 } 174 175 } 176 } 177 } 178 194 195 }; 196 errorMessages.add(COMPONENT_NOT_REGISTERED_ERROR + cmdComponentType.getComponentId()); 197 } 198 199 } 200 179 201 private boolean isDefinedInSeparateFile(CMDComponentType cmdComponentType) { 180 202 return cmdComponentType.getName() == null; … … 195 217 * has validated. If you are not going to alter this copy, you can re-use 196 218 * and share the copy used during validation by getting it from {@link #getCMDComponentSpec() 197 * }. 198 * <em>Do not call before having called {@link #validate() }!</em> 219 * }. <em>Do not call before having called {@link #validate() }!</em> 199 220 * 200 221 * @return a freshly unmarshalled copy of the spec based on the bytes … … 209 230 // Re-unmarshall original bytes 210 231 return unmarshalSpec(originalSpecBytes); 211 } 212 232 233 234 } 235 213 236 private CMDComponentSpec unmarshalSpec(byte[] inputBytes) throws JAXBException { 214 237 return marshaller.unmarshal(CMDComponentSpec.class, new ByteArrayInputStream(inputBytes), null);
Note: See TracChangeset
for help on using the changeset viewer.