55 */
66package com .yahoo .bullet .pubsub ;
77
8+ import com .google .gson .Gson ;
9+ import com .yahoo .bullet .common .SerializerDeserializer ;
810import com .yahoo .bullet .pubsub .Metadata .Signal ;
11+ import com .yahoo .bullet .query .Query ;
912import com .yahoo .bullet .result .JSONFormatter ;
1013import lombok .Getter ;
1114import lombok .Setter ;
1215
1316import java .io .Serializable ;
1417import java .nio .charset .Charset ;
1518import java .nio .charset .StandardCharsets ;
19+ import java .util .Base64 ;
1620import java .util .Objects ;
1721
1822/**
2125 */
2226@ Getter
2327public class PubSubMessage implements Serializable , JSONFormatter {
24- private static final long serialVersionUID = -5068189058170874687L ;
2528 public static final Charset CHARSET = StandardCharsets .UTF_8 ;
29+ private static final long serialVersionUID = 5096747716667851530L ;
2630
2731 private String id ;
28- private byte [] content ;
32+ // Serializable enforced through the constructors, getter and setter. Is Object so GSON can reify an instance.
33+ private Object content ;
2934 @ Setter
3035 private Metadata metadata ;
3136
@@ -52,50 +57,29 @@ public PubSubMessage(String id, Signal signal) {
5257 * @param id The ID associated with the message.
5358 * @param content The content of the message.
5459 */
55- public PubSubMessage (String id , byte [] content ) {
60+ public PubSubMessage (String id , Serializable content ) {
5661 this (id , content , (Metadata ) null );
5762 }
5863
59- /**
60- * Constructor for a message having only content as a String.
61- *
62- * @param id The ID associated with the message.
63- * @param content The content of the message as a String.
64- */
65- public PubSubMessage (String id , String content ) {
66- this (id , content , null );
67- }
68-
6964 /**
7065 * Constructor for a message having content and a {@link Metadata.Signal}.
7166 *
7267 * @param id The ID associated with the message.
7368 * @param content The content of the message.
7469 * @param signal The Signal to be sent with the message.
7570 */
76- public PubSubMessage (String id , byte [] content , Signal signal ) {
71+ public PubSubMessage (String id , Serializable content , Signal signal ) {
7772 this (id , content , new Metadata (signal , null ));
7873 }
7974
80- /**
81- * Constructor for a message having content as a String and {@link Metadata}.
82- *
83- * @param id The ID associated with the message.
84- * @param content The content of the message as a String.
85- * @param metadata The Metadata associated with the message.
86- */
87- public PubSubMessage (String id , String content , Metadata metadata ) {
88- this (id , content == null ? null : content .getBytes (CHARSET ), metadata );
89- }
90-
9175 /**
9276 * Constructor for a message having content and {@link Metadata}.
9377 *
9478 * @param id The ID associated with the message.
9579 * @param content The content of the message.
9680 * @param metadata The Metadata associated with the message.
9781 */
98- public PubSubMessage (String id , byte [] content , Metadata metadata ) {
82+ public PubSubMessage (String id , Serializable content , Metadata metadata ) {
9983 this .id = Objects .requireNonNull (id , "ID cannot be null" );
10084 this .content = content ;
10185 this .metadata = metadata ;
@@ -138,14 +122,52 @@ public boolean hasSignal() {
138122 return hasMetadata () && metadata .hasSignal ();
139123 }
140124
125+ /**
126+ * Returns the {@link Serializable} content stored in the message.
127+ *
128+ * @return The content stored.
129+ */
130+ public Serializable getContent () {
131+ return (Serializable ) content ;
132+ }
133+
134+ /**
135+ * Returns the content stored in the message as a byte[]. You should use this to read the byte[] back from the
136+ * message if you provided it originally to the message as a byte[].
137+ *
138+ * @return The content stored as a byte[].
139+ */
140+ public byte [] getContentAsByteArray () {
141+ return (byte []) content ;
142+ }
143+
141144 /**
142145 * Returns the content stored in the message as a String. You should use this to read the String back from the
143146 * message if you provided it originally to the message as a String.
144147 *
145- * @return The content stored as a String using the {@link PubSubMessage#CHARSET} .
148+ * @return The content stored as a String.
146149 */
147150 public String getContentAsString () {
148- return content == null ? null : new String (content , CHARSET );
151+ return (String ) content ;
152+ }
153+
154+ /**
155+ * Returns the content stored in the message as a {@link Query}. You should use this to read the {@link Query} back
156+ * if you originally provided to the message as a {@link Serializable}.
157+ *
158+ * @return The content stored as a {@link Query}.
159+ */
160+ public Query getContentAsQuery () {
161+ return (Query ) content ;
162+ }
163+
164+ /**
165+ * Set a {@link Serializable} content for this message.
166+ *
167+ * @param content The content for this message.
168+ */
169+ public void setContent (Serializable content ) {
170+ this .content = content ;
149171 }
150172
151173 @ Override
@@ -169,16 +191,38 @@ public String toString() {
169191
170192 @ Override
171193 public String asJSON () {
172- return JSONFormatter .asJSON (this );
194+ String data = Base64 .getEncoder ().encodeToString (SerializerDeserializer .toBytes ((Serializable ) content ));
195+ PubSubMessage message = new PubSubMessage (id , data , metadata );
196+ return JSONFormatter .asJSON (message );
173197 }
174198
175199 /**
176- * Converts a json representation back to an instance.
200+ * Converts a json representation back to an instance. Is the inverse of {@link #asJSON()}.
177201 *
178202 * @param json The string representation of the JSON.
179203 * @return An instance of this class.
180204 */
181205 public static PubSubMessage fromJSON (String json ) {
182- return JSONFormatter .fromJSON (json , PubSubMessage .class );
206+ return fromJSON (json , GSON );
207+ }
208+
209+ /**
210+ * Converts a JSON representation back to an instance using a specific {@link Gson} converter.
211+ * Is the inverse of {@link #asJSON()}.
212+ *
213+ * @param json The string representation of the JSON.
214+ * @param gson The {@link Gson} converter to use.
215+ * @return An instance of this class.
216+ */
217+ public static PubSubMessage fromJSON (String json , Gson gson ) {
218+ return fromJSON (gson .fromJson (json , PubSubMessage .class ));
219+ }
220+
221+ private static PubSubMessage fromJSON (PubSubMessage message ) {
222+ if (message == null || message .getContent () == null ) {
223+ return message ;
224+ }
225+ message .content = SerializerDeserializer .fromBytes (Base64 .getDecoder ().decode (message .getContentAsString ()));
226+ return message ;
183227 }
184228}
0 commit comments