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.serializer;
014
015import static org.apache.juneau.serializer.Serializer.*;
016
017import java.lang.reflect.*;
018import java.nio.charset.*;
019import java.util.*;
020
021import org.apache.juneau.*;
022import org.apache.juneau.http.*;
023import org.apache.juneau.httppart.*;
024import org.apache.juneau.svl.*;
025
026/**
027 * Runtime arguments common to all serializer sessions.
028 *
029 * <p>
030 * This object specifies information such as session locale or URI context.
031 */
032public final class SerializerSessionArgs extends BeanSessionArgs {
033
034   /**
035    * Default serializer session args.
036    */
037   public static final SerializerSessionArgs DEFAULT = new SerializerSessionArgs();
038
039   Method javaMethod;
040   VarResolverSession resolver;
041
042   /**
043    * Creator.
044    *
045    * @return A new parser session arguments object.
046    */
047   public static final SerializerSessionArgs create() {
048      return new SerializerSessionArgs();
049   }
050
051   //-----------------------------------------------------------------------------------------------------------------
052   // Properties.
053   //-----------------------------------------------------------------------------------------------------------------
054
055   /**
056    * File charset.
057    *
058    * <p>
059    * The character set to use for writing Files to the file system.
060    *
061    * <p>
062    * Used when passing in files to {@link Serializer#serialize(Object, Object)}.
063    *
064    * <p>
065    * If not specified, defaults to the JVM system default charset.
066    *
067    * @param value
068    *    The new property value.
069    *    <br>Can be <jk>null</jk>.
070    * @return This object (for method chaining).
071    */
072   public SerializerSessionArgs fileCharset(Charset value) {
073      property(WriterSerializer.WSERIALIZER_fileCharset, value);
074      return this;
075   }
076
077   /**
078    * The java method that called this serializer, usually the method in a REST servlet.
079    *
080    * @param value
081    *    The new property value.
082    *    <br>Can be <jk>null</jk>.
083    * @return This object (for method chaining).
084    */
085   public SerializerSessionArgs javaMethod(Method value) {
086      this.javaMethod = value;
087      return this;
088   }
089
090   /**
091    * String variable resolver.
092    *
093    * <p>
094    * If not specified, defaults to session created by {@link VarResolver#DEFAULT}.
095    *
096    * @param value
097    *    The new property value.
098    *    <br>Can be <jk>null</jk>.
099    * @return This object (for method chaining).
100    */
101   public SerializerSessionArgs resolver(VarResolverSession value) {
102      this.resolver = value;
103      return this;
104   }
105
106   /**
107    * Output stream charset.
108    *
109    * <p>
110    * The character set to use when writing to OutputStreams.
111    *
112    * <p>
113    * Used when passing in output streams and byte arrays to {@link WriterSerializer#serialize(Object, Object)}.
114    *
115    * <p>
116    * If not specified, defaults to UTF-8.
117    *
118    * @param value
119    *    The new property value.
120    *    <br>Can be <jk>null</jk>.
121    * @return This object (for method chaining).
122    */
123   public SerializerSessionArgs streamCharset(Charset value) {
124      property(WriterSerializer.WSERIALIZER_streamCharset, value);
125      return this;
126   }
127
128   /**
129    * URI context bean.
130    *
131    * <p>
132    * Bean used for resolution of URIs to absolute or root-relative form.
133    *
134    * <p>
135    * If not specified, defaults to {@link Serializer#SERIALIZER_uriContext}.
136    *
137    * @param value
138    *    The new property value.
139    *    <br>Can be <jk>null</jk>.
140    * @return This object (for method chaining).
141    */
142   public SerializerSessionArgs uriContext(UriContext value) {
143      property(SERIALIZER_uriContext, value);
144      return this;
145   }
146
147   /**
148    * Use whitespace.
149    *
150    * <p>
151    * If true, whitespace is added to the output to improve readability.
152    *
153    * @param value
154    *    The new property value.
155    *    <br>Can be <jk>null</jk>.
156    * @return This object (for method chaining).
157    */
158   public SerializerSessionArgs useWhitespace(Boolean value) {
159      property(WriterSerializer.WSERIALIZER_useWhitespace, value);
160      return this;
161   }
162
163   @Override /* BeanSessionArgs */
164   public SerializerSessionArgs debug(Boolean value) {
165      super.debug(value);
166      return this;
167   }
168
169   @Override /* BeanSessionArgs */
170   public SerializerSessionArgs locale(Locale value) {
171      super.locale(value);
172      return this;
173   }
174
175   @Override /* BeanSessionArgs */
176   public SerializerSessionArgs mediaType(MediaType value) {
177      super.mediaType(value);
178      return this;
179   }
180
181   @Override /* BeanSessionArgs */
182   public SerializerSessionArgs schema(HttpPartSchema value) {
183      super.schema(value);
184      return this;
185   }
186
187   @Override /* BeanSessionArgs */
188   public SerializerSessionArgs timeZone(TimeZone value) {
189      super.timeZone(value);
190      return this;
191   }
192
193   @Override /* SessionArgs */
194   public SerializerSessionArgs properties(ObjectMap value) {
195      super.properties(value);
196      return this;
197   }
198
199   @Override /* SessionArgs */
200   public SerializerSessionArgs property(String key, Object value) {
201      super.property(key, value);
202      return this;
203   }
204
205   //-----------------------------------------------------------------------------------------------------------------
206   // Other methods
207   //-----------------------------------------------------------------------------------------------------------------
208   
209   @Override /* SessionArgs */
210   public ObjectMap toMap() {
211      return super.toMap()
212         .append("SerializerSessionArgs", new DefaultFilteringObjectMap()
213            .append("javaMethod", javaMethod)
214            .append("resolver", resolver)
215         );
216   }
217}