Skip to content
This repository was archived by the owner on Jan 31, 2022. It is now read-only.

Commit 252c043

Browse files
committed
Index: exposed Raw methods
1 parent 5ef88a0 commit 252c043

File tree

2 files changed

+71
-14
lines changed

2 files changed

+71
-14
lines changed

algoliasearch/src/main/java/com/algolia/search/saas/BaseAPIClient.java

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.json.JSONObject;
3232
import org.json.JSONTokener;
3333

34+
import java.io.ByteArrayOutputStream;
3435
import java.io.IOException;
3536
import java.io.InputStream;
3637
import java.io.InputStreamReader;
@@ -292,7 +293,7 @@ protected JSONObject multipleQueries(List<IndexQuery> queries, String strategy)
292293
* Custom batch
293294
*
294295
* @param actions the array of actions
295-
* @throws AlgoliaException
296+
* @throws AlgoliaException if the response is not valid json
296297
*/
297298
protected JSONObject batch(JSONArray actions) throws AlgoliaException {
298299
try {
@@ -308,6 +309,10 @@ private enum Method {
308309
GET, POST, PUT, DELETE
309310
}
310311

312+
protected byte[] getRequestRaw(String url, boolean search) throws AlgoliaException {
313+
return _requestRaw(Method.GET, url, null, readHostsArray, httpConnectTimeoutMS, search ? httpSearchTimeoutMS : httpSocketTimeoutMS);
314+
}
315+
311316
protected JSONObject getRequest(String url, boolean search) throws AlgoliaException {
312317
return _request(Method.GET, url, null, readHostsArray, httpConnectTimeoutMS, search ? httpSearchTimeoutMS : httpSocketTimeoutMS);
313318
}
@@ -329,7 +334,7 @@ protected JSONObject putRequest(String url, String obj) throws AlgoliaException
329334
*
330335
* @param istream the InputStream to read
331336
* @return the stream's content
332-
* @throws IOException
337+
* @throws IOException if the stream can't be read or closed
333338
*/
334339
private String _getAnswer(InputStream istream) throws IOException {
335340
InputStreamReader is = new InputStreamReader(istream, "UTF-8");
@@ -348,7 +353,11 @@ private JSONObject _getJSONObject(String input) throws JSONException {
348353
return new JSONObject(new JSONTokener(input));
349354
}
350355

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 {
352361
return _getJSONObject(_getAnswer(istream));
353362
}
354363

@@ -362,9 +371,29 @@ private JSONObject _getAnswerObject(InputStream istream) throws IOException, JSO
362371
* @param connectTimeout maximum wait time to open connection
363372
* @param readTimeout maximum time to read data on socket
364373
* @return a JSONObject containing the resulting data or error
365-
* @throws AlgoliaException
374+
* @throws AlgoliaException if the request data is not valid json
366375
*/
367376
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 {
368397
String requestMethod;
369398
HashMap<String, String> errors = new HashMap<String, String>();
370399
// for each host
@@ -459,7 +488,7 @@ private synchronized JSONObject _request(Method m, String url, String json, List
459488
} else if (code / 100 == 4) {
460489
String message = "Error detected in backend";
461490
try {
462-
message = _getAnswerObject(stream).getString("message");
491+
message = _getAnswerJSONObject(stream).getString("message");
463492
} catch (IOException e) {
464493
addError(errors, host, e);
465494
continue;
@@ -481,13 +510,11 @@ private synchronized JSONObject _request(Method m, String url, String json, List
481510
try {
482511
String encoding = hostConnection.getContentEncoding();
483512
if (encoding != null && encoding.contains("gzip")) {
484-
return _getAnswerObject(new GZIPInputStream(stream));
513+
return _toByteArray(new GZIPInputStream(stream));
485514
}
486515
else {
487-
return _getAnswerObject(stream);
516+
return _toByteArray(stream);
488517
}
489-
} catch (JSONException e) {
490-
throw new AlgoliaException("JSON decode error:" + e.getMessage());
491518
} catch (IOException e) {
492519
throw new AlgoliaException("Data decoding error:" + e.getMessage());
493520
}
@@ -504,6 +531,23 @@ private synchronized JSONObject _request(Method m, String url, String json, List
504531
throw new AlgoliaException(builder.toString());
505532
}
506533

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+
507551
private void addError(HashMap<String, String> errors, String host, IOException e) {
508552
errors.put(host, String.format("%s=%s", e.getClass().getName(), e.getMessage()));
509553
}

algoliasearch/src/main/java/com/algolia/search/saas/BaseIndex.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ protected JSONObject addObject(JSONObject obj) throws AlgoliaException {
7474
/**
7575
* Add an object in this index
7676
*
77-
* @param obj the object to add.
78-
* @param objectID an objectID you want to attribute to this object
77+
* @param obj the object to add.
78+
* @param objectID an objectID you want to attribute to this object
7979
* (if the attribute already exist the old object will be overwrite)
8080
* @throws AlgoliaException
8181
*/
@@ -261,7 +261,7 @@ protected JSONObject saveObjects(JSONArray inputArray) throws AlgoliaException {
261261
}
262262

263263
/**
264-
* Delete an object from the index
264+
* Delete an object from the index
265265
*
266266
* @param objectID the unique identifier of object to delete
267267
* @throws AlgoliaException
@@ -330,7 +330,7 @@ protected void deleteByQuery(Query query) throws AlgoliaException {
330330

331331
/**
332332
* Search inside the index
333-
*
333+
* @return a JSONObject containing search results
334334
* @throws AlgoliaException
335335
*/
336336
protected JSONObject search(Query query) throws AlgoliaException {
@@ -342,7 +342,20 @@ protected JSONObject search(Query query) throws AlgoliaException {
342342
}
343343

344344
/**
345-
* Wait the publication of a task on the server.
345+
* Search inside the index
346+
* @return a byte array containing search results
347+
* @throws AlgoliaException
348+
*/
349+
protected byte[] searchRaw(Query query) throws AlgoliaException {
350+
String paramsString = query.getQueryString();
351+
if (paramsString.length() > 0)
352+
return client.getRequestRaw("/1/indexes/" + encodedIndexName + "?" + paramsString, true);
353+
else
354+
return client.getRequestRaw("/1/indexes/" + encodedIndexName, true);
355+
}
356+
357+
/**
358+
* Wait the publication of a task on the server.
346359
* All server task are asynchronous and you can check with this method that the task is published.
347360
*
348361
* @param taskID the id of the task returned by server

0 commit comments

Comments
 (0)