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

io.vrap.rmf.base.client.ApiHttpResponse Maven / Gradle / Ivy

There is a newer version: 17.17.0
Show newest version

package io.vrap.rmf.base.client;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;

public class ApiHttpResponse extends Base implements ContextAware> {

    private int statusCode;
    private ApiHttpHeaders headers;
    private final U body;
    private String message;

    private Map contextMap = new HashMap<>();

    public ApiHttpResponse(final int statusCode, final ApiHttpHeaders headers, final U body) {
        this(statusCode, headers, body, null);
    }

    public ApiHttpResponse(final int statusCode, final ApiHttpHeaders headers, final U body, final String message) {
        this.statusCode = statusCode;
        this.headers = Optional.ofNullable(headers).orElse(new ApiHttpHeaders());
        this.body = body;
        this.message = message;
    }

    public ApiHttpResponse(final int statusCode, final ApiHttpHeaders headers, final U body, final String message,
            final Map contextMap) {
        this.statusCode = statusCode;
        this.headers = Optional.ofNullable(headers).orElse(new ApiHttpHeaders());
        this.body = body;
        this.message = message;
        this.contextMap = contextMap;
    }

    public ApiHttpResponse(final ApiHttpResponse response) {
        this.statusCode = response.statusCode;
        this.headers = response.headers;
        this.body = response.body;
        this.message = response.message;
        this.contextMap = response.contextMap;
    }

    public Map getContextMap() {
        return contextMap;
    }

    public ApiHttpResponse withContextMap(final Map contextMap) {
        ApiHttpResponse response = copy();
        response.contextMap = new HashMap<>(contextMap);

        return response;
    }

    @SuppressWarnings("unchecked")
    public  T getContext(Class key) {
        return (T) contextMap.get(key);
    }

    public  ApiHttpResponse addContext(T value) {
        ApiHttpResponse response = copy();
        Map contextMap = new HashMap<>(response.contextMap);
        contextMap.put(value.getClass(), value);
        if (value instanceof ClassReferenceContext) {
            contextMap.put(((ClassReferenceContext) value).classReference(), value);
        }

        response.contextMap = contextMap;

        return response;
    }

    public Object getContext(Object key) {
        return contextMap.get(key);
    }

    public ApiHttpResponse addContext(Object key, Object value) {
        ApiHttpResponse response = copy();
        Map contextMap = new HashMap<>(response.contextMap);
        contextMap.put(key, value);
        response.contextMap = contextMap;

        return response;
    }

    public int getStatusCode() {
        return statusCode;
    }

    public ApiHttpResponse withStatusCode(final int statusCode) {
        ApiHttpResponse response = copy();
        response.statusCode = statusCode;

        return response;
    }

    public ApiHttpHeaders getHeaders() {
        return headers;
    }

    public ApiHttpResponse withHeaders(final ApiHttpHeaders headers) {
        ApiHttpResponse response = copy();
        response.headers = headers;

        return response;
    }

    public U getBody() {
        return body;
    }

    public  ApiHttpResponse withBody(final TBody body) {
        return new ApiHttpResponse<>(this.statusCode, this.headers, body, this.message, this.contextMap);
    }

    public  ApiHttpResponse withBody(final Function bodyFn) {
        return new ApiHttpResponse<>(this.statusCode, this.headers, bodyFn.apply(this.body), this.message,
            this.contextMap);
    }

    public String getMessage() {
        return message;
    }

    public ApiHttpResponse withMessage(final String message) {
        ApiHttpResponse response = copy();
        response.message = message;

        return response;
    }

    @Override
    public String toString() {
        return new ToStringBuilder(this).append("statusCode", statusCode)
                .append("headers", headers)
                .append("textInterpretedBody", getSecuredBody())
                .toString();
    }

    static String tryToFilter(final String input) {
        return input.replaceAll("(\"\\w*([Pp]ass|access_token|refresh_token)\\w*\"):\"[^\"]*\"",
            "$1:\"**removed from output**\"");
    }

    public Optional getBodyAsString() {
        if (body instanceof byte[]) {
            return Optional.of(body).map(b -> tryToFilter(new String((byte[]) b, StandardCharsets.UTF_8)));
        }
        else {
            return Optional.ofNullable(body).map(Object::toString);
        }
    }

    public String getSecuredBody() {
        return getBodyAsString().orElse("empty body");
    }

    private ApiHttpResponse copy() {
        return new ApiHttpResponse<>(this);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o)
            return true;

        if (o == null || getClass() != o.getClass())
            return false;

        ApiHttpResponse response = (ApiHttpResponse) o;

        return new EqualsBuilder().append(statusCode, response.statusCode)
                .append(headers, response.headers)
                .append(body, response.body)
                .append(message, response.message)
                .append(contextMap, response.contextMap)
                .isEquals();
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder(17, 37).append(statusCode)
                .append(headers)
                .append(body)
                .append(message)
                .append(contextMap)
                .toHashCode();
    }
}