source: ComponentRegistry/trunk/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/rest/MDValidator.java @ 207

Last change on this file since 207 was 207, checked in by patdui, 14 years ago
  • addd Basic security in webapp
  • Added Delete functionality in registry
  • Made POST and DELETE use authorisation
File size: 3.3 KB
Line 
1package clarin.cmdi.componentregistry.rest;
2
3import java.io.InputStream;
4import java.util.ArrayList;
5import java.util.List;
6
7import javax.xml.bind.JAXBException;
8
9import clarin.cmdi.componentregistry.ComponentRegistry;
10import clarin.cmdi.componentregistry.MDMarshaller;
11import clarin.cmdi.componentregistry.components.CMDComponentSpec;
12import clarin.cmdi.componentregistry.components.CMDComponentType;
13import clarin.cmdi.componentregistry.model.AbstractDescription;
14
15public class MDValidator implements Validator {
16
17    static final String ISPROFILE_NOT_SET_ERROR = "'isProfile' attribute is obligated for this registry please specify it in the xml.";
18    static final String MISMATCH_ERROR = "Cannot register component as a profile or vica versa.";
19    static final String COMPONENT_NOT_REGISTERED_ERROR = "referenced component is not registered or does not have a correct componentId : ";
20    static final String PARSE_ERROR = "Error in validation input file. Error is: ";
21
22    private List<String> errorMessages = new ArrayList<String>();
23    private CMDComponentSpec spec = null;
24    private final InputStream input;
25    private final AbstractDescription description;
26    private final ComponentRegistry registry;
27
28    /**
29     *
30     * @param input In order to validate the input is consumed. So use @see getCMDComponentSpec to get the parsed CMDComponentSpec.
31     * @param desc
32     */
33    public MDValidator(InputStream input, AbstractDescription description, ComponentRegistry registry) {
34        this.input = input;
35        this.description = description;
36        this.registry = registry;
37    }
38
39    public List<String> getErrorMessages() {
40        return errorMessages;
41    }
42
43    public boolean validate() {
44        try {
45            spec = MDMarshaller.unmarshal(CMDComponentSpec.class, input, MDMarshaller.getCMDComponentSchema());
46            if (spec.isIsProfile() == null) {
47                errorMessages.add(ISPROFILE_NOT_SET_ERROR);
48            } else if (spec.isIsProfile().booleanValue() != description.isProfile()) {
49                errorMessages.add(MISMATCH_ERROR);
50            }
51        } catch (JAXBException e) {
52            errorMessages.add(PARSE_ERROR + e);
53        }
54        if (errorMessages.isEmpty()) {
55            validateComponents(spec.getCMDComponent());
56        }
57        return errorMessages.isEmpty();
58    }
59
60    private void validateComponents(List<CMDComponentType> cmdComponents) {
61        for (CMDComponentType cmdComponentType : cmdComponents) {
62            validateDescribedComponents(cmdComponentType);
63            validateComponents(cmdComponentType.getCMDComponent());//Recursion
64        }
65    }
66
67    private void validateDescribedComponents(CMDComponentType cmdComponentType) {
68        if (isDefinedInSeparateFile(cmdComponentType)) {
69            String id = cmdComponentType.getComponentId();
70            CMDComponentSpec registeredComponent = registry.getMDComponent(id);
71            if (registeredComponent == null) {
72                errorMessages.add(COMPONENT_NOT_REGISTERED_ERROR + cmdComponentType.getComponentId());
73            }
74        }
75    }
76
77    private boolean isDefinedInSeparateFile(CMDComponentType cmdComponentType) {
78        return cmdComponentType.getName() == null;
79    }
80
81    public CMDComponentSpec getCMDComponentSpec() {
82        return spec;
83    }
84
85}
Note: See TracBrowser for help on using the repository browser.