cleaning up client

This commit is contained in:
leif stawnyczy 2024-08-01 08:55:51 -04:00
parent 5671c8c09b
commit a4b731d809
8 changed files with 141 additions and 12 deletions

View File

@ -71,4 +71,6 @@ public interface IHttpClient {
IHttpRequest createGetRequest(FhirContext theContext, EncodingEnum theEncoding);
IHttpRequest createRequest(HttpClientRequestParameters theParameters);
void addHeadersToRequest(IHttpRequest theRequest, EncodingEnum theEncodingEnum, FhirContext theContext);
}

View File

@ -87,13 +87,12 @@ public class ApacheRestfulClientFactory extends RestfulClientFactory {
public HttpClient getNativeHttpClient() {
if (myHttpClient == null) {
System.out.println("yyyy Socket timeout " + getSocketTimeout());
// TODO: Use of a deprecated method should be resolved.
RequestConfig defaultRequestConfig = RequestConfig.custom()
.setSocketTimeout(getSocketTimeout())
.setConnectTimeout(getConnectTimeout())
.setConnectionRequestTimeout(getConnectionRequestTimeout())
.setStaleConnectionCheckEnabled(true)
.setProxy(myProxy)
.build();
@ -106,6 +105,9 @@ public class ApacheRestfulClientFactory extends RestfulClientFactory {
new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS);
connectionManager.setMaxTotal(getPoolMaxTotal());
connectionManager.setDefaultMaxPerRoute(getPoolMaxPerRoute());
// default value for stale connection check
// this can be disabled (with a -ve value) if performance is bad
connectionManager.setValidateAfterInactivity(2000);
builder.setConnectionManager(connectionManager);
if (myProxy != null && isNotBlank(getProxyUsername()) && isNotBlank(getProxyPassword())) {

View File

@ -70,6 +70,7 @@ public abstract class BaseHttpClientInvocation {
// TODO implement
public IHttpRequest asHttpRequest(AsHttpRequestParams theParams) {
return asHttpRequest(
theParams.getUrlBase(),
theParams.getExtraParams(),
@ -106,7 +107,7 @@ public abstract class BaseHttpClientInvocation {
}
// todo
HttpClientRequestParameters clientRequestParameters =
new HttpClientRequestParameters(theParameters.getUrl(), RequestTypeEnum.GET);
new HttpClientRequestParameters(theParameters.getUrl(), theParameters.getRequestTypeEnum());
clientRequestParameters.setEncodingEnum(theParameters.getEncodingEnum());
return httpClient.createRequest(clientRequestParameters);
// return httpClient.createGetRequest(getContext(), theParameters.getEncodingEnum());

View File

@ -927,7 +927,6 @@ public class GenericClient extends BaseClient implements IGenericClient {
@Override
public MethodOutcome execute() {
Map<String, List<String>> additionalParams = new HashMap<>();
if (myCascadeMode != null) {
switch (myCascadeMode) {

View File

@ -24,12 +24,17 @@ import ca.uhn.fhir.context.FhirVersionEnum;
import ca.uhn.fhir.model.valueset.BundleTypeEnum;
import ca.uhn.fhir.parser.DataFormatException;
import ca.uhn.fhir.parser.IParser;
import ca.uhn.fhir.rest.api.Constants;
import ca.uhn.fhir.rest.api.EncodingEnum;
import ca.uhn.fhir.rest.api.IVersionSpecificBundleFactory;
import ca.uhn.fhir.rest.api.RequestTypeEnum;
import ca.uhn.fhir.rest.client.apache.ApacheHttpClient;
import ca.uhn.fhir.rest.client.api.Header;
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.model.AsHttpRequestParams;
import ca.uhn.fhir.rest.param.HttpClientRequestParameters;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.hl7.fhir.instance.model.api.IBaseBinary;
@ -106,6 +111,21 @@ abstract class BaseHttpClientInvocationWithContents extends BaseHttpClientInvoca
EncodingEnum theEncoding,
Boolean thePrettyPrint)
throws DataFormatException {
return asHttpRequest(
new AsHttpRequestParams().setUrlBase(theUrlBase)
.setExtraParams(theExtraParams)
.setEncodingEnum(theEncoding)
.setPrettyPrint(thePrettyPrint)
);
}
@Override
public IHttpRequest asHttpRequest(AsHttpRequestParams theParams) {
String theUrlBase = theParams.getUrlBase();
Map<String, List<String>> theExtraParams = theParams.getExtraParams();
EncodingEnum theEncoding = theParams.getEncodingEnum();
Boolean thePrettyPrint = theParams.getPrettyPrint();
StringBuilder url = new StringBuilder();
if (myUrlPath == null) {
@ -121,8 +141,15 @@ abstract class BaseHttpClientInvocationWithContents extends BaseHttpClientInvoca
}
appendExtraParamsWithQuestionMark(theExtraParams, url, url.indexOf("?") == -1);
IHttpClient httpClient = getRestfulClientFactory()
IHttpClient httpClient;
if (theParams.getClient() != null) {
// use the provided one
httpClient = theParams.getClient();
} else {
// make a new one
httpClient = getRestfulClientFactory()
.getHttpClient(url, myIfNoneExistParams, myIfNoneExistString, getRequestType(), getHeaders());
}
if (myResource != null && IBaseBinary.class.isAssignableFrom(myResource.getClass())) {
IBaseBinary binary = (IBaseBinary) myResource;
@ -140,12 +167,28 @@ abstract class BaseHttpClientInvocationWithContents extends BaseHttpClientInvoca
}
if (myParams != null) {
return httpClient.createParamRequest(getContext(), myParams, encoding);
IHttpRequest request = httpClient.createParamRequest(getContext(), myParams, encoding);
return request;
}
encoding = ObjectUtils.defaultIfNull(encoding, EncodingEnum.JSON);
String contents = encodeContents(thePrettyPrint, encoding);
String contentType = getContentType(encoding);
return httpClient.createByteRequest(getContext(), contents, contentType, encoding);
HttpClientRequestParameters parameters = new HttpClientRequestParameters(
url.toString(),
getRequestType()
);
parameters.setContents(contents);
parameters.setContentType(contentType);
parameters.setFhirContext(getContext());
parameters.setEncodingEnum(encoding);
IHttpRequest request = httpClient.createRequest(parameters);
for (Header header : getHeaders()) {
request.addHeader(header.getName(), header.getValue());
}
httpClient.addHeadersToRequest(request, theEncoding, parameters.getFhirContext());
request.addHeader(Constants.HEADER_CONTENT_TYPE, contentType + Constants.HEADER_SUFFIX_CT_UTF_8);
return request;
// return httpClient.createByteRequest(getContext(), contents, contentType, encoding);
}
private String getContentType(EncodingEnum encoding) {

View File

@ -24,6 +24,8 @@ import ca.uhn.fhir.rest.api.EncodingEnum;
import ca.uhn.fhir.rest.api.RequestTypeEnum;
import ca.uhn.fhir.rest.client.api.IHttpRequest;
import ca.uhn.fhir.rest.client.impl.BaseHttpClientInvocation;
import ca.uhn.fhir.rest.client.model.AsHttpRequestParams;
import ca.uhn.fhir.rest.client.model.CreateRequestParameters;
import org.hl7.fhir.instance.model.api.IIdType;
import java.util.List;
@ -54,6 +56,22 @@ public class HttpDeleteClientInvocation extends BaseHttpClientInvocation {
Map<String, List<String>> theExtraParams,
EncodingEnum theEncoding,
Boolean thePrettyPrint) {
return asHttpRequest(
new AsHttpRequestParams()
.setUrlBase(theUrlBase)
.setExtraParams(theExtraParams)
.setEncodingEnum(theEncoding)
.setPrettyPrint(thePrettyPrint)
);
}
@Override
public IHttpRequest asHttpRequest(AsHttpRequestParams theParams) {
String theUrlBase = theParams.getUrlBase();
Map<String, List<String>> theExtraParams = theParams.getExtraParams();
EncodingEnum theEncoding = theParams.getEncodingEnum();
Boolean thePrettyPrint = theParams.getPrettyPrint();
StringBuilder b = new StringBuilder();
b.append(theUrlBase);
if (!theUrlBase.endsWith("/")) {
@ -64,6 +82,12 @@ public class HttpDeleteClientInvocation extends BaseHttpClientInvocation {
appendExtraParamsWithQuestionMark(myParams, b, b.indexOf("?") == -1);
appendExtraParamsWithQuestionMark(theExtraParams, b, b.indexOf("?") == -1);
return createHttpRequest(b.toString(), theEncoding, RequestTypeEnum.DELETE);
// return createHttpRequest(b.toString(), theEncoding, RequestTypeEnum.DELETE);
CreateRequestParameters requestParameters = new CreateRequestParameters();
requestParameters.setClient(theParams.getClient());
requestParameters.setRequestTypeEnum(RequestTypeEnum.DELETE);
requestParameters.setEncodingEnum(theEncoding);
requestParameters.setUrl(b.toString());
return createHttpRequest(requestParameters);
}
}

View File

@ -25,6 +25,8 @@ import ca.uhn.fhir.rest.api.RequestTypeEnum;
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.model.AsHttpRequestParams;
import ca.uhn.fhir.rest.client.model.CreateRequestParameters;
import org.hl7.fhir.instance.model.api.IIdType;
import java.util.List;
@ -58,6 +60,22 @@ public class HttpPatchClientInvocation extends BaseHttpClientInvocation {
Map<String, List<String>> theExtraParams,
EncodingEnum theEncoding,
Boolean thePrettyPrint) {
return asHttpRequest(
new AsHttpRequestParams()
.setUrlBase(theUrlBase)
.setExtraParams(theExtraParams)
.setEncodingEnum(theEncoding)
.setPrettyPrint(thePrettyPrint)
);
}
@Override
public IHttpRequest asHttpRequest(AsHttpRequestParams theParams) {
String theUrlBase = theParams.getUrlBase();
Map<String, List<String>> theExtraParams = theParams.getExtraParams();
EncodingEnum theEncoding = theParams.getEncodingEnum();
Boolean thePrettyPrint = theParams.getPrettyPrint();
StringBuilder b = new StringBuilder();
b.append(theUrlBase);
if (!theUrlBase.endsWith("/")) {
@ -68,7 +86,13 @@ public class HttpPatchClientInvocation extends BaseHttpClientInvocation {
appendExtraParamsWithQuestionMark(myParams, b, b.indexOf("?") == -1);
appendExtraParamsWithQuestionMark(theExtraParams, b, b.indexOf("?") == -1);
return createHttpRequest(b.toString(), theEncoding, RequestTypeEnum.PATCH);
// return createHttpRequest(b.toString(), theEncoding, RequestTypeEnum.PATCH);
CreateRequestParameters requestParameters = new CreateRequestParameters();
requestParameters.setClient(theParams.getClient());
requestParameters.setUrl(b.toString());
requestParameters.setEncodingEnum(theEncoding);
requestParameters.setRequestTypeEnum(RequestTypeEnum.PATCH);
return createHttpRequest(requestParameters);
}
@Override
@ -77,4 +101,17 @@ public class HttpPatchClientInvocation extends BaseHttpClientInvocation {
.getHttpClient(new StringBuilder(theUrl), null, null, theRequestType, getHeaders());
return httpClient.createByteRequest(getContext(), myContents, myContentType, null);
}
@Override
protected IHttpRequest createHttpRequest(CreateRequestParameters theParameters) {
IHttpClient client;
if (theParameters.getClient() == null) {
client = getRestfulClientFactory()
.getHttpClient(new StringBuilder(theParameters.getUrl()), null, null, theParameters.getRequestTypeEnum(), getHeaders());
} else {
client = theParameters.getClient();
}
// preserving behaviour
return client.createByteRequest(getContext(), myContents, myContentType, null);
}
}

View File

@ -25,6 +25,8 @@ import ca.uhn.fhir.rest.api.PagingHttpMethodEnum;
import ca.uhn.fhir.rest.client.api.IHttpRequest;
import ca.uhn.fhir.rest.client.api.UrlSourceEnum;
import ca.uhn.fhir.rest.client.impl.BaseHttpClientInvocation;
import ca.uhn.fhir.rest.client.model.AsHttpRequestParams;
import ca.uhn.fhir.rest.client.model.CreateRequestParameters;
import java.util.List;
import java.util.Map;
@ -49,9 +51,28 @@ public class HttpSimpleClientInvocation extends BaseHttpClientInvocation {
Map<String, List<String>> theExtraParams,
EncodingEnum theEncoding,
Boolean thePrettyPrint) {
IHttpRequest retVal = createHttpRequest(myUrl, theEncoding, myPagingHttpMethod.getRequestType());
retVal.setUrlSource(myUrlSource);
return retVal;
return asHttpRequest(
new AsHttpRequestParams()
.setUrlBase(myUrl)
.setExtraParams(theExtraParams)
.setEncodingEnum(theEncoding)
.setPrettyPrint(thePrettyPrint)
);
}
@Override
public IHttpRequest asHttpRequest(AsHttpRequestParams theParams) {
CreateRequestParameters parameters = new CreateRequestParameters();
parameters.setUrl(theParams.getUrlBase());
parameters.setEncodingEnum(theParams.getEncodingEnum());
parameters.setRequestTypeEnum(myPagingHttpMethod.getRequestType());
parameters.setClient(theParams.getClient());
IHttpRequest request = createHttpRequest(parameters);
request.setUrlSource(myUrlSource);
return request;
// IHttpRequest retVal = createHttpRequest(myUrl, theEncoding, myPagingHttpMethod.getRequestType());
// retVal.setUrlSource(myUrlSource);
// return retVal;
}
public void setUrlSource(UrlSourceEnum theUrlSource) {