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