NetClient now vomits IOExceptions

This commit is contained in:
Sollace 2018-07-25 17:48:58 +02:00
parent b5185fa887
commit 9a629587ab

View file

@ -8,7 +8,6 @@ import java.io.InputStreamReader;
import java.net.URI; import java.net.URI;
import java.util.Map; import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity; import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus; import org.apache.http.HttpStatus;
import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.CloseableHttpResponse;
@ -25,7 +24,7 @@ import org.apache.http.util.EntityUtils;
*/ */
public class NetClient implements Closeable { public class NetClient implements Closeable {
private CloseableHttpClient client; private static CloseableHttpClient client = null;
private RequestBuilder rqBuilder; private RequestBuilder rqBuilder;
@ -89,7 +88,7 @@ public class NetClient implements Closeable {
/** /**
* Commits and sends the request. * Commits and sends the request.
*/ */
private void send() { private void send() throws IOException {
HttpUriRequest request = rqBuilder.build(); HttpUriRequest request = rqBuilder.build();
if (headers != null) { if (headers != null) {
@ -102,15 +101,13 @@ public class NetClient implements Closeable {
client = HttpClients.createSystem(); client = HttpClients.createSystem();
} }
try {
response = client.execute(request); response = client.execute(request);
} catch (IOException e) { }
} }
/** /**
* Gets or obtains the http response body. * Gets or obtains the http response body.
*/ */
public CloseableHttpResponse getResponse() { public CloseableHttpResponse getResponse() throws IOException {
if (response == null) { if (response == null) {
send(); send();
} }
@ -121,7 +118,7 @@ public class NetClient implements Closeable {
/** /**
* Gets or obtains a response status code. * Gets or obtains a response status code.
*/ */
public int getResponseCode() { public int getResponseCode() throws IOException {
if (getResponse() == null) { if (getResponse() == null) {
return HttpStatus.SC_NOT_FOUND; return HttpStatus.SC_NOT_FOUND;
} }
@ -132,7 +129,7 @@ public class NetClient implements Closeable {
/** /**
* Consumes and returns the entire response body. * Consumes and returns the entire response body.
*/ */
public String getResponseText() { public String getResponseText() throws IOException {
if (getResponse() == null || response.getEntity() == null) { if (getResponse() == null || response.getEntity() == null) {
return ""; return "";
} }
@ -146,23 +143,13 @@ public class NetClient implements Closeable {
} }
return builder.toString(); return builder.toString();
} catch (IOException e) {
} }
return "";
} }
@Override @Override
public void close() throws IOException { public void close() throws IOException {
if (response != null) { if (response != null) {
EntityUtils.consumeQuietly(response.getEntity()); EntityUtils.consumeQuietly(response.getEntity());
response = null;
}
if (client != null) {
IOUtils.closeQuietly(client);
client = null;
} }
} }
} }