Package scrumdo.transport

Source Code of scrumdo.transport.HttpClientTransport

/*
* Copyright (c) 2012, Soren Davidsen
*
* 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 scrumdo.transport;

import org.apache.commons.io.IOUtils;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.*;
import org.apache.http.entity.BufferedHttpEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

/**
* @author Soren <soren@tanesha.net>
*/
public class HttpClientTransport implements ITransport {

    HttpClient client = new DefaultHttpClient();

    private Response execute(HttpUriRequest request) {
        try {
            HttpResponse response = client.execute(request);
            String content = IOUtils.toString(response.getEntity().getContent());
            Header[] locations = response.getHeaders("Location");
            String location = null;
            if (locations != null && locations.length > 0)
                location = locations[0].getValue();
            return new Response(content, response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase(), location);
        } catch (Exception e) {
            throw new RuntimeException("RE: " + e.getMessage(), e);
        }
    }

    @Override
    public Response get(String url) {
        HttpGet get = new HttpGet(url);
        return execute(get);
    }

    @Override
    public Response post(String url, final String data) {
        try {
            BufferedHttpEntity entity = new BufferedHttpEntity(new StringEntity(data, "UTF-8"));
            HttpPost post = new HttpPost(url);
            post.setEntity(entity);
            post.setHeader("Content-Type", "application/json");
            return execute(post);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("Error encoding request: " + e.getMessage(), e);
        } catch (IOException e) {
            throw new RuntimeException("Error executing request: " + e.getMessage(), e);
        }
    }

    @Override
    public Response put(String url, final String data) {
        try {
            BufferedHttpEntity entity = new BufferedHttpEntity(new StringEntity(data, "UTF-8"));
            HttpPut put = new HttpPut(url);
            put.setEntity(entity);
            put.setHeader("Content-Type", "application/json");
            return execute(put);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("Error encoding request: " + e.getMessage(), e);
        } catch (IOException e) {
            throw new RuntimeException("Error executing request: " + e.getMessage(), e);
        }
    }

    @Override
    public Response delete(String url) {
        HttpDelete delete = new HttpDelete(url);
        return execute(delete);
    }
}
TOP

Related Classes of scrumdo.transport.HttpClientTransport

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.