Skip to content

Commit

Permalink
Added UserAgent Support
Browse files Browse the repository at this point in the history
Better Error Handling for Batch-Mode
Index creation incorrect in batch-mode
  • Loading branch information
jexp committed Aug 14, 2012
1 parent da16e45 commit 1339c90
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 19 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

2 changes: 1 addition & 1 deletion src/main/java/org/neo4j/rest/graphdb/ExecutingRestAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 13 additions & 8 deletions src/main/java/org/neo4j/rest/graphdb/ExecutingRestRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand All @@ -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 ) );
Expand Down Expand Up @@ -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 ) {
Expand Down Expand Up @@ -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 ) {
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/org/neo4j/rest/graphdb/RestGraphDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
16 changes: 9 additions & 7 deletions src/main/java/org/neo4j/rest/graphdb/UserAgent.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -63,8 +69,4 @@ public ClientResponse handle(ClientRequest cr) throws ClientHandlerException {
}
});
}

public void setUserAgent(String newValue) {
this.userAgent=newValue;
}
}
3 changes: 3 additions & 0 deletions src/main/java/org/neo4j/rest/graphdb/batch/BatchRestAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,9 @@ protected Map<Long, Object> convertRequestResultToEntities(RestOperations operat
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -74,6 +77,46 @@ public void testCreateRelationshipToNodeOutsideofBatch() throws Exception {
assertEquals("foo", getGraphDatabase().getRelationshipById(relationship.getId()).getType().name());
}

@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();
Expand Down
7 changes: 7 additions & 0 deletions src/test/java/org/neo4j/rest/graphdb/RestAPITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 1339c90

Please sign in to comment.