88import java .util .List ;
99import java .util .Map ;
1010import java .util .TreeMap ;
11+ import java .util .logging .Level ;
1112import java .util .logging .Logger ;
1213
1314import io .socket .emitter .Emitter ;
@@ -26,6 +27,8 @@ public class PollingXHR extends Polling {
2627
2728 private static final Logger logger = Logger .getLogger (PollingXHR .class .getName ());
2829
30+ private static boolean LOGGABLE_FINE = logger .isLoggable (Level .FINE );
31+
2932 public PollingXHR (Transport .Options opts ) {
3033 super (opts );
3134 }
@@ -66,6 +69,15 @@ public void run() {
6669
6770 @ Override
6871 protected void doWrite (byte [] data , final Runnable fn ) {
72+ this .doWrite ((Object ) data , fn );
73+ }
74+
75+ @ Override
76+ protected void doWrite (String data , final Runnable fn ) {
77+ this .doWrite ((Object ) data , fn );
78+ }
79+
80+ private void doWrite (Object data , final Runnable fn ) {
6981 Request .Options opts = new Request .Options ();
7082 opts .method = "POST" ;
7183 opts .data = data ;
@@ -140,13 +152,17 @@ public static class Request extends Emitter {
140152 public static final String EVENT_ERROR = "error" ;
141153 public static final String EVENT_REQUEST_HEADERS = "requestHeaders" ;
142154 public static final String EVENT_RESPONSE_HEADERS = "responseHeaders" ;
155+
143156 private static final String BINARY_CONTENT_TYPE = "application/octet-stream" ;
157+ private static final String TEXT_CONTENT_TYPE = "text/plain;charset=UTF-8" ;
158+
159+ private static final MediaType BINARY_MEDIA_TYPE = MediaType .parse (BINARY_CONTENT_TYPE );
160+ private static final MediaType TEXT_MEDIA_TYPE = MediaType .parse (TEXT_CONTENT_TYPE );
144161
145162 private String method ;
146163 private String uri ;
147164
148- // data is always a binary
149- private byte [] data ;
165+ private Object data ;
150166
151167 private Call .Factory callFactory ;
152168 private Response response ;
@@ -161,28 +177,42 @@ public Request(Options opts) {
161177
162178 public void create () {
163179 final Request self = this ;
164- logger .fine (String .format ("xhr open %s: %s" , this .method , this .uri ));
180+ if ( LOGGABLE_FINE ) logger .fine (String .format ("xhr open %s: %s" , this .method , this .uri ));
165181 Map <String , List <String >> headers = new TreeMap <String , List <String >>(String .CASE_INSENSITIVE_ORDER );
166182
167183 if ("POST" .equals (this .method )) {
168- headers .put ("Content-type" , new LinkedList <String >(Collections .singletonList (BINARY_CONTENT_TYPE )));
184+ if (this .data instanceof byte []) {
185+ headers .put ("Content-type" , new LinkedList <String >(Collections .singletonList (BINARY_CONTENT_TYPE )));
186+ } else {
187+ headers .put ("Content-type" , new LinkedList <String >(Collections .singletonList (TEXT_CONTENT_TYPE )));
188+ }
169189 }
170190
171191 headers .put ("Accept" , new LinkedList <String >(Collections .singletonList ("*/*" )));
172192
173- self .onRequestHeaders (headers );
193+ this .onRequestHeaders (headers );
194+
195+ if (LOGGABLE_FINE ) {
196+ logger .fine (String .format ("sending xhr with url %s | data %s" , this .uri ,
197+ this .data instanceof byte [] ? Arrays .toString ((byte []) this .data ) : this .data ));
198+ }
174199
175- logger .fine (String .format ("sending xhr with url %s | data %s" , this .uri , Arrays .toString (this .data )));
176200 okhttp3 .Request .Builder requestBuilder = new okhttp3 .Request .Builder ();
177201 for (Map .Entry <String , List <String >> header : headers .entrySet ()) {
178202 for (String v : header .getValue ()){
179203 requestBuilder .addHeader (header .getKey (), v );
180204 }
181205 }
206+ RequestBody body = null ;
207+ if (this .data instanceof byte []) {
208+ body = RequestBody .create (BINARY_MEDIA_TYPE , (byte [])this .data );
209+ } else if (this .data instanceof String ) {
210+ body = RequestBody .create (TEXT_MEDIA_TYPE , (String )this .data );
211+ }
212+
182213 okhttp3 .Request request = requestBuilder
183214 .url (HttpUrl .parse (self .uri ))
184- .method (self .method , (self .data != null ) ?
185- RequestBody .create (MediaType .parse (BINARY_CONTENT_TYPE ), self .data ) : null )
215+ .method (self .method , body )
186216 .build ();
187217
188218 requestCall = callFactory .newCall (request );
@@ -255,7 +285,7 @@ public static class Options {
255285
256286 public String uri ;
257287 public String method ;
258- public byte [] data ;
288+ public Object data ;
259289 public Call .Factory callFactory ;
260290 }
261291 }
0 commit comments