Start server profiles

This commit is contained in:
James Agnew 2014-08-12 17:22:05 -04:00
parent 0ff847ac21
commit 408114235f
5 changed files with 176 additions and 2 deletions

View File

@ -72,6 +72,10 @@
is "yyyy-mm-dd" anyhow, and this is correctly handled). Thanks to Jeffrey Ting of Systems Made Simple
for reporting!
</action>
<action type="add">
Server now adds a profile tag to returned results if the resource being returned
doesn't already have one
</action>
</release>
<release version="0.5" date="2014-Jul-30">
<action type="add">

View File

@ -48,4 +48,14 @@ public class TagList extends ArrayList<Tag> {
return null;
}
public ArrayList<Tag> getTagsWithScheme(String theScheme) {
ArrayList<Tag> retVal = new ArrayList<Tag>();
for (Tag next : this) {
if (theScheme.equals(next.getScheme())) {
retVal.add(next);
}
}
return retVal;
}
}

View File

@ -97,6 +97,7 @@ public class Constants {
public static final int STATUS_HTTP_500_INTERNAL_ERROR = 500;
public static final int STATUS_HTTP_501_NOT_IMPLEMENTED = 501;
public static final String URL_TOKEN_HISTORY = "_history";
public static final String TAG_SCHEME_PROFILE = "http://hl7.org/fhir/tag/profile ";
static {

View File

@ -55,6 +55,7 @@ import org.apache.commons.lang3.exception.ExceptionUtils;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.context.RuntimeResourceDefinition;
import ca.uhn.fhir.model.api.Bundle;
import ca.uhn.fhir.model.api.BundleEntry;
import ca.uhn.fhir.model.api.IResource;
import ca.uhn.fhir.model.api.ResourceMetadataKeyEnum;
import ca.uhn.fhir.model.api.Tag;
@ -846,17 +847,35 @@ public class RestfulServer extends HttpServlet {
addedResources.addAll(addedResourcesThisPass);
} while (references.isEmpty() == false);
bundle.addResource(next, theContext, theServerBase);
BundleEntry entry = bundle.addResource(next, theContext, theServerBase);
addProfileToBundleEntry(theContext, next, entry);
}
/*
* Actually add the resources to the bundle
*/
for (IResource next : addedResources) {
bundle.addResource(next, theContext, theServerBase);
BundleEntry entry = bundle.addResource(next, theContext, theServerBase);
addProfileToBundleEntry(theContext, next, entry);
}
bundle.getTotalResults().setValue(theTotalResults);
return bundle;
}
private static void addProfileToBundleEntry(FhirContext theContext, IResource next, BundleEntry entry) {
ArrayList<Tag> profileTags = entry.getCategories().getTagsWithScheme(Constants.TAG_SCHEME_PROFILE);
if (profileTags.isEmpty()) {
RuntimeResourceDefinition nextDef = theContext.getResourceDefinition(next);
String profile = nextDef.getResourceProfile();
if (isNotBlank(profile)) {
entry.addCategory(new Tag(Constants.TAG_SCHEME_PROFILE, profile, null));
}
}
}
public static String createPagingLink(String theServerBase, String theSearchId, int theOffset, int theCount, EncodingEnum theResponseEncoding, boolean thePrettyPrint) {
StringBuilder b = new StringBuilder();
b.append(theServerBase);

View File

@ -0,0 +1,140 @@
package ca.uhn.fhir.rest.server;
import static org.junit.Assert.assertEquals;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;
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.Bundle;
import ca.uhn.fhir.model.api.BundleEntry;
import ca.uhn.fhir.model.api.IResource;
import ca.uhn.fhir.model.api.Tag;
import ca.uhn.fhir.model.api.annotation.ResourceDef;
import ca.uhn.fhir.model.dstu.composite.CodingDt;
import ca.uhn.fhir.model.dstu.resource.Observation;
import ca.uhn.fhir.model.dstu.resource.Patient;
import ca.uhn.fhir.narrative.DefaultThymeleafNarrativeGenerator;
import ca.uhn.fhir.rest.annotation.OptionalParam;
import ca.uhn.fhir.rest.annotation.RequiredParam;
import ca.uhn.fhir.rest.annotation.Search;
import ca.uhn.fhir.rest.param.ReferenceParam;
import ca.uhn.fhir.rest.param.StringParam;
import ca.uhn.fhir.rest.param.TokenOrListParam;
import ca.uhn.fhir.testutil.RandomServerPortProvider;
/**
* Created by dsotnikov on 2/25/2014.
*/
public class CustomTypeTest {
private static CloseableHttpClient ourClient;
private static FhirContext ourCtx = new FhirContext(ExtendedPatient.class);
private static int ourPort;
private static Server ourServer;
@Test
public void testSearchReturnsProfile() throws Exception {
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient?_id=aaa");
HttpResponse status = ourClient.execute(httpGet);
String responseContent = IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
assertEquals(200, status.getStatusLine().getStatusCode());
Bundle bundle = ourCtx.newXmlParser().parseBundle(responseContent);
assertEquals(1, bundle.getEntries().size());
BundleEntry entry = bundle.getEntries().get(0);
ArrayList<Tag> profileTags = entry.getCategories().getTagsWithScheme(Constants.TAG_SCHEME_PROFILE);
assertEquals(1, profileTags.size());
assertEquals("http://foo/profiles/Profile", profileTags.get(0).getTerm());
Patient p = (Patient) bundle.getEntries().get(0).getResource();
assertEquals("idaaa", p.getNameFirstRep().getFamilyAsSingleString());
}
@AfterClass
public static void afterClass() throws Exception {
ourServer.stop();
}
@BeforeClass
public static void beforeClass() throws Exception {
ourPort = RandomServerPortProvider.findFreePort();
ourServer = new Server(ourPort);
DummyPatientResourceProvider patientProvider = new DummyPatientResourceProvider();
ServletHandler proxyHandler = new ServletHandler();
RestfulServer servlet = new RestfulServer();
servlet.getFhirContext().setNarrativeGenerator(new DefaultThymeleafNarrativeGenerator());
servlet.setResourceProviders(patientProvider);
ServletHolder servletHolder = new ServletHolder(servlet);
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();
}
@ResourceDef(name="Patient", profile="http://foo/profiles/Profile")
public static class ExtendedPatient extends Patient {
}
/**
* Created by dsotnikov on 2/25/2014.
*/
public static class DummyPatientResourceProvider implements IResourceProvider {
@Search
public List<Patient> findPatient(@OptionalParam(name = "_id") StringParam theParam) {
ArrayList<Patient> retVal = new ArrayList<Patient>();
ExtendedPatient patient = new ExtendedPatient();
patient.setId("1");
patient.addIdentifier("system", "identifier123");
if (theParam != null) {
patient.addName().addFamily("id" + theParam.getValue());
}
retVal.add(patient);
return retVal;
}
@Override
public Class<? extends IResource> getResourceType() {
return Patient.class;
}
}
}