1717import io .ably .lib .types .Param ;
1818import io .ably .lib .types .PresenceMessage ;
1919import io .ably .lib .types .PresenceSerializer ;
20+ import io .ably .lib .types .PublishResult ;
2021import io .ably .lib .types .UpdateDeleteResult ;
2122import io .ably .lib .util .Crypto ;
2223
@@ -66,6 +67,23 @@ void publish(Http http, String name, Object data) throws AblyException {
6667 publishImpl (http , name , data ).sync ();
6768 }
6869
70+ /**
71+ * Publish a message on this channel using the REST API and return the result.
72+ * Since the REST API is stateless, this request is made independently
73+ * of any other request on this or any other channel.
74+ * @param name the event name
75+ * @param data the message payload;
76+ * @return A {@link PublishResult} containing the message serial(s)
77+ * @throws AblyException
78+ */
79+ public PublishResult publishWithResult (String name , Object data ) throws AblyException {
80+ return publishWithResult (ably .http , name , data );
81+ }
82+
83+ PublishResult publishWithResult (Http http , String name , Object data ) throws AblyException {
84+ return publishImpl (http , name , data ).sync ();
85+ }
86+
6987 /**
7088 * Publish a message on this channel using the REST API.
7189 * Since the REST API is stateless, this request is made independently
@@ -76,16 +94,37 @@ void publish(Http http, String name, Object data) throws AblyException {
7694 * @param listener a listener to be notified of the outcome of this message.
7795 * <p>
7896 * This listener is invoked on a background thread.
97+ * @deprecated Use {@link #publishAsync(String, Object, Callback)} instead.
7998 */
99+ @ Deprecated
80100 public void publishAsync (String name , Object data , CompletionListener listener ) {
81101 publishAsync (ably .http , name , data , listener );
82102 }
83103
84104 void publishAsync (Http http , String name , Object data , CompletionListener listener ) {
85- publishImpl (http , name , data ).async (new CompletionListener .ToCallback (listener ));
105+ publishImpl (http , name , data ).async (new CompletionListener .ToCallback <> (listener ));
86106 }
87107
88- private Http .Request <Void > publishImpl (Http http , String name , Object data ) {
108+ /**
109+ * Asynchronously publish a message on this channel using the REST API.
110+ * Since the REST API is stateless, this request is made independently
111+ * of any other request on this or any other channel.
112+ *
113+ * @param name the event name
114+ * @param data the message payload;
115+ * @param callback a callback to be notified of the outcome of this message with the {@link PublishResult}.
116+ * <p>
117+ * This callback is invoked on a background thread.
118+ */
119+ public void publishAsync (String name , Object data , Callback <PublishResult > callback ) {
120+ publishAsync (ably .http , name , data , callback );
121+ }
122+
123+ void publishAsync (Http http , String name , Object data , Callback <PublishResult > callback ) {
124+ publishImpl (http , name , data ).async (callback );
125+ }
126+
127+ private Http .Request <PublishResult > publishImpl (Http http , String name , Object data ) {
89128 return publishImpl (http , new Message [] {new Message (name , data )});
90129 }
91130
@@ -105,26 +144,52 @@ void publish(Http http, final Message[] messages) throws AblyException {
105144 publishImpl (http , messages ).sync ();
106145 }
107146
147+ /**
148+ * Publish an array of messages on this channel. When there are
149+ * multiple messages to be sent, it is more efficient to use this
150+ * method to publish them in a single request, as compared with
151+ * publishing via multiple independent requests.
152+ * @param messages array of messages to publish.
153+ * @throws AblyException
154+ */
155+ public PublishResult publishWithResult (final Message [] messages ) throws AblyException {
156+ return publishImpl (ably .http , messages ).sync ();
157+ }
158+
108159 /**
109160 * Asynchronously publish an array of messages on this channel
110161 *
111162 * @param messages the message
112163 * @param listener a listener to be notified of the outcome of this message.
164+ * @deprecated Use {@link #publishAsync(Message[], Callback)} instead.
113165 * <p>
114166 * This listener is invoked on a background thread.
115167 */
168+ @ Deprecated
116169 public void publishAsync (final Message [] messages , final CompletionListener listener ) {
117170 publishAsync (ably .http , messages , listener );
118171 }
119172
120173 void publishAsync (Http http , final Message [] messages , final CompletionListener listener ) {
121- publishImpl (http , messages ).async (new CompletionListener .ToCallback (listener ));
174+ publishImpl (http , messages ).async (new CompletionListener .ToCallback <> (listener ));
122175 }
123176
124- private Http .Request <Void > publishImpl (Http http , final Message [] messages ) {
125- return http .request (new Http .Execute <Void >() {
177+ /**
178+ * Asynchronously publish an array of messages on this channel
179+ *
180+ * @param messages the message
181+ * @param listener a listener to be notified of the outcome of this message.
182+ * <p>
183+ * This listener is invoked on a background thread.
184+ */
185+ public void publishAsync (final Message [] messages , final Callback <PublishResult > listener ) {
186+ publishImpl (ably .http , messages ).async (listener );
187+ }
188+
189+ private Http .Request <PublishResult > publishImpl (Http http , final Message [] messages ) {
190+ return http .request (new Http .Execute <PublishResult >() {
126191 @ Override
127- public void execute (HttpScheduler http , final Callback <Void > callback ) throws AblyException {
192+ public void execute (HttpScheduler http , final Callback <PublishResult > callback ) throws AblyException {
128193 /* handle message ids */
129194 boolean hasClientSuppliedId = false ;
130195 for (Message message : messages ) {
@@ -145,7 +210,15 @@ public void execute(HttpScheduler http, final Callback<Void> callback) throws Ab
145210 HttpCore .RequestBody requestBody = ably .options .useBinaryProtocol ? MessageSerializer .asMsgpackRequest (messages ) : MessageSerializer .asJsonRequest (messages );
146211 final Param [] params = ably .options .addRequestIds ? Param .array (Crypto .generateRandomRequestId ()) : null ; // RSC7c
147212
148- http .post (basePath + "/messages" , HttpUtils .defaultAcceptHeaders (ably .options .useBinaryProtocol ), params , requestBody , null , true , callback );
213+ // Create ResponseHandler from BodyHandler
214+ HttpCore .BodyHandler <String > bodyHandler = PublishResult .getBodyHandler ();
215+ HttpCore .ResponseHandler <PublishResult > responseHandler = (response , error ) -> {
216+ if (error != null ) throw AblyException .fromErrorInfo (error );
217+ String [] serials = bodyHandler .handleResponseBody (response .contentType , response .body );
218+ return new PublishResult (serials );
219+ };
220+
221+ http .post (basePath + "/messages" , HttpUtils .defaultAcceptHeaders (ably .options .useBinaryProtocol ), params , requestBody , responseHandler , true , callback );
149222 }
150223 });
151224 }
0 commit comments