mkurz
mkurz

Reputation: 2826

Control gzip encoding filter in Java

In Play 2.3 you can configure gzip encoding for responses by setting up a gzip filter using the Global object. Please have a look here how to set up this filter when using Scala or when using Java.

When using Play with Scala you can also control which responses are and aren’t included in the filter, via the shouldGzip parameter, which accepts a function of a request header and a response header. E.g. here only documents with an encoding of text/html will be gzipped:

new GzipFilter(shouldGzip = (request, response) =>
    response.headers.get("Content-Type").exists(_.startsWith("text/html")))

How can I achive the same in the Java Version?

Upvotes: 4

Views: 1011

Answers (2)

Andriy Kuba
Andriy Kuba

Reputation: 8263

You can use scala file in your java play project. You do not need to do for this any special steps, so if you see that this filter is much easy to code in scala - just add scala file to your project. For example I have a java play project, but gzip filter is in scala:

package filters

import javax.inject.Inject

import play.api.http.HttpFilters
import play.filters.gzip.GzipFilter

import akka.stream.Materializer

import play.api.libs.concurrent.Execution.Implicits._

class Gzip @Inject() (implicit val mat: Materializer) extends HttpFilters {
  def filters = Seq(new GzipFilter(shouldGzip = (request, response) =>
     response.body.contentType.exists(_.startsWith("text/html"))))
}

and in the application.conf

play.http.filters = "filters.Gzip"

Upvotes: 2

Andrzej Jozwik
Andrzej Jozwik

Reputation: 14649

I found the solution. You have to implement own Filter and delegate all request to GzipFilter. The values 102400 are private in GzipFilter - so I have to hardcode it here.

    import play.api.mvc.EssentialAction;
    import play.api.mvc.EssentialFilter;
    import play.api.mvc.RequestHeader;
    import play.api.mvc.ResponseHeader;
    import play.filters.gzip.Gzip;
    import play.filters.gzip.GzipFilter;
    import scala.Function2;
    import scala.runtime.AbstractFunction2;

    public class MyGzipFilter implements EssentialFilter {

        private Function2<RequestHeader,ResponseHeader,Object> shouldGzip = new AbstractFunction2<RequestHeader,ResponseHeader,Object>(){

            @Override
            public Boolean apply(RequestHeader v1, ResponseHeader v2) {
                return shouldGzipFunction(v1,v2);
            }

        };
        private GzipFilter filter = new GzipFilter(Gzip.gzip(8192),102400,shouldGzip);

        @Override
        public EssentialAction apply(EssentialAction next) {
            return filter.apply(next);
        }

        private boolean shouldGzipFunction(RequestHeader v1, ResponseHeader v2) {
//your logic here

       }
    }
import play.GlobalSettings;
import play.api.mvc.EssentialFilter;

public class Global extends GlobalSettings {
    public <T extends EssentialFilter> Class<T>[] filters() {
        return new Class[]{MyGzipFilter.class};
    }
}

Upvotes: 2

Related Questions