View Javadoc
1   /*
2    * The contents of this file are subject to the terms of the Common Development and
3    * Distribution License (the License). You may not use this file except in compliance with the
4    * License.
5    *
6    * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
7    * specific language governing permission and limitations under the License.
8    *
9    * When distributing Covered Software, include this CDDL Header Notice in each file and include
10   * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
11   * Header, with the fields enclosed by brackets [] replaced by your own identifying
12   * information: "Portions copyright [year] [name of copyright owner]".
13   *
14   * Copyright 2016 ForgeRock AS.
15   */
16  package org.forgerock.api.jackson;
17  
18  import static org.forgerock.api.jackson.JacksonUtils.OBJECT_MAPPER;
19  
20  import com.fasterxml.jackson.module.jsonSchema.jakarta.types.AnySchema;
21  import jakarta.validation.ValidationException;
22  import java.io.IOException;
23  import org.forgerock.api.enums.ReadPolicy;
24  import org.forgerock.api.enums.WritePolicy;
25  import org.forgerock.json.JsonValue;
26  
27  /**
28   * An extension to the Jackson {@code AnySchema} that includes the custom CREST JSON Schema attributes.
29   */
30  public class CrestAnySchema extends AnySchema implements CrestReadWritePoliciesSchema, OrderedFieldSchema,
31          ValidatableSchema, WithExampleSchema<Object> {
32      private WritePolicy writePolicy;
33      private ReadPolicy readPolicy;
34      private Boolean errorOnWritePolicyFailure;
35      private Boolean returnOnDemand;
36      private Integer propertyOrder;
37      private Object example;
38  
39      @Override
40      public WritePolicy getWritePolicy() {
41          return writePolicy;
42      }
43  
44      @Override
45      public void setWritePolicy(WritePolicy policy) {
46          this.writePolicy = policy;
47      }
48  
49      @Override
50      public ReadPolicy getReadPolicy() {
51          return readPolicy;
52      }
53  
54      @Override
55      public void setReadPolicy(ReadPolicy readPolicy) {
56          this.readPolicy = readPolicy;
57      }
58  
59      @Override
60      public Boolean getErrorOnWritePolicyFailure() {
61          return errorOnWritePolicyFailure;
62      }
63  
64      @Override
65      public void setErrorOnWritePolicyFailure(Boolean errorOnWritePolicyFailure) {
66          this.errorOnWritePolicyFailure = errorOnWritePolicyFailure;
67      }
68  
69      @Override
70      public Boolean getReturnOnDemand() {
71          return returnOnDemand;
72      }
73  
74      @Override
75      public void setReturnOnDemand(Boolean returnOnDemand) {
76          this.returnOnDemand = returnOnDemand;
77      }
78  
79      @Override
80      public Integer getPropertyOrder() {
81          return propertyOrder;
82      }
83  
84      @Override
85      public void setPropertyOrder(Integer order) {
86          this.propertyOrder = order;
87      }
88  
89      @Override
90      public void validate(JsonValue object) throws ValidationException {
91          // any Object is a valid AnySchema.
92      }
93  
94      @Override
95      public Object getExample() {
96          return this.example;
97      }
98  
99      @Override
100     public void setExample(String example) throws IOException {
101         this.example = OBJECT_MAPPER.readValue(example, Object.class);
102     }
103 }