![JAR search and dependency download from the Maven repository](/logo.png)
io.neow3j.protocol.http.HttpService Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of core Show documentation
Show all versions of core Show documentation
neow3j: Java/Kotlin/Android Development Toolkit for the Neo Blockchain
package io.neow3j.protocol.http;
import io.neow3j.protocol.Service;
import io.neow3j.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;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
/**
* HTTP implementation of the Service 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:10333/";
private static final Logger log = LoggerFactory.getLogger(HttpService.class);
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 (log.isDebugEnabled()) {
HttpLoggingInterceptor logging = new HttpLoggingInterceptor(log::debug);
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();
ResponseBody responseBody = response.body();
if (response.isSuccessful()) {
if (responseBody != null) {
return buildInputStream(responseBody);
} else {
return null;
}
} else {
int code = response.code();
String text = responseBody == null ? "N/A" : responseBody.string();
throw new ClientConnectionException("Invalid response received: " + code + "; " + text);
}
}
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;
}
@Override
public void close() throws IOException {
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy