@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() + "'"); } } }
@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; }