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