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

uk.co.autotrader.traverson.http.Request Maven / Gradle / Ivy

Go to download

The kernel of traverson4j. This provides the main API for a client to traverse a Hypermedia REST service

The newest version!
package uk.co.autotrader.traverson.http;

import java.util.*;
import java.util.function.BiConsumer;

public class Request {

    private String url;
    private Method method;
    private String acceptMimeType;
    private final Map headers;
    private final Map> queryParameters;
    private final Map> templateParams;
    private Body body;
    private final List authCredentials;

    public Request() {
        queryParameters = new HashMap<>();
        templateParams = new HashMap<>();
        headers = new LinkedHashMap<>();
        authCredentials = new LinkedList<>();
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public Method getMethod() {
        return method;
    }

    public void setMethod(Method method) {
        this.method = method;
    }

    public Map getHeaders() {
        return headers;
    }

    public Body getBody() {
        return body;
    }

    public void setBody(Body body) {
        this.body = body;
    }

    public List getAuthCredentials() {
        return authCredentials;
    }

    public String getAcceptMimeType() {
        return acceptMimeType;
    }

    public void setAcceptMimeType(String acceptMimeType) {
        this.acceptMimeType = acceptMimeType;
    }

    public void addHeader(String key, String value) {
        this.headers.put(key, value);
    }

    public void addAuthCredential(AuthCredential authCredential) {
        this.authCredentials.add(authCredential);
    }

    public Map> getQueryParameters() {
        return queryParameters;
    }

    public void addQueryParam(String name, String... values) {
        addParameters(this.queryParameters).accept(name, values);
    }

    public Map> getTemplateParams() {
        return templateParams;
    }

    public void addTemplateParam(String name, String... values) {
        addParameters(this.templateParams).accept(name, values);
    }

    private BiConsumer addParameters(Map> parameterMap) {
        return (name, values) -> {
            if (!parameterMap.containsKey(name)) {
                parameterMap.put(name, new LinkedList<>());
            }
            parameterMap.get(name).addAll(Arrays.asList(values));
        };
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy