001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.juneau.json;
018
019import java.io.*;
020import java.lang.reflect.*;
021import java.nio.charset.*;
022import java.util.*;
023import java.util.function.*;
024
025import org.apache.juneau.*;
026import org.apache.juneau.httppart.*;
027import org.apache.juneau.jsonschema.*;
028import org.apache.juneau.serializer.*;
029import org.apache.juneau.svl.*;
030
031/**
032 * Session object that lives for the duration of a single use of {@link JsonSchemaSerializer}.
033 *
034 * <h5 class='section'>Notes:</h5><ul>
035 *    <li class='warn'>This class is not thread safe and is typically discarded after one use.
036 * </ul>
037 *
038 * <h5 class='section'>See Also:</h5><ul>
039 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JsonBasics">JSON Basics</a>
040 * </ul>
041 */
042public class JsonSchemaSerializerSession extends JsonSerializerSession {
043   /**
044    * Builder class.
045    */
046   public static class Builder extends JsonSerializerSession.Builder {
047
048      private JsonSchemaSerializer ctx;
049
050      /**
051       * Constructor
052       *
053       * @param ctx The context creating this session.
054       */
055      protected Builder(JsonSchemaSerializer ctx) {
056         super(ctx);
057         this.ctx = ctx;
058      }
059
060      @Override /* Overridden from Builder */
061      public <T> Builder apply(Class<T> type, Consumer<T> apply) {
062         super.apply(type, apply);
063         return this;
064      }
065
066      @Override
067      public JsonSchemaSerializerSession build() {
068         return new JsonSchemaSerializerSession(this);
069      }
070
071      @Override /* Overridden from Builder */
072      public Builder debug(Boolean value) {
073         super.debug(value);
074         return this;
075      }
076
077      @Override /* Overridden from Builder */
078      public Builder fileCharset(Charset value) {
079         super.fileCharset(value);
080         return this;
081      }
082
083      @Override /* Overridden from Builder */
084      public Builder javaMethod(Method value) {
085         super.javaMethod(value);
086         return this;
087      }
088
089      @Override /* Overridden from Builder */
090      public Builder locale(Locale value) {
091         super.locale(value);
092         return this;
093      }
094
095      @Override /* Overridden from Builder */
096      public Builder mediaType(MediaType value) {
097         super.mediaType(value);
098         return this;
099      }
100
101      @Override /* Overridden from Builder */
102      public Builder mediaTypeDefault(MediaType value) {
103         super.mediaTypeDefault(value);
104         return this;
105      }
106
107      @Override /* Overridden from Builder */
108      public Builder properties(Map<String,Object> value) {
109         super.properties(value);
110         return this;
111      }
112
113      @Override /* Overridden from Builder */
114      public Builder property(String key, Object value) {
115         super.property(key, value);
116         return this;
117      }
118
119      @Override /* Overridden from Builder */
120      public Builder resolver(VarResolverSession value) {
121         super.resolver(value);
122         return this;
123      }
124
125      @Override /* Overridden from Builder */
126      public Builder schema(HttpPartSchema value) {
127         super.schema(value);
128         return this;
129      }
130
131      @Override /* Overridden from Builder */
132      public Builder schemaDefault(HttpPartSchema value) {
133         super.schemaDefault(value);
134         return this;
135      }
136
137      @Override /* Overridden from Builder */
138      public Builder streamCharset(Charset value) {
139         super.streamCharset(value);
140         return this;
141      }
142
143      @Override /* Overridden from Builder */
144      public Builder timeZone(TimeZone value) {
145         super.timeZone(value);
146         return this;
147      }
148
149      @Override /* Overridden from Builder */
150      public Builder timeZoneDefault(TimeZone value) {
151         super.timeZoneDefault(value);
152         return this;
153      }
154
155      @Override /* Overridden from Builder */
156      public Builder unmodifiable() {
157         super.unmodifiable();
158         return this;
159      }
160
161      @Override /* Overridden from Builder */
162      public Builder uriContext(UriContext value) {
163         super.uriContext(value);
164         return this;
165      }
166
167      @Override /* Overridden from Builder */
168      public Builder useWhitespace(Boolean value) {
169         super.useWhitespace(value);
170         return this;
171      }
172   }
173
174   /**
175    * Creates a new builder for this object.
176    *
177    * @param ctx The context creating this session.
178    * @return A new builder.
179    */
180   public static Builder create(JsonSchemaSerializer ctx) {
181      return new Builder(ctx);
182   }
183
184   private final JsonSchemaGeneratorSession genSession;
185   private final JsonSchemaSerializer ctx;
186
187   /**
188    * Constructor.
189    *
190    * @param builder The builder for this object.
191    */
192   protected JsonSchemaSerializerSession(Builder builder) {
193      super(builder);
194      ctx = builder.ctx;
195      genSession = ctx.getGenerator().getSession();
196   }
197
198   @Override /* Overridden from SerializerSession */
199   protected void doSerialize(SerializerPipe out, Object o) throws IOException, SerializeException {
200      try {
201         super.doSerialize(out, genSession.getSchema(o));
202      } catch (BeanRecursionException e) {
203         throw new SerializeException(e);
204      }
205   }
206
207   /**
208    * Returns the language-specific metadata on the specified bean property.
209    *
210    * @param bpm The bean property to return the metadata on.
211    * @return The metadata.
212    */
213   protected JsonSchemaBeanPropertyMeta getJsonSchemaBeanPropertyMeta(BeanPropertyMeta bpm) {
214      return ctx.getJsonSchemaBeanPropertyMeta(bpm);
215   }
216
217   /**
218    * Returns the language-specific metadata on the specified class.
219    *
220    * @param cm The class to return the metadata on.
221    * @return The metadata.
222    */
223   protected JsonSchemaClassMeta getJsonSchemaClassMeta(ClassMeta<?> cm) {
224      return ctx.getJsonSchemaClassMeta(cm);
225   }
226}