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 */
016package org.forgerock.api.jackson;
017
018import static org.forgerock.api.jackson.JacksonUtils.OBJECT_MAPPER;
019
020import com.fasterxml.jackson.module.jsonSchema.jakarta.types.AnySchema;
021import jakarta.validation.ValidationException;
022import java.io.IOException;
023import org.forgerock.api.enums.ReadPolicy;
024import org.forgerock.api.enums.WritePolicy;
025import org.forgerock.json.JsonValue;
026
027/**
028 * An extension to the Jackson {@code AnySchema} that includes the custom CREST JSON Schema attributes.
029 */
030public class CrestAnySchema extends AnySchema implements CrestReadWritePoliciesSchema, OrderedFieldSchema,
031        ValidatableSchema, WithExampleSchema<Object> {
032    private WritePolicy writePolicy;
033    private ReadPolicy readPolicy;
034    private Boolean errorOnWritePolicyFailure;
035    private Boolean returnOnDemand;
036    private Integer propertyOrder;
037    private Object example;
038
039    @Override
040    public WritePolicy getWritePolicy() {
041        return writePolicy;
042    }
043
044    @Override
045    public void setWritePolicy(WritePolicy policy) {
046        this.writePolicy = policy;
047    }
048
049    @Override
050    public ReadPolicy getReadPolicy() {
051        return readPolicy;
052    }
053
054    @Override
055    public void setReadPolicy(ReadPolicy readPolicy) {
056        this.readPolicy = readPolicy;
057    }
058
059    @Override
060    public Boolean getErrorOnWritePolicyFailure() {
061        return errorOnWritePolicyFailure;
062    }
063
064    @Override
065    public void setErrorOnWritePolicyFailure(Boolean errorOnWritePolicyFailure) {
066        this.errorOnWritePolicyFailure = errorOnWritePolicyFailure;
067    }
068
069    @Override
070    public Boolean getReturnOnDemand() {
071        return returnOnDemand;
072    }
073
074    @Override
075    public void setReturnOnDemand(Boolean returnOnDemand) {
076        this.returnOnDemand = returnOnDemand;
077    }
078
079    @Override
080    public Integer getPropertyOrder() {
081        return propertyOrder;
082    }
083
084    @Override
085    public void setPropertyOrder(Integer order) {
086        this.propertyOrder = order;
087    }
088
089    @Override
090    public void validate(JsonValue object) throws ValidationException {
091        // any Object is a valid AnySchema.
092    }
093
094    @Override
095    public Object getExample() {
096        return this.example;
097    }
098
099    @Override
100    public void setExample(String example) throws IOException {
101        this.example = OBJECT_MAPPER.readValue(example, Object.class);
102    }
103}