| | |
| | | import static com.fasterxml.jackson.core.JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS; |
| | | import static java.util.Collections.emptyList; |
| | | import static org.forgerock.opendj.ldap.schema.Schema.getCoreSchema; |
| | | import static org.forgerock.opendj.rest2ldap.Rest2ldapMessages.ERR_JSON_IO_ERROR; |
| | | import static org.forgerock.opendj.rest2ldap.Rest2ldapMessages.ERR_JSON_PARSE_ERROR; |
| | | import static org.forgerock.opendj.rest2ldap.schema.JsonSchema.ValidationPolicy.LENIENT; |
| | | import static org.forgerock.opendj.rest2ldap.schema.JsonSchema.ValidationPolicy.STRICT; |
| | | import static org.forgerock.util.Options.defaultOptions; |
| | | |
| | | import java.io.IOException; |
| | | import java.io.InputStream; |
| | | import java.util.Collection; |
| | | |
| | | import org.forgerock.i18n.LocalizableMessage; |
| | | import org.forgerock.i18n.LocalizedIllegalArgumentException; |
| | | import org.forgerock.opendj.ldap.ByteString; |
| | | import org.forgerock.opendj.ldap.schema.MatchingRule; |
| | | import org.forgerock.opendj.ldap.schema.MatchingRuleImpl; |
| | | import org.forgerock.opendj.ldap.schema.Schema; |
| | | import org.forgerock.opendj.ldap.schema.SchemaBuilder; |
| | | import org.forgerock.opendj.ldap.schema.Syntax; |
| | | import org.forgerock.util.Function; |
| | | import org.forgerock.util.Option; |
| | | import org.forgerock.util.Options; |
| | | |
| | | import com.fasterxml.jackson.core.JsonFactory; |
| | | import com.fasterxml.jackson.core.JsonProcessingException; |
| | | import com.fasterxml.jackson.databind.ObjectMapper; |
| | | |
| | | /** |
| | |
| | | .enable(ALLOW_UNQUOTED_CONTROL_CHARS)), |
| | | /** JSON validation policy which does not perform any validation. */ |
| | | DISABLED(null); |
| | | |
| | | private final ObjectMapper objectMapper; |
| | | |
| | | ValidationPolicy(final ObjectMapper objectMapper) { |
| | |
| | | private static final Syntax JSON_QUERY_SYNTAX; |
| | | private static final MatchingRule CASE_IGNORE_JSON_QUERY_MATCHING_RULE; |
| | | private static final MatchingRule CASE_EXACT_JSON_QUERY_MATCHING_RULE; |
| | | private static final Function<ByteString, Object, LocalizedIllegalArgumentException> BYTESTRING_TO_JSON = |
| | | new Function<ByteString, Object, LocalizedIllegalArgumentException>() { |
| | | @Override |
| | | public Object apply(final ByteString value) { |
| | | try (final InputStream inputStream = value.asReader().asInputStream()) { |
| | | return LENIENT.getObjectMapper().readValue(inputStream, Object.class); |
| | | } catch (final IOException e) { |
| | | throw new LocalizedIllegalArgumentException(jsonParsingException(e)); |
| | | } |
| | | } |
| | | }; |
| | | |
| | | static LocalizableMessage jsonParsingException(final IOException e) { |
| | | if (e instanceof JsonProcessingException) { |
| | | final JsonProcessingException jpe = (JsonProcessingException) e; |
| | | if (jpe.getLocation() != null) { |
| | | return ERR_JSON_PARSE_ERROR.get(jpe.getLocation().getLineNr(), |
| | | jpe.getLocation().getColumnNr(), |
| | | jpe.getOriginalMessage()); |
| | | } |
| | | } |
| | | return ERR_JSON_IO_ERROR.get(e.getMessage()); |
| | | } |
| | | |
| | | private static final Function<Object, ByteString, JsonProcessingException> JSON_TO_BYTESTRING = |
| | | new Function<Object, ByteString, JsonProcessingException>() { |
| | | @Override |
| | | public ByteString apply(final Object value) throws JsonProcessingException { |
| | | return ByteString.wrap(LENIENT.getObjectMapper().writeValueAsBytes(value)); |
| | | } |
| | | }; |
| | | |
| | | static { |
| | | final Schema schema = addJsonSyntaxesAndMatchingRulesToSchema(new SchemaBuilder(getCoreSchema())).toSchema(); |
| | |
| | | return builder; |
| | | } |
| | | |
| | | /** |
| | | * Returns a function which parses {@code JSON} values. Invalid values will result in a |
| | | * {@code LocalizedIllegalArgumentException}. |
| | | * |
| | | * @return A function which parses {@code JSON} values. |
| | | */ |
| | | public static Function<ByteString, Object, LocalizedIllegalArgumentException> byteStringToJson() { |
| | | return BYTESTRING_TO_JSON; |
| | | } |
| | | |
| | | /** |
| | | * Returns a function which converts a JSON {@code Object} to a {@code ByteString}. |
| | | * |
| | | * @return A function which converts a JSON {@code Object} to a {@code ByteString}. |
| | | */ |
| | | public static Function<Object, ByteString, JsonProcessingException> jsonToByteString() { |
| | | return JSON_TO_BYTESTRING; |
| | | } |
| | | |
| | | private JsonSchema() { |
| | | // Prevent instantiation. |
| | | } |