Add possibility to set Jax-Rs components to the default client, add comments

This commit is contained in:
Sebastien Riviere 2017-08-01 10:52:09 +02:00
parent 9901b802c4
commit 4b2d9ff794
3 changed files with 109 additions and 8 deletions

View File

@ -33,13 +33,15 @@ import java.util.Map;
/**
* A Restful Client Factory, based on Jax Rs
*
* Default Jax-Rs client is NOT thread safe in static context, you should create a new factory every time or
* use a specific Jax-Rs client implementation which managed connection pool.
* @author Peter Van Houte | peter.vanhoute@agfa.com | Agfa Healthcare
*/
public class JaxRsRestfulClientFactory extends RestfulClientFactory {
private Client myNativeClient;
private List<Class<?>> registeredComponents;
/**
* Constructor. Note that you must set the {@link FhirContext} manually using {@link #setFhirContext(FhirContext)} if this constructor is used!
*/
@ -63,6 +65,12 @@ public class JaxRsRestfulClientFactory extends RestfulClientFactory {
myNativeClient = builder.build();
}
if (registeredComponents != null && !registeredComponents.isEmpty()) {
for (Class<?> c : registeredComponents) {
myNativeClient = myNativeClient.register(c);
}
}
return myNativeClient;
}
@ -72,21 +80,36 @@ public class JaxRsRestfulClientFactory extends RestfulClientFactory {
return new JaxRsHttpClient(client, url, theIfNoneExistParams, theIfNoneExistString, theRequestType, theHeaders);
}
/***
* Not supported with default Jax-Rs client implementation
* @param theHost
* The host (or null to disable proxying, as is the default)
* @param thePort
*/
@Override
public void setProxy(String theHost, Integer thePort) {
throw new UnsupportedOperationException("Proxies are not supported yet in JAX-RS client");
}
/**
* Only accept clients of type javax.ws.rs.client.Client
*
* @param theHttpClient
*/
/**
* Only accept clients of type javax.ws.rs.client.Client
* Can be used to set a specific Client implementation
* @param theHttpClient
*/
@Override
public synchronized void setHttpClient(Object theHttpClient) {
this.myNativeClient = (Client) theHttpClient;
}
/**
* Register a list of Jax-Rs component (provider, filter...)
* @param components list of Jax-Rs components to register
*/
public void register(List<Class<?>> components) {
registeredComponents = components;
}
@Override
protected JaxRsHttpClient getHttpClient(String theServerBase) {
return new JaxRsHttpClient(getNativeClientClient(), new StringBuilder(theServerBase), null, null, null, null);
@ -96,5 +119,12 @@ public class JaxRsRestfulClientFactory extends RestfulClientFactory {
protected void resetHttpClient() {
this.myNativeClient = null;
}
@Override
protected void resetHttpClient() {
if (myNativeClient != null)
myNativeClient.close(); // close client to avoid memory leak
myNativeClient = null;
}
}

View File

@ -0,0 +1,53 @@
package ca.uhn.fhir.jaxrs.client;
import ca.uhn.fhir.context.FhirContext;
import org.junit.Before;
import org.junit.Test;
import javax.ws.rs.client.Client;
import java.util.ArrayList;
import java.util.Arrays;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
/**
* Created by Sebastien Riviere on 31/07/2017.
*/
public class JaxRsRestfulClientFactoryTest {
private final FhirContext context = FhirContext.forDstu2();
private JaxRsRestfulClientFactory factory;
@Before
public void setUp() {
factory = new JaxRsRestfulClientFactory(context);
}
@Test
public void emptyConstructorTest() {
assertNotNull(new JaxRsRestfulClientFactory());
}
@Test
public void getDefaultNativeClientTest() {
assertNotNull(factory.getNativeClientClient());
}
@Test
public void getNativeClientEmptyRegisteredComponentListTest() {
factory.register(new ArrayList<Class<?>>());
final Client result = factory.getNativeClientClient();
assertNotNull(result);
assertTrue(result.getConfiguration().getClasses().isEmpty());
}
@Test
public void getNativeClientRegisteredComponentListTest() {
factory.register(Arrays.asList(MyFilter.class, String.class));
final Client result = factory.getNativeClientClient();
assertNotNull(result);
assertEquals(1, result.getConfiguration().getClasses().size());
}
}

View File

@ -0,0 +1,18 @@
package ca.uhn.fhir.jaxrs.client;
import javax.ws.rs.client.ClientRequestContext;
import javax.ws.rs.client.ClientResponseContext;
import javax.ws.rs.client.ClientResponseFilter;
import javax.ws.rs.ext.Provider;
import java.io.IOException;
/**
* Created by Sebastien Riviere on 31/07/2017.
*/
@Provider
public class MyFilter implements ClientResponseFilter {
@Override
public void filter(final ClientRequestContext requestContext, final ClientResponseContext responseContext) throws IOException {
}
}