Package org.apache.syncope.common.validation

Examples of org.apache.syncope.common.validation.SyncopeClientException


            }
        }
    }

    private void fill(final SyncTask task, final SyncTaskTO taskTO) {
        SyncopeClientException sce = new SyncopeClientException(SyncopeClientExceptionType.InvalidSyncTask);

        // 1. validate JEXL expressions in user and role templates
        if (taskTO.getUserTemplate() != null) {
            UserTO template = taskTO.getUserTemplate();

            if (StringUtils.isNotBlank(template.getUsername()) && !JexlUtil.isExpressionValid(template.getUsername())) {
                sce.addElement("Invalid JEXL: " + template.getUsername());
            }
            if (StringUtils.isNotBlank(template.getPassword()) && !JexlUtil.isExpressionValid(template.getPassword())) {
                sce.addElement("Invalid JEXL: " + template.getPassword());
            }

            checkJexl(template, sce);

            for (MembershipTO memb : template.getMemberships()) {
                checkJexl(memb, sce);
            }
        }
        if (taskTO.getRoleTemplate() != null) {
            RoleTO template = taskTO.getRoleTemplate();

            if (StringUtils.isNotBlank(template.getName()) && !JexlUtil.isExpressionValid(template.getName())) {
                sce.addElement("Invalid JEXL: " + template.getName());
            }

            checkJexl(template, sce);
        }
        if (!sce.isEmpty()) {
            SyncopeClientCompositeErrorException scce = new SyncopeClientCompositeErrorException(
                    HttpStatus.BAD_REQUEST);
            scce.addException(sce);
            throw scce;
        }
View Full Code Here


    }

    private SyncopeClientException checkMandatory(final AttributableUtil attrUtil,
            final AbstractAttributable attributable) {

        SyncopeClientException reqValMissing = new SyncopeClientException(
                SyncopeClientExceptionType.RequiredValuesMissing);

        LOG.debug("Check mandatory constraint among resources {}", attributable.getResources());

        // Check if there is some mandatory schema defined for which no value has been provided
        for (AbstractSchema schema : schemaDAO.findAll(attrUtil.schemaClass())) {
            if (attributable.getAttribute(schema.getName()) == null
                    && !schema.isReadonly()
                    && (JexlUtil.evaluateMandatoryCondition(schema.getMandatoryCondition(), attributable)
                    || evaluateMandatoryCondition(attrUtil, attributable, schema.getName(),
                    attrUtil.intMappingType()))) {

                LOG.error("Mandatory schema " + schema.getName() + " not provided with values");

                reqValMissing.addElement(schema.getName());
            }
        }
        for (AbstractDerSchema derSchema : derSchemaDAO.findAll(attrUtil.derSchemaClass())) {
            if (attributable.getDerivedAttribute(derSchema.getName()) == null
                    && evaluateMandatoryCondition(attrUtil, attributable, derSchema.getName(),
                    attrUtil.derIntMappingType())) {

                LOG.error("Mandatory derived schema " + derSchema.getName() + " does not evaluate to any value");

                reqValMissing.addElement(derSchema.getName());
            }
        }
        for (AbstractVirSchema virSchema : virSchemaDAO.findAll(attrUtil.virSchemaClass())) {
            if (attributable.getVirtualAttribute(virSchema.getName()) == null
                    && !virSchema.isReadonly()
                    && evaluateMandatoryCondition(attrUtil, attributable, virSchema.getName(),
                    attrUtil.virIntMappingType())) {

                LOG.error("Mandatory virtual schema " + virSchema.getName() + " not provided with values");

                reqValMissing.addElement(virSchema.getName());
            }
        }

        return reqValMissing;
    }
View Full Code Here

            final AbstractAttributableMod attributableMod, final AttributableUtil attrUtil,
            final SyncopeClientCompositeErrorException scce) {

        PropagationByResource propByRes = new PropagationByResource();

        SyncopeClientException invalidValues = new SyncopeClientException(SyncopeClientExceptionType.InvalidValues);

        // 1. resources to be removed
        for (String resourceToBeRemoved : attributableMod.getResourcesToBeRemoved()) {
            ExternalResource resource = getResource(resourceToBeRemoved);
            if (resource != null) {
                propByRes.add(ResourceOperation.DELETE, resource.getName());
                attributable.removeResource(resource);
            }
        }

        LOG.debug("Resources to be removed:\n{}", propByRes);

        // 2. resources to be added
        for (String resourceToBeAdded : attributableMod.getResourcesToBeAdded()) {
            ExternalResource resource = getResource(resourceToBeAdded);
            if (resource != null) {
                propByRes.add(ResourceOperation.CREATE, resource.getName());
                attributable.addResource(resource);
            }
        }

        LOG.debug("Resources to be added:\n{}", propByRes);

        // 3. attributes to be removed
        for (String attributeToBeRemoved : attributableMod.getAttributesToBeRemoved()) {
            AbstractSchema schema = getSchema(attributeToBeRemoved, attrUtil.schemaClass());
            if (schema != null) {
                AbstractAttr attribute = attributable.getAttribute(schema.getName());
                if (attribute == null) {
                    LOG.debug("No attribute found for schema {}", schema);
                } else {
                    String newValue = null;
                    for (AttributeMod mod : attributableMod.getAttributesToBeUpdated()) {
                        if (schema.getName().equals(mod.getSchema())) {
                            newValue = mod.getValuesToBeAdded().get(0);
                        }
                    }

                    if (!schema.isUniqueConstraint()
                            || (!attribute.getUniqueValue().getStringValue().equals(newValue))) {

                        attributable.removeAttribute(attribute);
                        attrDAO.delete(attribute.getId(), attrUtil.attrClass());
                    }
                }

                for (ExternalResource resource : resourceDAO.findAll()) {
                    for (AbstractMappingItem mapItem : attrUtil.getMappingItems(resource, MappingPurpose.PROPAGATION)) {
                        if (schema.getName().equals(mapItem.getIntAttrName())
                                && mapItem.getIntMappingType() == attrUtil.intMappingType()
                                && attributable.getResources().contains(resource)) {

                            propByRes.add(ResourceOperation.UPDATE, resource.getName());

                            if (mapItem.isAccountid() && attribute != null
                                    && !attribute.getValuesAsStrings().isEmpty()) {

                                propByRes.addOldAccountId(resource.getName(),
                                        attribute.getValuesAsStrings().iterator().next());
                            }
                        }
                    }
                }
            }
        }

        LOG.debug("Attributes to be removed:\n{}", propByRes);

        // 4. attributes to be updated
        for (AttributeMod attributeMod : attributableMod.getAttributesToBeUpdated()) {
            AbstractSchema schema = getSchema(attributeMod.getSchema(), attrUtil.schemaClass());
            if (schema != null) {
                for (ExternalResource resource : resourceDAO.findAll()) {
                    for (AbstractMappingItem mapItem : attrUtil.getMappingItems(resource, MappingPurpose.PROPAGATION)) {
                        if (schema.getName().equals(mapItem.getIntAttrName())
                                && mapItem.getIntMappingType() == attrUtil.intMappingType()
                                && attributable.getResources().contains(resource)) {

                            propByRes.add(ResourceOperation.UPDATE, resource.getName());
                        }
                    }
                }

                AbstractAttr attribute = attributable.getAttribute(schema.getName());
                if (attribute == null) {
                    attribute = attrUtil.newAttr();
                    attribute.setSchema(schema);
                    attribute.setOwner(attributable);

                    attributable.addAttribute(attribute);
                }

                // 1.1 remove values
                Set<Long> valuesToBeRemoved = new HashSet<Long>();
                for (String valueToBeRemoved : attributeMod.getValuesToBeRemoved()) {
                    if (attribute.getSchema().isUniqueConstraint()) {
                        if (attribute.getUniqueValue() != null
                                && valueToBeRemoved.equals(attribute.getUniqueValue().getValueAsString())) {

                            valuesToBeRemoved.add(attribute.getUniqueValue().getId());
                        }
                    } else {
                        for (AbstractAttrValue mav : attribute.getValues()) {
                            if (valueToBeRemoved.equals(mav.getValueAsString())) {
                                valuesToBeRemoved.add(mav.getId());
                            }
                        }
                    }
                }
                for (Long attributeValueId : valuesToBeRemoved) {
                    attributeValueDAO.delete(attributeValueId, attrUtil.attrValueClass());
                }

                // 1.2 add values
                List<String> valuesToBeAdded = attributeMod.getValuesToBeAdded();
                if (valuesToBeAdded != null && !valuesToBeAdded.isEmpty()
                        && (!schema.isUniqueConstraint() || attribute.getUniqueValue() == null
                        || !valuesToBeAdded.iterator().next().equals(attribute.getUniqueValue().getValueAsString()))) {

                    fillAttribute(attributeMod.getValuesToBeAdded(), attrUtil, schema, attribute, invalidValues);
                }

                // if no values are in, the attribute can be safely removed
                if (attribute.getValuesAsStrings().isEmpty()) {
                    attrDAO.delete(attribute);
                }
            }
        }

        if (!invalidValues.isEmpty()) {
            scce.addException(invalidValues);
        }

        LOG.debug("Attributes to be updated:\n{}", propByRes);

        // 5. derived attributes to be removed
        for (String derAttrToBeRemoved : attributableMod.getDerivedAttributesToBeRemoved()) {
            AbstractDerSchema derSchema = getDerivedSchema(derAttrToBeRemoved, attrUtil.derSchemaClass());
            if (derSchema != null) {
                AbstractDerAttr derAttr = attributable.getDerivedAttribute(derSchema.getName());
                if (derAttr == null) {
                    LOG.debug("No derived attribute found for schema {}", derSchema.getName());
                } else {
                    derAttrDAO.delete(derAttr);
                }

                for (ExternalResource resource : resourceDAO.findAll()) {
                    for (AbstractMappingItem mapItem : attrUtil.getMappingItems(resource, MappingPurpose.PROPAGATION)) {
                        if (derSchema.getName().equals(mapItem.getIntAttrName())
                                && mapItem.getIntMappingType() == attrUtil.derIntMappingType()
                                && attributable.getResources().contains(resource)) {

                            propByRes.add(ResourceOperation.UPDATE, resource.getName());

                            if (mapItem.isAccountid() && derAttr != null
                                    && !derAttr.getValue(attributable.getAttributes()).isEmpty()) {

                                propByRes.addOldAccountId(resource.getName(),
                                        derAttr.getValue(attributable.getAttributes()));
                            }
                        }
                    }
                }
            }
        }

        LOG.debug("Derived attributes to be removed:\n{}", propByRes);

        // 6. derived attributes to be added
        for (String derAttrToBeAdded : attributableMod.getDerivedAttributesToBeAdded()) {
            AbstractDerSchema derSchema = getDerivedSchema(derAttrToBeAdded, attrUtil.derSchemaClass());
            if (derSchema != null) {
                for (ExternalResource resource : resourceDAO.findAll()) {
                    for (AbstractMappingItem mapItem : attrUtil.getMappingItems(resource, MappingPurpose.PROPAGATION)) {
                        if (derSchema.getName().equals(mapItem.getIntAttrName())
                                && mapItem.getIntMappingType() == attrUtil.derIntMappingType()
                                && attributable.getResources().contains(resource)) {

                            propByRes.add(ResourceOperation.UPDATE, resource.getName());
                        }
                    }
                }

                AbstractDerAttr derAttr = attrUtil.newDerAttr();
                derAttr.setDerivedSchema(derSchema);
                derAttr.setOwner(attributable);
                attributable.addDerivedAttribute(derAttr);
            }
        }

        LOG.debug("Derived attributes to be added:\n{}", propByRes);

        // 7. virtual attributes: for users and roles this is delegated to PropagationManager
        if (AttributableType.USER != attrUtil.getType() && AttributableType.ROLE != attrUtil.getType()) {
            fillVirtual(attributable, attributableMod.getVirtualAttributesToBeRemoved(),
                    attributableMod.getVirtualAttributesToBeUpdated(), attrUtil);
        }

        // Finally, check if mandatory values are missing
        SyncopeClientException requiredValuesMissing = checkMandatory(attrUtil, attributable);
        if (!requiredValuesMissing.isEmpty()) {
            scce.addException(requiredValuesMissing);
        }

        // Throw composite exception if there is at least one element set in the composing exceptions
        if (scce.hasExceptions()) {
View Full Code Here

    protected void fill(final AbstractAttributable attributable, final AbstractAttributableTO attributableTO,
            final AttributableUtil attributableUtil, final SyncopeClientCompositeErrorException scce) {

        // 1. attributes
        SyncopeClientException invalidValues = new SyncopeClientException(SyncopeClientExceptionType.InvalidValues);

        AbstractSchema schema;
        AbstractAttr attribute;

        // Only consider attributeTO with values
        for (AttributeTO attributeTO : attributableTO.getAttributes()) {
            if (attributeTO.getValues() != null && !attributeTO.getValues().isEmpty()) {

                schema = getSchema(attributeTO.getSchema(), attributableUtil.schemaClass());

                if (schema != null) {
                    attribute = attributable.getAttribute(schema.getName());
                    if (attribute == null) {
                        attribute = attributableUtil.newAttr();
                        attribute.setSchema(schema);
                    }

                    fillAttribute(attributeTO.getValues(), attributableUtil, schema, attribute, invalidValues);

                    if (!attribute.getValuesAsStrings().isEmpty()) {
                        attributable.addAttribute(attribute);
                        attribute.setOwner(attributable);
                    }
                }
            }
        }

        if (!invalidValues.isEmpty()) {
            scce.addException(invalidValues);
        }

        // 2. derived attributes
        AbstractDerSchema derivedSchema;
        AbstractDerAttr derivedAttribute;
        for (AttributeTO attributeTO : attributableTO.getDerivedAttributes()) {

            derivedSchema = getDerivedSchema(attributeTO.getSchema(), attributableUtil.derSchemaClass());

            if (derivedSchema != null) {
                derivedAttribute = attributableUtil.newDerAttr();
                derivedAttribute.setDerivedSchema(derivedSchema);
                derivedAttribute.setOwner(attributable);
                attributable.addDerivedAttribute(derivedAttribute);
            }
        }

        // 3. user and role virtual attributes will be evaluated by the propagation manager only (if needed).
        if (AttributableType.USER == attributableUtil.getType()
                || AttributableType.ROLE == attributableUtil.getType()) {

            for (AttributeTO vattrTO : attributableTO.getVirtualAttributes()) {
                AbstractVirSchema uVirSchema = getVirtualSchema(vattrTO.getSchema(), attributableUtil.virSchemaClass());

                if (uVirSchema != null) {
                    AbstractVirAttr vattr = attributableUtil.newVirAttr();
                    vattr.setVirtualSchema(uVirSchema);
                    vattr.setOwner(attributable);
                    attributable.addVirtualAttribute(vattr);
                }
            }
        }

        fillVirtual(attributable, attributableTO.getVirtualAttributes(), attributableUtil);

        // 4. resources
        ExternalResource resource;
        for (String resourceName : attributableTO.getResources()) {
            resource = getResource(resourceName);

            if (resource != null) {
                attributable.addResource(resource);
            }
        }

        SyncopeClientException requiredValuesMissing = checkMandatory(attributableUtil, attributable);
        if (!requiredValuesMissing.isEmpty()) {
            scce.addException(requiredValuesMissing);
        }

        // Throw composite exception if there is at least one element set
        // in the composing exceptions
View Full Code Here

    private <T extends AbstractDerSchema> void populate(final AbstractSchema schema, final SchemaTO schemaTO) {
        if (!JexlUtil.isExpressionValid(schemaTO.getMandatoryCondition())) {
            SyncopeClientCompositeErrorException scce =
                    new SyncopeClientCompositeErrorException(HttpStatus.BAD_REQUEST);

            SyncopeClientException invalidMandatoryCondition = new SyncopeClientException(
                    SyncopeClientExceptionType.InvalidValues);
            invalidMandatoryCondition.addElement(schemaTO.getMandatoryCondition());

            scce.addException(invalidMandatoryCondition);
            throw scce;
        }
View Full Code Here

        SyncopeClientCompositeErrorException scce = new SyncopeClientCompositeErrorException(HttpStatus.BAD_REQUEST);

        List<AbstractAttr> attrs = schemaDAO.getAttributes(schema, attributableUtil.attrClass());
        if (!attrs.isEmpty()) {
            if (schema.getType() != schemaTO.getType()) {
                SyncopeClientException e = new SyncopeClientException(SyncopeClientExceptionType.valueOf("Invalid"
                        + schema.getClass().getSimpleName()));
                e.addElement("Cannot change type since " + schema.getName() + " has attributes");

                scce.addException(e);
            }
            if (schema.isUniqueConstraint() != schemaTO.isUniqueConstraint()) {
                SyncopeClientException e = new SyncopeClientException(SyncopeClientExceptionType.valueOf("Invalid"
                        + schema.getClass().getSimpleName()));
                e.addElement("Cannot alter unique contraint since " + schema.getName() + " has attributes");

                scce.addException(e);
            }
        }
View Full Code Here

        try {
            createResource(resourceTO);
            fail("Create should not have worked");
        } catch (SyncopeClientCompositeErrorException e) {
            SyncopeClientException requiredValueMissing = e
                    .getException(SyncopeClientExceptionType.RequiredValuesMissing);
            assertNotNull(requiredValueMissing);
            assertNotNull(requiredValueMissing.getElements());
            assertEquals(1, requiredValueMissing.getElements().size());
            assertEquals("intAttrName", requiredValueMissing.getElements().iterator().next());
        }
    }
View Full Code Here

        try {
            resourceService.create(
                    buildResourceTO("http://schemas.examples.org/security/authorization/organizationUnit"));
            fail();
        } catch (SyncopeClientCompositeErrorException scce) {
            SyncopeClientException sce = scce.getException(SyncopeClientExceptionType.InvalidExternalResource);

            assertNotNull(sce.getElements());
            assertEquals(1, sce.getElements().size());
            assertTrue(sce.getElements().iterator().next().contains(EntityViolationType.InvalidName.name()));
        }
    }
View Full Code Here

    }

    private SyncopeClientException checkMandatory(final AttributableUtil attrUtil,
            final AbstractAttributable attributable) {

        SyncopeClientException reqValMissing = new SyncopeClientException(
                SyncopeClientExceptionType.RequiredValuesMissing);

        LOG.debug("Check mandatory constraint among resources {}", attributable.getResources());

        // Check if there is some mandatory schema defined for which no value has been provided
        for (AbstractSchema schema : schemaDAO.findAll(attrUtil.schemaClass())) {
            if (attributable.getAttribute(schema.getName()) == null
                    && !schema.isReadonly()
                    && (JexlUtil.evaluateMandatoryCondition(schema.getMandatoryCondition(), attributable)
                    || evaluateMandatoryCondition(attrUtil, attributable, schema.getName(),
                    attrUtil.intMappingType()))) {

                LOG.error("Mandatory schema " + schema.getName() + " not provided with values");

                reqValMissing.addElement(schema.getName());
            }
        }
        for (AbstractDerSchema derSchema : derSchemaDAO.findAll(attrUtil.derSchemaClass())) {
            if (attributable.getDerivedAttribute(derSchema.getName()) == null
                    && evaluateMandatoryCondition(attrUtil, attributable, derSchema.getName(),
                    attrUtil.derIntMappingType())) {

                LOG.error("Mandatory derived schema " + derSchema.getName() + " does not evaluate to any value");

                reqValMissing.addElement(derSchema.getName());
            }
        }
        for (AbstractVirSchema virSchema : virSchemaDAO.findAll(attrUtil.virSchemaClass())) {
            if (attributable.getVirtualAttribute(virSchema.getName()) == null
                    && !virSchema.isReadonly()
                    && evaluateMandatoryCondition(attrUtil, attributable, virSchema.getName(),
                    attrUtil.virIntMappingType())) {

                LOG.error("Mandatory virtual schema " + virSchema.getName() + " not provided with values");

                reqValMissing.addElement(virSchema.getName());
            }
        }

        return reqValMissing;
    }
View Full Code Here

            connInstance = connInstanceDAO.save(connInstance);
        } catch (Exception e) {
            SyncopeClientCompositeErrorException scce =
                    new SyncopeClientCompositeErrorException(HttpStatus.BAD_REQUEST);

            SyncopeClientException invalidConnInstance = new SyncopeClientException(
                    SyncopeClientExceptionType.InvalidConnInstance);
            invalidConnInstance.addElement(e.getMessage());

            scce.addException(invalidConnInstance);
            throw scce;
        }
View Full Code Here

TOP

Related Classes of org.apache.syncope.common.validation.SyncopeClientException

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.