4.7. Unmanaged Extensions

Some projects want extremely fine control over their server-side code. For this we’ve introduced an unmanaged extension API. It’s a sharp tool, allowing users to deploy arbitrary JAX-RS classes to the server and so you should be careful when thinking about using this. In particular you should understand that it’s easy to consume lots of heap space on the server and hinder performance if you’re not careful. Still, if you understand the disclaimer, then you load your JAX-RS classes into the Neo4j server simply by adding adding a @Context annotation to your code, compiling against the Neo4j server jar, and then adding your classes to the runtime classpath (just drop it in the lib directory of the Neo4j server). In return you get access to the hosted environment of the Neo4j server like logging through the org.neo4j.server.logging.Logger.

In your code, you get access to the underlying GraphDatabaseService through the @Context annotation like so:

public MyCoolService(@Context GraphDatabaseService database)
{
  // Have fun here, but be safe!
}

Remember, the unmanaged API is a very sharp tool. It’s all to easy to compromise the server by deploying code this way, so think first and see if you can’t use the managed extensions in preference. However, a number of context parameters can be automatically provided for you, like the reference to the database.

In order to specify the mount point of your extension, a full class looks like

@Path( "/helloworld" )
public class HelloWorldResource
{

    private final GraphDatabaseService database;

    public HelloWorldResource( @Context GraphDatabaseService database)
    {
      this.database = database;
    }

    @GET
    @Produces( MediaType.TEXT_PLAIN )
    @Path( "/{nodeId}" )
    public Response hello( @PathParam( "nodeId" ) long nodeId )
    {
        // Do stuff with the database
        return Response.status( Status.OK ).entity(
                ( "Hello World, nodeId=" + nodeId).getBytes() ).build();
    }
}

Build this code, and place the resulting jar file (and any custom dependencies) into the $NEO4J_SERVER_HOME/plugins directory, and include this class in neo4j-server.propertie, like so:

#Comma separated list of JAXRS packages containing JAXRS Resource, one package name for each mountpoint.
org.neo4j.server.thirdparty_jaxrs_classes=org.neo4j.examples.server.unmanaged=/examples/unmanaged

curl http://localhost:7474/examples/unmanaged/helloworld/123

which results in

Hello World, nodeId=123