added initial web service implementation under ca.uhn.fhir.ws package

This commit is contained in:
Yogthos 2014-02-27 11:48:46 -05:00
parent 00411b64f6
commit 7e979449b2
15 changed files with 764 additions and 0 deletions

View File

@ -0,0 +1,41 @@
package ca.uhn.fhir.ws;
/**
* Created by dsotnikov on 2/25/2014.
*/
public class Parameter {
private String name;
private boolean required;
private Class<?> type;
public Parameter(){}
public Parameter(String name, boolean required) {
this.name = name;
this.required = required;
}
public Class<?> getType() {
return type;
}
public void setType(Class<?> type) {
this.type = type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isRequired() {
return required;
}
public void setRequired(boolean required) {
this.required = required;
}
}

View File

@ -0,0 +1,71 @@
package ca.uhn.fhir.ws;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
/**
* Created by dsotnikov on 2/25/2014.
*/
public class Resource {
private String resourceName;
private Class resourceClass;
private List<ResourceMethod> methods = new ArrayList<ResourceMethod>();
public Resource() {}
public Resource(String resourceName, Class resourceClass, List<ResourceMethod> methods) {
this.resourceName = resourceName;
this.methods = methods;
}
public ResourceMethod getMethod(Set<String> parameters) throws Exception {
if (null == methods) return null;
for (ResourceMethod rm : methods) {
if (rm.matches(parameters)) return rm;
}
return null;
}
public Class getResourceClass() {
return resourceClass;
}
public void setResourceClass(Class resourceClass) {
this.resourceClass = resourceClass;
}
public String getResourceName() {
return resourceName;
}
public void setResourceName(String resourceName) {
this.resourceName = resourceName;
}
public List<ResourceMethod> getMethods() {
return methods;
}
public void setMethods(List<ResourceMethod> methods) {
this.methods = methods;
}
public void addMethod(ResourceMethod method) {
this.methods.add(method);
}
@Override
public boolean equals(Object o) {
if (!(o instanceof Resource)) return false;
return resourceName.equals(((Resource)o).getResourceName());
}
@Override
public int hashCode() {
return 0;
}
}

View File

@ -0,0 +1,94 @@
package ca.uhn.fhir.ws;
import ca.uhn.fhir.model.api.IResource;
import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* Created by dsotnikov on 2/25/2014.
*/
public class ResourceMethod {
private Class<?> resourceType;
public static enum RequestType {
GET,
POST,
PUT,
DELETE
}
RequestType requestType;
List<Parameter> parameters;
Method method;
public ResourceMethod() {}
public ResourceMethod(Method method, List<Parameter> parameters) {
this.method = method;
this.parameters = parameters;
}
public void setResourceType(Class<?> resourceType) {
this.resourceType = resourceType;
}
public IResource getResourceType() throws IllegalAccessException, InstantiationException {
return (IResource)resourceType.newInstance();
}
public RequestType getRequestType() {
return requestType;
}
public void setRequestType(RequestType requestType) {
this.requestType = requestType;
}
public List<Parameter> getParameters() {
return parameters;
}
public void setParameters(List<Parameter> parameters) {
this.parameters = parameters;
}
public Method getMethod() {
return method;
}
public void setMethod(Method method) {
this.method = method;
}
public boolean matches(Set<String> parameterNames) {
Set<String> methodParamsTemp = new HashSet<String>();
for (int i = 0; i < this.parameters.size(); i++){
Parameter temp = this.parameters.get(i);
methodParamsTemp.add(temp.getName());
if (temp.isRequired() && !parameterNames.contains(temp.getName())){
return false;
}
}
return methodParamsTemp.containsAll(parameterNames);
}
public IResource invoke(Map<String,String> parameterValues) {
Object[] params = new Object[parameters.size()];
for (int i = 0; i < parameters.size(); i++) {
Parameter param = parameters.get(i);
String value = parameterValues.get(param.getName());
if (null != value) {
//TODO
//param.getType().newInstance().getClass();
}
else {
params[i] = null;
}
}
return null;
}
}

View File

@ -0,0 +1,10 @@
package ca.uhn.fhir.ws;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface ResourceName {
String value();
}

View File

@ -0,0 +1,224 @@
package ca.uhn.fhir.ws;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.model.dstu.resource.Observation;
import ca.uhn.fhir.parser.XmlParser;
import ca.uhn.fhir.ws.exceptions.MethodNotFoundException;
import ca.uhn.fhir.ws.operations.DELETE;
import ca.uhn.fhir.ws.operations.GET;
import ca.uhn.fhir.ws.operations.POST;
import ca.uhn.fhir.ws.operations.PUT;
import org.apache.log4j.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.net.URL;
import java.util.*;
public class Service extends HttpServlet {
private static final Logger log = Logger.getLogger(Service.class);
private static final String handlerPackage = "ca.uhn.rest.handlers";
//map of request handler resources keyed by resource name
private static Map<String, Resource> resources = new HashMap<String, Resource>();
/**
* looks up all the methods from the classes specified in jsonHandlerPackage environment variable, and puts them in a map,
* keyed on the method name.
* The handlers for incoming requests will be looked up based on the name.
* Method names must be public, static, unique, and must be annotated with JsonOperation.
*/
private List<String> findRESTHanlderPackages(Context env) throws Exception {
List<String> packages = new ArrayList<String>();
int i = 1;
String origName = handlerPackage;
String name = origName;
while (true) {
String handlerPackage = null;
try {
log.debug("Looking up:: " + name);
handlerPackage = (String) env.lookup(name);
} catch (NamingException ne) {
log.debug(ne);
}
if (handlerPackage != null) {
log.debug("Found:: " + name);
packages.add(handlerPackage);
name = origName + i;
i++;
} else {
log.debug("Not Found:: " + name);
break;
}
}
if (packages.isEmpty()) {
throw new Exception("No packages defined as '" + handlerPackage + "' in web.xml");
}
return packages;
}
private void addResourceMethod(Resource resource, Method method) throws Exception {
Class<?>[] params = method.getParameterTypes();
ResourceMethod rm = new ResourceMethod();
rm.setResourceType(method.getReturnType());
//each operation name must have a request type annotation and be unique
if (null != method.getAnnotation(GET.class)) {
rm.setRequestType(ResourceMethod.RequestType.GET);
} else if (null != method.getAnnotation(PUT.class)) {
rm.setRequestType(ResourceMethod.RequestType.PUT);
} else if (null != method.getAnnotation(POST.class)) {
rm.setRequestType(ResourceMethod.RequestType.POST);
} else if (null != method.getAnnotation(DELETE.class)) {
rm.setRequestType(ResourceMethod.RequestType.DELETE);
}
rm.setParameters(Util.getResourceParameters(method));
resource.addMethod(rm);
}
private void findResourceMethods(Class<?> handler) throws Exception {
for (Method m : handler.getDeclaredMethods()) {
//only static methods are valid request handlers
if (Modifier.isStatic(m.getModifiers()) && Modifier.isPublic(m.getModifiers())) {
String resourceName = Util.getResourceName(m);
Resource r = resources.get(resourceName);
if (null == r) {
r = new Resource();
r.setResourceName(resourceName);
resources.put(resourceName, r);
}
addResourceMethod(r, m);
log.debug("found handler: " + m.getName());
}
}
}
public void init() {
try {
Context env = (Context) new InitialContext().lookup("java:comp/env");
List<String> packages = findRESTHanlderPackages(env);
for (String packagePath : packages) {
log.info("searching for JSON operation handlers in package: " + packagePath);
List<Class<?>> handlers = getClasses(packagePath);
for (Class<?> handler : handlers) {
log.info("found class: " + handler.getName());
findResourceMethods(handler);
}
}
} catch (Exception ex) {
log.error("An error occurred while loading request handlers!", ex);
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
handleRequest(ResourceMethod.RequestType.GET, request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
handleRequest(ResourceMethod.RequestType.POST, request, response);
}
@Override
protected void doPut(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
handleRequest(ResourceMethod.RequestType.PUT, request, response);
}
@Override
protected void doDelete(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
handleRequest(ResourceMethod.RequestType.DELETE, request, response);
}
protected void handleRequest(ResourceMethod.RequestType requestType, HttpServletRequest request, HttpServletResponse response) {
try {
response.setContentType(request.getHeader("Accept"));
String resourceName = request.getRequestURI();
Map<String, String> params = Util.getQueryParams(request.getQueryString());
Resource resource = resources.get(resourceName);
if (null == resource) throw new MethodNotFoundException("No resource available for " + resourceName);
ResourceMethod resourceMethod = resource.getMethod(params.keySet());
if (null == resourceMethod) throw new MethodNotFoundException("No resource method available for the supplied parameters " + params);
FhirContext ctx = new FhirContext(resourceMethod.getResourceType().getClass());
XmlParser p = new XmlParser(ctx);
response.getWriter().write(p.encodeResourceToString(resourceMethod.invoke(params)));
} catch (Throwable t) {
//TODO: handle errors
}
}
private static List<Class<?>> getClasses(String packageName)
throws ClassNotFoundException, IOException {
if (null == packageName) throw new ClassNotFoundException("package name must be specified for JSON operations");
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
assert classLoader != null;
String path = packageName.replace('.', '/');
Enumeration<URL> resources = classLoader.getResources(path);
List<File> dirs = new ArrayList<File>();
while (resources.hasMoreElements()) {
URL resource = resources.nextElement();
dirs.add(new File(resource.getFile()));
}
ArrayList<Class<?>> classes = new ArrayList<Class<?>>();
for (File directory : dirs) {
classes.addAll(findClasses(directory, packageName));
}
return classes;
}
/**
* Recursive method used to find all classes in a given directory and subdirs.
*
* @param directory The base directory
* @param packageName The package name for classes found inside the base directory
* @return The classes
* @throws ClassNotFoundException
*/
private static List<Class<?>> findClasses(File directory, String packageName) throws ClassNotFoundException {
List<Class<?>> classes = new ArrayList<Class<?>>();
if (!directory.exists()) {
return classes;
}
File[] files = directory.listFiles();
for (File file : files) {
if (file.isDirectory()) {
assert !file.getName().contains(".");
classes.addAll(findClasses(file, packageName + "." + file.getName()));
} else if (file.getName().endsWith(".class")) {
classes.add(Class.forName(packageName + '.' + file.getName().substring(0, file.getName().length() - 6)));
}
}
return classes;
}
}

View File

@ -0,0 +1,67 @@
package ca.uhn.fhir.ws;
import ca.uhn.fhir.ws.parameters.Optional;
import ca.uhn.fhir.ws.parameters.Required;
import java.io.UnsupportedEncodingException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by dsotnikov on 2/25/2014.
*/
public class Util {
public static Map<String, String> getQueryParams(String query) throws UnsupportedEncodingException {
try {
Map<String, String> params = new HashMap<String, String>();
for (String param : query.split("&")) {
String[] pair = param.split("=");
String key = URLDecoder.decode(pair[0], "UTF-8");
String value = URLDecoder.decode(pair[1], "UTF-8");
params.put(key, value);
}
return params;
} catch (UnsupportedEncodingException ex) {
throw new AssertionError(ex);
}
}
public static String getResourceName(Method method) {
ResourceName resourceNameAnnotation = method.getAnnotation(ResourceName.class);
if (null != resourceNameAnnotation) {
return resourceNameAnnotation.value();
}
return null;
}
public static List<Parameter> getResourceParameters(Method method) {
List<Parameter> parameters = new ArrayList<Parameter>();
Class<?>[] parameterTypes = method.getParameterTypes();
for (Annotation[] annotations : method.getParameterAnnotations()) {
for (int i = 0; i < annotations.length; i++) {
Annotation a = annotations[i];
Parameter parameter = new Parameter();
if (a instanceof Required) {
parameter.setName(((Required) a).name());
parameter.setRequired(true);
parameter.setType(parameterTypes[i]);
} else if (a instanceof Optional) {
parameter.setName(((Optional) a).name());
parameter.setRequired(false);
parameter.setType(parameterTypes[i]);
}
parameters.add(parameter);
}
}
return parameters;
}
}

View File

@ -0,0 +1,8 @@
package ca.uhn.fhir.ws.exceptions;
/**
* Created by dsotnikov on 2/27/2014.
*/
public class MethodNotFoundException extends Exception {
public MethodNotFoundException(String error) { super(error); }
}

View File

@ -0,0 +1,10 @@
package ca.uhn.fhir.ws.operations;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface DELETE {
}

View File

@ -0,0 +1,10 @@
package ca.uhn.fhir.ws.operations;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface GET {
}

View File

@ -0,0 +1,10 @@
package ca.uhn.fhir.ws.operations;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface POST {
}

View File

@ -0,0 +1,10 @@
package ca.uhn.fhir.ws.operations;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface PUT {
}

View File

@ -0,0 +1,10 @@
package ca.uhn.fhir.ws.parameters;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface Optional {
String name();
}

View File

@ -0,0 +1,10 @@
package ca.uhn.fhir.ws.parameters;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface Required {
String name();
}

View File

@ -0,0 +1,18 @@
package ca.uhn.fhir.ws;
import ca.uhn.fhir.model.api.IResource;
import ca.uhn.fhir.model.dstu.resource.Patient;
import ca.uhn.fhir.ws.operations.GET;
import ca.uhn.fhir.ws.parameters.Required;
/**
* Created by dsotnikov on 2/25/2014.
*/
public class PatientResource {
@GET
@ResourceName(value="Patient")
public static Patient getPatient(@Required(name="mrn") String mrn) {
return null;
}
}

View File

@ -0,0 +1,171 @@
package ca.uhn.fhir.ws;
import junit.framework.TestCase;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.junit.Before;
import org.junit.Test;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* Created by dsotnikov on 2/25/2014.
*/
public class ResourceTest extends TestCase {
@Before
public void setUp() throws Exception {
/*
System.setProperty("java.naming.factory.initial", "org.apache.naming.java.javaURLContextFactory");
System.setProperty("java.naming.factory.url.pkgs", "org.apache.naming");
InitialContext context = new InitialContext();
//context.bind("java:comp", "env");
context.bind(context.composeName("java:comp", "env"), "ca.uhn.rest.handlers");
//Context subcontext = context.createSubcontext("java:comp/env");
//context.bind("java:comp/env/ca.uhn.rest.handlers", "ca.uhn.test");
Context env = (Context) new InitialContext().lookup("java:comp/env");
//System.out.println((String) env.lookup("ca.uhn.rest.handlers"));
*/
}
@Test
public void testServlet() throws Exception {
Server server = new Server(3000);
ServletHandler proxyHandler = new ServletHandler();
ServletHolder servletHolder = new ServletHolder(new Service());
proxyHandler.addServletWithMapping(servletHolder, "/");
server.setHandler(proxyHandler);
server.start();
MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
HttpClient client = new HttpClient(connectionManager);
PostMethod httpPost = new PostMethod("http://localhost:3000/foo/bar?bar=123&more=params");
httpPost.setRequestEntity(new StringRequestEntity("test", "application/json", "UTF-8"));
int status = client.executeMethod(httpPost);
System.out.println(status);
// server.join();
}
@Test
public void testResource() throws Exception {
//TODO: better params handling
for (Method m : PatientResource.class.getDeclaredMethods()) {
Util.getResourceParameters(m);
Integer i = 0;
Class<?> c = i.getClass();
System.out.println(c.getCanonicalName());
}
}
@Test
public void testRequiredParamsMissing() {
ResourceMethod rm = new ResourceMethod();
List<Parameter> methodParams = new ArrayList<Parameter>();
methodParams.add(new Parameter("firstName", false));
methodParams.add(new Parameter("lastName", false));
methodParams.add(new Parameter("mrn", true));
rm.setParameters(methodParams);
Set<String> inputParams = new HashSet<String>();
inputParams.add("firstName");
inputParams.add("lastName");
assertEquals(false, rm.matches(inputParams)); //False
}
@Test
public void testRequiredParamsOnly() {
ResourceMethod rm = new ResourceMethod();
List<Parameter> methodParams = new ArrayList<Parameter>();
methodParams.add(new Parameter("firstName", false));
methodParams.add(new Parameter("lastName", false));
methodParams.add(new Parameter("mrn", true));
rm.setParameters(methodParams);
Set<String> inputParams = new HashSet<String>();
inputParams.add("mrn");
assertEquals(true, rm.matches(inputParams)); //True
}
@Test
public void testMixedParams() {
ResourceMethod rm = new ResourceMethod();
List<Parameter> methodParams = new ArrayList<Parameter>();
methodParams.add(new Parameter("firstName", false));
methodParams.add(new Parameter("lastName", false));
methodParams.add(new Parameter("mrn", true));
rm.setParameters(methodParams);
Set<String> inputParams = new HashSet<String>();
inputParams.add("firstName");
inputParams.add("mrn");
assertEquals(true, rm.matches(inputParams)); //True
}
@Test
public void testAllParams() {
ResourceMethod rm = new ResourceMethod();
List<Parameter> methodParams = new ArrayList<Parameter>();
methodParams.add(new Parameter("firstName", false));
methodParams.add(new Parameter("lastName", false));
methodParams.add(new Parameter("mrn", true));
rm.setParameters(methodParams);
Set<String> inputParams = new HashSet<String>();
inputParams.add("firstName");
inputParams.add("lastName");
inputParams.add("mrn");
assertEquals(true, rm.matches(inputParams)); //True
}
@Test
public void testAllParamsWithExtra() {
ResourceMethod rm = new ResourceMethod();
List<Parameter> methodParams = new ArrayList<Parameter>();
methodParams.add(new Parameter("firstName", false));
methodParams.add(new Parameter("lastName", false));
methodParams.add(new Parameter("mrn", true));
rm.setParameters(methodParams);
Set<String> inputParams = new HashSet<String>();
inputParams.add("firstName");
inputParams.add("lastName");
inputParams.add("mrn");
inputParams.add("foo");
assertEquals(false, rm.matches(inputParams)); //False
}
}