Hi Jose, thanks so much for your investigative work.
Would you mind writing this up as a blog post? And publish it, then we can help you promote it too. Cheers, Michael > Am 17.06.2015 um 00:30 schrieb José Álvarez de Lara <[email protected]>: > > Based on "The Neo4j v2.3.0-M01 Manual" manual under "7.1. How to use the REST > API from Java" > I have tried to develop a similar code and using the latest Jersey library to > date is 2.18. > > > Authentication would take place the following code snippet, > > private static WebTarget testDatabaseAuthentication() > { > // START SNIPPET: testAuthentication > Client client = ClientBuilder.newClient(); > HttpAuthenticationFeature authFeature = > HttpAuthenticationFeature.basic(username, password); > client.register(authFeature); WebTarget target = > client.target(SERVER_ROOT_URI); > > Response response = target > .request() > .header("application/xml", "true") > .get(); > > String entity = response.readEntity(String.class); > System.out.println( String.format( > "GET, status code [%d], returned data: " > + System.getProperty( "line.separator" ) + "%s", > response.getStatus(), entity ) ); response.close(); > return target; > // END SNIPPET: testAuthentication > }Introducir código aquí... > > being the username and password those used to log onto the Neo4j server. > > With the authentication method I recover the 'target' object of the WebTarget > class and keep it throughout the whole program so as I do not to need to login > in each of the different and consecutive methods again. > > I added a helper method to extract the path of each node that then I will use > in the different methods. The code snippet is as follows, > > private static String extractPathFromNode( URI node ) { > String auxUri = node.toString(); > return auxUri.replace(SERVER_ROOT_URI, ""); > }Introducir código aquí... > > and what I do is to recover the resulting part of removing the > SERVER_ROOT_URI root > (the root "http://localhost:7474/db/data/ <http://localhost:7474/db/data/>" > is what I used to login) which is the > 'target' object now. > > The rest of the methods will use the 'target' object with the path extracted > from > the nodes or properties. I supply now the code snippet of each one, > > > private static void sendTransactionalCypherQuery( WebTarget target, > String query ) { > // START SNIPPET: queryAllNodes > final String txUri = "transaction/commit"; > //WebResource resource = Client.create().resource( txUri ); > String payload = "{\"statements\" : [ {\"statement\" : \"" + query + "\"} ]}"; > > Response response = target > .path(txUri) > .request(MediaType.APPLICATION_JSON) > .header("application/xml", "true") > .accept(MediaType.APPLICATION_JSON) > .post(Entity.entity(payload, > MediaType.APPLICATION_JSON_TYPE)); > > String entity = response.readEntity(String.class); > System.out.println( String.format( > "GET, status code [%d], returned data: " > + System.getProperty( "line.separator" ) + "%s", > response.getStatus(), entity ) ); response.close(); > // END SNIPPET: queryAllNodes > } > > private static void findSingersInBands( WebTarget target, URI startNode ) > //throws URISyntaxException > { > // START SNIPPET: traversalDesc > // TraversalDefinition turns into JSON to send to the Server > TraversalDefinition t = new TraversalDefinition(); > t.setOrder( TraversalDefinition.DEPTH_FIRST ); > t.setUniqueness( TraversalDefinition.NODE ); > t.setMaxDepth( 10 ); > t.setReturnFilter( TraversalDefinition.ALL ); > t.setRelationships( new Relation( "singer", Relation.OUT ) ); > // END SNIPPET: traversalDesc // START SNIPPET: traverse > //URI traverserUri = new URI( startNode.toString() + "/traverse/node" > ); > String auxPath = extractPathFromNode( startNode ); > String traverserUri = auxPath + "/traverse/node"; > String jsonTraverserPayload = t.toJson(); > > // POST {} to the node entry point URI > Response response = target > .path(traverserUri) > .request(MediaType.APPLICATION_JSON) > .header("application/xml", "true") > .accept(MediaType.APPLICATION_JSON) > .post(Entity.entity(jsonTraverserPayload, > MediaType.APPLICATION_JSON_TYPE)); > > System.out.println( String.format( > "POST [%s] to [%s], status code [%d], returned data: " > + System.getProperty( "line.separator" ) + "%s", > jsonTraverserPayload, traverserUri, response.getStatus(), > response.getEntity().toString() ) ); > > response.close(); > // END SNIPPET: traverse > } > > // START SNIPPET: insideAddMetaToProp > private static void addMetadataToProperty( WebTarget target, URI > relationshipUri, > String name, String value ) //throws URISyntaxException > { > //URI propertyUri = new URI( relationshipUri.toString() + > "/properties" ); > String auxPath = extractPathFromNode( relationshipUri ); > String propertyUri = auxPath + "/properties"; > String entity = toJsonNameValuePairCollection( name, value ); > > Response response = target > .path(propertyUri) > .request(MediaType.APPLICATION_JSON) > .header("application/xml", "true") > .accept(MediaType.APPLICATION_JSON) > .put(Entity.entity(entity, MediaType.APPLICATION_JSON_TYPE)); > > System.out.println( String.format( > "PUT [%s] to [%s], status code [%d]", entity, propertyUri, > response.getStatus() ) ); > > response.close(); > } > // END SNIPPET: insideAddMetaToProp > > private static String toJsonNameValuePairCollection( String name, > String value ) > { > return String.format( "{ \"%s\" : \"%s\" }", name, value ); > } > > private static URI createNode( WebTarget target ) > { > // START SNIPPET: createNode > final String nodeEntryPointUri = "node"; > // http://localhost:7474/db/data/node > <http://localhost:7474/db/data/node> // POST {} to the node entry > point URI > Response response = target > .path(nodeEntryPointUri) > .request(MediaType.APPLICATION_JSON) > .header("application/xml", "true") > .accept(MediaType.APPLICATION_JSON) > .post(Entity.entity("{}", MediaType.APPLICATION_JSON_TYPE)); > final URI location = response.getLocation(); > System.out.println( String.format( > "POST to [%s], status code [%d], location header [%s]", > nodeEntryPointUri, response.getStatus(), location.toString() ) ); > > response.close(); return location; > // END SNIPPET: createNode > } > > // START SNIPPET: insideAddRel > private static URI addRelationship( WebTarget target, URI startNode, URI > endNode, > String relationshipType, String jsonAttributes ) > //throws URISyntaxException > { > //URI fromUri = new URI( startNode.toString() + "/relationships" ); > String auxPath = extractPathFromNode( startNode ); > String fromUri = auxPath + "/relationships"; > String relationshipJson = generateJsonRelationship( endNode, > relationshipType, jsonAttributes ); // POST {} to the > node entry point URI > Response response = target > .path(fromUri) > .request(MediaType.APPLICATION_JSON) > .header("application/xml", "true") > .accept(MediaType.APPLICATION_JSON) > .post(Entity.entity(relationshipJson, > MediaType.APPLICATION_JSON_TYPE)); > > final URI location = response.getLocation(); > System.out.println( String.format( > "POST to [%s], status code [%d], location header [%s]", > fromUri, response.getStatus(), location.toString() ) ); > response.close(); > return location; > } > // END SNIPPET: insideAddRel > > private static String generateJsonRelationship( URI endNode, > String relationshipType, String... jsonAttributes ) > { > StringBuilder sb = new StringBuilder(); > sb.append( "{ \"to\" : \"" ); > sb.append( endNode.toString() ); > sb.append( "\", " ); sb.append( "\"type\" : \"" ); > sb.append( relationshipType ); > if ( jsonAttributes == null || jsonAttributes.length < 1 ) > { > sb.append( "\"" ); > } > else > { > sb.append( "\", \"data\" : " ); > for ( int i = 0; i < jsonAttributes.length; i++ ) > { > sb.append( jsonAttributes[i] ); > if ( i < jsonAttributes.length - 1 ) > { // Miss off the final comma > sb.append( ", " ); > } > } > } sb.append( " }" ); > return sb.toString(); > } > > private static void addProperty( WebTarget target, URI nodeUri, String > propertyName, > String propertyValue ) > { > // START SNIPPET: addProp > String auxPath = extractPathFromNode( nodeUri ); > String propertyUri = auxPath + "/properties/" + propertyName; > // > http://localhost:7474/db/data/node/{node_id}/properties/{property_name > <http://localhost:7474/db/data/node/%7Bnode_id%7D/properties/%7Bproperty_name>} > Response response = target > .path(propertyUri) > .request(MediaType.APPLICATION_JSON) > .header("application/xml", "true") > .accept(MediaType.APPLICATION_JSON) > .put(Entity.entity("\"" + propertyValue + "\"", > MediaType.APPLICATION_JSON_TYPE)); > > System.out.println( String.format( "PUT to [%s], status code [%d]", > propertyUri, response.getStatus() ) ); > > response.close(); > // END SNIPPET: addProp > }Introducir código aquí... > > I hope this be helpful > > All the best, > Jose > > -- > You received this message because you are subscribed to the Google Groups > "Neo4j" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected] > <mailto:[email protected]>. > For more options, visit https://groups.google.com/d/optout > <https://groups.google.com/d/optout>. -- You received this message because you are subscribed to the Google Groups "Neo4j" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/d/optout.
