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.serializer; 014 015import org.apache.juneau.*; 016import org.apache.juneau.annotation.*; 017 018/** 019 * Subclass of {@link Serializer} for byte-based serializers. 020 */ 021@ConfigurableContext 022public abstract class OutputStreamSerializer extends Serializer { 023 024 //------------------------------------------------------------------------------------------------------------------- 025 // Configurable properties 026 //------------------------------------------------------------------------------------------------------------------- 027 028 static final String PREFIX = "OutputStreamSerializer"; 029 030 /** 031 * Configuration property: Binary output format. 032 * 033 * <h5 class='section'>Property:</h5> 034 * <ul class='spaced-list'> 035 * <li><b>ID:</b> {@link org.apache.juneau.serializer.OutputStreamSerializer#OSSERIALIZER_binaryFormat OSSERIALIZER_binaryFormat} 036 * <li><b>Name:</b> <js>"OutputStreamSerializer.binaryFormat.s"</js> 037 * <li><b>Data type:</b> {@link org.apache.juneau.BinaryFormat} 038 * <li><b>System property:</b> <c>OutputStreamSerializer.binaryFormat</c> 039 * <li><b>Environment variable:</b> <c>OUTPUTSTREAMSERIALIZER_BINARYFORMAT</c> 040 * <li><b>Default:</b> {@link org.apache.juneau.BinaryFormat#HEX} 041 * <li><b>Session property:</b> <jk>false</jk> 042 * <li><b>Annotations:</b> 043 * <ul> 044 * <li class='ja'>{@link org.apache.juneau.serializer.annotation.SerializerConfig#binaryFormat()} 045 * </ul> 046 * <li><b>Methods:</b> 047 * <ul> 048 * <li class='jm'>{@link org.apache.juneau.serializer.OutputStreamSerializerBuilder#binaryFormat(BinaryFormat)} 049 * </ul> 050 * </ul> 051 * 052 * <h5 class='section'>Description:</h5> 053 * <p> 054 * When using the {@link #serializeToString(Object)} method on stream-based serializers, this defines the format to use 055 * when converting the resulting byte array to a string. 056 * 057 * 058 * <h5 class='section'>Example:</h5> 059 * <p class='bcode w800'> 060 * <jc>// Create a serializer that serializes to BASE64.</jc> 061 * OutputStreamSerializer s = MsgPackSerializer 062 * .<jsm>create</jsm>() 063 * .binaryFormat(<jsf>BASE64</jsf>) 064 * .build(); 065 * 066 * <jc>// Same, but use property.</jc> 067 * OutputStreamSerializer s = MsgPackSerializer 068 * .<jsm>create</jsm>() 069 * .set(<jsf>SERIALIZER_binaryOutputFormat</jsf>, <js>"BASE64"</js>) 070 * .build(); 071 * 072 * <jc>// The bean we want to serialize.</jc> 073 * <jk>public class</jk> MyBean {...} 074 * 075 * <jc>// MessagePack will generate BASE64-encoded string.</jc> 076 * String msgPack = s.serializeToString(<jk>new</jk> MyBean()); 077 * </p> 078 */ 079 public static final String OSSERIALIZER_binaryFormat = PREFIX + ".binaryFormat.s"; 080 081 static final OutputStreamSerializer DEFAULT = new OutputStreamSerializer(PropertyStore.create().build(), "", "") { 082 @Override 083 public OutputStreamSerializerSession createSession(SerializerSessionArgs args) { 084 throw new NoSuchMethodError(); 085 } 086 }; 087 088 //------------------------------------------------------------------------------------------------------------------- 089 // Instance 090 //------------------------------------------------------------------------------------------------------------------- 091 092 private final BinaryFormat binaryFormat; 093 094 /** 095 * Constructor. 096 * 097 * @param ps 098 * The property store containing all the settings for this object. 099 * @param produces 100 * The media type that this serializer produces. 101 * @param accept 102 * The accept media types that the serializer can handle. 103 * <p> 104 * Can contain meta-characters per the <c>media-type</c> specification of {@doc RFC2616.section14.1} 105 * <p> 106 * If empty, then assumes the only media type supported is <c>produces</c>. 107 * <p> 108 * For example, if this serializer produces <js>"application/json"</js> but should handle media types of 109 * <js>"application/json"</js> and <js>"text/json"</js>, then the arguments should be: 110 * <p class='bcode w800'> 111 * <jk>super</jk>(ps, <js>"application/json"</js>, <js>"application/json,text/json"</js>); 112 * </p> 113 * <br>...or... 114 * <p class='bcode w800'> 115 * <jk>super</jk>(ps, <js>"application/json"</js>, <js>"*​/json"</js>); 116 * </p> 117 * <p> 118 * The accept value can also contain q-values. 119 */ 120 protected OutputStreamSerializer(PropertyStore ps, String produces, String accept) { 121 super(ps, produces, accept); 122 123 binaryFormat = getProperty(OSSERIALIZER_binaryFormat, BinaryFormat.class, BinaryFormat.HEX); 124 } 125 126 //----------------------------------------------------------------------------------------------------------------- 127 // Abstract methods 128 //----------------------------------------------------------------------------------------------------------------- 129 130 @Override /* SerializerSession */ 131 public abstract OutputStreamSerializerSession createSession(SerializerSessionArgs args); 132 133 134 //----------------------------------------------------------------------------------------------------------------- 135 // Other methods 136 //----------------------------------------------------------------------------------------------------------------- 137 138 @Override /* Context */ 139 public OutputStreamSerializerSession createSession() { 140 return createSession(createDefaultSessionArgs()); 141 } 142 143 @Override /* Serializer */ 144 public final boolean isWriterSerializer() { 145 return false; 146 } 147 148 /** 149 * Convenience method for serializing an object to a <code><jk>byte</jk></code>. 150 * 151 * @param o The object to serialize. 152 * @return The output serialized to a byte array. 153 * @throws SerializeException If a problem occurred trying to convert the output. 154 */ 155 @Override 156 public final byte[] serialize(Object o) throws SerializeException { 157 return createSession(createDefaultSessionArgs()).serialize(o); 158 } 159 160 //----------------------------------------------------------------------------------------------------------------- 161 // Properties 162 //----------------------------------------------------------------------------------------------------------------- 163 164 /** 165 * Configuration property: Binary output format. 166 * 167 * @see #OSSERIALIZER_binaryFormat 168 * @return 169 * The format to use for the {@link #serializeToString(Object)} method on stream-based serializers when converting byte arrays to strings. 170 */ 171 protected final BinaryFormat getBinaryFormat() { 172 return binaryFormat; 173 } 174 175 //----------------------------------------------------------------------------------------------------------------- 176 // Other methods 177 //----------------------------------------------------------------------------------------------------------------- 178 179 @Override /* Context */ 180 public ObjectMap toMap() { 181 return super.toMap() 182 .append("OutputStreamSerializer", new DefaultFilteringObjectMap() 183 .append("binaryFormat", binaryFormat) 184 ); 185 } 186}