diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/tls/StoreInfo.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/tls/StoreInfo.java index cdeb9ad3e0c..7aec06ee98d 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/tls/StoreInfo.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/tls/StoreInfo.java @@ -59,11 +59,6 @@ public abstract class StoreInfo { public static class StoreInfoException extends RuntimeException { private static final long serialVersionUID = 1l; - - public StoreInfoException(String theMessage, Throwable theCause) { - super(theMessage, theCause); - } - public StoreInfoException(String theMessage) { super(theMessage); } diff --git a/hapi-fhir-client-okhttp/src/main/java/ca/uhn/fhir/okhttp/client/OkHttpRestfulClientFactory.java b/hapi-fhir-client-okhttp/src/main/java/ca/uhn/fhir/okhttp/client/OkHttpRestfulClientFactory.java index 962cf492e6d..2efdda9fe3e 100644 --- a/hapi-fhir-client-okhttp/src/main/java/ca/uhn/fhir/okhttp/client/OkHttpRestfulClientFactory.java +++ b/hapi-fhir-client-okhttp/src/main/java/ca/uhn/fhir/okhttp/client/OkHttpRestfulClientFactory.java @@ -1,5 +1,11 @@ package ca.uhn.fhir.okhttp.client; +import java.net.InetSocketAddress; +import java.net.Proxy; +import java.util.List; +import java.util.Map; +import java.util.concurrent.TimeUnit; + /* * #%L * HAPI FHIR OkHttp Client @@ -29,13 +35,7 @@ import ca.uhn.fhir.rest.client.impl.RestfulClientFactory; import ca.uhn.fhir.tls.TlsAuthentication; import okhttp3.Call; import okhttp3.OkHttpClient; - -import java.net.InetSocketAddress; -import java.net.Proxy; -import java.util.List; -import java.util.Map; import java.util.Optional; -import java.util.concurrent.TimeUnit; /** * A Restful client factory based on OkHttp. @@ -44,51 +44,50 @@ import java.util.concurrent.TimeUnit; */ public class OkHttpRestfulClientFactory extends RestfulClientFactory { - private Call.Factory myNativeClient; + private Call.Factory myNativeClient; - public OkHttpRestfulClientFactory() { - super(); - } + public OkHttpRestfulClientFactory() { + super(); + } - public OkHttpRestfulClientFactory(FhirContext theFhirContext) { - super(theFhirContext); - } + public OkHttpRestfulClientFactory(FhirContext theFhirContext) { + super(theFhirContext); + } - @Override - protected IHttpClient getHttpClient(String theServerBase) { - return getHttpClient(theServerBase, Optional.empty()); - } + @Override + protected IHttpClient getHttpClient(String theServerBase) { + return new OkHttpRestfulClient(getNativeClient(), new StringBuilder(theServerBase), null, null, null, null); + } @Override protected IHttpClient getHttpClient(String theServerBase, Optional theTlsAuthentication) { return new OkHttpRestfulClient(getNativeClient(theTlsAuthentication), new StringBuilder(theServerBase), null, null, null, null); } - @Override - protected void resetHttpClient() { - myNativeClient = null; - } + @Override + protected void resetHttpClient() { + myNativeClient = null; + } public synchronized Call.Factory getNativeClient() { return getNativeClient(Optional.empty()); } - public synchronized Call.Factory getNativeClient(Optional theTlsAuthentication) { - if(theTlsAuthentication.isPresent()){ - throw new UnsupportedOperationException(Msg.code(2118)+"HTTPS not supported for OkHttpCLient"); - } - - if (myNativeClient == null) { - myNativeClient = new OkHttpClient() + public synchronized Call.Factory getNativeClient(Optional theTlsAuthentication) { + if(theTlsAuthentication.isPresent()){ + throw new UnsupportedOperationException(Msg.code(2118)+"HTTPS not supported for OkHttpCLient"); + } + if (myNativeClient == null) { + myNativeClient = new OkHttpClient() .newBuilder() .connectTimeout(getConnectTimeout(), TimeUnit.MILLISECONDS) - .readTimeout(getSocketTimeout(), TimeUnit.MILLISECONDS) - .writeTimeout(getSocketTimeout(), TimeUnit.MILLISECONDS) + .readTimeout(getSocketTimeout(), TimeUnit.MILLISECONDS) + .writeTimeout(getSocketTimeout(), TimeUnit.MILLISECONDS) .build(); - } + } - return myNativeClient; - } + return myNativeClient; + } @Override public IHttpClient getHttpClient(StringBuilder theUrl, @@ -109,20 +108,21 @@ public class OkHttpRestfulClientFactory extends RestfulClientFactory { return new OkHttpRestfulClient(getNativeClient(theTlsAuthentication), theUrl, theIfNoneExistParams, theIfNoneExistString, theRequestType, theHeaders); } - /** - * Only accepts clients of type {@link OkHttpClient} - * - * @param okHttpClient - */ - @Override - public void setHttpClient(Object okHttpClient) { - myNativeClient = (Call.Factory) okHttpClient; - } + /** + * Only accepts clients of type {@link OkHttpClient} + * + * @param okHttpClient + */ + @Override + public void setHttpClient(Object okHttpClient) { + myNativeClient = (Call.Factory) okHttpClient; + } + + @Override + public void setProxy(String theHost, Integer thePort) { + Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(theHost, thePort)); + OkHttpClient.Builder builder = ((OkHttpClient)getNativeClient()).newBuilder().proxy(proxy); + setHttpClient(builder.build()); + } - @Override - public void setProxy(String theHost, Integer thePort) { - Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(theHost, thePort)); - OkHttpClient.Builder builder = ((OkHttpClient) getNativeClient()).newBuilder().proxy(proxy); - setHttpClient(builder.build()); - } } diff --git a/hapi-fhir-client/src/test/java/ca/uhn/fhir/rest/client/tls/TlsAuthenticationSvcTest.java b/hapi-fhir-client/src/test/java/ca/uhn/fhir/rest/client/tls/TlsAuthenticationSvcTest.java index 2ba2a79bae2..da0b4323ce7 100644 --- a/hapi-fhir-client/src/test/java/ca/uhn/fhir/rest/client/tls/TlsAuthenticationSvcTest.java +++ b/hapi-fhir-client/src/test/java/ca/uhn/fhir/rest/client/tls/TlsAuthenticationSvcTest.java @@ -18,6 +18,7 @@ import java.util.Optional; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -81,7 +82,7 @@ public class TlsAuthenticationSvcTest { } @Test - public void testCreateKeyStoreNonExistentFile() throws Exception { + public void testCreateKeyStoreNonExistentFile() { KeyStoreInfo keyStoreInfo = new KeyStoreInfo("classpath:/non-existent.p12", "changeit", "changeit", "server"); Exception exceptionThrown = assertThrows(TlsAuthenticationSvc.TlsAuthenticationException.class, () -> { TlsAuthenticationSvc.createKeyStore(keyStoreInfo); @@ -112,7 +113,7 @@ public class TlsAuthenticationSvcTest { } @Test - public void testCreateTrustManager() throws Exception{ + public void testCreateTrustManager() throws Exception { X509TrustManager trustManager = TlsAuthenticationSvc.createTrustManager(Optional.of(myClientTrustStoreInfo)); KeyStore keyStore = TlsAuthenticationSvc.createKeyStore(myServerKeyStoreInfo); Certificate serverCertificate = keyStore.getCertificate(myServerKeyStoreInfo.getAlias()); @@ -122,7 +123,14 @@ public class TlsAuthenticationSvcTest { } @Test - public void testCreateTrustManagerInvalid() throws Exception{ + public void testCreateTrustManagerNoTrustStore() { + // trust manager should contain common certifications if no trust store information is used + X509TrustManager trustManager = TlsAuthenticationSvc.createTrustManager(Optional.empty()); + assertNotEquals(0, trustManager.getAcceptedIssuers().length); + } + + @Test + public void testCreateTrustManagerInvalid() { TrustStoreInfo invalidKeyStoreInfo = new TrustStoreInfo("file:///INVALID.p12", "changeit", "client"); Exception exceptionThrown = assertThrows(TlsAuthenticationSvc.TlsAuthenticationException.class, () -> { TlsAuthenticationSvc.createTrustManager(Optional.of(invalidKeyStoreInfo)); diff --git a/hapi-fhir-test-utilities/src/main/java/ca/uhn/fhir/test/utilities/RestServerDstu3Helper.java b/hapi-fhir-test-utilities/src/main/java/ca/uhn/fhir/test/utilities/RestServerDstu3Helper.java index b9e54b119c8..68ad421a1b1 100644 --- a/hapi-fhir-test-utilities/src/main/java/ca/uhn/fhir/test/utilities/RestServerDstu3Helper.java +++ b/hapi-fhir-test-utilities/src/main/java/ca/uhn/fhir/test/utilities/RestServerDstu3Helper.java @@ -29,6 +29,7 @@ import ca.uhn.fhir.rest.api.MethodOutcome; import ca.uhn.fhir.rest.api.server.RequestDetails; import ca.uhn.fhir.rest.server.IResourceProvider; import ca.uhn.fhir.rest.server.RestfulServer; +import ca.uhn.fhir.rest.server.exceptions.PreconditionFailedException; import ca.uhn.fhir.rest.server.provider.HashMapResourceProvider; import ca.uhn.test.concurrency.IPointcutLatch; import ca.uhn.test.concurrency.PointcutLatch; @@ -289,7 +290,7 @@ public class RestServerDstu3Helper extends BaseRestServerHelper implements IPoin @Override public MethodOutcome update(T theResource, String theConditional, RequestDetails theRequestDetails) { if (myFailNextPut) { - throw new RuntimeException(Msg.code(2113)+"Failed update operation"); + throw new PreconditionFailedException(Msg.code(2113)+"Failed update operation"); } return super.update(theResource, theConditional, theRequestDetails); } diff --git a/hapi-fhir-test-utilities/src/main/java/ca/uhn/fhir/test/utilities/RestServerR4Helper.java b/hapi-fhir-test-utilities/src/main/java/ca/uhn/fhir/test/utilities/RestServerR4Helper.java index 3ae84f3d567..63f1c28e6d9 100644 --- a/hapi-fhir-test-utilities/src/main/java/ca/uhn/fhir/test/utilities/RestServerR4Helper.java +++ b/hapi-fhir-test-utilities/src/main/java/ca/uhn/fhir/test/utilities/RestServerR4Helper.java @@ -29,6 +29,7 @@ import ca.uhn.fhir.rest.api.server.RequestDetails; import ca.uhn.fhir.rest.server.FifoMemoryPagingProvider; import ca.uhn.fhir.rest.server.IResourceProvider; import ca.uhn.fhir.rest.server.RestfulServer; +import ca.uhn.fhir.rest.server.exceptions.PreconditionFailedException; import ca.uhn.fhir.rest.server.provider.HashMapResourceProvider; import org.eclipse.jetty.server.Request; import org.hl7.fhir.instance.model.api.IBaseResource; @@ -367,7 +368,7 @@ public class RestServerR4Helper extends BaseRestServerHelper implements BeforeEa @Override public MethodOutcome update(T theResource, String theConditional, RequestDetails theRequestDetails) { if (myFailNextPut) { - throw new RuntimeException(Msg.code(2111)+"Failed update operation"); + throw new PreconditionFailedException(Msg.code(2111)+"Failed update operation"); } return super.update(theResource, theConditional, theRequestDetails); }