@Override public PlainSchemaTO getPlainSchemaTO(final String key) { PlainSchema schema = plainSchemaDAO.find(key); if (schema == null) { throw new NotFoundException("Schema '" + key + "'"); } PlainSchemaTO schemaTO = new PlainSchemaTO(); schemaTO.setKey(schema.getKey()); schemaTO.setType(schema.getType()); schemaTO.setCipherAlgorithm(schema.getCipherAlgorithm()); schemaTO.setConversionPattern(schema.getConversionPattern()); schemaTO.setEnumerationKeys(schema.getEnumerationKeys()); schemaTO.setEnumerationValues(schema.getEnumerationValues()); schemaTO.setMandatoryCondition(schema.getMandatoryCondition()); schemaTO.setMimeType(schema.getMimeType()); schemaTO.setMultivalue(schema.isMultivalue()); schemaTO.setReadonly(schema.isReadonly()); schemaTO.setSecretKey(schema.getSecretKey()); schemaTO.setUniqueConstraint(schema.isUniqueConstraint()); labels(schema, schemaTO); schemaTO.setAnyTypeClass(schema.getAnyTypeClass() == null ? null : schema.getAnyTypeClass().getKey()); if (schema.getValidator() != null) { schemaTO.setValidator(schema.getValidator().getKey()); } return schemaTO; }
private void checkMandatory( final PlainSchema schema, final PlainAttr<?> attr, final Any<?> any, final SyncopeClientException reqValMissing) { if (attr == null && !schema.isReadonly() && JexlUtils.evaluateMandatoryCondition(schema.getMandatoryCondition(), any)) { LOG.error("Mandatory schema " + schema.getKey() + " not provided with values"); reqValMissing.getElements().add(schema.getKey()); } }
@Override protected void doValidate(final PlainAttrValue attrValue) { if (AttrSchemaType.Enum == schema.getType()) { final String[] enumeration = schema.getEnumerationValues().split(SyncopeConstants.ENUM_VALUES_SEPARATOR); final String value = attrValue.getStringValue(); boolean found = false; for (int i = 0; i < enumeration.length && !found; i++) { if (enumeration[i].trim().equals(value)) { found = true; } } if (!found) { throw new InvalidPlainAttrValueException( "'" + value + "' is not one of: " + schema.getEnumerationValues()); } } } }
private void fillAttr( final List<String> values, final AnyUtils anyUtils, final PlainSchema schema, final PlainAttr<?> attr, final SyncopeClientException invalidValues) { // if schema is multivalue, all values are considered for addition; // otherwise only the fist one - if provided - is considered List<String> valuesProvided = schema.isMultivalue() ? values : (values.isEmpty() ? Collections.<String>emptyList() : Collections.singletonList(values.get(0))); valuesProvided.forEach(value -> { if (StringUtils.isBlank(value)) { LOG.debug("Null value for {}, ignoring", schema.getKey()); } else { try { attr.add(value, anyUtils); } catch (InvalidPlainAttrValueException e) { String valueToPrint = value.length() > 40 ? value.substring(0, 20) + "..." : value; LOG.warn("Invalid value for attribute " + schema.getKey() + ": " + valueToPrint, e); invalidValues.getElements().add(schema.getKey() + ": " + valueToPrint + " - " + e.getMessage()); } } }); }
schema.setKey(schemaTO.getKey()); schema.setType(schemaTO.getType()); schema.setCipherAlgorithm(schemaTO.getCipherAlgorithm()); schema.setConversionPattern(schemaTO.getConversionPattern()); schema.setEnumerationKeys(schemaTO.getEnumerationKeys()); schema.setEnumerationValues(schemaTO.getEnumerationValues()); schema.setMandatoryCondition(schemaTO.getMandatoryCondition()); schema.setMimeType(schemaTO.getMimeType()); schema.setMultivalue(schemaTO.isMultivalue()); schema.setReadonly(schemaTO.isReadonly()); schema.setSecretKey(schemaTO.getSecretKey()); schema.setUniqueConstraint(schemaTO.isUniqueConstraint()); schema.setValidator(null); } else { Implementation validator = implementationDAO.find(schemaTO.getValidator()); schemaTO.getValidator()); } else { schema.setValidator(validator); && (merged.getAnyTypeClass() == null || !schemaTO.getAnyTypeClass().equals(merged.getAnyTypeClass().getKey()))) { } else { anyTypeClass.add(merged); merged.setAnyTypeClass(anyTypeClass); } else if (schemaTO.getAnyTypeClass() == null && merged.getAnyTypeClass() != null) {
@Override public Collection<? extends P> getPlainAttrs(final String plainSchema) { return internalGetPlainAttrs().stream().filter(plainAttr -> plainAttr != null && plainAttr.getSchema() != null && plainSchema.equals(plainAttr.getSchema().getKey())). collect(Collectors.toList()); }
@Override public boolean isValid(final PlainSchema schema, final ConstraintValidatorContext context) { boolean isValid = schema.getType() != AttrSchemaType.Enum || StringUtils.isNotBlank(schema.getEnumerationValues()); if (!isValid) { context.disableDefaultConstraintViolation(); addPropertyNode("enumerationValues").addConstraintViolation(); } else { isValid = schema.getType() != AttrSchemaType.Encrypted || (schema.getSecretKey() != null && schema.getCipherAlgorithm() != null); if (!isValid) { context.disableDefaultConstraintViolation(); addPropertyNode("secretKey").addPropertyNode("cipherAlgorithm").addConstraintViolation(); } else { isValid = !schema.isMultivalue() || !schema.isUniqueConstraint(); if (!isValid) { context.disableDefaultConstraintViolation();
switch (schema.getType()) { this.setLongValue(schema.getConversionPattern() == null ? Long.valueOf(value) : FormatUtils.parseNumber(value, schema.getConversionPattern()).longValue()); } catch (Exception pe) { exception = pe; this.setDoubleValue(schema.getConversionPattern() == null ? Double.valueOf(value) : FormatUtils.parseNumber(value, schema.getConversionPattern()).doubleValue()); } catch (Exception pe) { exception = pe; this.setDateValue(schema.getConversionPattern() == null ? FormatUtils.parseDate(value) : new Date(FormatUtils.parseDate(value, schema.getConversionPattern()).getTime())); } catch (Exception pe) { exception = pe; this.setStringValue(Encryptor.getInstance(schema.getSecretKey()). encode(value, schema.getCipherAlgorithm())); } catch (Exception pe) { exception = pe; throw new ParsingValidationException("While trying to parse '" + value + "' as " + schema.getKey(), exception);
List<String> valuesProvided = schema.isMultivalue() ? values : (values.isEmpty() JexlUtils.addPlainAttrsToContext(confDAO.get().getPlainAttrs(), jexlContext); if (!schema.isReadonly() && Boolean.parseBoolean(JexlUtils.evaluate(schema.getMandatoryCondition(), jexlContext))) { LOG.error("Mandatory schema " + schema.getKey() + " not provided with values"); reqValMissing.getElements().add(schema.getKey()); throw reqValMissing; LOG.debug("Null value for {}, ignoring", schema.getKey()); } else { try { PlainAttrValue attrValue; if (schema.isUniqueConstraint()) { attrValue = entityFactory.newEntity(CPlainAttrUniqueValue.class); ((PlainAttrUniqueValue) attrValue).setSchema(schema); LOG.warn("Invalid value for attribute " + schema.getKey() + ": " + value, e); invalidValues.getElements().add(schema.getKey() + ": " + value + " - " + e.getMessage());
if (not && schema.isMultivalue() && !(cond instanceof AnyCond) && cond.getType() != AttributeCond.Type.ISNULL && cond.getType() != AttributeCond.Type.ISNOTNULL) { if (schema.isUniqueConstraint()) { query.append(svs.asSearchViewSupport().uniqueAttr().name); } else { query.append(svs.asSearchViewSupport().attr().name); query.append(" WHERE schema_id='").append(schema.getKey()); fillAttrQuery(query, attrValue, schema, cond, false, parameters, svs); query.append(")"); String column = (cond instanceof AnyCond) ? cond.getSchema() : svs.fieldName(schema.getType()); if ((schema.getType() == AttrSchemaType.String || schema.getType() == AttrSchemaType.Enum) && ignoreCase) { column = "LOWER (" + column + ")"; if (schema.getType() == AttrSchemaType.String || schema.getType() == AttrSchemaType.Enum) { query.append(column); if (not) { query.append('='); if ((schema.getType() == AttrSchemaType.String || schema.getType() == AttrSchemaType.Enum) && ignoreCase) { query.append("LOWER(?").append(setParameter(parameters, attrValue.getValue())).append(')'); } else {
switch (cond.getType()) { case ISNOTNULL: query.append(checked.getLeft().isUniqueConstraint() ? svs.asSearchViewSupport().uniqueAttr().name : svs.asSearchViewSupport().attr().name). append(" WHERE schema_id=").append("'").append(checked.getLeft().getKey()).append("'"); break; append("("). append("SELECT DISTINCT any_id FROM "). append(checked.getLeft().isUniqueConstraint() ? svs.asSearchViewSupport().uniqueAttr().name : svs.asSearchViewSupport().attr().name). append(" WHERE schema_id=").append("'").append(checked.getLeft().getKey()).append("'"). append(")"); break; if (not && !(cond instanceof AnyCond) && checked.getLeft().isMultivalue()) { query.append(svs.field().name).append(" WHERE "); } else { if (checked.getLeft().isUniqueConstraint()) { query.append(svs.asSearchViewSupport().uniqueAttr().name); } else { query.append(svs.asSearchViewSupport().attr().name); query.append(" WHERE schema_id='").append(checked.getLeft().getKey());
@Override public Conf save(final CPlainAttr attr) { Conf instance = get(); Optional<? extends CPlainAttr> old = instance.getPlainAttr(attr.getSchema().getKey()); if (old.isPresent() && (!attr.getSchema().isUniqueConstraint() || (!attr.getUniqueValue().getStringValue().equals(old.get().getUniqueValue().getStringValue())))) { old.get().setOwner(null); instance.remove(old.get()); } instance.add(attr); attr.setOwner(instance); return entityManager().merge(instance); }
@Override public PlainSchema update(final PlainSchemaTO schemaTO, final PlainSchema schema) { SyncopeClientCompositeException scce = SyncopeClientException.buildComposite(); boolean hasAttrs = false; for (AnyTypeKind anyTypeKind : AnyTypeKind.values()) { AnyUtils anyUtils = anyUtilsFactory.getInstance(anyTypeKind); hasAttrs |= plainSchemaDAO.findAttrs(schema, anyUtils.plainAttrClass()).isEmpty(); } if (hasAttrs) { if (schema.getType() != schemaTO.getType()) { SyncopeClientException e = SyncopeClientException.build(ClientExceptionType.InvalidPlainSchema); e.getElements().add("Cannot change type since " + schema.getKey() + " has attributes"); scce.addException(e); } if (schema.isUniqueConstraint() != schemaTO.isUniqueConstraint()) { SyncopeClientException e = SyncopeClientException.build(ClientExceptionType.InvalidPlainSchema); e.getElements().add("Cannot alter unique contraint since " + schema.getKey() + " has attributes"); scce.addException(e); } } if (scce.hasExceptions()) { throw scce; } return fill(schema, schemaTO); }
protected void parseOrderByForPlainSchema( final SearchSupport svs, final OrderBySupport obs, final OrderBySupport.Item item, final OrderByClause clause, final PlainSchema schema, final String fieldName) { // keep track of involvement of non-mandatory schemas in the order by clauses obs.nonMandatorySchemas = !"true".equals(schema.getMandatoryCondition()); if (schema.isUniqueConstraint()) { obs.views.add(svs.asSearchViewSupport().uniqueAttr()); item.select = new StringBuilder(). append(svs.asSearchViewSupport().uniqueAttr().alias).append('.'). append(svs.fieldName(schema.getType())). append(" AS ").append(fieldName).toString(); item.where = new StringBuilder(). append(svs.asSearchViewSupport().uniqueAttr().alias). append(".schema_id='").append(fieldName).append("'").toString(); item.orderBy = fieldName + " " + clause.getDirection().name(); } else { obs.views.add(svs.asSearchViewSupport().attr()); item.select = new StringBuilder(). append(svs.asSearchViewSupport().attr().alias).append('.').append(svs.fieldName(schema.getType())). append(" AS ").append(fieldName).toString(); item.where = new StringBuilder(). append(svs.asSearchViewSupport().attr().alias). append(".schema_id='").append(fieldName).append("'").toString(); item.orderBy = fieldName + " " + clause.getDirection().name(); } }
schema.setAnyTypeClass(null); }); anyTypeClassTO.getPlainSchemas().forEach(schemaName -> { PlainSchema schema = plainSchemaDAO.find(schemaName); if (schema == null || schema.getAnyTypeClass() != null) { LOG.debug("Invalid or already in use" + PlainSchema.class.getSimpleName() + "{}, ignoring...", schemaName);
@Override public String getValueAsString(final PlainSchema schema) { return getValueAsString(schema.getType(), schema); }
@Override public void delete(final String key) { PlainSchema schema = find(key); if (schema == null) { return; } schema.getLabels().forEach(label -> label.setSchema(null)); deleteAttrs(schema); resourceDAO.deleteMapping(key); if (schema.getAnyTypeClass() != null) { schema.getAnyTypeClass().getPlainSchemas().remove(schema); } entityManager().remove(schema); } }
@Override protected void doValidate(final PlainAttrValue attrValue) { // check Binary schemas MIME Type mismatches if (attrValue.getBinaryValue() != null) { byte[] binaryValue = attrValue.getBinaryValue(); String mimeType = TIKA.detect(binaryValue); boolean valid = true; if (!mimeType.equals(attrValue.getAttr().getSchema().getMimeType())) { if (MediaType.TEXT_PLAIN.equals(mimeType) && MediaType.APPLICATION_JSON.equals(attrValue.getAttr().getSchema().getMimeType())) { String decoded = new String(binaryValue).trim(); valid = (decoded.startsWith("{") && decoded.endsWith("}")) || (decoded.startsWith("[") && decoded.endsWith("]")) && isValidJSON(decoded); } else { valid = false; } } if (!valid) { throw new InvalidPlainAttrValueException( "Found MIME type: '" + mimeType + "', expecting: '" + attrValue.getAttr().getSchema().getMimeType() + "'"); } } }
result = schema == null || schema.getConversionPattern() == null ? getLongValue().toString() : FormatUtils.format(getLongValue(), schema.getConversionPattern()); break; result = schema == null || schema.getConversionPattern() == null ? getDoubleValue().toString() : FormatUtils.format(getDoubleValue(), schema.getConversionPattern()); break; result = schema == null || schema.getConversionPattern() == null ? FormatUtils.format(getDateValue()) : FormatUtils.format(getDateValue(), false, schema.getConversionPattern()); break;
@Override public Optional<? extends GPlainAttr> getPlainAttr(final String plainSchema) { return getPlainAttrs().stream().filter(plainAttr -> plainAttr != null && plainAttr.getSchema() != null && plainSchema.equals(plainAttr.getSchema().getKey())).findFirst(); }