all IOExceptions are now passed up and not handled within the HTTP library. (#386)

This commit is contained in:
Mark Iantorno 2020-11-26 17:29:02 -05:00 committed by GitHub
parent 830e9e3cc4
commit 3e1e00b37d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 54 deletions

View File

@ -37,6 +37,7 @@ import org.hl7.fhir.r5.utils.client.network.ResourceRequest;
import org.hl7.fhir.utilities.ToolingClientLogger;
import org.hl7.fhir.utilities.Utilities;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
@ -232,7 +233,7 @@ public class FHIRToolingClient {
try {
result = client.issuePutRequest(resourceAddress.resolveGetUriFromResourceClassAndId(resourceClass, id),
ByteUtils.resourceToByteArray(resource, false, isJson(getPreferredResourceFormat())),
getPreferredResourceFormat(),"Update " + resource.fhirType() + "/" + id, TIMEOUT_OPERATION);
getPreferredResourceFormat(), "Update " + resource.fhirType() + "/" + id, TIMEOUT_OPERATION);
if (result.isUnsuccessfulRequest()) {
throw new EFhirClientException("Server returned error code " + result.getHttpStatus(), (OperationOutcome) result.getPayload());
}
@ -355,10 +356,10 @@ public class FHIRToolingClient {
try {
result = client.issuePostRequest(resourceAddress.resolveOperationUri(ValueSet.class, "expand"),
ByteUtils.resourceToByteArray(p, false, isJson(getPreferredResourceFormat())), getPreferredResourceFormat(), null, "ValueSet/$expand?url=" + source.getUrl(), TIMEOUT_OPERATION_EXPAND);
if (result.isUnsuccessfulRequest()) {
throw new EFhirClientException("Server returned error code " + result.getHttpStatus(), (OperationOutcome) result.getPayload());
}
} catch (MalformedURLException e) {
if (result.isUnsuccessfulRequest()) {
throw new EFhirClientException("Server returned error code " + result.getHttpStatus(), (OperationOutcome) result.getPayload());
}
} catch (IOException e) {
e.printStackTrace();
}
return result == null ? null : (ValueSet) result.getPayload();
@ -369,7 +370,7 @@ public class FHIRToolingClient {
org.hl7.fhir.r5.utils.client.network.ResourceRequest<Resource> result = null;
try {
result = client.issueGetResourceRequest(resourceAddress.resolveOperationUri(CodeSystem.class, "lookup", params), getPreferredResourceFormat(), "CodeSystem/$lookup", TIMEOUT_NORMAL);
} catch (MalformedURLException e) {
} catch (IOException e) {
e.printStackTrace();
}
if (result.isUnsuccessfulRequest()) {
@ -388,10 +389,10 @@ public class FHIRToolingClient {
try {
result = client.issuePostRequest(resourceAddress.resolveOperationUri(ValueSet.class, "expand", params),
ByteUtils.resourceToByteArray(p, false, isJson(getPreferredResourceFormat())), getPreferredResourceFormat(), null, "ValueSet/$expand?url=" + source.getUrl(), TIMEOUT_OPERATION_EXPAND);
if (result.isUnsuccessfulRequest()) {
throw new EFhirClientException("Server returned error code " + result.getHttpStatus(), (OperationOutcome) result.getPayload());
}
} catch (MalformedURLException e) {
if (result.isUnsuccessfulRequest()) {
throw new EFhirClientException("Server returned error code " + result.getHttpStatus(), (OperationOutcome) result.getPayload());
}
} catch (IOException e) {
e.printStackTrace();
}
return result == null ? null : (ValueSet) result.getPayload();
@ -408,10 +409,10 @@ public class FHIRToolingClient {
try {
result = client.issuePostRequest(resourceAddress.resolveOperationUri(null, "closure", new HashMap<String, String>()),
ByteUtils.resourceToByteArray(params, false, isJson(getPreferredResourceFormat())), getPreferredResourceFormat(), null, "Closure?name=" + name, TIMEOUT_NORMAL);
if (result.isUnsuccessfulRequest()) {
throw new EFhirClientException("Server returned error code " + result.getHttpStatus(), (OperationOutcome) result.getPayload());
}
} catch (MalformedURLException e) {
if (result.isUnsuccessfulRequest()) {
throw new EFhirClientException("Server returned error code " + result.getHttpStatus(), (OperationOutcome) result.getPayload());
}
} catch (IOException e) {
e.printStackTrace();
}
return result == null ? null : (ConceptMap) result.getPayload();
@ -425,10 +426,10 @@ public class FHIRToolingClient {
try {
result = client.issuePostRequest(resourceAddress.resolveOperationUri(null, "closure", new HashMap<String, String>()),
ByteUtils.resourceToByteArray(params, false, isJson(getPreferredResourceFormat())), getPreferredResourceFormat(), null, "UpdateClosure?name=" + name, TIMEOUT_OPERATION);
if (result.isUnsuccessfulRequest()) {
throw new EFhirClientException("Server returned error code " + result.getHttpStatus(), (OperationOutcome) result.getPayload());
}
} catch (MalformedURLException e) {
if (result.isUnsuccessfulRequest()) {
throw new EFhirClientException("Server returned error code " + result.getHttpStatus(), (OperationOutcome) result.getPayload());
}
} catch (IOException e) {
e.printStackTrace();
}
return result == null ? null : (ConceptMap) result.getPayload();

View File

@ -74,7 +74,7 @@ public class Client {
public <T extends Resource> ResourceRequest<T> issueOptionsRequest(URI optionsUri,
String resourceFormat,
String message,
long timeout) throws MalformedURLException {
long timeout) throws IOException {
Request.Builder request = new Request.Builder()
.method("OPTIONS", null)
.url(optionsUri.toURL());
@ -85,7 +85,7 @@ public class Client {
public <T extends Resource> ResourceRequest<T> issueGetResourceRequest(URI resourceUri,
String resourceFormat,
String message,
long timeout) throws MalformedURLException {
long timeout) throws IOException {
Request.Builder request = new Request.Builder()
.url(resourceUri.toURL());
@ -96,7 +96,7 @@ public class Client {
byte[] payload,
String resourceFormat,
String message,
long timeout) throws MalformedURLException {
long timeout) throws IOException {
return issuePutRequest(resourceUri, payload, resourceFormat, null, message, timeout);
}
@ -105,7 +105,7 @@ public class Client {
String resourceFormat,
Headers headers,
String message,
long timeout) throws MalformedURLException {
long timeout) throws IOException {
if (payload == null) throw new EFhirClientException("PUT requests require a non-null payload");
RequestBody body = RequestBody.create(payload);
Request.Builder request = new Request.Builder()
@ -119,7 +119,7 @@ public class Client {
byte[] payload,
String resourceFormat,
String message,
long timeout) throws MalformedURLException {
long timeout) throws IOException {
return issuePostRequest(resourceUri, payload, resourceFormat, null, message, timeout);
}
@ -128,7 +128,7 @@ public class Client {
String resourceFormat,
Headers headers,
String message,
long timeout) throws MalformedURLException {
long timeout) throws IOException {
if (payload == null) throw new EFhirClientException("POST requests require a non-null payload");
RequestBody body = RequestBody.create(MediaType.parse(resourceFormat + ";charset=" + DEFAULT_CHARSET), payload);
Request.Builder request = new Request.Builder()
@ -138,14 +138,14 @@ public class Client {
return executeFhirRequest(request, resourceFormat, headers, message, retryCount, timeout);
}
public boolean issueDeleteRequest(URI resourceUri) throws MalformedURLException {
public boolean issueDeleteRequest(URI resourceUri) throws IOException {
Request.Builder request = new Request.Builder()
.url(resourceUri.toURL())
.delete();
return executeFhirRequest(request, null, null, null, retryCount, timeout).isSuccessfulRequest();
}
public Bundle issueGetFeedRequest(URI resourceUri, String resourceFormat) throws MalformedURLException {
public Bundle issueGetFeedRequest(URI resourceUri, String resourceFormat) throws IOException {
Request.Builder request = new Request.Builder()
.url(resourceUri.toURL());
@ -171,7 +171,7 @@ public class Client {
byte[] payload,
String resourceFormat,
String message,
int timeout) throws MalformedURLException {
int timeout) throws IOException {
if (payload == null) throw new EFhirClientException("POST requests require a non-null payload");
RequestBody body = RequestBody.create(MediaType.parse(resourceFormat + ";charset=" + DEFAULT_CHARSET), payload);
Request.Builder request = new Request.Builder()
@ -186,7 +186,7 @@ public class Client {
Headers headers,
String message,
int retryCount,
long timeout) {
long timeout) throws IOException {
return new FhirRequestBuilder(request)
.withLogger(logger)
.withResourceFormat(resourceFormat)
@ -202,7 +202,7 @@ public class Client {
Headers headers,
String message,
int retryCount,
long timeout) {
long timeout) throws IOException {
return new FhirRequestBuilder(request)
.withLogger(logger)
.withResourceFormat(resourceFormat)

View File

@ -1,6 +1,5 @@
package org.hl7.fhir.r5.utils.client.network;
import kotlin.Pair;
import okhttp3.*;
import org.apache.commons.lang3.StringUtils;
import org.hl7.fhir.r5.formats.IParser;
@ -15,7 +14,10 @@ import org.hl7.fhir.r5.utils.client.ResourceFormat;
import org.hl7.fhir.utilities.ToolingClientLogger;
import java.io.IOException;
import java.util.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
public class FhirRequestBuilder {
@ -200,31 +202,17 @@ public class FhirRequestBuilder {
return httpRequest.build();
}
public <T extends Resource> ResourceRequest<T> execute() {
public <T extends Resource> ResourceRequest<T> execute() throws IOException {
formatHeaders(httpRequest, resourceFormat, null);
try {
Response response = getHttpClient().newCall(httpRequest.build()).execute();
T resource = unmarshalReference(response, resourceFormat);
return new ResourceRequest<T>(resource, response.code(), getLocationHeader(response.headers()));
} catch (IOException e) {
e.printStackTrace();
}
return null;
Response response = getHttpClient().newCall(httpRequest.build()).execute();
T resource = unmarshalReference(response, resourceFormat);
return new ResourceRequest<T>(resource, response.code(), getLocationHeader(response.headers()));
}
public Bundle executeAsBatch() {
public Bundle executeAsBatch() throws IOException {
formatHeaders(httpRequest, resourceFormat, null);
try {
Response response = getHttpClient().newCall(httpRequest.build()).execute();
return unmarshalFeed(response, resourceFormat);
} catch (IOException e) {
e.printStackTrace();
}
return null;
Response response = getHttpClient().newCall(httpRequest.build()).execute();
return unmarshalFeed(response, resourceFormat);
}
/**
@ -316,9 +304,9 @@ public class FhirRequestBuilder {
* Logs the given {@link Response}, using the current {@link ToolingClientLogger}. If the current
* {@link FhirRequestBuilder#logger} is null, no action is taken.
*
* @param responseCode HTTP response code
* @param responseCode HTTP response code
* @param responseHeaders {@link Headers} from response
* @param responseBody Byte array response
* @param responseBody Byte array response
*/
protected void log(int responseCode, Headers responseHeaders, byte[] responseBody) {
if (logger != null) {