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

com.nike.backstopper.handler.riposte.RequestInfoForLoggingRiposteAdapter Maven / Gradle / Ivy

There is a newer version: 0.20.0
Show newest version
package com.nike.backstopper.handler.riposte;

import com.nike.backstopper.handler.RequestInfoForLogging;
import com.nike.riposte.server.http.RequestInfo;
import com.nike.riposte.util.HttpUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Adapter that allows {@link RequestInfo} to be used as a {@link RequestInfoForLogging}.
 *
 * @author Nic Munroe
 */
@SuppressWarnings("WeakerAccess")
public class RequestInfoForLoggingRiposteAdapter implements RequestInfoForLogging {

    private final RequestInfo request;
    private Map> headersMapCache;

    public RequestInfoForLoggingRiposteAdapter(RequestInfo request) {
        if (request == null)
            throw new IllegalArgumentException("request cannot be null");

        this.request = request;
    }

    @Override
    public String getRequestUri() {
        return request.getPath();
    }

    @Override
    public String getRequestHttpMethod() {
        return String.valueOf(request.getMethod());
    }

    @Override
    public String getQueryString() {
        return HttpUtils.extractQueryString(request.getUri());
    }

    @Override
    public Map> getHeadersMap() {
        if (headersMapCache == null) {
            Map> headersMap = new HashMap<>();

            Set headerNames = request.getHeaders().names();
            if (headerNames != null) {
                for (String headerName : headerNames) {
                    List headerValues = request.getHeaders().getAll(headerName);
                    if (headerValues != null) {
                        headersMap.put(headerName, headerValues);
                    }
                }
            }

            headersMapCache = headersMap;
        }

        return headersMapCache;
    }

    @Override
    public String getHeader(String headerName) {
        return request.getHeaders().get(headerName);
    }

    @Override
    public List getHeaders(String headerName) {
        return getHeadersMap().get(headerName);
    }

    @Override
    public Object getAttribute(String key) {
        return null;
    }

    @Override
    public String getBody() throws GetBodyException {
        return request.getRawContent();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy