31
31
import org .json .JSONObject ;
32
32
import org .json .JSONTokener ;
33
33
34
+ import java .io .ByteArrayOutputStream ;
34
35
import java .io .IOException ;
35
36
import java .io .InputStream ;
36
37
import java .io .InputStreamReader ;
@@ -292,7 +293,7 @@ protected JSONObject multipleQueries(List<IndexQuery> queries, String strategy)
292
293
* Custom batch
293
294
*
294
295
* @param actions the array of actions
295
- * @throws AlgoliaException
296
+ * @throws AlgoliaException if the response is not valid json
296
297
*/
297
298
protected JSONObject batch (JSONArray actions ) throws AlgoliaException {
298
299
try {
@@ -308,6 +309,10 @@ private enum Method {
308
309
GET , POST , PUT , DELETE
309
310
}
310
311
312
+ protected byte [] getRequestRaw (String url , boolean search ) throws AlgoliaException {
313
+ return _requestRaw (Method .GET , url , null , readHostsArray , httpConnectTimeoutMS , search ? httpSearchTimeoutMS : httpSocketTimeoutMS );
314
+ }
315
+
311
316
protected JSONObject getRequest (String url , boolean search ) throws AlgoliaException {
312
317
return _request (Method .GET , url , null , readHostsArray , httpConnectTimeoutMS , search ? httpSearchTimeoutMS : httpSocketTimeoutMS );
313
318
}
@@ -329,7 +334,7 @@ protected JSONObject putRequest(String url, String obj) throws AlgoliaException
329
334
*
330
335
* @param istream the InputStream to read
331
336
* @return the stream's content
332
- * @throws IOException
337
+ * @throws IOException if the stream can't be read or closed
333
338
*/
334
339
private String _getAnswer (InputStream istream ) throws IOException {
335
340
InputStreamReader is = new InputStreamReader (istream , "UTF-8" );
@@ -348,7 +353,11 @@ private JSONObject _getJSONObject(String input) throws JSONException {
348
353
return new JSONObject (new JSONTokener (input ));
349
354
}
350
355
351
- private JSONObject _getAnswerObject (InputStream istream ) throws IOException , JSONException {
356
+ private JSONObject _getJSONObject (byte [] array ) throws JSONException {
357
+ return new JSONObject (new JSONTokener (new String (array )));
358
+ }
359
+
360
+ private JSONObject _getAnswerJSONObject (InputStream istream ) throws IOException , JSONException {
352
361
return _getJSONObject (_getAnswer (istream ));
353
362
}
354
363
@@ -362,9 +371,29 @@ private JSONObject _getAnswerObject(InputStream istream) throws IOException, JSO
362
371
* @param connectTimeout maximum wait time to open connection
363
372
* @param readTimeout maximum time to read data on socket
364
373
* @return a JSONObject containing the resulting data or error
365
- * @throws AlgoliaException
374
+ * @throws AlgoliaException if the request data is not valid json
366
375
*/
367
376
private synchronized JSONObject _request (Method m , String url , String json , List <String > hostsArray , int connectTimeout , int readTimeout ) throws AlgoliaException {
377
+ try {
378
+ return _getJSONObject (_requestRaw (m , url , json , hostsArray , connectTimeout , readTimeout ));
379
+ } catch (JSONException e ) {
380
+ throw new AlgoliaException ("JSON decode error:" + e .getMessage ());
381
+ }
382
+ }
383
+
384
+ /**
385
+ * Send the query according to parameters and returns its result as a JSONObject
386
+ *
387
+ * @param m HTTP Method to use
388
+ * @param url endpoint URL
389
+ * @param json optional JSON Object to send
390
+ * @param hostsArray array of hosts to try successively
391
+ * @param connectTimeout maximum wait time to open connection
392
+ * @param readTimeout maximum time to read data on socket
393
+ * @return a JSONObject containing the resulting data or error
394
+ * @throws AlgoliaException in case of connection or data handling error
395
+ */
396
+ private synchronized byte [] _requestRaw (Method m , String url , String json , List <String > hostsArray , int connectTimeout , int readTimeout ) throws AlgoliaException {
368
397
String requestMethod ;
369
398
HashMap <String , String > errors = new HashMap <String , String >();
370
399
// for each host
@@ -459,7 +488,7 @@ private synchronized JSONObject _request(Method m, String url, String json, List
459
488
} else if (code / 100 == 4 ) {
460
489
String message = "Error detected in backend" ;
461
490
try {
462
- message = _getAnswerObject (stream ).getString ("message" );
491
+ message = _getAnswerJSONObject (stream ).getString ("message" );
463
492
} catch (IOException e ) {
464
493
addError (errors , host , e );
465
494
continue ;
@@ -481,13 +510,11 @@ private synchronized JSONObject _request(Method m, String url, String json, List
481
510
try {
482
511
String encoding = hostConnection .getContentEncoding ();
483
512
if (encoding != null && encoding .contains ("gzip" )) {
484
- return _getAnswerObject (new GZIPInputStream (stream ));
513
+ return _toByteArray (new GZIPInputStream (stream ));
485
514
}
486
515
else {
487
- return _getAnswerObject (stream );
516
+ return _toByteArray (stream );
488
517
}
489
- } catch (JSONException e ) {
490
- throw new AlgoliaException ("JSON decode error:" + e .getMessage ());
491
518
} catch (IOException e ) {
492
519
throw new AlgoliaException ("Data decoding error:" + e .getMessage ());
493
520
}
@@ -504,6 +531,23 @@ private synchronized JSONObject _request(Method m, String url, String json, List
504
531
throw new AlgoliaException (builder .toString ());
505
532
}
506
533
534
+ private byte [] _toByteArray (InputStream stream ) throws AlgoliaException {
535
+ ByteArrayOutputStream out = new ByteArrayOutputStream ();
536
+ int read ;
537
+ byte [] buffer = new byte [1024 ];
538
+
539
+ try {
540
+ while ((read = stream .read (buffer , 0 , buffer .length )) != -1 ) {
541
+ out .write (buffer , 0 , read );
542
+ }
543
+
544
+ out .flush ();
545
+ return out .toByteArray ();
546
+ } catch (IOException e ) {
547
+ throw new AlgoliaException ("Error while reading stream: " + e .getMessage ());
548
+ }
549
+ }
550
+
507
551
private void addError (HashMap <String , String > errors , String host , IOException e ) {
508
552
errors .put (host , String .format ("%s=%s" , e .getClass ().getName (), e .getMessage ()));
509
553
}
0 commit comments