com.citahub.cita.protocol.http.HttpService Maven / Gradle / Ivy
The newest version!
package com.citahub.cita.protocol.http;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import com.citahub.cita.protocol.Service;
import com.citahub.cita.protocol.exceptions.ClientConnectionException;
import okhttp3.Headers;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import okhttp3.logging.HttpLoggingInterceptor;
import okio.Buffer;
import okio.BufferedSource;
/**
* HTTP implementation of our services API.
*/
public class HttpService extends Service {
public static final MediaType JSON_MEDIA_TYPE
= MediaType.parse("application/json; charset=utf-8");
public static final String DEFAULT_URL = "http://localhost:8545/";
private static boolean debug = false;
private OkHttpClient httpClient;
private final String url;
private final boolean includeRawResponse;
private HashMap headers = new HashMap<>();
public HttpService(String url, OkHttpClient httpClient, boolean includeRawResponses) {
super(includeRawResponses);
this.url = url;
this.httpClient = httpClient;
this.includeRawResponse = includeRawResponses;
}
public HttpService(OkHttpClient httpClient, boolean includeRawResponses) {
this(DEFAULT_URL, httpClient, includeRawResponses);
}
private HttpService(String url, OkHttpClient httpClient) {
this(url, httpClient, false);
}
public HttpService(String url) {
this(url, createOkHttpClient());
}
public HttpService(String url, boolean includeRawResponse) {
this(url, createOkHttpClient(), includeRawResponse);
}
public HttpService(OkHttpClient httpClient) {
this(DEFAULT_URL, httpClient);
}
public HttpService(boolean includeRawResponse) {
this(DEFAULT_URL, includeRawResponse);
}
public HttpService() {
this(DEFAULT_URL);
}
private static OkHttpClient createOkHttpClient() {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
configureLogging(builder);
return builder.build();
}
private static void configureLogging(OkHttpClient.Builder builder) {
if (debug) {
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
builder.addInterceptor(logging);
}
}
@Override
protected InputStream performIO(String request) throws IOException {
RequestBody requestBody = RequestBody.create(JSON_MEDIA_TYPE, request);
Headers headers = buildHeaders();
okhttp3.Request httpRequest = new okhttp3.Request.Builder()
.url(url)
.headers(headers)
.post(requestBody)
.build();
okhttp3.Response response = httpClient.newCall(httpRequest).execute();
if (response.isSuccessful()) {
ResponseBody responseBody = response.body();
if (responseBody != null) {
return buildInputStream(responseBody);
} else {
return null;
}
} else {
ResponseBody responseBody = response.body();
response.close();
throw new ClientConnectionException(
"Invalid response received: " + responseBody);
}
}
private InputStream buildInputStream(ResponseBody responseBody) throws IOException {
InputStream inputStream = responseBody.byteStream();
if (includeRawResponse) {
// we have to buffer the entire input payload, so that after processing
// it can be re-read and used to populate the rawResponse field.
BufferedSource source = responseBody.source();
source.request(Long.MAX_VALUE); // Buffer the entire body
Buffer buffer = source.buffer();
long size = buffer.size();
if (size > Integer.MAX_VALUE) {
throw new UnsupportedOperationException(
"Non-integer input buffer size specified: " + size);
}
int bufferSize = (int) size;
BufferedInputStream bufferedinputStream =
new BufferedInputStream(inputStream, bufferSize);
bufferedinputStream.mark(inputStream.available());
return bufferedinputStream;
} else {
return inputStream;
}
}
private Headers buildHeaders() {
return Headers.of(headers);
}
public void addHeader(String key, String value) {
headers.put(key, value);
}
public void addHeaders(Map headersToAdd) {
headers.putAll(headersToAdd);
}
public HashMap getHeaders() {
return headers;
}
public static void setDebug(boolean debug) {
HttpService.debug = debug;
}
@Override
public void close() throws IOException {
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy