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.jso;
014
015import java.io.*;
016import java.util.*;
017import java.util.concurrent.*;
018
019import org.apache.juneau.*;
020import org.apache.juneau.annotation.*;
021import org.apache.juneau.parser.*;
022
023/**
024 * Parses POJOs from HTTP responses as Java {@link ObjectInputStream ObjectInputStreams}.
025 *
026 * <h5 class='topic'>Media types</h5>
027 *
028 * Consumes <c>Content-Type</c> types:  <bc>application/x-java-serialized-object</bc>
029 */
030@ConfigurableContext
031public final class JsoParser extends InputStreamParser implements JsoMetaProvider, JsoCommon {
032
033   //-------------------------------------------------------------------------------------------------------------------
034   // Configurable properties
035   //-------------------------------------------------------------------------------------------------------------------
036
037   static final String PREFIX = "JsoParser";
038
039   //-------------------------------------------------------------------------------------------------------------------
040   // Predefined instances
041   //-------------------------------------------------------------------------------------------------------------------
042
043   /** Default parser, all default settings.*/
044   public static final JsoParser DEFAULT = new JsoParser(PropertyStore.DEFAULT);
045
046   //-------------------------------------------------------------------------------------------------------------------
047   // Instance
048   //-------------------------------------------------------------------------------------------------------------------
049
050   private final Map<ClassMeta<?>,JsoClassMeta> jsoClassMetas = new ConcurrentHashMap<>();
051   private final Map<BeanPropertyMeta,JsoBeanPropertyMeta> jsoBeanPropertyMetas = new ConcurrentHashMap<>();
052
053   /**
054    * Constructor.
055    *
056    * @param ps The property store containing all the settings for this object.
057    */
058   public JsoParser(PropertyStore ps) {
059      super(ps, "application/x-java-serialized-object");
060   }
061
062   @Override /* Context */
063   public JsoParserBuilder builder() {
064      return new JsoParserBuilder(getPropertyStore());
065   }
066
067   /**
068    * Instantiates a new clean-slate {@link JsoParserBuilder} object.
069    *
070    * <p>
071    * This is equivalent to simply calling <code><jk>new</jk> JsoParserBuilder()</code>.
072    *
073    * <p>
074    * Note that this method creates a builder initialized to all default settings, whereas {@link #builder()} copies
075    * the settings of the object called on.
076    *
077    * @return A new {@link JsoParserBuilder} object.
078    */
079   public static JsoParserBuilder create() {
080      return new JsoParserBuilder();
081   }
082
083   @Override /* Parser */
084   public JsoParserSession createSession() {
085      return createSession(createDefaultSessionArgs());
086   }
087
088   @Override /* Parser */
089   public JsoParserSession createSession(ParserSessionArgs args) {
090      return new JsoParserSession(args);
091   }
092
093   //-----------------------------------------------------------------------------------------------------------------
094   // Extended metadata
095   //-----------------------------------------------------------------------------------------------------------------
096
097   @Override /* JsoMetaProvider */
098   public JsoClassMeta getJsoClassMeta(ClassMeta<?> cm) {
099      JsoClassMeta m = jsoClassMetas.get(cm);
100      if (m == null) {
101         m = new JsoClassMeta(cm, this);
102         jsoClassMetas.put(cm, m);
103      }
104      return m;
105   }
106
107   @Override /* JsoMetaProvider */
108   public JsoBeanPropertyMeta getJsoBeanPropertyMeta(BeanPropertyMeta bpm) {
109      if (bpm == null)
110         return JsoBeanPropertyMeta.DEFAULT;
111      JsoBeanPropertyMeta m = jsoBeanPropertyMetas.get(bpm);
112      if (m == null) {
113         m = new JsoBeanPropertyMeta(bpm.getDelegateFor(), this);
114         jsoBeanPropertyMetas.put(bpm, m);
115      }
116      return m;
117   }
118
119   //-----------------------------------------------------------------------------------------------------------------
120   // Other methods
121   //-----------------------------------------------------------------------------------------------------------------
122
123   @Override /* Context */
124   public ObjectMap toMap() {
125      return super.toMap()
126         .append("JsoParser", new DefaultFilteringObjectMap()
127         );
128   }
129}