Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added UserAgent Support

Better Error Handling for Batch-Mode
Index creation incorrect in batch-mode
  • Loading branch information...
commit 1339c90245b1f3ade8253e7f5cd2b95f7372fa31 1 parent da16e45
@jexp jexp authored
View
12 README.md
@@ -59,3 +59,15 @@ References / Community:
* [Neo4j REST API](http://docs.neo4j.org/chunked/milestone/rest-api.html)
* [Neo4j Docs](http://docs.neo4j.org)
* [Neo4j Mailing List](http://neo4j.org/forums)
+
+
+Configuration (System-Properties)
+-------------
+
+_timeouts in seconds_
+
+* org.neo4j.rest.read_timeout=30
+* org.neo4j.rest.connect_timeout=30
+* org.neo4j.rest.driver="neo4j-rest-graphdb/1.8M07"
+* org.neo4j.rest.stream=true
+
View
2  src/main/java/org/neo4j/rest/graphdb/ExecutingRestAPI.java
@@ -81,7 +81,7 @@ protected RestRequest createRestRequest(String uri, String user, String password
@Override
public RestIndexManager index() {
- return new RestIndexManager(this);
+ return new RestIndexManager(facade);
}
@Override
View
21 src/main/java/org/neo4j/rest/graphdb/ExecutingRestRequest.java
@@ -24,7 +24,6 @@
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.util.Map;
-import java.util.Properties;
import java.util.concurrent.TimeUnit;
import javax.ws.rs.core.MediaType;
@@ -43,8 +42,16 @@
public class ExecutingRestRequest implements RestRequest {
- public static final int CONNECT_TIMEOUT = (int) TimeUnit.SECONDS.toMillis(30);
- public static final int READ_TIMEOUT = (int) TimeUnit.SECONDS.toMillis(30);
+ public final static String CONFIG_PREFIX = "org.neo4j.rest.";
+
+ public static final int CONNECT_TIMEOUT = getTimeout("connect_timeout", 30);
+ public static final int READ_TIMEOUT = getTimeout("read_timeout", 30);
+ public static final boolean STREAM_ENABLED = Boolean.parseBoolean(System.getProperty(CONFIG_PREFIX+"stream","true"));
+
+ private static int getTimeout(final String param, final int defaultValue) {
+ return (int) TimeUnit.SECONDS.toMillis(Integer.parseInt(System.getProperty(CONFIG_PREFIX + param, "" + defaultValue)));
+ }
+
public static final MediaType STREAMING_JSON_TYPE = new MediaType(APPLICATION_JSON_TYPE.getType(),APPLICATION_JSON_TYPE.getSubtype(), MapUtil.stringMap("stream","true"));
private final String baseUri;
private final UserAgent userAgent = new UserAgent();
@@ -61,9 +68,6 @@ public ExecutingRestRequest( String baseUri, String username, String password )
}
- public void setUserAgent(String newValue) {
- userAgent.setUserAgent(newValue);
- }
protected void addAuthFilter(String username, String password) {
if (username == null) return;
client.addFilter( new HTTPBasicAuthFilter( username, password ) );
@@ -99,7 +103,8 @@ public static String encode( Object value ) {
private Builder builder( String path ) {
WebResource resource = client.resource( uri( pathOrAbsolute( path ) ) );
- return resource.accept(STREAMING_JSON_TYPE).header("X-Stream","true");
+ if (STREAM_ENABLED) return resource.accept(STREAMING_JSON_TYPE).header("X-Stream","true");
+ return resource.accept(APPLICATION_JSON_TYPE);
}
private String pathOrAbsolute( String path ) {
@@ -162,7 +167,7 @@ public RequestResult put( String path, Object data ) {
@Override
public RestRequest with( String uri ) {
- return new ExecutingRestRequest( uri , client );
+ return new ExecutingRestRequest(uri, client);
}
private URI uri( String uri ) {
View
3  src/main/java/org/neo4j/rest/graphdb/RestGraphDatabase.java
@@ -51,8 +51,7 @@ public RestGraphDatabase( String uri, String user, String password ) {
public RestAPI getRestAPI(){
return this.restAPI;
}
-
-
+
public RestIndexManager index() {
return this.restAPI.index();
}
View
8 src/main/java/org/neo4j/rest/graphdb/RestResultException.java
@@ -38,12 +38,18 @@ private static String format(Map<?, ?> result) {
sb.append(" ").append(line).append("\n");
}
}
+ if (result.containsKey("body")) sb.append(format(toMap(result.get("body"))));
return sb.toString();
}
public static boolean isExceptionResult(Object result) {
final Map<String, Object> map = toMap(result);
- return map!=null && map.containsKey("exception") && map.containsKey("message");
+ return map!=null && (hasErrorStatus(map) || map.containsKey("exception") || map.containsKey("message") || isExceptionResult(map.get("body")));
+ }
+
+ private static boolean hasErrorStatus(Map<String, Object> map) {
+ Object status = map.get("status");
+ return status != null && !status.toString().startsWith("2");
}
private static Map<String, Object> toMap(Object result) {
View
16 src/main/java/org/neo4j/rest/graphdb/UserAgent.java
@@ -33,11 +33,17 @@
* @since 10.08.12
*/
public class UserAgent {
- private String userAgent = determineUserAgent();
+ public static final String NEO4J_DRIVER_PROPERTY = "org.neo4j.rest.driver";
+
+ private final String userAgent = determineUserAgent();
private String determineUserAgent() {
- final Properties props = loadPomProperties();
- return String.format("%s/%s",props.getProperty("artifactId","neo4j-rest-graphdb"),props.getProperty("version","0"));
+ String property = System.getProperty(NEO4J_DRIVER_PROPERTY);
+ if (property == null || property.trim().isEmpty()) {
+ final Properties props = loadPomProperties();
+ return String.format("%s/%s", props.getProperty("artifactId", "neo4j-rest-graphdb"), props.getProperty("version", "0"));
+ }
+ return property;
}
private Properties loadPomProperties() {
@@ -63,8 +69,4 @@ public ClientResponse handle(ClientRequest cr) throws ClientHandlerException {
}
});
}
-
- public void setUserAgent(String newValue) {
- this.userAgent=newValue;
- }
}
View
3  src/main/java/org/neo4j/rest/graphdb/batch/BatchRestAPI.java
@@ -163,6 +163,9 @@ private boolean isNewEntity(RestEntity restEntity) {
Collection<Map<String, Object>> responseCollection = (Collection<Map<String, Object>>) result;
Map<Long, Object> mappedObjects = new HashMap<Long, Object>(responseCollection.size());
for (Map<String, Object> entry : responseCollection) {
+ if (RestResultException.isExceptionResult(entry)) {
+ throw new RestResultException(entry);
+ }
final Long batchId = getBatchId(entry);
final RequestResult subResult = RequestResult.extractFrom(entry);
RestOperations.RestOperation restOperation = operations.getOperation(batchId);
View
43 src/test/java/org/neo4j/rest/graphdb/BatchRestAPITransactionTest.java
@@ -27,8 +27,11 @@
import org.neo4j.graphdb.index.Index;
import org.neo4j.graphdb.index.IndexHits;
import org.neo4j.graphdb.index.IndexManager;
+import org.neo4j.helpers.collection.MapUtil;
+import org.neo4j.index.impl.lucene.LuceneIndexImplementation;
import org.neo4j.rest.graphdb.entity.RestNode;
import org.neo4j.rest.graphdb.entity.RestRelationship;
+import org.neo4j.rest.graphdb.index.RestIndex;
import org.neo4j.rest.graphdb.util.TestHelper;
import static org.junit.Assert.*;
@@ -75,6 +78,46 @@ public void testCreateRelationshipToNodeOutsideofBatch() throws Exception {
}
@Test
+ public void testCreateNodeAndAddToIndex() throws Exception {
+ RestIndex<Node> index = restAPI.createIndex(Node.class, "index", LuceneIndexImplementation.FULLTEXT_CONFIG);
+ final Transaction tx = restAPI.beginTx();
+
+ Node n1 = restAPI.createNode(map());
+ index.add(n1,"key","value");
+
+ tx.success();
+ tx.finish();
+ Node node = index.get("key", "value").getSingle();
+ assertEquals("created node found in index",n1,node);
+ }
+
+ @Test(expected = RestResultException.class)
+ public void testFailingDoubleDelete() throws Exception {
+ final Transaction tx = restAPI.beginTx();
+
+ Node n1 = restAPI.createNode(map());
+ n1.delete();
+ n1.delete();
+
+ tx.success();
+ tx.finish();
+ }
+
+ @Test(expected = RestResultException.class)
+ public void testFailingCreateNodeAndAddToIndex() throws Exception {
+ RestIndex<Node> index = restAPI.createIndex(Node.class, "index", MapUtil.stringMap(IndexManager.PROVIDER, "lucene", "type", "fulltext_ _"));
+ final Transaction tx = restAPI.beginTx();
+
+ Node n1 = restAPI.createNode(map());
+ index.add(n1, "key", "value");
+
+ tx.success();
+ tx.finish();
+ Node node = index.get("key", "value").getSingle();
+ assertEquals("created node found in index",n1,node);
+ }
+
+ @Test
public void testSetNodeProperties() {
final Transaction tx = restAPI.beginTx();
View
7 src/test/java/org/neo4j/rest/graphdb/RestAPITest.java
@@ -55,6 +55,13 @@ public void testUserAgent() throws Exception {
restAPI.createNode(map());
assertTrue(getUserAgent().matches("neo4j-rest-graphdb/[\\d.]+"));
}
+ @Test
+ public void testOverrideUserAgent() throws Exception {
+ System.setProperty(UserAgent.NEO4J_DRIVER_PROPERTY,"foo/bar");
+ new RestAPIFacade(restAPI.getBaseUri()).createNode(map());
+ assertTrue(getUserAgent().matches("foo/bar"));
+ System.setProperty(UserAgent.NEO4J_DRIVER_PROPERTY,"");
+ }
@Test
public void testCreateNodeWithParams() {
Please sign in to comment.
Something went wrong with that request. Please try again.