Removing leftover okttp usage
This commit is contained in:
parent
a5ef974c3d
commit
2fdfaf8602
|
@ -25,16 +25,8 @@ import org.hl7.fhir.utilities.ToolingClientLogger;
|
|||
import org.hl7.fhir.utilities.http.*;
|
||||
import org.hl7.fhir.utilities.xhtml.XhtmlUtils;
|
||||
|
||||
import okhttp3.Authenticator;
|
||||
import okhttp3.Credentials;
|
||||
import okhttp3.Headers;
|
||||
import okhttp3.Request;
|
||||
|
||||
public class FhirRequestBuilder {
|
||||
|
||||
protected static final String HTTP_PROXY_USER = "http.proxyUser";
|
||||
protected static final String HTTP_PROXY_PASS = "http.proxyPassword";
|
||||
protected static final String HEADER_PROXY_AUTH = "Proxy-Authorization";
|
||||
protected static final String LOCATION_HEADER = "location";
|
||||
protected static final String CONTENT_LOCATION_HEADER = "content-location";
|
||||
protected static final String DEFAULT_CHARSET = "UTF-8";
|
||||
|
@ -72,11 +64,11 @@ public class FhirRequestBuilder {
|
|||
|
||||
/**
|
||||
* Adds necessary default headers, formatting headers, and any passed in
|
||||
* {@link Headers} to the passed in {@link okhttp3.Request.Builder}
|
||||
* {@link HTTPHeader}s to the passed in {@link okhttp3.Request.Builder}
|
||||
*
|
||||
* @param request {@link okhttp3.Request.Builder} to add headers to.
|
||||
* @param format Expected {@link Resource} format.
|
||||
* @param headers Any additional {@link Headers} to add to the request.
|
||||
* @param headers Any additional {@link HTTPHeader}s to add to the request.
|
||||
*/
|
||||
protected static HTTPRequest formatHeaders(HTTPRequest request, String format, Iterable<HTTPHeader> headers) {
|
||||
List<HTTPHeader> allHeaders = new ArrayList<>();
|
||||
|
@ -99,39 +91,6 @@ public class FhirRequestBuilder {
|
|||
return headers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds necessary headers for all REST requests.
|
||||
* <li>User-Agent : hapi-fhir-tooling-client</li>
|
||||
*
|
||||
* @param request {@link Request.Builder} to add default headers to.
|
||||
*/
|
||||
protected static void addDefaultHeaders(Request.Builder request, Headers headers) {
|
||||
if (headers == null || !headers.names().contains("User-Agent")) {
|
||||
request.addHeader("User-Agent", "hapi-fhir-tooling-client");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds necessary headers for the given resource format provided.
|
||||
*
|
||||
* @param request {@link Request.Builder} to add default headers to.
|
||||
*/
|
||||
protected static void addResourceFormatHeaders(Request.Builder request, String format) {
|
||||
request.addHeader("Accept", format);
|
||||
request.addHeader("Content-Type", format + ";charset=" + DEFAULT_CHARSET);
|
||||
}
|
||||
|
||||
/**
|
||||
* Iterates through the passed in {@link Headers} and adds them to the provided
|
||||
* {@link Request.Builder}.
|
||||
*
|
||||
* @param request {@link Request.Builder} to add headers to.
|
||||
* @param headers {@link Headers} to add to request.
|
||||
*/
|
||||
protected static void addHeaders(Request.Builder request, Headers headers) {
|
||||
headers.forEach(header -> request.addHeader(header.getFirst(), header.getSecond()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if any of the
|
||||
* {@link org.hl7.fhir.r4.model.OperationOutcome.OperationOutcomeIssueComponent}
|
||||
|
@ -169,20 +128,6 @@ public class FhirRequestBuilder {
|
|||
return new ManagedFhirWebAccessBuilder("hapi-fhir-tooling-client", null).withRetries(retryCount).withTimeout(timeout, timeoutUnit).withLogger(logger);
|
||||
}
|
||||
|
||||
|
||||
@Nonnull
|
||||
private static Authenticator getAuthenticator() {
|
||||
return (route, response) -> {
|
||||
final String httpProxyUser = System.getProperty(HTTP_PROXY_USER);
|
||||
final String httpProxyPass = System.getProperty(HTTP_PROXY_PASS);
|
||||
if (httpProxyUser != null && httpProxyPass != null) {
|
||||
String credential = Credentials.basic(httpProxyUser, httpProxyPass);
|
||||
return response.request().newBuilder().header(HEADER_PROXY_AUTH, credential).build();
|
||||
}
|
||||
return response.request().newBuilder().build();
|
||||
};
|
||||
}
|
||||
|
||||
public FhirRequestBuilder withResourceFormat(String resourceFormat) {
|
||||
this.resourceFormat = resourceFormat;
|
||||
return this;
|
||||
|
|
|
@ -0,0 +1,104 @@
|
|||
package org.hl7.fhir.r4.utils.client.network;
|
||||
|
||||
import org.hl7.fhir.exceptions.FHIRException;
|
||||
import org.hl7.fhir.utilities.http.HTTPHeader;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class ClientHeadersTest {
|
||||
ClientHeaders clientHeaders;
|
||||
|
||||
HTTPHeader h1 = new HTTPHeader("header1", "value1");
|
||||
HTTPHeader h2 = new HTTPHeader("header2", "value2");
|
||||
HTTPHeader h3 = new HTTPHeader("header3", "value3");
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
clientHeaders = new ClientHeaders();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Happy path add headers individually.")
|
||||
void addHeader() {
|
||||
clientHeaders.addHeader(h1);
|
||||
Assertions.assertEquals(1, clientHeaders.headers().size());
|
||||
clientHeaders.addHeader(h2);
|
||||
Assertions.assertEquals(2, clientHeaders.headers().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Test duplicate header added individually throws FHIRException.")
|
||||
void addHeaderDuplicateAdd() {
|
||||
clientHeaders.addHeader(h1);
|
||||
Assertions.assertThrows(FHIRException.class, () -> clientHeaders.addHeader(h1));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Happy path add headers as list.")
|
||||
void addHeaders() {
|
||||
List<HTTPHeader> headersList = Arrays.asList(h1, h2, h3);
|
||||
clientHeaders.addHeaders(headersList);
|
||||
Assertions.assertEquals(3, clientHeaders.headers().size());
|
||||
Assertions.assertEquals(headersList, clientHeaders.headers());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Happy path add headers as list.")
|
||||
void addHeadersDuplicateAdd() {
|
||||
List<HTTPHeader> headersList = Arrays.asList(h1, h2, h1);
|
||||
Assertions.assertThrows(FHIRException.class, () -> clientHeaders.addHeaders(headersList));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Happy path remove existing header.")
|
||||
void removeHeader() {
|
||||
clientHeaders.addHeader(h1);
|
||||
clientHeaders.addHeader(h2);
|
||||
clientHeaders.addHeader(h3);
|
||||
clientHeaders.removeHeader(h2);
|
||||
Assertions.assertEquals(2, clientHeaders.headers().size());
|
||||
clientHeaders.removeHeader(new HTTPHeader("header3", "value3"));
|
||||
Assertions.assertEquals(1, clientHeaders.headers().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Remove header not contained in list.")
|
||||
void removeHeaderUnknown() {
|
||||
clientHeaders.addHeader(h1);
|
||||
clientHeaders.addHeader(h2);
|
||||
Assertions.assertThrows(FHIRException.class, () -> clientHeaders.removeHeader(h3));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Happy path remove list of existing headers.")
|
||||
void removeHeaders() {
|
||||
List<HTTPHeader> headersToAddList = Arrays.asList(h1, h2, h3);
|
||||
List<HTTPHeader> headersToRemoveList = Arrays.asList(h2, h3);
|
||||
clientHeaders.addHeaders(headersToAddList);
|
||||
clientHeaders.removeHeaders(headersToRemoveList);
|
||||
Assertions.assertEquals(1, clientHeaders.headers().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Remove list containing unknown header.")
|
||||
void removeHeadersUnknown() {
|
||||
List<HTTPHeader> headersToAddList = Arrays.asList(h1, h3);
|
||||
List<HTTPHeader> headersToRemoveList = Arrays.asList(h2, h3);
|
||||
clientHeaders.addHeaders(headersToAddList);
|
||||
Assertions.assertThrows(FHIRException.class, () -> clientHeaders.removeHeaders(headersToRemoveList));
|
||||
}
|
||||
|
||||
@Test
|
||||
void clearHeaders() {
|
||||
List<HTTPHeader> headersToAddList = Arrays.asList(h1, h3);
|
||||
clientHeaders.addHeaders(headersToAddList);
|
||||
Assertions.assertEquals(2, clientHeaders.headers().size());
|
||||
clientHeaders.clearHeaders();
|
||||
Assertions.assertEquals(0, clientHeaders.headers().size());
|
||||
}
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package org.hl7.fhir.r4.utils.client;
|
||||
package org.hl7.fhir.r4.utils.client.network;
|
||||
|
||||
import okhttp3.HttpUrl;
|
||||
import okhttp3.mockwebserver.MockWebServer;
|
||||
|
@ -7,9 +7,6 @@ import okhttp3.mockwebserver.RecordedRequest;
|
|||
import org.hl7.fhir.r4.context.HTMLClientLogger;
|
||||
import org.hl7.fhir.r4.formats.JsonParser;
|
||||
import org.hl7.fhir.r4.model.*;
|
||||
import org.hl7.fhir.r4.utils.client.network.ByteUtils;
|
||||
import org.hl7.fhir.r4.utils.client.network.Client;
|
||||
import org.hl7.fhir.r4.utils.client.network.ResourceRequest;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.DisplayName;
|
|
@ -0,0 +1,153 @@
|
|||
package org.hl7.fhir.r4.utils.client.network;
|
||||
|
||||
import okhttp3.Request;
|
||||
import org.hl7.fhir.utilities.http.HTTPHeader;
|
||||
import org.hl7.fhir.utilities.http.HTTPHeaderUtil;
|
||||
import org.hl7.fhir.utilities.http.HTTPRequest;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class FhirRequestBuilderTest {
|
||||
@Test
|
||||
@DisplayName("Test resource format headers are added correctly.")
|
||||
void addResourceFormatHeadersGET() {
|
||||
String testFormat = "yaml";
|
||||
HTTPRequest request = new HTTPRequest().withUrl("http://www.google.com").withMethod(HTTPRequest.HttpMethod.GET);
|
||||
|
||||
Iterable<HTTPHeader> headers = FhirRequestBuilder.getResourceFormatHeaders(request, testFormat);
|
||||
|
||||
Map<String, List<String>> headersMap = HTTPHeaderUtil.getMultimap(headers);
|
||||
Assertions.assertNotNull(headersMap.get("Accept"), "Accept header null.");
|
||||
Assertions.assertEquals(testFormat, headersMap.get("Accept").get(0),
|
||||
"Accept header not populated with expected value " + testFormat + ".");
|
||||
|
||||
Assertions.assertNull(headersMap.get("Content-Type"), "Content-Type header null.");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Test resource format headers are added correctly (POST).")
|
||||
void addResourceFormatHeadersPOST() {
|
||||
//FIXME tested here. Should get list of HTTPHeader.
|
||||
String testFormat = "yaml";
|
||||
HTTPRequest request = new HTTPRequest().withUrl("http://www.google.com").withMethod(HTTPRequest.HttpMethod.POST);
|
||||
|
||||
Iterable<HTTPHeader> headers = FhirRequestBuilder.getResourceFormatHeaders(request, testFormat);
|
||||
|
||||
Map<String, List<String>> headersMap = HTTPHeaderUtil.getMultimap(headers);
|
||||
Assertions.assertNotNull(headersMap.get("Accept"), "Accept header null.");
|
||||
Assertions.assertEquals(testFormat, headersMap.get("Accept").get(0),
|
||||
"Accept header not populated with expected value " + testFormat + ".");
|
||||
|
||||
Assertions.assertNotNull(headersMap.get("Content-Type"), "Content-Type header null.");
|
||||
Assertions.assertEquals(testFormat + ";charset=" + FhirRequestBuilder.DEFAULT_CHARSET, headersMap.get("Content-Type").get(0),
|
||||
"Content-Type header not populated with expected value \"" + testFormat + ";charset=" + FhirRequestBuilder.DEFAULT_CHARSET + "\".");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Test a list of provided headers are added correctly.")
|
||||
void addHeaders() {
|
||||
String headerName1 = "headerName1";
|
||||
String headerValue1 = "headerValue1";
|
||||
String headerName2 = "headerName2";
|
||||
String headerValue2 = "headerValue2";
|
||||
|
||||
List<HTTPHeader> headers = List.of(
|
||||
new HTTPHeader(headerName1, headerValue1),
|
||||
new HTTPHeader(headerName2, headerValue2)
|
||||
);
|
||||
|
||||
Request.Builder request = new Request.Builder().url("http://www.google.com");
|
||||
FhirRequestBuilder.addHeaders(request, headers);
|
||||
|
||||
Map<String, List<String>> headersMap = request.build().headers().toMultimap();
|
||||
Assertions.assertNotNull(headersMap.get(headerName1), headerName1 + " header null.");
|
||||
Assertions.assertEquals(headerValue1, headersMap.get(headerName1).get(0),
|
||||
headerName1 + " header not populated with expected value " + headerValue1 + ".");
|
||||
Assertions.assertNotNull(headersMap.get(headerName2), headerName2 + " header null.");
|
||||
Assertions.assertEquals(headerValue2, headersMap.get(headerName2).get(0),
|
||||
headerName2 + " header not populated with expected value " + headerValue2 + ".");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Test that FATAL issue severity triggers error.")
|
||||
void hasErrorTestFatal() {
|
||||
OperationOutcome outcome = new OperationOutcome();
|
||||
outcome.addIssue(
|
||||
new OperationOutcome.OperationOutcomeIssueComponent().setSeverity(OperationOutcome.IssueSeverity.INFORMATION));
|
||||
outcome.addIssue(
|
||||
new OperationOutcome.OperationOutcomeIssueComponent().setSeverity(OperationOutcome.IssueSeverity.NULL));
|
||||
outcome.addIssue(
|
||||
new OperationOutcome.OperationOutcomeIssueComponent().setSeverity(OperationOutcome.IssueSeverity.WARNING));
|
||||
outcome.addIssue(
|
||||
new OperationOutcome.OperationOutcomeIssueComponent().setSeverity(OperationOutcome.IssueSeverity.FATAL));
|
||||
Assertions.assertTrue(FhirRequestBuilder.hasError(outcome), "Error check not triggered for FATAL issue severity.");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Test that ERROR issue severity triggers error.")
|
||||
void hasErrorTestError() {
|
||||
OperationOutcome outcome = new OperationOutcome();
|
||||
outcome.addIssue(
|
||||
new OperationOutcome.OperationOutcomeIssueComponent().setSeverity(OperationOutcome.IssueSeverity.INFORMATION));
|
||||
outcome.addIssue(
|
||||
new OperationOutcome.OperationOutcomeIssueComponent().setSeverity(OperationOutcome.IssueSeverity.NULL));
|
||||
outcome.addIssue(
|
||||
new OperationOutcome.OperationOutcomeIssueComponent().setSeverity(OperationOutcome.IssueSeverity.WARNING));
|
||||
outcome.addIssue(
|
||||
new OperationOutcome.OperationOutcomeIssueComponent().setSeverity(OperationOutcome.IssueSeverity.ERROR));
|
||||
Assertions.assertTrue(FhirRequestBuilder.hasError(outcome), "Error check not triggered for ERROR issue severity.");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Test that no FATAL or ERROR issue severity does not trigger error.")
|
||||
void hasErrorTestNoErrors() {
|
||||
OperationOutcome outcome = new OperationOutcome();
|
||||
outcome.addIssue(
|
||||
new OperationOutcome.OperationOutcomeIssueComponent().setSeverity(OperationOutcome.IssueSeverity.INFORMATION));
|
||||
outcome.addIssue(
|
||||
new OperationOutcome.OperationOutcomeIssueComponent().setSeverity(OperationOutcome.IssueSeverity.NULL));
|
||||
outcome.addIssue(
|
||||
new OperationOutcome.OperationOutcomeIssueComponent().setSeverity(OperationOutcome.IssueSeverity.WARNING));
|
||||
Assertions.assertFalse(FhirRequestBuilder.hasError(outcome), "Error check triggered unexpectedly.");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Test that getLocationHeader returns header for 'location'.")
|
||||
void getLocationHeaderWhenOnlyLocationIsSet() {
|
||||
final String expectedLocationHeader = "location_header_value";
|
||||
Iterable<HTTPHeader> headers = List.of(new HTTPHeader(FhirRequestBuilder.LOCATION_HEADER, expectedLocationHeader));
|
||||
Assertions.assertEquals(expectedLocationHeader, FhirRequestBuilder.getLocationHeader(headers));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Test that getLocationHeader returns header for 'content-location'.")
|
||||
void getLocationHeaderWhenOnlyContentLocationIsSet() {
|
||||
final String expectedContentLocationHeader = "content_location_header_value";
|
||||
Iterable<HTTPHeader> headers = List.of(new HTTPHeader(FhirRequestBuilder.CONTENT_LOCATION_HEADER, expectedContentLocationHeader));
|
||||
Assertions.assertEquals(expectedContentLocationHeader, FhirRequestBuilder.getLocationHeader(headers));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Test that getLocationHeader returns 'location' header when both 'location' and 'content-location' are set.")
|
||||
void getLocationHeaderWhenLocationAndContentLocationAreSet() {
|
||||
final String expectedLocationHeader = "location_header_value";
|
||||
final String expectedContentLocationHeader = "content_location_header_value";
|
||||
Iterable<HTTPHeader> headers = List.of(
|
||||
new HTTPHeader(FhirRequestBuilder.LOCATION_HEADER, expectedLocationHeader),
|
||||
new HTTPHeader(FhirRequestBuilder.CONTENT_LOCATION_HEADER, expectedContentLocationHeader)
|
||||
);
|
||||
Assertions.assertEquals(expectedLocationHeader, FhirRequestBuilder.getLocationHeader(headers));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Test that getLocationHeader returns null when no location available.")
|
||||
void getLocationHeaderWhenNoLocationSet() {
|
||||
|
||||
Assertions.assertNull(FhirRequestBuilder.getLocationHeader(Collections.emptyList()));
|
||||
}
|
||||
}
|
|
@ -8,7 +8,7 @@ import java.util.concurrent.TimeUnit;
|
|||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
import okhttp3.*;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.hl7.fhir.r4b.formats.IParser;
|
||||
import org.hl7.fhir.r4b.formats.JsonParser;
|
||||
|
@ -32,10 +32,7 @@ public class FhirRequestBuilder {
|
|||
protected static final String LOCATION_HEADER = "location";
|
||||
protected static final String CONTENT_LOCATION_HEADER = "content-location";
|
||||
protected static final String DEFAULT_CHARSET = "UTF-8";
|
||||
/**
|
||||
* The singleton instance of the HttpClient, used for all requests.
|
||||
*/
|
||||
//private static OkHttpClient okHttpClient;
|
||||
|
||||
private final HTTPRequest httpRequest;
|
||||
private String resourceFormat = null;
|
||||
private Iterable<HTTPHeader> headers = null;
|
||||
|
@ -65,11 +62,11 @@ public class FhirRequestBuilder {
|
|||
|
||||
/**
|
||||
* Adds necessary default headers, formatting headers, and any passed in
|
||||
* {@link Headers} to the passed in {@link okhttp3.Request.Builder}
|
||||
* {@link HTTPHeader} to the passed in {@link okhttp3.Request.Builder}
|
||||
*
|
||||
* @param request {@link okhttp3.Request.Builder} to add headers to.
|
||||
* @param format Expected {@link Resource} format.
|
||||
* @param headers Any additional {@link Headers} to add to the request.
|
||||
* @param headers Any additional {@link HTTPHeader} to add to the request.
|
||||
*/
|
||||
protected static HTTPRequest formatHeaders(HTTPRequest request, String format, Iterable<HTTPHeader> headers) {
|
||||
List<HTTPHeader> allHeaders = new ArrayList<>();
|
||||
|
@ -92,17 +89,6 @@ public class FhirRequestBuilder {
|
|||
return headers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Iterates through the passed in {@link Headers} and adds them to the provided
|
||||
* {@link Request.Builder}.
|
||||
*
|
||||
* @param request {@link Request.Builder} to add headers to.
|
||||
* @param headers {@link Headers} to add to request.
|
||||
*/
|
||||
protected static void addHeaders(Request.Builder request, Iterable<HTTPHeader> headers) {
|
||||
headers.forEach(header -> request.addHeader(header.getName(), header.getValue()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if any of the
|
||||
* {@link org.hl7.fhir.r4b.model.OperationOutcome.OperationOutcomeIssueComponent}
|
||||
|
@ -144,53 +130,6 @@ public class FhirRequestBuilder {
|
|||
return new ManagedFhirWebAccessBuilder("hapi-fhir-tooling-client", null).withRetries(retryCount).withTimeout(timeout, timeoutUnit).withLogger(logger);
|
||||
}
|
||||
|
||||
/**
|
||||
* We only ever want to have one copy of the HttpClient kicking around at any
|
||||
* given time. If we need to make changes to any configuration, such as proxy
|
||||
* settings, timeout, caches, etc, we can do a per-call configuration through
|
||||
* the {@link OkHttpClient#newBuilder()} method. That will return a builder that
|
||||
* shares the same connection pool, dispatcher, and configuration with the
|
||||
* original client.
|
||||
* </p>
|
||||
* The {@link OkHttpClient} uses the proxy auth properties set in the current
|
||||
* system properties. The reason we don't set the proxy address and
|
||||
* authentication explicitly, is due to the fact that this class is often used
|
||||
* in conjunction with other http client tools which rely on the
|
||||
* system.properties settings to determine proxy settings. It's easier to keep
|
||||
* the method consistent across the board. ...for now.
|
||||
*
|
||||
* @return {@link OkHttpClient} instance
|
||||
*/
|
||||
/*FIXME remove after refactor
|
||||
protected OkHttpClient getHttpClient() {
|
||||
if (okHttpClient == null) {
|
||||
okHttpClient = new OkHttpClient();
|
||||
}
|
||||
|
||||
Authenticator proxyAuthenticator = getAuthenticator();
|
||||
|
||||
OkHttpClient.Builder builder = okHttpClient.newBuilder();
|
||||
if (logger != null)
|
||||
builder.addInterceptor(logger);
|
||||
builder.addInterceptor(new RetryInterceptor(retryCount));
|
||||
|
||||
return builder.connectTimeout(timeout, timeoutUnit).writeTimeout(timeout, timeoutUnit)
|
||||
.readTimeout(timeout, timeoutUnit).proxyAuthenticator(proxyAuthenticator).build();
|
||||
}
|
||||
*/
|
||||
@Nonnull
|
||||
private static Authenticator getAuthenticator() {
|
||||
return (route, response) -> {
|
||||
final String httpProxyUser = System.getProperty(HTTP_PROXY_USER);
|
||||
final String httpProxyPass = System.getProperty(HTTP_PROXY_PASS);
|
||||
if (httpProxyUser != null && httpProxyPass != null) {
|
||||
String credential = Credentials.basic(httpProxyUser, httpProxyPass);
|
||||
return response.request().newBuilder().header(HEADER_PROXY_AUTH, credential).build();
|
||||
}
|
||||
return response.request().newBuilder().build();
|
||||
};
|
||||
}
|
||||
|
||||
public FhirRequestBuilder withResourceFormat(String resourceFormat) {
|
||||
this.resourceFormat = resourceFormat;
|
||||
return this;
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
package org.hl7.fhir.r4b.utils.client.network;
|
||||
|
||||
import okhttp3.Request;
|
||||
|
||||
import org.hl7.fhir.r4b.model.OperationOutcome;
|
||||
import org.hl7.fhir.utilities.http.HTTPHeader;
|
||||
import org.hl7.fhir.utilities.http.HTTPHeaderUtil;
|
||||
|
@ -49,31 +49,6 @@ class FhirRequestBuilderTest {
|
|||
"Content-Type header not populated with expected value \"" + testFormat + ";charset=" + FhirRequestBuilder.DEFAULT_CHARSET + "\".");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Test a list of provided headers are added correctly.")
|
||||
void addHeaders() {
|
||||
String headerName1 = "headerName1";
|
||||
String headerValue1 = "headerValue1";
|
||||
String headerName2 = "headerName2";
|
||||
String headerValue2 = "headerValue2";
|
||||
|
||||
List<HTTPHeader> headers = List.of(
|
||||
new HTTPHeader(headerName1, headerValue1),
|
||||
new HTTPHeader(headerName2, headerValue2)
|
||||
);
|
||||
|
||||
Request.Builder request = new Request.Builder().url("http://www.google.com");
|
||||
FhirRequestBuilder.addHeaders(request, headers);
|
||||
|
||||
Map<String, List<String>> headersMap = request.build().headers().toMultimap();
|
||||
Assertions.assertNotNull(headersMap.get(headerName1), headerName1 + " header null.");
|
||||
Assertions.assertEquals(headerValue1, headersMap.get(headerName1).get(0),
|
||||
headerName1 + " header not populated with expected value " + headerValue1 + ".");
|
||||
Assertions.assertNotNull(headersMap.get(headerName2), headerName2 + " header null.");
|
||||
Assertions.assertEquals(headerValue2, headersMap.get(headerName2).get(0),
|
||||
headerName2 + " header not populated with expected value " + headerValue2 + ".");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Test that FATAL issue severity triggers error.")
|
||||
void hasErrorTestFatal() {
|
||||
|
|
|
@ -5,7 +5,7 @@ import java.util.ArrayList;
|
|||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import okhttp3.*;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.hl7.fhir.r5.formats.IParser;
|
||||
import org.hl7.fhir.r5.formats.JsonParser;
|
||||
|
@ -62,12 +62,12 @@ public class FhirRequestBuilder {
|
|||
}
|
||||
|
||||
/**
|
||||
* Adds necessary default headers, formatting headers, and any passed in {@link Headers} to the passed in
|
||||
* {@link Request.Builder}
|
||||
* Adds necessary default headers, formatting headers, and any passed in {@link HTTPHeader}s to the passed in
|
||||
* {@link HTTPRequest}
|
||||
*
|
||||
* @param request {@link Request.Builder} to add headers to.
|
||||
* @param request {@link HTTPRequest} to add headers to.
|
||||
* @param format Expected {@link Resource} format.
|
||||
* @param headers Any additional {@link Headers} to add to the request.
|
||||
* @param headers Any additional {@link HTTPHeader}s to add to the request.
|
||||
*/
|
||||
protected static HTTPRequest formatHeaders(HTTPRequest request, String format, Iterable<HTTPHeader> headers) {
|
||||
List<HTTPHeader> allHeaders = new ArrayList<>();
|
||||
|
@ -84,7 +84,7 @@ public class FhirRequestBuilder {
|
|||
/**
|
||||
* Adds necessary headers for the given resource format provided.
|
||||
*
|
||||
* @param httpRequest {@link Request.Builder} to add default headers to.
|
||||
* @param httpRequest {@link HTTPRequest} to add default headers to.
|
||||
*/
|
||||
protected static Iterable<HTTPHeader> getResourceFormatHeaders(HTTPRequest httpRequest, String format) {
|
||||
List<HTTPHeader> headers = new ArrayList<>();
|
||||
|
@ -98,16 +98,6 @@ public class FhirRequestBuilder {
|
|||
return headers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Iterates through the passed in {@link Headers} and adds them to the provided {@link Request.Builder}.
|
||||
*
|
||||
* @param request {@link Request.Builder} to add headers to.
|
||||
* @param headers {@link Headers} to add to request.
|
||||
*/
|
||||
protected static void addHeaders(Request.Builder request, Iterable<HTTPHeader> headers) {
|
||||
headers.forEach(header -> request.addHeader(header.getName(), header.getValue()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if any of the {@link OperationOutcome.OperationOutcomeIssueComponent} within the
|
||||
* provided {@link OperationOutcome} have an {@link OperationOutcome.IssueSeverity} of
|
||||
|
|
|
@ -13,8 +13,6 @@ import org.junit.jupiter.api.Assertions;
|
|||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import okhttp3.Request;
|
||||
|
||||
class FhirRequestBuilderTest {
|
||||
|
||||
@Test
|
||||
|
@ -67,7 +65,7 @@ class FhirRequestBuilderTest {
|
|||
new HTTPHeader(headerName2, headerValue2)
|
||||
);
|
||||
|
||||
Request.Builder request = new Request.Builder().url("http://www.google.com");
|
||||
HTTPRequest request = new HTTPRequest().withUrl("http://www.google.com");
|
||||
headers.forEach(header -> request.addHeader(header.getName(), header.getValue()));
|
||||
|
||||
Map<String, List<String>> headersMap = request.build().headers().toMultimap();
|
||||
|
|
Loading…
Reference in New Issue