Ignore:
Timestamp:
08/11/14 16:07:55 (10 years ago)
Author:
olhsha@mpi.nl
Message:

Added group service. Tested via the tomcat on loclahots (test URI and postman), old unit tests are adjusted and work well. Todo: retest on localhost tomcat, look at run-time exceptions, add new unit tests, adjust front-end

File:
1 edited

Legend:

Unmodified
Added
Removed
  • ComponentRegistry/trunk/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/rest/MDValidator.java

    r4550 r5549  
    55import clarin.cmdi.componentregistry.ComponentRegistryResourceResolver;
    66import clarin.cmdi.componentregistry.Configuration;
     7import clarin.cmdi.componentregistry.ItemNotFoundException;
    78import clarin.cmdi.componentregistry.MDMarshaller;
     9import clarin.cmdi.componentregistry.NullIdException;
     10import clarin.cmdi.componentregistry.RegistrySpace;
     11import clarin.cmdi.componentregistry.UserUnauthorizedException;
    812import clarin.cmdi.componentregistry.components.CMDComponentSpec;
    913import clarin.cmdi.componentregistry.components.CMDComponentType;
    1014import clarin.cmdi.componentregistry.model.BaseDescription;
    11 import clarin.cmdi.componentregistry.model.ComponentDescription;
    1215import clarin.cmdi.schema.cmd.Validator.Message;
    1316import clarin.cmdi.schema.cmd.ValidatorException;
     
    2932
    3033public class MDValidator implements Validator {
    31    
     34
    3235    private final static Logger LOG = LoggerFactory.getLogger(MDValidator.class);
    3336    static final String MISMATCH_ERROR = "Cannot register component as a profile or vica versa.";
     
    3639    static final String SCHEMA_ERROR = "Error in reading general component schema: ";
    3740    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 published components: ";
     41    static final String COMPONENT_NOT_REGISTERED_IN_APPROPRIATE_SPACE_ERROR = "referenced component cannot be found in the appropriate registry components: ";
    3942    static final String COMPONENT_REGISTRY_EXCEPTION_ERROR = "An exception occurred while accessing the component registry: ";
    4043    static final String ILLEGAL_ATTRIBUTE_NAME_ERROR = "Illegal attribute name: ";
     
    4750    private final BaseDescription description;
    4851    private final ComponentRegistry registry;
    49     private final ComponentRegistry userRegistry;
    50     private final ComponentRegistry publicRegistry;
    5152    private final MDMarshaller marshaller;
    5253
     
    6162     * @param registry
    6263     */
    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) {
    6465        this.input = input;
    6566        this.description = description;
    6667        this.registry = registry;
    67         this.userRegistry = userRegistry;
    68         this.publicRegistry = publicRegistry;
    6968        this.marshaller = marshaller;
    7069    }
    71    
     70
    7271    @Override
    7372    public List<String> getErrorMessages() {
    7473        return errorMessages;
    7574    }
    76    
     75
    7776    @Override
    78     public boolean validate() {
     77    public boolean validate() throws UserUnauthorizedException {
    7978        try {
    8079            clarin.cmdi.schema.cmd.Validator validator = new clarin.cmdi.schema.cmd.Validator(new URL(Configuration.getInstance().getGeneralComponentSchema()));
     
    116115            } catch (ComponentRegistryException e) {
    117116                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);
    118121            }
    119122        }
    120123        return errorMessages.isEmpty();
    121124    }
    122    
     125
    123126    private byte[] getBytesFromInputStream() throws IOException {
    124127        int len;
    125128        byte[] b = new byte[4096];
    126129        final ByteArrayOutputStream bOS = new ByteArrayOutputStream();
    127        
     130
    128131        while ((len = input.read(b)) > 0) {
    129132            bOS.write(b, 0, len);
    130133        }
    131        
     134
    132135        return bOS.toByteArray();
    133136    }
    134137
    135     private void validateComponents(CMDComponentSpec componentSpec) throws ComponentRegistryException {
     138    private void validateComponents(CMDComponentSpec componentSpec) throws ComponentRegistryException, UserUnauthorizedException, ItemNotFoundException, NullIdException {
    136139        validateComponents(Collections.singletonList(componentSpec.getCMDComponent()));
    137140    }
    138    
    139     private void validateComponents(List<CMDComponentType> cmdComponents) throws ComponentRegistryException {
     141
     142    private void validateComponents(List<CMDComponentType> cmdComponents) throws ComponentRegistryException, UserUnauthorizedException, ItemNotFoundException, NullIdException {
    140143        for (CMDComponentType cmdComponentType : cmdComponents) {
    141             validateDescribedComponents(cmdComponentType);
    142             validateComponents(cmdComponentType.getCMDComponent());//Recursion
    143         }
    144     }
    145    
    146     private void validateDescribedComponents(CMDComponentType cmdComponentType) throws ComponentRegistryException {
    147         checkPublicComponents(cmdComponentType);
    148     }
    149    
    150     private void checkPublicComponents(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 {
    151154        if (isDefinedInSeparateFile(cmdComponentType)) {
    152155            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;
    162183                        }
     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;
    163190                    }
    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;
    165193                }
    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
    179201    private boolean isDefinedInSeparateFile(CMDComponentType cmdComponentType) {
    180202        return cmdComponentType.getName() == null;
     
    195217     * has validated. If you are not going to alter this copy, you can re-use
    196218     * 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>
    199220     *
    200221     * @return a freshly unmarshalled copy of the spec based on the bytes
     
    209230        // Re-unmarshall original bytes
    210231        return unmarshalSpec(originalSpecBytes);
    211     }
    212    
     232
     233
     234    }
     235
    213236    private CMDComponentSpec unmarshalSpec(byte[] inputBytes) throws JAXBException {
    214237        return marshaller.unmarshal(CMDComponentSpec.class, new ByteArrayInputStream(inputBytes), null);
Note: See TracChangeset for help on using the changeset viewer.