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 com.fasterxml.jackson.annotation.JsonProperty;
19  import com.fasterxml.jackson.module.jsonSchema.jakarta.types.BooleanSchema;
20  import jakarta.validation.ValidationException;
21  import org.forgerock.api.enums.ReadPolicy;
22  import org.forgerock.api.enums.WritePolicy;
23  import org.forgerock.json.JsonValue;
24  
25  /**
26   * An extension to the Jackson {@code BooleanSchema} that includes the custom CREST JSON Schema attributes.
27   */
28  public class CrestBooleanSchema extends BooleanSchema implements CrestReadWritePoliciesSchema, OrderedFieldSchema,
29          ValidatableSchema, WithExampleSchema<Boolean> {
30      private WritePolicy writePolicy;
31      private ReadPolicy readPolicy;
32      private Boolean errorOnWritePolicyFailure;
33      private Boolean returnOnDemand;
34      private Integer propertyOrder;
35      private Boolean example;
36  
37      @Override
38      public WritePolicy getWritePolicy() {
39          return writePolicy;
40      }
41  
42      @Override
43      public void setWritePolicy(WritePolicy policy) {
44          this.writePolicy = policy;
45      }
46  
47      @Override
48      public ReadPolicy getReadPolicy() {
49          return readPolicy;
50      }
51  
52      @Override
53      public void setReadPolicy(ReadPolicy readPolicy) {
54          this.readPolicy = readPolicy;
55      }
56  
57      @Override
58      public Boolean getErrorOnWritePolicyFailure() {
59          return errorOnWritePolicyFailure;
60      }
61  
62      @Override
63      public void setErrorOnWritePolicyFailure(Boolean errorOnWritePolicyFailure) {
64          this.errorOnWritePolicyFailure = errorOnWritePolicyFailure;
65      }
66  
67      @Override
68      public Boolean getReturnOnDemand() {
69          return returnOnDemand;
70      }
71  
72      @Override
73      public void setReturnOnDemand(Boolean returnOnDemand) {
74          this.returnOnDemand = returnOnDemand;
75      }
76  
77      @Override
78      public Integer getPropertyOrder() {
79          return propertyOrder;
80      }
81  
82      @Override
83      public void setPropertyOrder(Integer order) {
84          this.propertyOrder = order;
85      }
86  
87      @Override
88      public void validate(JsonValue object) throws ValidationException {
89          if (!object.isBoolean()) {
90              throw new ValidationException("Expected boolean, but got " + object.getObject());
91          }
92      }
93  
94      /**
95       * Gets read-only property. This method overrides the superclass' definition of "readOnly" being all lower-case,
96       * via the {@code JsonProperty} annotation.
97       *
98       * @return {@code true} if property is read-only, otherwise {@code false} or {@code null}
99       */
100     @JsonProperty("readOnly")
101     @Override
102     public Boolean getReadonly() {
103         return super.getReadonly();
104     }
105 
106     @Override
107     public Boolean getExample() {
108         return example;
109     }
110 
111     @Override
112     public void setExample(String example) {
113         this.example = Boolean.valueOf(example);
114     }
115 }