From 5671c8c09bb48469502679020619f9db908e4393 Mon Sep 17 00:00:00 2001 From: leif stawnyczy Date: Tue, 30 Jul 2024 17:56:26 -0400 Subject: [PATCH] implementing most --- .../okhttp/client/OkHttpRestfulClient.java | 10 ++++ .../rest/client/apache/ApacheHttpClient.java | 6 --- .../fhir/jaxrs/client/JaxRsHttpClient.java | 47 +++++++++++++++++-- 3 files changed, 53 insertions(+), 10 deletions(-) diff --git a/hapi-fhir-client-okhttp/src/main/java/ca/uhn/fhir/okhttp/client/OkHttpRestfulClient.java b/hapi-fhir-client-okhttp/src/main/java/ca/uhn/fhir/okhttp/client/OkHttpRestfulClient.java index f55aa7b79f4..44eafcec143 100644 --- a/hapi-fhir-client-okhttp/src/main/java/ca/uhn/fhir/okhttp/client/OkHttpRestfulClient.java +++ b/hapi-fhir-client-okhttp/src/main/java/ca/uhn/fhir/okhttp/client/OkHttpRestfulClient.java @@ -29,6 +29,7 @@ import ca.uhn.fhir.rest.client.api.IHttpClient; import ca.uhn.fhir.rest.client.api.IHttpRequest; import ca.uhn.fhir.rest.client.impl.BaseHttpClientInvocation; import ca.uhn.fhir.rest.client.method.MethodUtil; +import ca.uhn.fhir.rest.param.HttpClientRequestParameters; import okhttp3.Call; import okhttp3.FormBody; import okhttp3.MediaType; @@ -127,6 +128,15 @@ public class OkHttpRestfulClient implements IHttpClient { return myRequest; } + @Override + public IHttpRequest createRequest(HttpClientRequestParameters theParameters) { + initBaseRequest( + theParameters.getFhirContext(), + theParameters.getEncodingEnum(), + createPostBody(theParameters.getByteContents(), theParameters.getContents())); + return myRequest; + } + private void addHeadersToRequest( OkHttpRestfulRequest theHttpRequest, EncodingEnum theEncoding, FhirContext theContext) { if (myHeaders != null) { diff --git a/hapi-fhir-client/src/main/java/ca/uhn/fhir/rest/client/apache/ApacheHttpClient.java b/hapi-fhir-client/src/main/java/ca/uhn/fhir/rest/client/apache/ApacheHttpClient.java index d5cae381aa8..b472e0c862a 100644 --- a/hapi-fhir-client/src/main/java/ca/uhn/fhir/rest/client/apache/ApacheHttpClient.java +++ b/hapi-fhir-client/src/main/java/ca/uhn/fhir/rest/client/apache/ApacheHttpClient.java @@ -132,12 +132,6 @@ public class ApacheHttpClient extends BaseHttpClient implements IHttpClient { return new ApacheHttpRequest(myClient, request); } - // @Override - // public IHttpRequest createHttpRequest(CreateHttpRequestParameters theCreateHttpRequestParameters) { - // HttpRequestBase request = constructRequestBase(theCreateHttpRequestParameters); - // return new ApacheHttpRequest(myClient, request); - // } - @Override protected IHttpRequest createHttpRequest() { return createHttpRequest((HttpEntity) null); diff --git a/hapi-fhir-jaxrsserver-base/src/main/java/ca/uhn/fhir/jaxrs/client/JaxRsHttpClient.java b/hapi-fhir-jaxrsserver-base/src/main/java/ca/uhn/fhir/jaxrs/client/JaxRsHttpClient.java index a929560c2a5..bf550678077 100644 --- a/hapi-fhir-jaxrsserver-base/src/main/java/ca/uhn/fhir/jaxrs/client/JaxRsHttpClient.java +++ b/hapi-fhir-jaxrsserver-base/src/main/java/ca/uhn/fhir/jaxrs/client/JaxRsHttpClient.java @@ -29,6 +29,7 @@ import ca.uhn.fhir.rest.client.api.IHttpClient; import ca.uhn.fhir.rest.client.api.IHttpRequest; import ca.uhn.fhir.rest.client.impl.BaseHttpClientInvocation; import ca.uhn.fhir.rest.client.method.MethodUtil; +import ca.uhn.fhir.rest.param.HttpClientRequestParameters; import jakarta.ws.rs.client.Client; import jakarta.ws.rs.client.Entity; import jakarta.ws.rs.client.Invocation.Builder; @@ -37,9 +38,12 @@ import jakarta.ws.rs.core.MultivaluedHashMap; import jakarta.ws.rs.core.MultivaluedMap; import org.hl7.fhir.instance.model.api.IBaseBinary; +import java.util.HashMap; import java.util.List; import java.util.Map; +import static org.apache.commons.lang3.StringUtils.isNotBlank; + /** * A Http Request based on JaxRs. This is an adapter around the class * {@link jakarta.ws.rs.client.Client Client} @@ -83,7 +87,14 @@ public class JaxRsHttpClient implements IHttpClient { @Override public IHttpRequest createParamRequest( FhirContext theContext, Map> theParams, EncodingEnum theEncoding) { - MultivaluedMap map = new MultivaluedHashMap(); + Entity
entity = getFormEntity(theParams); + JaxRsHttpRequest retVal = createHttpRequest(entity); + addHeadersToRequest(retVal, theEncoding, theContext); + return retVal; + } + + private static Entity getFormEntity(Map> theParams) { + MultivaluedMap map = new MultivaluedHashMap<>(); for (Map.Entry> nextParam : theParams.entrySet()) { List value = nextParam.getValue(); for (String s : value) { @@ -91,9 +102,7 @@ public class JaxRsHttpClient implements IHttpClient { } } Entity entity = Entity.form(map); - JaxRsHttpRequest retVal = createHttpRequest(entity); - addHeadersToRequest(retVal, theEncoding, theContext); - return retVal; + return entity; } @Override @@ -111,6 +120,36 @@ public class JaxRsHttpClient implements IHttpClient { return result; } + @Override + public IHttpRequest createRequest(HttpClientRequestParameters theParameters) { + Map additionalHeaders = new HashMap<>(); + Entity entity; + switch (theParameters.getRequestTypeEnum()) { + case POST: + case PUT: + if (theParameters.getBaseBinary() != null) { + entity = Entity.entity(theParameters.getBaseBinary().getContentAsBase64(), theParameters.getBaseBinary().getContentType()); + } else if (theParameters.getByteContents() != null) { + entity = Entity.entity(theParameters.getByteContents(), theParameters.getContentType() + Constants.HEADER_SUFFIX_CT_UTF_8); + additionalHeaders.put(Constants.HEADER_CONTENT_TYPE, theParameters.getContentType() + Constants.HEADER_SUFFIX_CT_UTF_8); + } else if (isNotBlank(theParameters.getContents())) { + entity = Entity.entity(theParameters.getContents(), theParameters.getContentType() + Constants.HEADER_SUFFIX_CT_UTF_8); + additionalHeaders.put(Constants.HEADER_CONTENT_TYPE, theParameters.getContentType() + Constants.HEADER_SUFFIX_CT_UTF_8); + } else { + entity = getFormEntity(theParameters.getFormParams()); + } + break; + default: + entity = null; + } + JaxRsHttpRequest request = createHttpRequest(entity); + for (Map.Entry entrySet : additionalHeaders.entrySet()) { + request.addHeader(entrySet.getKey(), entrySet.getValue()); + } + addHeadersToRequest(request, theParameters.getEncodingEnum(), theParameters.getFhirContext()); + return request; + } + public void addHeadersToRequest(JaxRsHttpRequest theHttpRequest, EncodingEnum theEncoding, FhirContext theContext) { if (myHeaders != null) { for (Header next : myHeaders) {