Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Neo4j Server Extension for Single Page Experiments

DZone's Guide to

Neo4j Server Extension for Single Page Experiments

· Database Zone
Free Resource

Running out of memory? Learn how Redis Enterprise enables large dataset analysis with the highest throughput and lowest latency while reducing costs over 75%! 


Sometimes you have a nice dataset in Neo4j and you’d want to provide a self-contained way of quickly exposing it to the outside world without a multi-tier setup.

So for experiments and proofs of concepts it would be helpful to be able to extend Neo4j Browser to accomodate new types of frames and commands.

Unfortunately we’re not there yet, there is still some work to be done until this will be possible.

Until then …. why not use what we already have.

I was discussing some helpful database and server extensions which would benefit from a tiny built-in UI with different people.

Then I had the idea to just use the JAX-RS mechanisms that Neo4j Server supports to not only serve JSON/text or XML but also deliver HTML, JS, CSS and image files to the browser.

Those files would not live on the file-system but be packaged directly into the jar of the extension, e.g. residing in a resources/webapp folder.

How it Works

This was actually much easier than expected.
This is a normal JAX-RS resource class that can then be mounted on an endpoint using the neo4j-server.properties configuration.

The HTTP-GET endpoint handles certain patterns declared by a regular expression.
There is one function that tries to find that file within the webapp folder within the JAR classpath, returning null if not found or the InputStream otherwise.
And one function for determining the content-type to be returned.

@Path("/")
public class StaticWebResource {

    @GET
    @Path("{file:(?i).+\\.(png|jpg|jpeg|svg|gif|html?|js|css|txt)}")
    public Response file(@PathParam("file") String file) throws IOException {
        InputStream fileStream = findFileStream(file);
        if (fileStream == null) return Response.status(Response.Status.NOT_FOUND).build();
        else return Response.ok(fileStream, mediaType(file)).build();
    }

    private InputStream findFileStream(String file) throws IOException {
        URL fileUrl = ClassLoader.getSystemResource("webapp/" + file);
        if (fileUrl==null) return null;
        return fileUrl.openStream();
    }

    public String mediaType(String file) {
        int dot = file.lastIndexOf(".");
        if (dot == -1) return MediaType.TEXT_PLAIN;
        String ext = file.substring(dot + 1).toLowerCase();
        switch (ext) {
            case "png":
                return "image/png";
            case "js":
                return "text/javascript";
            case "css":
                return "text/css";
            case "html":
                return MediaType.TEXT_HTML;
            default:
                return MediaType.TEXT_PLAIN;
        }
    }
}


You can easily use this approach for your own Neo4j extension just by copying that StaticWebResource into your project and providing the html,js and css files in the webapp directory.

The Demo: Popoto.js

As my demo I used a setup that exposes popoto.js automatcially on top of the data you have in your graph.

The StaticWebResource provides the web-files of the visualization from the resources/webapp directory.
And PopotoResource adds a second endpoint to provide a config/config.js file which uses label, property and index information
to provide the necessary config for popoto’s visualization.

Note that you have to disable auth for this demo as I haven’t added means for it to configure a username/password.

You can use the demo by cloning and building (mvn clean install) this repository.
Copy the resulting jar in the server’s plugin directory.

Edit conf/neo4j-server.properties to register the package name with an endpoint.

cp target/neo4j-web-extension-2.2-SNAPSHOT.jar /path/to/neo/plugins/
echo 'org.neo4j.server.thirdparty_jaxrs_classes=extension.web=/popoto' >>  /path/to/neo/conf/neo4j-server.properties
/path/to/neo/bin/neo4j restart
open http://localhost:7474/popoto


popoto in neo demo


You can also download the JAR from here.

Enjoy exploring!


Running out of memory? Never run out of memory with Redis Enterprise databaseStart your free trial today.

Topics:
database ,nosql ,server extension

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}