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