All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.quarkus.vertx.web.runtime.HttpCompressionHandler Maven / Gradle / Ivy

There is a newer version: 3.15.0
Show newest version
package io.quarkus.vertx.web.runtime;

import java.util.Set;

import io.quarkus.vertx.http.runtime.HttpCompression;
import io.vertx.core.Handler;
import io.vertx.core.MultiMap;
import io.vertx.core.http.HttpHeaders;
import io.vertx.ext.web.RoutingContext;

public class HttpCompressionHandler implements Handler {

    private final Handler routeHandler;
    private final HttpCompression compression;
    private final Set compressedMediaTypes;

    public HttpCompressionHandler(Handler routeHandler, HttpCompression compression,
            Set compressedMediaTypes) {
        this.routeHandler = routeHandler;
        this.compression = compression;
        this.compressedMediaTypes = compressedMediaTypes;
    }

    @Override
    public void handle(RoutingContext context) {
        context.addHeadersEndHandler(new Handler() {
            @Override
            public void handle(Void result) {
                MultiMap headers = context.response().headers();
                String contentEncoding = headers.get(HttpHeaders.CONTENT_ENCODING);
                if (contentEncoding != null && HttpHeaders.IDENTITY.toString().equals(contentEncoding)) {
                    switch (compression) {
                        case ON:
                            headers.remove(HttpHeaders.CONTENT_ENCODING);
                            break;
                        case UNDEFINED:
                            String contentType = headers.get(HttpHeaders.CONTENT_TYPE);
                            if (contentType != null) {
                                int paramIndex = contentType.indexOf(';');
                                if (paramIndex > -1) {
                                    contentType = contentType.substring(0, paramIndex);
                                }
                                if (compressedMediaTypes.contains(contentType)) {
                                    headers.remove(HttpHeaders.CONTENT_ENCODING);
                                }
                            }
                            break;
                        default:
                            // OFF - no action is needed because the "Content-Encoding: identity" header is set
                            break;
                    }
                }
            }
        });
        routeHandler.handle(context);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy