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 static org.forgerock.api.jackson.JacksonUtils.OBJECT_MAPPER;
20  
21  import java.io.IOException;
22  
23  import javax.validation.ValidationException;
24  
25  import org.forgerock.api.enums.ReadPolicy;
26  import org.forgerock.json.JsonValue;
27  
28  import com.fasterxml.jackson.module.jsonSchema.types.AnySchema;
29  import org.forgerock.api.enums.WritePolicy;
30  
31  /**
32   * An extension to the Jackson {@code AnySchema} that includes the custom CREST JSON Schema attributes.
33   */
34  public class CrestAnySchema extends AnySchema implements CrestReadWritePoliciesSchema, OrderedFieldSchema,
35          ValidatableSchema, WithExampleSchema<Object> {
36      private WritePolicy writePolicy;
37      private ReadPolicy readPolicy;
38      private Boolean errorOnWritePolicyFailure;
39      private Boolean returnOnDemand;
40      private Integer propertyOrder;
41      private Object example;
42  
43      @Override
44      public WritePolicy getWritePolicy() {
45          return writePolicy;
46      }
47  
48      @Override
49      public void setWritePolicy(WritePolicy policy) {
50          this.writePolicy = policy;
51      }
52  
53      @Override
54      public ReadPolicy getReadPolicy() {
55          return readPolicy;
56      }
57  
58      @Override
59      public void setReadPolicy(ReadPolicy readPolicy) {
60          this.readPolicy = readPolicy;
61      }
62  
63      @Override
64      public Boolean getErrorOnWritePolicyFailure() {
65          return errorOnWritePolicyFailure;
66      }
67  
68      @Override
69      public void setErrorOnWritePolicyFailure(Boolean errorOnWritePolicyFailure) {
70          this.errorOnWritePolicyFailure = errorOnWritePolicyFailure;
71      }
72  
73      @Override
74      public Boolean getReturnOnDemand() {
75          return returnOnDemand;
76      }
77  
78      @Override
79      public void setReturnOnDemand(Boolean returnOnDemand) {
80          this.returnOnDemand = returnOnDemand;
81      }
82  
83      @Override
84      public Integer getPropertyOrder() {
85          return propertyOrder;
86      }
87  
88      @Override
89      public void setPropertyOrder(Integer order) {
90          this.propertyOrder = order;
91      }
92  
93      @Override
94      public void validate(JsonValue object) throws ValidationException {
95          // any Object is a valid AnySchema.
96      }
97  
98      @Override
99      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 }