com.tapstream.sdk.http.StdLibHttpClient Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of tapstream-core Show documentation
Show all versions of tapstream-core Show documentation
This library contains the building blocks for a Tapstream Java SDK
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