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