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

com.intuit.karate.http.HttpRequestBuilder Maven / Gradle / Ivy

There is a newer version: 1.4.1
Show newest version
/*
 * The MIT License
 *
 * Copyright 2017 Intuit Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package com.intuit.karate.http;

import com.intuit.karate.ScriptValue;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author pthomas3
 */
public class HttpRequestBuilder {

    private String url;
    private List paths;
    private MultiValuedMap headers;
    private MultiValuedMap params;
    private Map cookies;
    private MultiValuedMap formFields;
    private List multiPartItems;
    private ScriptValue body;
    private String method;
    private String soapAction;
    private int pauseTime;

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

    public String getUrl() {
        return url;
    }
    
    private static String getFirst(MultiValuedMap map, String name) {
        if (map == null) {
            return null;
        }
        Object temp = map.getFirst(name);
        return temp == null ? null : temp.toString();        
    }

    public String getHeader(String name) {
        return getFirst(headers, name);
    }

    public String getParam(String name) {
        return getFirst(params, name);
    }

    public String getUrlAndPath() {
        String temp = url;
        if (!temp.endsWith("/")) {
            temp = temp + "/";
        }
        if (paths == null) {
            return temp;
        }
        for (String path : paths) {
            if (path.startsWith("/")) {
                path = path.substring(1);
            }
            temp = temp + path;
        }
        return temp;
    }

    public void addPath(String path) {
        if (path == null) {
            return;
        }
        if (paths == null) {
            paths = new ArrayList<>();
        }
        paths.add(path);
    }

    public List getPaths() {
        return paths;
    }

    public void removeHeader(String name) {
        if (headers == null) {
            return;
        }
        headers.remove(name);
    }

    public void setHeaders(MultiValuedMap headers) {
        this.headers = headers;
    }

    public void setHeader(String name, String value) {
        setHeader(name, Collections.singletonList(value));
    }

    public void setHeader(String name, List values) {
        if (headers == null) {
            headers = new MultiValuedMap();
        }
        headers.put(name, values);
    }

    public MultiValuedMap getHeaders() {
        return headers;
    }

    public void removeParam(String name) {
        if (params == null) {
            return;
        }
        params.remove(name);
    }

    public void setParam(String name, String value) {
        setParam(name, Collections.singletonList(value));
    }

    public void setParam(String name, List values) {
        if (params == null) {
            params = new MultiValuedMap();
        }
        params.put(name, values);
    }

    public MultiValuedMap getParams() {
        return params;
    }

    public void removeCookie(String name) {
        if (cookies == null) {
            return;
        }
        cookies.remove(name);
    }

    public void setCookie(Cookie cookie) {
        if (cookies == null) {
            cookies = new LinkedHashMap<>();
        }
        cookies.put(cookie.getName(), cookie);
    }

    public Map getCookies() {
        return cookies;
    }

    public void setCookies(Map cookies) {
        this.cookies = cookies;
    }

    public void removeFormField(String name) {
        if (formFields == null) {
            return;
        }
        formFields.remove(name);
    }

    public void setFormField(String name, String value) {
        setFormField(name, Collections.singletonList(value));
    }

    public void setFormField(String name, List values) {
        if (formFields == null) {
            formFields = new MultiValuedMap();
        }
        formFields.put(name, values);
    }

    public MultiValuedMap getFormFields() {
        return formFields;
    }

    public void addMultiPartItem(String name, ScriptValue value) {
        MultiPartItem item = new MultiPartItem(name, value);
        if (value.isStream()) { // short-cut, assume that intent is file-upload
            item.setFilename(name);
        }
        addMultiPartItem(item);
    }

    public void addMultiPartItem(MultiPartItem item) {
        if (multiPartItems == null) {
            multiPartItems = new ArrayList<>();
        }
        multiPartItems.add(item);
    }

    public List getMultiPartItems() {
        return multiPartItems;
    }

    public ScriptValue getBody() {
        return body;
    }

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

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

    public String getMethod() {
        return method;
    }

    public String getContentType() {
        if (headers == null) {
            return null;
        }
        Object contentType = headers.getFirst("Content-Type");
        if (contentType == null) {
            return null;
        }
        return contentType.toString();
    }

    public void setSoapAction(String soapAction) {
        this.soapAction = soapAction;
    }

    public String getSoapAction() {
        return soapAction;
    }
    
    public int getPauseTime() {
        return pauseTime;
    }

    public void setPauseTime(int pauseTime) {
        this.pauseTime = pauseTime;
    }    

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy