001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2016 ForgeRock AS.
015 */
016
017package org.forgerock.api.jackson;
018
019import static org.forgerock.api.jackson.JacksonUtils.OBJECT_MAPPER;
020
021import java.io.IOException;
022
023import javax.validation.ValidationException;
024
025import org.forgerock.api.enums.ReadPolicy;
026import org.forgerock.json.JsonValue;
027
028import com.fasterxml.jackson.module.jsonSchema.types.AnySchema;
029import org.forgerock.api.enums.WritePolicy;
030
031/**
032 * An extension to the Jackson {@code AnySchema} that includes the custom CREST JSON Schema attributes.
033 */
034public class CrestAnySchema extends AnySchema implements CrestReadWritePoliciesSchema, OrderedFieldSchema,
035        ValidatableSchema, WithExampleSchema<Object> {
036    private WritePolicy writePolicy;
037    private ReadPolicy readPolicy;
038    private Boolean errorOnWritePolicyFailure;
039    private Boolean returnOnDemand;
040    private Integer propertyOrder;
041    private Object example;
042
043    @Override
044    public WritePolicy getWritePolicy() {
045        return writePolicy;
046    }
047
048    @Override
049    public void setWritePolicy(WritePolicy policy) {
050        this.writePolicy = policy;
051    }
052
053    @Override
054    public ReadPolicy getReadPolicy() {
055        return readPolicy;
056    }
057
058    @Override
059    public void setReadPolicy(ReadPolicy readPolicy) {
060        this.readPolicy = readPolicy;
061    }
062
063    @Override
064    public Boolean getErrorOnWritePolicyFailure() {
065        return errorOnWritePolicyFailure;
066    }
067
068    @Override
069    public void setErrorOnWritePolicyFailure(Boolean errorOnWritePolicyFailure) {
070        this.errorOnWritePolicyFailure = errorOnWritePolicyFailure;
071    }
072
073    @Override
074    public Boolean getReturnOnDemand() {
075        return returnOnDemand;
076    }
077
078    @Override
079    public void setReturnOnDemand(Boolean returnOnDemand) {
080        this.returnOnDemand = returnOnDemand;
081    }
082
083    @Override
084    public Integer getPropertyOrder() {
085        return propertyOrder;
086    }
087
088    @Override
089    public void setPropertyOrder(Integer order) {
090        this.propertyOrder = order;
091    }
092
093    @Override
094    public void validate(JsonValue object) throws ValidationException {
095        // any Object is a valid AnySchema.
096    }
097
098    @Override
099    public Object getExample() {
100        return this.example;
101    }
102
103    @Override
104    public void setExample(String example) throws IOException {
105        this.example = OBJECT_MAPPER.readValue(example, Object.class);
106    }
107}