DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Securing Your Software Supply Chain with JFrog and Azure
Register Today

Trending

  • What ChatGPT Needs Is Context
  • Operator Overloading in Java
  • RBAC With API Gateway and Open Policy Agent (OPA)
  • Mastering Time Series Analysis: Techniques, Models, and Strategies

Trending

  • What ChatGPT Needs Is Context
  • Operator Overloading in Java
  • RBAC With API Gateway and Open Policy Agent (OPA)
  • Mastering Time Series Analysis: Techniques, Models, and Strategies
  1. DZone
  2. Data Engineering
  3. Databases
  4. neo4j Unmanaged Extension: Creating gzipped streamed responses with Jetty

neo4j Unmanaged Extension: Creating gzipped streamed responses with Jetty

Mark Needham user avatar by
Mark Needham
·
Jul. 10, 13 · Interview
Like (0)
Save
Tweet
Share
5.92K Views

Join the DZone community and get the full member experience.

Join For Free

I recently wrote a blog post describing how we created a streamed response and the next thing we wanted to do was gzip the response to shrink it’s size a bit.

A bit of searching led to GZIPContentEncodingFilter popping up a lot of times but this is actually needed for a client processing a gripped response rather than helping us to gzip a response from the server.

I noticed that there was a question about this on the mailing list from about a year ago although Michaelpointed out that the repository has now moved and the example is available here instead.

I adapted the example a bit and ended up with the following lifecycle plugin:

package com.markandjim;
 
public class GZipInitialiser implements SPIPluginLifecycle {
    private WebServer webServer;
 
    @Override
    public Collection<Injectable<?>> start(NeoServer neoServer) {
        webServer = getWebServer(neoServer);
        GzipFilter filter = new GzipFilter();
 
        webServer.addFilter(filter, "/*");
        return Collections.emptyList();
 
    }
 
    private WebServer getWebServer(final NeoServer neoServer) {
        if (neoServer instanceof AbstractNeoServer) {
            return ((AbstractNeoServer) neoServer).getWebServer();
        }
        throw new IllegalArgumentException("expected AbstractNeoServer");
    }
 
    @Override
    public Collection<Injectable<?>> start(GraphDatabaseService graphDatabaseService, Configuration configuration) {
        throw new IllegalAccessError();
    }
 
    @Override
    public void stop() {
 
    }
}

I then added the following line to ‘resources/META-INF/services/org.neo4j.server.plugins.PluginLifecycle:

com.markandjim.GZipInitialiser

After compiling that and deploying the JAR to the ‘plugins’ I tried calling the end point using cURL:

$ curl -H "Accept-Encoding:gzip,deflate" -v  http://localhost:7474/unmanaged/subgraph/1000/1
* About to connect() to localhost port 7474 (#0)
*   Trying ::1...
* Connection refused
*   Trying 127.0.0.1...
* connected
* Connected to localhost (127.0.0.1) port 7474 (#0)
> GET /unmanaged/subgraph/1000/1 HTTP/1.1
> User-Agent: curl/7.24.0 (x86_64-apple-darwin12.0) libcurl/7.24.0 OpenSSL/0.9.8r zlib/1.2.5
> Host: localhost:7474
> Accept: */*
> Accept-Encoding:gzip,deflate
>
< HTTP/1.1 200 OK
< Content-Type: text/plain
< Access-Control-Allow-Origin: *
< Content-Encoding: gzip
< Transfer-Encoding: chunked
< Server: Jetty(6.1.25)
 
---
 
gibberish!

The GZIPContentEncodingFilter that I mentioned earlier comes in handy if we want to call the end point from a Jersey client:

public class Neo4jJerseyClient {
    public static void main(String[] args) {
        Client client = Client.create();
        client.addFilter(new GZIPContentEncodingFilter(false));
 
        WebResource webResource = client.resource("http://localhost:7474/unmanaged/subgraph/1000/1");
 
        ClientResponse response  = webResource.header("accept-encoding", "gzip,deflate").get(ClientResponse.class);
 
        System.out.println("response = " + response.getEntity(String.class));
    }
}

We could also call it using HTTParty from the land of Ruby:

response = HTTParty.get("http://localhost:7474/unmanaged/subgraph/1000/1", 
  :headers => { 'Accept-Encoding' => 'gzip,deflate' } )
 
p response






Neo4j Jetty (web server)

Published at DZone with permission of Mark Needham, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Trending

  • What ChatGPT Needs Is Context
  • Operator Overloading in Java
  • RBAC With API Gateway and Open Policy Agent (OPA)
  • Mastering Time Series Analysis: Techniques, Models, and Strategies

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: