Server-plugins are an easy way to extend the Neo4j REST API with your own functionality. In order to compile your plugin, you should include the neo4j-server-xx.jar in your classpath, e.g. via pom.xml:
<dependency> <groupId>org.neo4j.app</groupId> <artifactId>neo4j-server</artifactId> <version>1.3-SNAPSHOT</version> </dependency>
from the neo4j repository at http://m2.neo4j.org/org/neo4j/app/neo4j-server/ or in the system/lib directory of the Neo4j Server distribution.
@Description( "An extension to the Neo4j Server for getting all nodes or relationships" )
public class GetAll extends ServerPlugin
{
@Name( "get_all_nodes" )
@Description( "Get all nodes from the Neo4j graph database" )
@PluginTarget( GraphDatabaseService.class )
public Iterable<Node> getAllNodes( @Source GraphDatabaseService graphDb )
{
return graphDb.getAllNodes();
}
@Description( "Get all relationships from the Neo4j graph database" )
@PluginTarget( GraphDatabaseService.class )
public Iterable<Relationship> getAllRelationships( @Source GraphDatabaseService graphDb )
{
return new NestingIterable<Relationship, Node>( graphDb.getAllNodes() )
{
@Override
protected Iterator<Relationship> createNestedIterator( Node item )
{
return item.getRelationships( Direction.OUTGOING ).iterator();
}
};
}
}
In order to let the Neo4j Server find your extension, you need to add the file /META-INF/services/org.neo4j.server.plugins.ServerPlugin to the resulting .jar file, containing the classname of your plugin as a single line per plugin:
org.neo4j.examples.server.plugins.GetAll
Now, place the myextension.jar (and any custom dependencies) into the $NEO$J_SERVER_HOME/plugins directory, start the server check out the database representation for the listing of your extension:
curl -v htto://localhost:7474/db/data
will get you
{
"extensions-info" : "http://localhost:7474/db/data/ext",
"node" : "http://localhost:7474/db/data/node",
"node-index" : "http://localhost:7474/db/data/index/node",
"relationship-index" : "http://localhost:7474/db/data/index/relationship",
"reference_node" : "http://localhost:7474/db/data/node/0",
"extensions" : {
"GetAll" : {
"get_all_nodes" : "http://localhost:7474/db/data/ext/GetAll/graphdb/get_all_nodes",
"getAllRelationships" : "http://localhost:7474/db/data/ext/GetAll/graphdb/getAllRelationships"
}
}Also, all extensions are listed upon a GET to `http://localhost:7474/db/data/ext/`
Requesting a GET on one of the two extension endpoints...
curl http://localhost:7474/db/data/ext/GetAll/graphdb/get_all_nodes
...gives back the meta information about the service:
{
"extends" : "graphdb",
"description" : "Get all nodes from the Neo4j graph database",
"name" : "get_all_nodes",
"parameters" : [ ]
}To use it, just POST to this URL, with parameters as specified in the description (none in this case).
Full source code: GetAll.java