Add possibility to set Jax-Rs components to the default client, add comments
This commit is contained in:
parent
9901b802c4
commit
4b2d9ff794
|
@ -33,12 +33,14 @@ import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A Restful Client Factory, based on Jax Rs
|
* 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
|
* @author Peter Van Houte | peter.vanhoute@agfa.com | Agfa Healthcare
|
||||||
*/
|
*/
|
||||||
public class JaxRsRestfulClientFactory extends RestfulClientFactory {
|
public class JaxRsRestfulClientFactory extends RestfulClientFactory {
|
||||||
|
|
||||||
private Client myNativeClient;
|
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!
|
* 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();
|
myNativeClient = builder.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (registeredComponents != null && !registeredComponents.isEmpty()) {
|
||||||
|
for (Class<?> c : registeredComponents) {
|
||||||
|
myNativeClient = myNativeClient.register(c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return myNativeClient;
|
return myNativeClient;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -72,6 +80,12 @@ public class JaxRsRestfulClientFactory extends RestfulClientFactory {
|
||||||
return new JaxRsHttpClient(client, url, theIfNoneExistParams, theIfNoneExistString, theRequestType, theHeaders);
|
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
|
@Override
|
||||||
public void setProxy(String theHost, Integer thePort) {
|
public void setProxy(String theHost, Integer thePort) {
|
||||||
throw new UnsupportedOperationException("Proxies are not supported yet in JAX-RS client");
|
throw new UnsupportedOperationException("Proxies are not supported yet in JAX-RS client");
|
||||||
|
@ -79,7 +93,7 @@ public class JaxRsRestfulClientFactory extends RestfulClientFactory {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Only accept clients of type javax.ws.rs.client.Client
|
* Only accept clients of type javax.ws.rs.client.Client
|
||||||
*
|
* Can be used to set a specific Client implementation
|
||||||
* @param theHttpClient
|
* @param theHttpClient
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
|
@ -87,6 +101,15 @@ public class JaxRsRestfulClientFactory extends RestfulClientFactory {
|
||||||
this.myNativeClient = (Client) 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
|
@Override
|
||||||
protected JaxRsHttpClient getHttpClient(String theServerBase) {
|
protected JaxRsHttpClient getHttpClient(String theServerBase) {
|
||||||
return new JaxRsHttpClient(getNativeClientClient(), new StringBuilder(theServerBase), null, null, null, null);
|
return new JaxRsHttpClient(getNativeClientClient(), new StringBuilder(theServerBase), null, null, null, null);
|
||||||
|
@ -97,4 +120,11 @@ public class JaxRsRestfulClientFactory extends RestfulClientFactory {
|
||||||
this.myNativeClient = null;
|
this.myNativeClient = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void resetHttpClient() {
|
||||||
|
if (myNativeClient != null)
|
||||||
|
myNativeClient.close(); // close client to avoid memory leak
|
||||||
|
myNativeClient = null;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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());
|
||||||
|
}
|
||||||
|
}
|
|
@ -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 {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue