Databases Reference
In-Depth Information
@Path ( "/{name1}/{name2}" )
public String getDistance ( @PathParam ( "name1" ) String name1 ,
@PathParam ( "name2" ) String name2 )
{
String query = "START first=node:user(name={name1}),\n" +
"second=node:user(name={name2})\n" +
"MATCH p=shortestPath(first-[*..4]-second)\n" +
"RETURN length(p) AS depth" ;
Map < String , Object > params = new HashMap < String , Object >();
params . put ( "name1" , name1 );
params . put ( "name2" , name2 );
ExecutionResult result = executionEngine . execute ( query , params );
return String . valueOf ( result . columnAs ( "depth" ). next () );
}
}
Of particular interest here are the various annotations:
@Path("/distance") specifies that this extension will respond to requests directed
to relative URIs beginning /distance .
• The @Path("/{name1}/{name2}") annotation on getDistance() further qualifies
the URI template associated with this extension. The fragment here is concatenated
with /distance to produce /distance/{name1}/{name2} , where {name1} and {name2}
are placeholders for any characters occurring between the forward slashes. Later
on, in “Testing server extensions” on page 87 , we'll register this extension under
the /socnet relative URI. At that point, these several different parts of the path ensure
that HTTP requests directed to a relative URI beginning /socnet/distance/{name1}/
{name2} (for example, http://<server>/socnet/distance/Ben/Mike ) will be dispatch‐
ed to an instance of this extension.
@GET specifies that getDistance() should be invoked only if the request is an HTTP
GET. @Produces indicates that the response entity body will be formatted as text/
plain .
• The two @PathParam annotations prefacing the parameters to getDistance() serve
to map the contents of the {name1} and {name2} path placeholders to the method's
name1 and name2 parameters. Given the URI http://<server>/socnet/distance/Ben/
Mike , getDistance() will be invoked with Ben for name1 and Mike for name2 .
• The @Context annotation in the constructor causes this extension to be handed a
reference to the embedded graph database inside the server. The server infrastruc‐
ture takes care of creating an extension and injecting it with a graph database in‐
stance, but the very presence of the GraphDatabaseService parameter here makes
Search WWH ::




Custom Search