From e997f7bb084d2882212ed46c0219fd3a0698f1d4 Mon Sep 17 00:00:00 2001 From: dotasek Date: Tue, 23 Nov 2021 14:09:42 -0500 Subject: [PATCH] Add request logging for dstu3 FhirRequestBuilder --- .../client/network/FhirRequestBuilder.java | 29 +++++++++++++++++-- .../hl7/fhir/r5/context/HTMLClientLogger.java | 5 ++-- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/org.hl7.fhir.dstu3/src/main/java/org/hl7/fhir/dstu3/utils/client/network/FhirRequestBuilder.java b/org.hl7.fhir.dstu3/src/main/java/org/hl7/fhir/dstu3/utils/client/network/FhirRequestBuilder.java index 3508a9adb..870957cda 100644 --- a/org.hl7.fhir.dstu3/src/main/java/org/hl7/fhir/dstu3/utils/client/network/FhirRequestBuilder.java +++ b/org.hl7.fhir.dstu3/src/main/java/org/hl7/fhir/dstu3/utils/client/network/FhirRequestBuilder.java @@ -206,14 +206,19 @@ public class FhirRequestBuilder { public ResourceRequest execute() throws IOException { formatHeaders(httpRequest, resourceFormat, headers); - Response response = getHttpClient().newCall(httpRequest.build()).execute(); + final Request request = httpRequest.build(); + log(request.method(), request.url().toString(), request.headers(), request.body() != null ? request.body().toString().getBytes() : null); + Response response = getHttpClient().newCall(request).execute(); T resource = unmarshalReference(response, resourceFormat); return new ResourceRequest(resource, response.code(), getLocationHeader(response.headers())); } public Bundle executeAsBatch() throws IOException { formatHeaders(httpRequest, resourceFormat, null); - Response response = getHttpClient().newCall(httpRequest.build()).execute(); + final Request request = httpRequest.build(); + log(request.method(), request.url().toString(), request.headers(), request.body() != null ? request.body().toString().getBytes() : null); + + Response response = getHttpClient().newCall(request).execute(); return unmarshalFeed(response, resourceFormat); } @@ -302,6 +307,26 @@ public class FhirRequestBuilder { } } + /** + * Logs the given {@link Request}, using the current {@link ToolingClientLogger}. If the current + * {@link FhirRequestBuilder#logger} is null, no action is taken. + * + * @param method HTTP request method + * @param url request URL + * @param requestHeaders {@link Headers} for request + * @param requestBody Byte array request + */ + protected void log(String method, String url, Headers requestHeaders, byte[] requestBody) { + if (logger != null) { + List headerList = new ArrayList<>(Collections.emptyList()); + Map> headerMap = requestHeaders.toMultimap(); + headerMap.keySet().forEach(key -> headerMap.get(key).forEach(value -> headerList.add(key + ":" + value))); + + logger.logRequest(method, url, headerList, requestBody); + } + + } + /** * Logs the given {@link Response}, using the current {@link ToolingClientLogger}. If the current * {@link FhirRequestBuilder#logger} is null, no action is taken. 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 122dd2a3a..9d6803e15 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 @@ -46,7 +46,7 @@ public class HTMLClientLogger extends BaseLogger implements ToolingClientLogger private boolean req = false; private PrintStream file; - + public HTMLClientLogger(String log) { if (log != null) { try { @@ -89,12 +89,13 @@ public class HTMLClientLogger extends BaseLogger implements ToolingClientLogger if (DEBUG) { System.out.println(" txlog resp: " +outcome+" "+present(body)); } - req = false; + if (file == null) return; if (!req) { System.out.println("Record Response without request"); } + req = false; file.println("
");
     file.println(outcome);
     for (String s : headers)