Skip to content

Commit 16c95c3

Browse files
author
Achim Brandt
committed
Merge pull request #30 from miguel-porto/master
Function to execute AQL query and get the raw JSON response from Arango server
2 parents d163f33 + 4552952 commit 16c95c3

File tree

5 files changed

+109
-34
lines changed

5 files changed

+109
-34
lines changed

.classpath

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@
1212
<attribute name="maven.pomderived" value="true"/>
1313
</attributes>
1414
</classpathentry>
15-
<classpathentry including="**/*.java" kind="src" path="src/test/resources"/>
15+
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
16+
<attributes>
17+
<attribute name="maven.pomderived" value="true"/>
18+
</attributes>
19+
</classpathentry>
1620
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
1721
<attributes>
1822
<attribute name="maven.pomderived" value="true"/>

src/main/java/com/arangodb/ArangoDriver.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2469,6 +2469,27 @@ public <T> CursorResult<T> executeAqlQuery(
24692469
return cursorDriver.executeAqlQuery(getDefaultDatabase(), query, bindVars, aqlQueryOptions, clazz);
24702470
}
24712471

2472+
/**
2473+
* Executes an AQL query and returns the raw JSON response
2474+
* @param query an AQL query as string
2475+
* @param bindVars a map containing all bind variables,
2476+
* @param aqlQueryOptions AQL query options
2477+
* @return A JSON string with the results from server
2478+
* @throws ArangoException
2479+
*/
2480+
public String executeAqlQueryJSON(
2481+
String query,
2482+
Map<String, Object> bindVars,
2483+
AqlQueryOptions aqlQueryOptions
2484+
) throws ArangoException {
2485+
2486+
if (aqlQueryOptions == null) {
2487+
aqlQueryOptions = getDefaultAqlQueryOptions();
2488+
}
2489+
2490+
return cursorDriver.executeAqlQueryJSON(getDefaultDatabase(), query, bindVars, aqlQueryOptions);
2491+
}
2492+
24722493
/**
24732494
* This method executes an AQL query and returns a DocumentCursorResult
24742495
*

src/main/java/com/arangodb/BaseArangoDriver.java

Lines changed: 66 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -170,39 +170,14 @@ protected ReplicationDumpHeader toReplicationDumpHeader(HttpResponseEntity res)
170170
}
171171

172172
/**
173-
* Creates an entity object
174-
*
175-
* @param res
176-
* the response of the database
177-
* @param clazz
178-
* the class of the entity object
179-
* @param pclazz
180-
* the class of the object wrapped in the entity object
181-
* @param validate
182-
* true for validation
183-
* @return the result entity object of class T (T extends BaseEntity)
184-
* @throws ArangoException
173+
* Checks the Http response for database or server errors
174+
* @param res the response of the database
175+
* @return The Http status code
176+
* @throws ArangoException if any error happened
185177
*/
186-
protected <T extends BaseEntity> T createEntity(
187-
HttpResponseEntity res,
188-
Class<T> clazz,
189-
Class<?>[] pclazz,
190-
boolean validate) throws ArangoException {
191-
if (res == null) {
192-
return null;
193-
}
194-
boolean isDocumentEntity = false;
195-
boolean requestSuccessful = true;
196-
// the following was added to ensure, that attributes with a key like
197-
// "error", "code", "errorNum"
198-
// and "etag" will be serialized, when no error was thrown by the
199-
// database
200-
if (clazz == DocumentEntity.class) {
201-
isDocumentEntity = true;
202-
}
178+
private int checkServerErrors(HttpResponseEntity res) throws ArangoException {
203179
int statusCode = res.getStatusCode();
204-
if (statusCode >= 400) {
205-
requestSuccessful = false;
180+
if (statusCode >= 400) { // always throws ArangoException
206181
DefaultEntity defaultEntity = new DefaultEntity();
207182
if (res.getText() != null && !res.getText().equalsIgnoreCase("") && statusCode != 500) {
208183
JsonParser jsonParser = new JsonParser();
@@ -263,7 +238,44 @@ protected <T extends BaseEntity> T createEntity(
263238
arangoException.setCode(statusCode);
264239
throw arangoException;
265240
}
266-
241+
242+
return statusCode;
243+
}
244+
/**
245+
* Creates an entity object
246+
*
247+
* @param res
248+
* the response of the database
249+
* @param clazz
250+
* the class of the entity object
251+
* @param pclazz
252+
* the class of the object wrapped in the entity object
253+
* @param validate
254+
* true for validation
255+
* @return the result entity object of class T (T extends BaseEntity)
256+
* @throws ArangoException
257+
*/
258+
protected <T extends BaseEntity> T createEntity(
259+
HttpResponseEntity res,
260+
Class<T> clazz,
261+
Class<?>[] pclazz,
262+
boolean validate) throws ArangoException {
263+
if (res == null) {
264+
return null;
265+
}
266+
boolean isDocumentEntity = false;
267+
//boolean requestSuccessful = true;
268+
269+
// the following was added to ensure, that attributes with a key like
270+
// "error", "code", "errorNum"
271+
// and "etag" will be serialized, when no error was thrown by the
272+
// database
273+
if (clazz == DocumentEntity.class) {
274+
isDocumentEntity = true;
275+
}
276+
277+
int statusCode=checkServerErrors(res);
278+
267279
try {
268280
EntityDeserializers.setParameterized(pclazz);
269281

@@ -283,7 +295,7 @@ protected <T extends BaseEntity> T createEntity(
283295
validate(res, entity);
284296
}
285297

286-
if (isDocumentEntity && requestSuccessful) {
298+
if (isDocumentEntity) { // && requestSuccessful NOTE: no need for this, an exception is always thrown
287299
entity.setCode(statusCode);
288300
entity.setErrorMessage(null);
289301
entity.setError(false);
@@ -295,6 +307,27 @@ protected <T extends BaseEntity> T createEntity(
295307
EntityDeserializers.removeParameterized();
296308
}
297309
}
310+
311+
/**
312+
* Gets the raw JSON string with results, from the Http response
313+
* @param res the response of the database
314+
* @return A valid JSON string with the results
315+
* @throws ArangoException
316+
*/
317+
protected String getJSONResponseText(HttpResponseEntity res) throws ArangoException {
318+
if (res == null) {
319+
return null;
320+
}
321+
322+
checkServerErrors(res);
323+
324+
// no errors, return results as a JSON string
325+
JsonParser jsonParser = new JsonParser();
326+
JsonElement jsonElement = jsonParser.parse(res.getText());
327+
JsonObject jsonObject = jsonElement.getAsJsonObject();
328+
JsonElement result = jsonObject.get("result");
329+
return result.toString();
330+
}
298331

299332
protected <T> T createEntity(String str, Class<T> clazz, Class<?>... pclazz) throws ArangoException {
300333
try {

src/main/java/com/arangodb/InternalCursorDriver.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ <T> CursorResult<T> executeAqlQuery(
4040
AqlQueryOptions aqlQueryOptions,
4141
Class<T> clazz) throws ArangoException;
4242

43+
// return the raw JSON response from server
44+
String executeAqlQueryJSON(
45+
String database,
46+
String query,
47+
Map<String, Object> bindVars,
48+
AqlQueryOptions aqlQueryOptions) throws ArangoException;
49+
4350
// request a cursor with DocumentEntity
4451
<T, S extends DocumentEntity<T>> DocumentCursorResult<T, S> executeBaseCursorQuery(
4552
String database,

src/main/java/com/arangodb/impl/InternalCursorDriverImpl.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,16 @@ public CursorEntity<?> validateQuery(String database, String query) throws Arang
5454
return createEntity(res, CursorEntity.class);
5555
}
5656

57+
@Override
58+
public String executeAqlQueryJSON(
59+
String database,
60+
String query,
61+
Map<String, Object> bindVars,
62+
AqlQueryOptions aqlQueryOptions) throws ArangoException {
63+
64+
return getJSONResponseText(getCursor(database, query, bindVars, aqlQueryOptions));
65+
}
66+
5767
@SuppressWarnings("unchecked")
5868
@Override
5969
public <T> CursorEntity<T> executeCursorEntityQuery(

0 commit comments

Comments
 (0)