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

com.tapstream.sdk.http.StdLibHttpClient Maven / Gradle / Ivy

There is a newer version: 4.0.0
Show newest version
package com.tapstream.sdk.http;

import com.tapstream.sdk.Utils;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;


public class StdLibHttpClient implements HttpClient{

    public static final int DEFAULT_CONNECT_TIMEOUT = 5000;
    public static final int DEFAULT_READ_TIMEOUT = 5000;


    @Override
    public HttpResponse sendRequest(HttpRequest request) throws IOException {
        HttpURLConnection connection = (HttpURLConnection)request.getURL().openConnection();
        connection.setConnectTimeout(DEFAULT_CONNECT_TIMEOUT);
        connection.setReadTimeout(DEFAULT_READ_TIMEOUT);
        connection.setUseCaches(false);

        switch (request.getMethod()){
            case GET:
                connection.setRequestMethod("GET");
                break;
            case POST:
                connection.setRequestMethod("POST");

                if (request.getBody() != null){
                    String contentType = request.getBody().contentType();
                    byte[] bodyBytes = request.getBody().toBytes();
                    connection.setFixedLengthStreamingMode(bodyBytes.length);
                    connection.setRequestProperty("Content-Type", contentType);
                    connection.setDoOutput(true);

                    OutputStream os = connection.getOutputStream();
                    try {
                        os.write(bodyBytes);
                    } finally {
                        if (os != null)
                            os.close();
                    }

                }

                break;
        }

        try {
            int responseCode = connection.getResponseCode();
            String responseMsg = connection.getResponseMessage();

            InputStream is;
            try {
                is = connection.getInputStream();
            } catch (IOException e){
                is = connection.getErrorStream();
            }

            byte[] responseBody;
            try{
                responseBody = Utils.readFully(is);
            } finally {
                if (is != null)
                    is.close();
            }

            return new HttpResponse(
                    responseCode,
                    responseMsg,
                    responseBody);

        } catch (IOException e){
            throw e;
        } catch (Exception e){
            throw new IOException(e);
        }
    }

    @Override
    public void close() throws IOException {
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy