com.mashape.unirest.request.HttpRequest.java Source code

Java tutorial

Introduction

Here is the source code for com.mashape.unirest.request.HttpRequest.java

Source

/*
The MIT License
    
Copyright (c) 2013 Mashape (http://mashape.com)
    
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.mashape.unirest.request;

import java.net.URI;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.codec.binary.Base64;

import com.mashape.unirest.http.HttpMethod;
import com.mashape.unirest.request.body.Body;

public class HttpRequest extends BaseRequest {

    private HttpMethod httpMethod;
    private String url;
    private Map<String, String> headers = new HashMap<String, String>();
    protected Body body;

    private URL parseUrl(String s) throws Exception {
        URL u = new URL(s);
        return new URI(u.getProtocol(), u.getAuthority(), u.getPath(), u.getQuery(), u.getRef()).toURL();
    }

    public HttpRequest(HttpMethod method, String url) {
        this.httpMethod = method;
        try {
            this.url = parseUrl(url).toString();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        super.httpRequest = this;
    }

    public HttpRequest basicAuth(String username, String password) {
        header("Authorization", "Basic " + Base64.encodeBase64String((username + ":" + password).getBytes()));
        return this;
    }

    public HttpRequest header(String name, String value) {
        this.headers.put(name.toLowerCase(), value);
        return this;
    }

    public HttpRequest headers(Map<String, String> headers) {
        if (headers != null) {
            for (Map.Entry<String, String> entry : headers.entrySet()) {
                header(entry.getKey(), entry.getValue());
            }
        }
        return this;
    }

    public HttpMethod getHttpMethod() {
        return httpMethod;
    }

    public String getUrl() {
        return url;
    }

    public Map<String, String> getHeaders() {
        if (headers == null)
            return new HashMap<String, String>();
        return headers;
    }

    public Body getBody() {
        return body;
    }

}