io.datakernel.http.GzipServlet Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of datakernel-http Show documentation
Show all versions of datakernel-http Show documentation
High-performance asynchronous HTTP clients and servers collection.
Package contains a bunch of different built-in servlets for request dispatching, loading of a static content, etc.
/*
* Copyright (C) 2015 SoftIndex LLC.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.datakernel.http;
import io.datakernel.async.Stage;
import io.datakernel.bytebuf.ByteBuf;
@SuppressWarnings({"unused", "WeakerAccess"})
public final class GzipServlet implements AsyncServlet {
private static final int DEFAULT_BODY_SIZE_THRESHOLD = 200;
private final AsyncServlet asyncServlet;
private final int minBodySizeThreshold;
public static GzipServlet create(int minBodySizeThreshold, AsyncServlet asyncServlet) {
return new GzipServlet(minBodySizeThreshold, asyncServlet);
}
public static GzipServlet create(AsyncServlet asyncServlet) {
return new GzipServlet(DEFAULT_BODY_SIZE_THRESHOLD, asyncServlet);
}
private GzipServlet(int minBodySizeThreshold, AsyncServlet asyncServlet) {
this.asyncServlet = asyncServlet;
this.minBodySizeThreshold = minBodySizeThreshold;
}
@Override
public Stage serve(HttpRequest request) {
Stage serve = asyncServlet.serve(request);
if (!test(request)) return serve;
return serve.thenApply(response ->
test(response, minBodySizeThreshold) ? response.withBodyGzipCompression() : response);
}
private static boolean test(HttpRequest request) {
return request.isAcceptEncodingGzip();
}
private static boolean test(HttpResponse response, int minimumSizeThreshold) {
int code = response.getCode();
ByteBuf body = response.getBody();
return code == 200 && body != null && body.readRemaining() > minimumSizeThreshold;
}
}