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;
014
015import static org.apache.juneau.Context.*;
016
017import java.util.*;
018
019import org.apache.juneau.collections.*;
020import org.apache.juneau.http.*;
021import org.apache.juneau.internal.*;
022import org.apache.juneau.json.*;
023
024/**
025 * Runtime arguments common to all bean, serializer, and parser sessions.
026 */
027public class SessionArgs {
028
029   /**
030    * Default empty session arguments.
031    */
032   public static final SessionArgs DEFAULT = new SessionArgs();
033
034   OMap properties;
035
036   /**
037    * Constructor.
038    */
039   public SessionArgs() {}
040
041   //-----------------------------------------------------------------------------------------------------------------
042   // Properties.
043   //-----------------------------------------------------------------------------------------------------------------
044
045   /**
046    * Debug mode.
047    *
048    * <p>
049    * Enables the following additional information during parsing:
050    * <ul>
051    *    <li> When bean setters throws exceptions, the exception includes the object stack information in order to determine how that method was invoked.
052    * </ul>
053    *
054    * <p>
055    * If not specified, defaults to {@link Context#CONTEXT_debug}.
056    *
057    * @param value
058    *    The new value for this property.
059    *    <br>Can be <jk>null</jk>.
060    * @return This object (for method chaining).
061    */
062   @FluentSetter
063   public SessionArgs debug(Boolean value) {
064      property(CONTEXT_debug, value);
065      return this;
066   }
067
068   /**
069    * The session locale.
070    *
071    * <p>
072    * Specifies the default locale for serializer and parser sessions.
073    *
074    * <p>
075    * If not specified, defaults to {@link Context#CONTEXT_locale}.
076    *
077    * @param value
078    *    The new value for this property.
079    *    <br>If <jk>null</jk>, then the locale defined on the context is used.
080    * @return This object (for method chaining).
081    */
082   @FluentSetter
083   public SessionArgs locale(Locale value) {
084      property(CONTEXT_locale, value);
085      return this;
086   }
087
088   /**
089    * The session media type.
090    *
091    * <p>
092    * Specifies the default media type value for serializer and parser sessions.
093    *
094    * <p>
095    * If not specified, defaults to {@link Context#CONTEXT_mediaType}.
096    *
097    * @param value
098    *    The new value for this property.
099    *    <br>Can be <jk>null</jk>.
100    * @return This object (for method chaining).
101    */
102   @FluentSetter
103   public SessionArgs mediaType(MediaType value) {
104      property(CONTEXT_mediaType, value);
105      return this;
106   }
107
108   /**
109    * The session timezone.
110    *
111    * <p>
112    * Specifies the default timezone for serializer and parser sessions.
113    *
114    * <p>
115    * If not specified, defaults to {@link BeanContext#CONTEXT_timeZone}.
116    *
117    * @param value
118    *    The new value for this property.
119    *    <br>Can be <jk>null</jk>.
120    * @return This object (for method chaining).
121    */
122   @FluentSetter
123   public SessionArgs timeZone(TimeZone value) {
124      property(CONTEXT_timeZone, value);
125      return this;
126   }
127
128   /**
129    * Session-level properties.
130    *
131    * <p>
132    * Overrides context-level properties.
133    *
134    * @param value
135    *    The new value for this property.
136    *    <br>Can be <jk>null</jk>.
137    * @return This object (for method chaining).
138    */
139   @FluentSetter
140   public SessionArgs properties(OMap value) {
141      this.properties = value;
142      return this;
143   }
144
145   /**
146    * Adds a property to this session.
147    *
148    * @param key The property key.
149    * @param value The property value.
150    * @return This object (for method chaining).
151    */
152   @FluentSetter
153   public SessionArgs property(String key, Object value) {
154      if (value == null) {
155         if (properties != null)
156            properties.remove(key);
157      } else {
158         if (properties == null)
159            properties = new OMap();
160         properties.put(key, value);
161      }
162      return this;
163   }
164
165   /**
166    * Returns a property on this session.
167    *
168    * @param key The property key.
169    * @return The property value, or <jk>null</jk> if not set.
170    */
171   public Object getProperty(String key) {
172      if (properties != null)
173         return properties.get(key);
174      return null;
175   }
176
177   //-----------------------------------------------------------------------------------------------------------------
178   // Other methods
179   //-----------------------------------------------------------------------------------------------------------------
180
181   /**
182    * Returns the properties defined on this object as a simple map for debugging purposes.
183    *
184    * @return A new map containing the properties defined on this object.
185    */
186   public OMap toMap() {
187      return new DefaultFilteringOMap()
188         .append("SessionArgs", new DefaultFilteringOMap()
189            .append("properties", properties)
190         );
191   }
192
193   @Override /* Object */
194   public String toString() {
195      return SimpleJsonSerializer.DEFAULT_READABLE.toString(toMap());
196   }
197
198   // <FluentSetters>
199
200   // </FluentSetters>
201}