diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/context/HTMLClientLogger.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/context/HTMLClientLogger.java index 4096a5dc0..63c8170b8 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/context/HTMLClientLogger.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/context/HTMLClientLogger.java @@ -44,6 +44,7 @@ public class HTMLClientLogger extends BaseLogger implements ToolingClientLogger private static final boolean DEBUG = false; + private boolean req = false; private PrintStream file; public HTMLClientLogger(String log) { @@ -80,6 +81,7 @@ public class HTMLClientLogger extends BaseLogger implements ToolingClientLogger } } file.println(""); + req = true; } @Override @@ -87,6 +89,10 @@ public class HTMLClientLogger extends BaseLogger implements ToolingClientLogger if (DEBUG) { System.out.println(" txlog resp: " +outcome+" "+present(body)); } + if (!req) { + System.out.println("Record Response without request"); + } + req = false; if (file == null) return; file.println("
"); diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/utils/client/network/Client.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/utils/client/network/Client.java index f24e054fa..1eecc2c7e 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/utils/client/network/Client.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/utils/client/network/Client.java @@ -21,6 +21,7 @@ public class Client { private ToolingClientLogger logger; private int retryCount; private long timeout = DEFAULT_TIMEOUT; + private byte[] payload; public ToolingClientLogger getLogger() { return logger; @@ -50,6 +51,7 @@ public class Client { String resourceFormat, String message, long timeout) throws IOException { + this.payload = null; Request.Builder request = new Request.Builder() .method("OPTIONS", null) .url(optionsUri.toURL()); @@ -62,6 +64,7 @@ public class Client { Headers headers, String message, long timeout) throws IOException { + this.payload = null; Request.Builder request = new Request.Builder() .url(resourceUri.toURL()); @@ -86,6 +89,7 @@ public class Client { String message, long timeout) throws IOException { if (payload == null) throw new EFhirClientException("PUT requests require a non-null payload"); + this.payload = payload; RequestBody body = RequestBody.create(payload); Request.Builder request = new Request.Builder() .url(resourceUri.toURL()) @@ -109,6 +113,7 @@ public class Client { String message, long timeout) throws IOException { if (payload == null) throw new EFhirClientException("POST requests require a non-null payload"); + this.payload = payload; RequestBody body = RequestBody.create(MediaType.parse(resourceFormat + ";charset=" + DEFAULT_CHARSET), payload); Request.Builder request = new Request.Builder() .url(resourceUri.toURL()) @@ -174,7 +179,7 @@ public class Client { .withMessage(message) .withHeaders(headers == null ? new Headers.Builder().build() : headers) .withTimeout(timeout, TimeUnit.MILLISECONDS) - .executeAsBatch(); + .executeAsBatch(payload); } publicResourceRequest executeFhirRequest(Request.Builder request, @@ -190,6 +195,6 @@ public class Client { .withMessage(message) .withHeaders(headers == null ? new Headers.Builder().build() : headers) .withTimeout(timeout, TimeUnit.MILLISECONDS) - .execute(); + .execute(payload); } } diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/utils/client/network/FhirRequestBuilder.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/utils/client/network/FhirRequestBuilder.java index 1e55afe54..891e9c069 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/utils/client/network/FhirRequestBuilder.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/utils/client/network/FhirRequestBuilder.java @@ -1,6 +1,8 @@ package org.hl7.fhir.r5.utils.client.network; import okhttp3.*; +import okio.RealBufferedSink; + import org.apache.commons.lang3.StringUtils; import org.hl7.fhir.r5.formats.IParser; import org.hl7.fhir.r5.formats.JsonParser; @@ -201,18 +203,30 @@ public class FhirRequestBuilder { } protected Request buildRequest() { - return httpRequest.build(); + Request req = httpRequest.build(); + return req; } - public ResourceRequest execute() throws IOException { + public ResourceRequest execute(byte[] payload) throws IOException { formatHeaders(httpRequest, resourceFormat, headers); + logRequest(payload); Response response = getHttpClient().newCall(httpRequest.build()).execute(); T resource = unmarshalReference(response, resourceFormat); return new ResourceRequest (resource, response.code(), getLocationHeader(response.headers())); } - public Bundle executeAsBatch() throws IOException { + public void logRequest(byte[] payload) { + if (logger != null) { + List headerList = new ArrayList<>(Collections.emptyList()); + Map > headerMap = headers.toMultimap(); + headerMap.keySet().forEach(key -> headerMap.get(key).forEach(value -> headerList.add(key + ":" + value))); + logger.logRequest(httpRequest.getMethod$okhttp().toString(), httpRequest.getUrl$okhttp().toString(), headerList, payload); + } + } + + public Bundle executeAsBatch(byte[] payload) throws IOException { formatHeaders(httpRequest, resourceFormat, null); + logRequest(payload); Response response = getHttpClient().newCall(httpRequest.build()).execute(); return unmarshalFeed(response, resourceFormat); } @@ -302,6 +316,7 @@ 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.