Merge pull request #714 from SRiviere/jaxrs-sever-evolution

Jaxrs server evolution
This commit is contained in:
James Agnew 2017-10-26 18:00:37 -04:00 committed by GitHub
commit b35cab2996
3 changed files with 109 additions and 13 deletions

View File

@ -34,13 +34,15 @@ import ca.uhn.fhir.rest.client.impl.RestfulClientFactory;
/**
* 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!
*/
@ -64,6 +66,12 @@ public class JaxRsRestfulClientFactory extends RestfulClientFactory {
myNativeClient = builder.build();
}
if (registeredComponents != null && !registeredComponents.isEmpty()) {
for (Class<?> c : registeredComponents) {
myNativeClient = myNativeClient.register(c);
}
}
return myNativeClient;
}
@ -73,29 +81,46 @@ 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);
}
@Override
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 {
}
}