El jueves, 18 de junio de 2015, 7:49:55 (UTC+2), Michael Hunger escribió: > > And also put it please up as a github gist or repository > > Thanks!! > > Von meinem iPhone gesendet > > Am 18.06.2015 um 03:13 schrieb José Álvarez de Lara <[email protected] > <javascript:>>: > > > > El jueves, 18 de junio de 2015, 0:23:31 (UTC+2), Michael Hunger escribió: >> >> 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/" 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, >> >> > > > Hi Michael, > > Thank you for to be so kind. > > I will publish it in WordPress where I have opened an account. > > This time I will include the main method to finish completing the blog > post. > > Kind regards, > José > >> ... > > Here is the blog post I posted. > > http://neo4j-and-java-rest-api-auth.blogspot.com.es/p/how-to-use-rest-api-from-java-when-auth.html
> Finally I decided to Google Blogger because I did not see the way of how >> to use SyntaxHighlighter in WordPress. > > Cheers > -- > 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] <javascript:>. > For more options, visit 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.
