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