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

com.vtence.molecule.middlewares.ContentLengthHeader Maven / Gradle / Ivy

There is a newer version: 0.15.0
Show newest version
package com.vtence.molecule.middlewares;

import com.vtence.molecule.Request;
import com.vtence.molecule.Response;

import static com.vtence.molecule.http.HeaderNames.CONTENT_LENGTH;
import static com.vtence.molecule.http.HeaderNames.TRANSFER_ENCODING;

public class ContentLengthHeader extends AbstractMiddleware {

    public void handle(Request request, Response response) throws Exception {
        forward(request, response).whenSuccessful(this::addContentLengthHeader);
    }

    private void addContentLengthHeader(Response response) {
        if (requiresContentLengthHeader(response)) response.contentLength(response.size());
    }

    public boolean requiresContentLengthHeader(Response response) {
        return !hasContentLengthHeader(response) && isFixedLengthSize(response) && !isChunked(response);
    }

    private boolean isFixedLengthSize(Response response) {
        return response.size() > 0;
    }

    private boolean hasContentLengthHeader(Response response) {
        return response.hasHeader(CONTENT_LENGTH);
    }

    private boolean isChunked(Response response) {
        return response.hasHeader(TRANSFER_ENCODING)
                && response.header(TRANSFER_ENCODING).equalsIgnoreCase("chunked");
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy