Merge branch 'master' of github.com:jamesagnew/hapi-fhir

This commit is contained in:
jamesagnew 2015-09-09 22:46:48 -04:00
commit 8120ab2228
10 changed files with 2511 additions and 2216 deletions

View File

@ -63,6 +63,7 @@ import ca.uhn.fhir.rest.server.exceptions.InternalErrorException;
import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException;
import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException;
import ca.uhn.fhir.rest.server.interceptor.IServerInterceptor;
import ca.uhn.fhir.rest.server.interceptor.ResponseHighlighterInterceptor;
import ca.uhn.fhir.util.ReflectionUtil;
import ca.uhn.fhir.util.UrlUtil;
@ -81,6 +82,7 @@ abstract class BaseResourceReturningMethodBinding extends BaseMethodBinding<Obje
set.add(Constants.PARAM_COUNT);
set.add(Constants.PARAM_SUMMARY);
set.add(Constants.PARAM_ELEMENTS);
set.add(ResponseHighlighterInterceptor.PARAM_RAW);
ALLOWED_PARAMS = Collections.unmodifiableSet(set);
}

View File

@ -94,9 +94,9 @@ public class RestfulServer extends HttpServlet {
private String myImplementationDescription;
private final List<IServerInterceptor> myInterceptors = new ArrayList<IServerInterceptor>();
private IPagingProvider myPagingProvider;
private Collection<Object> myPlainProviders = new ArrayList<Object>();
private final List<Object> myPlainProviders = new ArrayList<Object>();
private Map<String, ResourceBinding> myResourceNameToBinding = new HashMap<String, ResourceBinding>();
private Collection<IResourceProvider> myResourceProviders = new ArrayList<IResourceProvider>();
private final List<IResourceProvider> myResourceProviders = new ArrayList<IResourceProvider>();
private Map<String,IResourceProvider> myTypeToProvider = new HashMap<String, IResourceProvider>();
private IServerAddressStrategy myServerAddressStrategy = new IncomingRequestAddressStrategy();
private ResourceBinding myServerBinding = new ResourceBinding();
@ -1208,7 +1208,10 @@ public class RestfulServer extends HttpServlet {
* @see #setResourceProviders(Collection)
*/
public void setPlainProviders(Collection<Object> theProviders) {
myPlainProviders = theProviders;
myPlainProviders.clear();
if (theProviders != null) {
myPlainProviders.addAll(theProviders);
}
}
/**
@ -1226,21 +1229,30 @@ public class RestfulServer extends HttpServlet {
* @see #setResourceProviders(Collection)
*/
public void setProviders(Object... theProviders) {
myPlainProviders = Arrays.asList(theProviders);
myPlainProviders.clear();
if (theProviders != null) {
myPlainProviders.addAll(Arrays.asList(theProviders));
}
}
/**
* Sets the resource providers for this server
*/
public void setResourceProviders(Collection<IResourceProvider> theResourceProviders) {
myResourceProviders = theResourceProviders;
myResourceProviders.clear();
if (theResourceProviders != null) {
myResourceProviders.addAll(theResourceProviders);
}
}
/**
* Sets the resource providers for this server
*/
public void setResourceProviders(IResourceProvider... theResourceProviders) {
myResourceProviders = Arrays.asList(theResourceProviders);
myResourceProviders.clear();
if (theResourceProviders != null) {
myResourceProviders.addAll(Arrays.asList(theResourceProviders));
}
}
/**

View File

@ -188,6 +188,11 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<archive>
<manifestEntries>
<Build-Time>${maven.build.timestamp}</Build-Time>
</manifestEntries>
</archive>
<overlays>
<overlay>
<groupId>ca.uhn.hapi.fhir</groupId>

View File

@ -198,6 +198,11 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<archive>
<manifestEntries>
<Build-Time>${maven.build.timestamp}</Build-Time>
</manifestEntries>
</archive>
<overlays>
<overlay>
<groupId>ca.uhn.hapi.fhir</groupId>

View File

@ -20,14 +20,15 @@ package ca.uhn.fhir.rest.server.provider;
* #L%
*/
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.io.IOException;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.jar.Manifest;
import ca.uhn.fhir.parser.DataFormatException;
import org.apache.commons.lang3.StringUtils;
import ca.uhn.fhir.context.RuntimeResourceDefinition;
@ -121,7 +122,7 @@ public class ServerConformanceProvider implements IServerConformanceProvider<Con
Conformance retVal = new Conformance();
retVal.setPublisher(myPublisher);
retVal.setDate(DateTimeDt.withCurrentTime());
retVal.setDate(conformanceDate());
retVal.setFhirVersion("0.0.82-3059"); // TODO: pull from model
retVal.setAcceptUnknown(false); // TODO: make this configurable - this is a fairly big effort since the parser needs to be modified to actually allow it
@ -213,6 +214,33 @@ public class ServerConformanceProvider implements IServerConformanceProvider<Con
return retVal;
}
private DateTimeDt conformanceDate() {
String buildDate = getBuildDateFromManifest();
if (buildDate != null) {
try {
return new DateTimeDt(buildDate);
} catch (DataFormatException e) {
// fall through
}
}
return DateTimeDt.withCurrentTime();
}
private String getBuildDateFromManifest() {
if (myRestfulServer != null && myRestfulServer.getServletContext() != null) {
InputStream inputStream = myRestfulServer.getServletContext().getResourceAsStream("/META-INF/MANIFEST.MF");
if (inputStream != null) {
try {
Manifest manifest = new Manifest(inputStream);
return manifest.getMainAttributes().getValue("Build-Time");
} catch (IOException e) {
// fall through
}
}
}
return null;
}
private void handleDynamicSearchMethodBinding(RestResource resource, RuntimeResourceDefinition def, TreeSet<String> includes, DynamicSearchMethodBinding searchMethodBinding) {
includes.addAll(searchMethodBinding.getIncludes());

View File

@ -21,21 +21,15 @@ package ca.uhn.fhir.rest.server.provider.dstu2;
*/
import static org.apache.commons.lang3.StringUtils.isNotBlank;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.jar.Manifest;
import javax.servlet.http.HttpServletRequest;
import ca.uhn.fhir.parser.DataFormatException;
import org.apache.commons.lang3.StringUtils;
import ca.uhn.fhir.context.RuntimeResourceDefinition;
@ -171,7 +165,7 @@ public class ServerConformanceProvider implements IServerConformanceProvider<Con
Conformance retVal = new Conformance();
retVal.setPublisher(myPublisher);
retVal.setDate(DateTimeDt.withCurrentTime());
retVal.setDate(conformanceDate());
retVal.setFhirVersion("1.0.0"); // TODO: pull from model
retVal.setAcceptUnknown(UnknownContentCodeEnum.UNKNOWN_EXTENSIONS); // TODO: make this configurable - this is a fairly big effort since the parser
// needs to be modified to actually allow it
@ -302,6 +296,33 @@ public class ServerConformanceProvider implements IServerConformanceProvider<Con
return retVal;
}
private DateTimeDt conformanceDate() {
String buildDate = getBuildDateFromManifest();
if (buildDate != null) {
try {
return new DateTimeDt(buildDate);
} catch (DataFormatException e) {
// fall through
}
}
return DateTimeDt.withCurrentTime();
}
private String getBuildDateFromManifest() {
if (myRestfulServer != null && myRestfulServer.getServletContext() != null) {
InputStream inputStream = myRestfulServer.getServletContext().getResourceAsStream("/META-INF/MANIFEST.MF");
if (inputStream != null) {
try {
Manifest manifest = new Manifest(inputStream);
return manifest.getMainAttributes().getValue("Build-Time");
} catch (IOException e) {
// fall through
}
}
}
return null;
}
private void handleDynamicSearchMethodBinding(RestResource resource, RuntimeResourceDefinition def, TreeSet<String> includes, DynamicSearchMethodBinding searchMethodBinding) {
includes.addAll(searchMethodBinding.getIncludes());

View File

@ -0,0 +1,154 @@
package ca.uhn.fhir.rest.server;
import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpOptions;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.model.api.IResource;
import ca.uhn.fhir.model.dstu2.resource.Patient;
import ca.uhn.fhir.model.primitive.IdDt;
import ca.uhn.fhir.rest.annotation.IdParam;
import ca.uhn.fhir.rest.annotation.Read;
import ca.uhn.fhir.util.PortUtil;
/**
* Created by dsotnikov on 2/25/2014.
*/
public class ServerFeaturesDstu2Test {
private static CloseableHttpClient ourClient;
private static int ourPort;
private static Server ourServer;
private static FhirContext ourCtx = FhirContext.forDstu2();
private static RestfulServer ourServlet;
@Test
public void testOptions() throws Exception {
HttpOptions httpGet = new HttpOptions("http://localhost:" + ourPort + "");
HttpResponse status = ourClient.execute(httpGet);
String responseContent = IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
assertEquals(200, status.getStatusLine().getStatusCode());
assertThat(responseContent, containsString("<Conformance"));
}
@Test
public void testRegisterAndUnregisterResourceProviders() throws Exception {
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient/1");
HttpResponse status = ourClient.execute(httpGet);
String responseContent = IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
assertEquals(200, status.getStatusLine().getStatusCode());
assertThat(responseContent, containsString("PRP1"));
Collection<IResourceProvider> providers = new ArrayList<IResourceProvider>(ourServlet.getResourceProviders());
for (IResourceProvider provider : providers) {
ourServlet.unregisterProvider(provider);
}
ourServlet.registerProvider(new DummyPatientResourceProvider2());
httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient/1");
status = ourClient.execute(httpGet);
responseContent = IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
assertEquals(200, status.getStatusLine().getStatusCode());
assertThat(responseContent, containsString("PRP2"));
}
@Test
public void testOptionsJson() throws Exception {
HttpOptions httpGet = new HttpOptions("http://localhost:" + ourPort + "?_format=json");
HttpResponse status = ourClient.execute(httpGet);
String responseContent = IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
assertEquals(200, status.getStatusLine().getStatusCode());
assertThat(responseContent, containsString("resourceType\":\"Conformance"));
}
@AfterClass
public static void afterClass() throws Exception {
ourServer.stop();
}
@BeforeClass
public static void beforeClass() throws Exception {
ourPort = PortUtil.findFreePort();
ourServer = new Server(ourPort);
DummyPatientResourceProvider patientProvider = new DummyPatientResourceProvider();
ServletHandler proxyHandler = new ServletHandler();
ourServlet = new RestfulServer(ourCtx);
ourServlet.setFhirContext(ourCtx);
ourServlet.setResourceProviders(patientProvider);
ServletHolder servletHolder = new ServletHolder(ourServlet);
proxyHandler.addServletWithMapping(servletHolder, "/*");
ourServer.setHandler(proxyHandler);
ourServer.start();
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS);
HttpClientBuilder builder = HttpClientBuilder.create();
builder.setConnectionManager(connectionManager);
ourClient = builder.build();
}
public static class DummyPatientResourceProvider implements IResourceProvider {
@Read
public Patient read(@IdParam IdDt theId) {
Patient p1 = new Patient();
p1.setId("p1ReadId");
p1.addIdentifier().setValue("PRP1");
return p1;
}
@Override
public Class<? extends IResource> getResourceType() {
return Patient.class;
}
}
public static class DummyPatientResourceProvider2 implements IResourceProvider {
@Read
public Patient read(@IdParam IdDt theId) {
Patient p1 = new Patient();
p1.setId("p1ReadId");
p1.addIdentifier().setValue("PRP2");
return p1;
}
@Override
public Class<? extends IResource> getResourceType() {
return Patient.class;
}
}
}

View File

@ -21,6 +21,11 @@ package org.hl7.fhir.instance.conf;
*/
import static org.apache.commons.lang3.StringUtils.isNotBlank;
import java.io.IOException;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
@ -34,6 +39,7 @@ import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.jar.Manifest;
import javax.servlet.http.HttpServletRequest;
@ -75,6 +81,7 @@ import ca.uhn.fhir.rest.server.Constants;
import ca.uhn.fhir.rest.server.IServerConformanceProvider;
import ca.uhn.fhir.rest.server.ResourceBinding;
import ca.uhn.fhir.rest.server.RestfulServer;
import ca.uhn.fhir.rest.server.exceptions.InternalErrorException;
import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException;
/**
@ -92,29 +99,30 @@ import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException;
*/
public class ServerConformanceProvider implements IServerConformanceProvider<Conformance> {
private boolean myCache = true;
private volatile Conformance myConformance;
private IdentityHashMap<OperationMethodBinding, String> myOperationBindingToName;
private HashMap<String, List<OperationMethodBinding>> myOperationNameToBindings;
private String myPublisher = "Not provided";
private RestfulServer myRestfulServer;
private boolean myCache = true;
private volatile Conformance myConformance;
private IdentityHashMap<OperationMethodBinding, String> myOperationBindingToName;
private HashMap<String, List<OperationMethodBinding>> myOperationNameToBindings;
private String myPublisher = "Not provided";
private RestfulServer myRestfulServer;
public ServerConformanceProvider(RestfulServer theRestfulServer) {
myRestfulServer = theRestfulServer;
}
/*
* Add a no-arg constructor and seetter so that the ServerConfirmanceProvider
* can be Spring-wired with the RestfulService avoiding the potential
* reference cycle that would happen.
*/
public ServerConformanceProvider() {
super();
}
public void setRestfulServer(RestfulServer theRestfulServer) {
myRestfulServer = theRestfulServer;
}
public ServerConformanceProvider(RestfulServer theRestfulServer) {
myRestfulServer = theRestfulServer;
}
/*
* Add a no-arg constructor and seetter so that the
* ServerConfirmanceProvider can be Spring-wired with
* the RestfulService avoiding the potential reference
* cycle that would happen.
*/
public ServerConformanceProvider () {
super();
}
public void setRestfulServer (RestfulServer theRestfulServer) {
myRestfulServer = theRestfulServer;
}
private void checkBindingForSystemOps(ConformanceRestComponent rest, Set<SystemRestfulInteraction> systemOps,
BaseMethodBinding<?> nextMethodBinding) {
@ -184,13 +192,9 @@ public class ServerConformanceProvider implements IServerConformanceProvider<Con
Conformance retVal = new Conformance();
retVal.setPublisher(myPublisher);
retVal.setDate(new Date());
retVal.setDate(conformanceDate());
retVal.setFhirVersion("1.0.0"); // TODO: pull from model
retVal.setAcceptUnknown(UnknownContentCode.EXTENSIONS); // TODO: make this
// configurable -
// this is a fairly
// big effort since
// the parser
retVal.setAcceptUnknown(UnknownContentCode.EXTENSIONS); // TODO: make this configurable - this is a fairly big effort since the parser
// needs to be modified to actually allow it
retVal.getImplementation().setDescription(myRestfulServer.getImplementationDescription());
@ -325,6 +329,34 @@ public class ServerConformanceProvider implements IServerConformanceProvider<Con
return retVal;
}
private Date conformanceDate() {
String buildDate = getBuildDateFromManifest();
if (buildDate != null) {
DateFormat dateFormat = new SimpleDateFormat();
try {
return dateFormat.parse(buildDate);
} catch (ParseException e) {
// fall through
}
}
return new Date();
}
private String getBuildDateFromManifest() {
if (myRestfulServer != null && myRestfulServer.getServletContext() != null) {
InputStream inputStream = myRestfulServer.getServletContext().getResourceAsStream("/META-INF/MANIFEST.MF");
if (inputStream != null) {
try {
Manifest manifest = new Manifest(inputStream);
return manifest.getMainAttributes().getValue("Build-Time");
} catch (IOException e) {
// fall through
}
}
}
return null;
}
private void handleDynamicSearchMethodBinding(ConformanceRestResourceComponent resource,
RuntimeResourceDefinition def, TreeSet<String> includes, DynamicSearchMethodBinding searchMethodBinding) {
includes.addAll(searchMethodBinding.getIncludes());

View File

@ -196,6 +196,9 @@
</licenses>
<properties>
<!-- configure timestamp in MANIFEST.MF for maven-war-provider -->
<maven.build.timestamp.format>yyyy-MM-dd'T'HH:mm:ss'Z'</maven.build.timestamp.format>
<!-- This property is used in some of the site documentation where the version is shown, so that we can deploy the site even if the project is on a snapshot version. -->
<hapi_stable_version>1.1</hapi_stable_version>