From b15504ab6af00727419d4888cd3a1c5215f5b5e3 Mon Sep 17 00:00:00 2001 From: jamesagnew Date: Wed, 5 Mar 2014 08:26:26 -0500 Subject: [PATCH] Starting client implementation --- hapi-fhir-base/pom.xml | 7 +-- .../ca/uhn/fhir/context/ModelScanner.java | 4 ++ .../ca/uhn/fhir/rest/annotation/Read.java | 12 ++++ .../rest/client/ClientInvocationHandler.java | 22 +++++++ .../uhn/fhir/rest/client/MethodBinding.java | 5 ++ .../rest/client/RestfulClientFactory.java | 60 +++++++++++++++++++ .../fhir/rest/client/api/IRestfulClient.java | 5 ++ .../java/ca/uhn/fhir/ws/RestfulServer.java | 4 +- .../ca/uhn/fhir/rest/client/ClientTest.java | 12 ++++ 9 files changed, 125 insertions(+), 6 deletions(-) create mode 100644 hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/annotation/Read.java create mode 100644 hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/client/ClientInvocationHandler.java create mode 100644 hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/client/MethodBinding.java create mode 100644 hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/client/RestfulClientFactory.java create mode 100644 hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/client/api/IRestfulClient.java create mode 100644 hapi-fhir-base/src/test/java/ca/uhn/fhir/rest/client/ClientTest.java diff --git a/hapi-fhir-base/pom.xml b/hapi-fhir-base/pom.xml index 444284f6524..caa552954f2 100644 --- a/hapi-fhir-base/pom.xml +++ b/hapi-fhir-base/pom.xml @@ -64,15 +64,12 @@ 4.2.3 + javax.servlet javax.servlet-api 3.1.0 - - - log4j - log4j - 1.2.17 + provided diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/context/ModelScanner.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/context/ModelScanner.java index da6c8e5b4cb..751319dbe04 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/context/ModelScanner.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/context/ModelScanner.java @@ -92,6 +92,10 @@ class ModelScanner { } + public ModelScanner(Class theClass) { + // TODO Auto-generated constructor stub + } + public RuntimeChildUndeclaredExtensionDefinition getRuntimeChildUndeclaredExtensionDefinition() { return myRuntimeChildUndeclaredExtensionDefinition; } diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/annotation/Read.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/annotation/Read.java new file mode 100644 index 00000000000..079024dd244 --- /dev/null +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/annotation/Read.java @@ -0,0 +1,12 @@ +package ca.uhn.fhir.rest.annotation; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.METHOD) +public @interface Read { + +} diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/client/ClientInvocationHandler.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/client/ClientInvocationHandler.java new file mode 100644 index 00000000000..eed8e841f0b --- /dev/null +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/client/ClientInvocationHandler.java @@ -0,0 +1,22 @@ +package ca.uhn.fhir.rest.client; + +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.Method; + +import org.apache.http.client.HttpClient; + +public class ClientInvocationHandler implements InvocationHandler { + + private HttpClient myClient; + + public ClientInvocationHandler(HttpClient theClient) { + myClient = theClient; + } + + @Override + public Object invoke(Object theProxy, Method theMethod, Object[] theArgs) throws Throwable { + // TODO Auto-generated method stub + return null; + } + +} diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/client/MethodBinding.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/client/MethodBinding.java new file mode 100644 index 00000000000..ebffa812b0b --- /dev/null +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/client/MethodBinding.java @@ -0,0 +1,5 @@ +package ca.uhn.fhir.rest.client; + +class MethodBinding { + +} diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/client/RestfulClientFactory.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/client/RestfulClientFactory.java new file mode 100644 index 00000000000..68f28df3f5f --- /dev/null +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/client/RestfulClientFactory.java @@ -0,0 +1,60 @@ +package ca.uhn.fhir.rest.client; + +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.Proxy; +import java.util.concurrent.TimeUnit; + +import org.apache.http.client.HttpClient; +import org.apache.http.impl.client.DefaultHttpClient; +import org.apache.http.impl.conn.PoolingClientConnectionManager; +import org.apache.http.impl.conn.SchemeRegistryFactory; + +import ca.uhn.fhir.context.ConfigurationException; +import ca.uhn.fhir.context.FhirContext; +import ca.uhn.fhir.rest.client.api.IRestfulClient; + +public class RestfulClientFactory { + + private FhirContext myContext; + + /** + * Constructor + * + * @param theContext The context + */ + public RestfulClientFactory(FhirContext theContext) { + myContext = theContext; + } + + + /** + * Instantiates a new client instance + * + * @param theClientType The client type, which is an interface type to be instantiated + * @param theServerBase The URL of the base for the restful FHIR server to connect to + * @return A newly created client + * @throws ConfigurationException If the interface type is not an interface + */ + public T newClient(Class theClientType, String theServerBase) { + if (!theClientType.isInterface()) { + throw new ConfigurationException(theClientType.getCanonicalName() + " is not an interface"); + } + + PoolingClientConnectionManager connectionManager = new PoolingClientConnectionManager(SchemeRegistryFactory.createDefault(), 5000, TimeUnit.MILLISECONDS); + HttpClient client = new DefaultHttpClient(connectionManager); + + ClientInvocationHandler theInvocationHandler = new ClientInvocationHandler(client); + + T proxy = instantiateProxy(theClientType, theInvocationHandler); + + return proxy; + } + + + @SuppressWarnings("unchecked") + private T instantiateProxy(Class theClientType, InvocationHandler theInvocationHandler) { + T proxy = (T) Proxy.newProxyInstance(RestfulClientFactory.class.getClassLoader(), new Class[] {theClientType}, theInvocationHandler); + return proxy; + } + +} diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/client/api/IRestfulClient.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/client/api/IRestfulClient.java new file mode 100644 index 00000000000..bc3ced31aed --- /dev/null +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/client/api/IRestfulClient.java @@ -0,0 +1,5 @@ +package ca.uhn.fhir.rest.client.api; + +public interface IRestfulClient { + +} diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/ws/RestfulServer.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/ws/RestfulServer.java index 87b38bc4b1e..63af7621fde 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/ws/RestfulServer.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/ws/RestfulServer.java @@ -40,6 +40,8 @@ public abstract class RestfulServer extends HttpServlet { private Map, IResourceProvider> myTypeToProvider = new HashMap, IResourceProvider>(); + private FhirContext myFhirContext; + public abstract Collection> getResourceProviders(); @Override @@ -59,7 +61,7 @@ public abstract class RestfulServer extends HttpServlet { myFhirContext = new FhirContext(myTypeToProvider.keySet()); - findResourceMethods(nextProvider.getClass()); +// findResourceMethods(nextProvider.getClass()); } catch (Exception ex) { diff --git a/hapi-fhir-base/src/test/java/ca/uhn/fhir/rest/client/ClientTest.java b/hapi-fhir-base/src/test/java/ca/uhn/fhir/rest/client/ClientTest.java new file mode 100644 index 00000000000..47644163384 --- /dev/null +++ b/hapi-fhir-base/src/test/java/ca/uhn/fhir/rest/client/ClientTest.java @@ -0,0 +1,12 @@ +package ca.uhn.fhir.rest.client; + +import org.junit.Test; + +public class ClientTest { + + @Test + public void testClient() { + + } + +}