From ac0d119a8fdc7f9615839c789674d1873f525b34 Mon Sep 17 00:00:00 2001 From: a-brandt Date: Thu, 21 Jan 2016 15:55:41 +0100 Subject: [PATCH] added createDocumentRaw(...) and getDocumentRaw(...). --- ChangeLog | 5 +- pom.xml | 14 +- src/main/java/com/arangodb/ArangoDriver.java | 47 ++++++ .../com/arangodb/InternalDocumentDriver.java | 4 +- .../impl/InternalDocumentDriverImpl.java | 17 +- .../document/DocumentExamplesTestSuite.java | 4 +- .../example/document/RawDocumentExample.java | 146 ++++++++++++++++++ 7 files changed, 226 insertions(+), 11 deletions(-) create mode 100644 src/test/java/com/arangodb/example/document/RawDocumentExample.java diff --git a/ChangeLog b/ChangeLog index 2b7e2d62d..0f8c0f448 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,8 +1,9 @@ -c2.7.1 (????-??-??) +v2.7.1 (2016-01-21) --------------------------- * added examples for new AQL traversal functions (since ArangoDB 2.8) * added AQL warnings to CursorResult (hasWarning() and getWarnings()) - +* added createDocumentRaw(...) and getDocumentRaw(...). Examples src/test/java/com/arangodb/example/document/RawDocumentExample.java +* Updated dependencies gson (2.5), httpclient (4.5.1) and slf4j-api (1.7.13) v2.7.0 (2015-11-20) --------------------------- diff --git a/pom.xml b/pom.xml index f2129aa94..497f84b40 100644 --- a/pom.xml +++ b/pom.xml @@ -198,9 +198,9 @@ UTF-8 4.0 - 2.3.1 - 4.4.1 - 1.7.7 + 2.5 + 4.5.1 + 1.7.13 1.1.3 1.3 4.12 @@ -246,6 +246,14 @@ ${hamcrest-all.version} test + + org.json + json + 20151123 + test + + + diff --git a/src/main/java/com/arangodb/ArangoDriver.java b/src/main/java/com/arangodb/ArangoDriver.java index 7a6a3251b..747a90ac9 100644 --- a/src/main/java/com/arangodb/ArangoDriver.java +++ b/src/main/java/com/arangodb/ArangoDriver.java @@ -5765,4 +5765,51 @@ public DefaultEntity killQuery(String database, String id) throws ArangoExceptio public HttpManager getHttpManager() { return httpManager; } + + /** + * Creates a document in the collection defined by the collection's name + * + * @param collectionName + * The name of the collection + * @param rawJsonString + * A string containing a JSON object + * @param createCollection + * if set to true the collection is created if it does not exist + * @param waitForSync + * if set to true the response is returned when the server has + * finished. + * @return DocumentEntity + * @throws ArangoException + */ + public DocumentEntity createDocumentRaw( + String collectionName, + String rawJsonString, + Boolean createCollection, + Boolean waitForSync) throws ArangoException { + return documentDriver.createDocumentRaw(getDefaultDatabase(), collectionName, rawJsonString, createCollection, + waitForSync); + } + + /** + * Returns the document as a JSON string. Note that the + * *ifNoneMatchRevision* and *ifMatchRevision* can not be used at the same + * time, one of these two has to be null. + * + * @param documentHandle + * The document handle + * @param ifNoneMatchRevision + * if set the document is only returned id it has a different + * revision. + * @param ifMatchRevision + * if set the document is only returned id it has the same + * revision. + * @return a String + * @throws ArangoException + */ + public String getDocumentRaw(String documentHandle, Long ifNoneMatchRevision, Long ifMatchRevision) + throws ArangoException { + return documentDriver.getDocumentRaw(getDefaultDatabase(), documentHandle, ifNoneMatchRevision, + ifMatchRevision); + } + } diff --git a/src/main/java/com/arangodb/InternalDocumentDriver.java b/src/main/java/com/arangodb/InternalDocumentDriver.java index eab1eccf2..bbe83e706 100644 --- a/src/main/java/com/arangodb/InternalDocumentDriver.java +++ b/src/main/java/com/arangodb/InternalDocumentDriver.java @@ -21,7 +21,6 @@ DocumentEntity createDocument( DocumentEntity createDocumentRaw( String database, String collectionName, - String documentKey, String rawJsonString, Boolean createCollection, Boolean waitForSync) throws ArangoException; @@ -54,6 +53,9 @@ DocumentEntity getDocument( Long ifNoneMatchRevision, Long ifMatchRevision) throws ArangoException; + String getDocumentRaw(String database, String documentHandle, Long ifNoneMatchRevision, Long ifMatchRevision) + throws ArangoException; + DocumentEntity deleteDocument(String database, String documentHandle, Long rev, Policy policy) throws ArangoException; } diff --git a/src/main/java/com/arangodb/impl/InternalDocumentDriverImpl.java b/src/main/java/com/arangodb/impl/InternalDocumentDriverImpl.java index 4e914b0dd..5c62c8e52 100644 --- a/src/main/java/com/arangodb/impl/InternalDocumentDriverImpl.java +++ b/src/main/java/com/arangodb/impl/InternalDocumentDriverImpl.java @@ -100,11 +100,10 @@ public DocumentEntity createDocument( public DocumentEntity createDocumentRaw( String database, String collectionName, - String documentKey, - String rawJsonString, + String rawJsonObjectString, Boolean createCollection, Boolean waitForSync) throws ArangoException { - return _createDocument(database, collectionName, documentKey, rawJsonString, createCollection, waitForSync, + return _createDocument(database, collectionName, null, rawJsonObjectString, createCollection, waitForSync, true); } @@ -215,6 +214,18 @@ public DocumentEntity getDocument( return entity; } + @Override + public String getDocumentRaw(String database, String documentHandle, Long ifNoneMatchRevision, Long ifMatchRevision) + throws ArangoException { + + validateDocumentHandle(documentHandle); + HttpResponseEntity res = httpManager.doGet(createEndpointUrl(database, "/_api/document", documentHandle), + new MapBuilder().put("If-None-Match", ifNoneMatchRevision, true).put("If-Match", ifMatchRevision).get(), + null); + + return res.getText(); + } + @Override public DocumentEntity deleteDocument(String database, String documentHandle, Long rev, Policy policy) throws ArangoException { diff --git a/src/test/java/com/arangodb/example/document/DocumentExamplesTestSuite.java b/src/test/java/com/arangodb/example/document/DocumentExamplesTestSuite.java index e0573a3b9..3ba68efad 100644 --- a/src/test/java/com/arangodb/example/document/DocumentExamplesTestSuite.java +++ b/src/test/java/com/arangodb/example/document/DocumentExamplesTestSuite.java @@ -45,9 +45,9 @@ SimplePersonAqlQueryWithLimitExample.class, - AqlQueryWithSpecialReturnTypesExample.class + AqlQueryWithSpecialReturnTypesExample.class, -}) + RawDocumentExample.class }) public class DocumentExamplesTestSuite { diff --git a/src/test/java/com/arangodb/example/document/RawDocumentExample.java b/src/test/java/com/arangodb/example/document/RawDocumentExample.java new file mode 100644 index 000000000..e2d01100a --- /dev/null +++ b/src/test/java/com/arangodb/example/document/RawDocumentExample.java @@ -0,0 +1,146 @@ +/* + * Copyright (C) 2015 ArangoDB GmbH + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.arangodb.example.document; + +import org.json.JSONML; +import org.json.JSONObject; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.arangodb.ArangoDriver; +import com.arangodb.ArangoException; +import com.arangodb.entity.DocumentEntity; + +public class RawDocumentExample extends BaseExample { + + private static final String DATABASE_NAME = "RawDocument"; + + private static final String COLLECTION_NAME = "RawDocument"; + + public ArangoDriver arangoDriver; + + @Before + public void _before() { + removeTestDatabase(DATABASE_NAME); + + arangoDriver = getArangoDriver(getConfiguration()); + createDatabase(arangoDriver, DATABASE_NAME); + createCollection(arangoDriver, COLLECTION_NAME); + } + + @Test + public void ReadDocuments() { + // + // You can find the ArangoDB Web interface here: + // http://127.0.0.1:8529/ + // + // change the log level to "debug" in /src/test/resource/logback.xml to + // see the HTTP communication + + // + printHeadline("create example document 1"); + // + + String documentHandle1 = null; + String documentHandle2 = null; + String documentHandle3 = null; + + String x = "{\"test\":123}"; + try { + DocumentEntity entity = arangoDriver.createDocumentRaw(COLLECTION_NAME, x, true, false); + // the DocumentEntity contains the key, document handle and revision + System.out.println("Key: " + entity.getDocumentKey()); + System.out.println("Id: " + entity.getDocumentHandle()); + System.out.println("Revision: " + entity.getDocumentRevision()); + documentHandle1 = entity.getDocumentHandle(); + } catch (ArangoException e) { + Assert.fail("Failed to create document. " + e.getMessage()); + } + + // + printHeadline("read example document 1"); + // + + try { + String str = arangoDriver.getDocumentRaw(documentHandle1, null, null); + System.out.println("value: " + str); + } catch (ArangoException e) { + Assert.fail("Failed to read document. " + e.getMessage()); + } + + // + printHeadline("create example document 2 with key"); + // + + x = "{\"_key\":\"key2\",\"test\":123}"; + try { + DocumentEntity entity = arangoDriver.createDocumentRaw(COLLECTION_NAME, x, true, false); + // the DocumentEntity contains the key, document handle and revision + System.out.println("Key: " + entity.getDocumentKey()); + System.out.println("Id: " + entity.getDocumentHandle()); + System.out.println("Revision: " + entity.getDocumentRevision()); + documentHandle2 = entity.getDocumentHandle(); + } catch (ArangoException e) { + Assert.fail("Failed to create document. " + e.getMessage()); + } + + // + printHeadline("read example document 2"); + // + + try { + String str = arangoDriver.getDocumentRaw(documentHandle2, null, null); + System.out.println("value: " + str); + } catch (ArangoException e) { + Assert.fail("Failed to read document. " + e.getMessage()); + } + + // + printHeadline("using org.json.JSONML to save a xml file"); + // + String string = " Basic bread Flour Yeast Water Salt Mix all ingredients together. Knead thoroughly. Cover with a cloth, and leave for one hour in warm room. Knead again. Place in a bread baking tin. Cover with a cloth, and leave for one hour in warm room. Bake in the oven at 180(degrees)C for 30 minutes. "; + System.out.println("Orig XML value: " + string); + JSONObject jsonObject = JSONML.toJSONObject(string); + try { + DocumentEntity entity = arangoDriver.createDocumentRaw(COLLECTION_NAME, jsonObject.toString(), true, + false); + // the DocumentEntity contains the key, document handle and revision + System.out.println("Key: " + entity.getDocumentKey()); + System.out.println("Id: " + entity.getDocumentHandle()); + System.out.println("Revision: " + entity.getDocumentRevision()); + documentHandle3 = entity.getDocumentHandle(); + } catch (ArangoException e) { + Assert.fail("Failed to create document. " + e.getMessage()); + } + + // + printHeadline("read example and convert it back to XML"); + // + + try { + String str = arangoDriver.getDocumentRaw(documentHandle3, null, null); + System.out.println("JSON value: " + str); + JSONObject jsonObject2 = new JSONObject(str); + System.out.println("XML value: " + JSONML.toString(jsonObject2)); + } catch (ArangoException e) { + Assert.fail("Failed to read document. " + e.getMessage()); + } + + } + +}