This commit is contained in:
nathaniel.doef 2022-07-17 14:35:17 -04:00
parent 893ac96882
commit 7d1de3dc3f
5 changed files with 63 additions and 58 deletions

View File

@ -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);
}

View File

@ -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<TlsAuthentication> 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<TlsAuthentication> 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<TlsAuthentication> 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());
}
}

View File

@ -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));

View File

@ -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);
}

View File

@ -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);
}