From f9419ea9105aec5d95e19d49af22b8e7b0cfc559 Mon Sep 17 00:00:00 2001 From: James Agnew Date: Wed, 3 Sep 2014 18:03:44 -0400 Subject: [PATCH] Fix up examples --- bormb.txt | 61 -------------- bormbwrp.txt | 68 ---------------- check_for_maven_updates.sh | 3 + hapi-fhir-base/pom.xml | 12 ++- .../src/test/resources/logback-test.xml | 5 +- hapi-fhir-jpaserver-test/pom.xml | 4 +- pom.xml | 71 +++++++---------- restful-server-example-test/.classpath | 26 ++++++ restful-server-example-test/.gitignore | 1 + restful-server-example-test/.project | 23 ++++++ .../org.eclipse.core.resources.prefs | 2 + .../.settings/org.eclipse.jdt.core.prefs | 5 ++ .../.settings/org.eclipse.m2e.core.prefs | 4 + restful-server-example-test/pom.xml | 79 +++++++++++++++++++ .../test/java/ca/uhn/example/ExampleTest.java | 61 ++++++++++++++ .../uhn/example/RandomServerPortProvider.java | 37 +++++++++ .../org.eclipse.wst.common.component | 2 +- restful-server-example/pom.xml | 2 - .../OrganizationResourceProvider.java | 1 + .../provider/PatientResourceProvider.java | 37 ++++++--- 20 files changed, 311 insertions(+), 193 deletions(-) delete mode 100644 bormb.txt delete mode 100644 bormbwrp.txt create mode 100644 check_for_maven_updates.sh create mode 100644 restful-server-example-test/.classpath create mode 100644 restful-server-example-test/.gitignore create mode 100644 restful-server-example-test/.project create mode 100644 restful-server-example-test/.settings/org.eclipse.core.resources.prefs create mode 100644 restful-server-example-test/.settings/org.eclipse.jdt.core.prefs create mode 100644 restful-server-example-test/.settings/org.eclipse.m2e.core.prefs create mode 100644 restful-server-example-test/pom.xml create mode 100644 restful-server-example-test/src/test/java/ca/uhn/example/ExampleTest.java create mode 100644 restful-server-example-test/src/test/java/ca/uhn/example/RandomServerPortProvider.java diff --git a/bormb.txt b/bormb.txt deleted file mode 100644 index f813181c0cc..00000000000 --- a/bormb.txt +++ /dev/null @@ -1,61 +0,0 @@ - @Override - public void invokeServer(RestfulServer theServer, Request theRequest, HttpServletResponse theResponse) throws BaseServerResponseException, IOException { - Object[] params = new Object[getParameters().size()]; - for (int i = 0; i < getParameters().size(); i++) { - IParameter param = getParameters().get(i); - if (param != null) { - params[i] = param.translateQueryParametersIntoServerArgument(theRequest, null); - } - } - - addParametersForServerRequest(theRequest, params); - - MethodOutcome response = (MethodOutcome) invokeServerMethod(getProvider(), params); - - if (response == null) { - if (myReturnVoid == false) { - throw new ConfigurationException("Method " + getMethod().getName() + " in type " + getMethod().getDeclaringClass().getCanonicalName() + " returned null"); - } else { - theResponse.setStatus(Constants.STATUS_HTTP_204_NO_CONTENT); - } - } else if (!myReturnVoid) { - if (response.isCreated()) { - theResponse.setStatus(Constants.STATUS_HTTP_201_CREATED); - StringBuilder b = new StringBuilder(); - b.append(theRequest.getFhirServerBase()); - b.append('/'); - b.append(getResourceName()); - b.append('/'); - b.append(response.getId().getValue()); - if (response.getVersionId() != null && response.getVersionId().isEmpty() == false) { - b.append("/_history/"); - b.append(response.getVersionId().getValue()); - } - theResponse.addHeader("Location", b.toString()); - } else { - theResponse.setStatus(Constants.STATUS_HTTP_200_OK); - } - } else { - theResponse.setStatus(Constants.STATUS_HTTP_204_NO_CONTENT); - } - - theServer.addHeadersToResponse(theResponse); - - Writer writer = theResponse.getWriter(); - try { - if (response != null) { - OperationOutcome outcome = new OperationOutcome(); - if (response.getOperationOutcome() != null && response.getOperationOutcome().getIssue() != null) { - outcome.getIssue().addAll(response.getOperationOutcome().getIssue()); - } - EncodingUtil encoding = BaseMethodBinding.determineResponseEncoding(theRequest.getServletRequest(), theRequest.getParameters()); - theResponse.setContentType(encoding.getResourceContentType()); - IParser parser = encoding.newParser(getContext()); - parser.encodeResourceToWriter(outcome, writer); - } - } finally { - writer.close(); - } - // getMethod().in - } - diff --git a/bormbwrp.txt b/bormbwrp.txt deleted file mode 100644 index 8b6ac0bc6ff..00000000000 --- a/bormbwrp.txt +++ /dev/null @@ -1,68 +0,0 @@ - @Override - public void invokeServer(RestfulServer theServer, Request theRequest, HttpServletResponse theResponse) throws BaseServerResponseException, IOException { - EncodingUtil encoding = BaseMethodBinding.determineResponseEncoding(theRequest.getServletRequest(), theRequest.getParameters()); - IParser parser = encoding.newParser(getContext()); - IResource resource = parser.parseResource(theRequest.getInputReader()); - - Object[] params = new Object[getParameters().size()]; - for (int i = 0; i < getParameters().size(); i++) { - IParameter param = getParameters().get(i); - if (param == null) { - continue; - } - params[i] = param.translateQueryParametersIntoServerArgument(theRequest, resource); - } - - addParametersForServerRequest(theRequest, params); - - MethodOutcome response; - try { - response = (MethodOutcome) invokeServerMethod(getProvider(), params); - } catch (BaseServerResponseException e) { - streamOperationOutcome(e, theServer, encoding, theResponse); - return; - } - - if (response == null) { - if (isReturnVoid() == false) { - throw new ConfigurationException("Method " + getMethod().getName() + " in type " + getMethod().getDeclaringClass().getCanonicalName() + " returned null"); - } - } else if (!isReturnVoid()) { - if (response.isCreated()) { - theResponse.setStatus(Constants.STATUS_HTTP_201_CREATED); - StringBuilder b = new StringBuilder(); - b.append(theRequest.getFhirServerBase()); - b.append('/'); - b.append(getResourceName()); - b.append('/'); - b.append(response.getId().getValue()); - if (response.getVersionId() != null && response.getVersionId().isEmpty() == false) { - b.append("/_history/"); - b.append(response.getVersionId().getValue()); - } - theResponse.addHeader("Location", b.toString()); - } else { - theResponse.setStatus(Constants.STATUS_HTTP_200_OK); - } - } else { - theResponse.setStatus(Constants.STATUS_HTTP_204_NO_CONTENT); - } - - theServer.addHeadersToResponse(theResponse); - - if (response != null && response.getOperationOutcome() != null) { - theResponse.setContentType(encoding.getResourceContentType()); - Writer writer = theResponse.getWriter(); - try { - parser.encodeResourceToWriter(response.getOperationOutcome(), writer); - } finally { - writer.close(); - } - } else { - theResponse.setContentType(Constants.CT_TEXT); - Writer writer = theResponse.getWriter(); - writer.close(); - } - - // getMethod().in - } diff --git a/check_for_maven_updates.sh b/check_for_maven_updates.sh new file mode 100644 index 00000000000..12fd0e26352 --- /dev/null +++ b/check_for_maven_updates.sh @@ -0,0 +1,3 @@ +mvn versions:display-dependency-updates +mvn versions:display-plugin-updates + diff --git a/hapi-fhir-base/pom.xml b/hapi-fhir-base/pom.xml index 45f4e8fab48..c1fafffc7f9 100644 --- a/hapi-fhir-base/pom.xml +++ b/hapi-fhir-base/pom.xml @@ -40,7 +40,7 @@ org.codehaus.woodstox woodstox-core-asl - 4.2.0 + ${woodstox_version} @@ -106,7 +106,7 @@ org.apache.commons commons-lang3 - 3.2.1 + ${commons_lang_version} commons-codec @@ -274,7 +274,7 @@ com.google.guava guava - 17.0 + ${guava_version} test - + + diff --git a/restful-server-example-test/.classpath b/restful-server-example-test/.classpath new file mode 100644 index 00000000000..fd7ad7fbda7 --- /dev/null +++ b/restful-server-example-test/.classpath @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/restful-server-example-test/.gitignore b/restful-server-example-test/.gitignore new file mode 100644 index 00000000000..b83d22266ac --- /dev/null +++ b/restful-server-example-test/.gitignore @@ -0,0 +1 @@ +/target/ diff --git a/restful-server-example-test/.project b/restful-server-example-test/.project new file mode 100644 index 00000000000..7e28d1c5f31 --- /dev/null +++ b/restful-server-example-test/.project @@ -0,0 +1,23 @@ + + + restful-server-example-test + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.jdt.core.javanature + org.eclipse.m2e.core.maven2Nature + + diff --git a/restful-server-example-test/.settings/org.eclipse.core.resources.prefs b/restful-server-example-test/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 00000000000..99f26c0203a --- /dev/null +++ b/restful-server-example-test/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +encoding/=UTF-8 diff --git a/restful-server-example-test/.settings/org.eclipse.jdt.core.prefs b/restful-server-example-test/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 00000000000..60105c1b951 --- /dev/null +++ b/restful-server-example-test/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,5 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 +org.eclipse.jdt.core.compiler.compliance=1.6 +org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning +org.eclipse.jdt.core.compiler.source=1.6 diff --git a/restful-server-example-test/.settings/org.eclipse.m2e.core.prefs b/restful-server-example-test/.settings/org.eclipse.m2e.core.prefs new file mode 100644 index 00000000000..f897a7f1cb2 --- /dev/null +++ b/restful-server-example-test/.settings/org.eclipse.m2e.core.prefs @@ -0,0 +1,4 @@ +activeProfiles= +eclipse.preferences.version=1 +resolveWorkspaceProjects=true +version=1 diff --git a/restful-server-example-test/pom.xml b/restful-server-example-test/pom.xml new file mode 100644 index 00000000000..8fcc6e5a8f1 --- /dev/null +++ b/restful-server-example-test/pom.xml @@ -0,0 +1,79 @@ + + 4.0.0 + + + ca.uhn.hapi.fhir + hapi-fhir + 0.6-SNAPSHOT + ../pom.xml + + + restful-server-example-test + jar + + HAPI Tinder Plugin - Test Project + + + + ca.uhn.hapi.fhir + hapi-fhir-base + 0.6-SNAPSHOT + + + ch.qos.logback + logback-classic + ${logback_version} + true + + + junit + junit + ${junit_version} + test + + + org.eclipse.jetty + jetty-servlets + ${jetty_version} + test + + + org.eclipse.jetty + jetty-servlet + ${jetty_version} + test + + + org.eclipse.jetty + jetty-server + ${jetty_version} + test + + + org.eclipse.jetty + jetty-util + ${jetty_version} + test + + + org.eclipse.jetty + jetty-webapp + ${jetty_version} + test + + + + + + + + org.apache.maven.plugins + maven-deploy-plugin + + true + + + + + + diff --git a/restful-server-example-test/src/test/java/ca/uhn/example/ExampleTest.java b/restful-server-example-test/src/test/java/ca/uhn/example/ExampleTest.java new file mode 100644 index 00000000000..61a853a5e65 --- /dev/null +++ b/restful-server-example-test/src/test/java/ca/uhn/example/ExampleTest.java @@ -0,0 +1,61 @@ +package ca.uhn.example; + +import static org.junit.Assert.*; + +import org.eclipse.jetty.server.Server; +import org.eclipse.jetty.webapp.WebAppContext; +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.dstu.resource.Patient; +import ca.uhn.fhir.rest.client.IGenericClient; + +public class ExampleTest { + + private static int ourPort; + private static Server ourServer; + private static FhirContext ourCtx; + private static IGenericClient ourClient; + + @AfterClass + public static void afterClass() throws Exception { + ourServer.stop(); + } + + @Test + public void test01Search() { + Bundle results = ourClient.search().forResource(Patient.class).execute(); + assertEquals(1, results.size()); + + + } + + + @BeforeClass + public static void beforeClass() throws Exception { + + ourPort = RandomServerPortProvider.findFreePort(); + ourServer = new Server(ourPort); + + WebAppContext root = new WebAppContext(); + + root.setWar("file:../restful-server-example/target/restful-server-example.war"); + root.setContextPath("/"); + root.setAttribute(WebAppContext.BASETEMPDIR, "target/tempextrtact"); + root.setParentLoaderPriority(false); + root.setCopyWebInf(true); + root.setCopyWebDir(true); + + ourServer.setHandler(root); + + ourServer.start(); + + ourCtx = new FhirContext(); + ourClient = ourCtx.newRestfulGenericClient("http://localhost:" + ourPort+"/fhir"); + + } + +} diff --git a/restful-server-example-test/src/test/java/ca/uhn/example/RandomServerPortProvider.java b/restful-server-example-test/src/test/java/ca/uhn/example/RandomServerPortProvider.java new file mode 100644 index 00000000000..3cdad966f8e --- /dev/null +++ b/restful-server-example-test/src/test/java/ca/uhn/example/RandomServerPortProvider.java @@ -0,0 +1,37 @@ +package ca.uhn.example; + + +import java.io.IOException; +import java.net.ServerSocket; +import java.util.ArrayList; +import java.util.List; + +/** + * Provides server ports + */ +public class RandomServerPortProvider { + + private static List ourPorts = new ArrayList(); + + public static int findFreePort() { + ServerSocket server; + try { + server = new ServerSocket(0); + int port = server.getLocalPort(); + ourPorts.add(port); + server.close(); + Thread.sleep(500); + return port; + } catch (IOException e) { + throw new Error(e); + } catch (InterruptedException e) { + throw new Error(e); + } + } + + public static List list() { + return ourPorts; + } + +} + \ No newline at end of file diff --git a/restful-server-example/.settings/org.eclipse.wst.common.component b/restful-server-example/.settings/org.eclipse.wst.common.component index e2cd387d6d8..263e87f0f0d 100644 --- a/restful-server-example/.settings/org.eclipse.wst.common.component +++ b/restful-server-example/.settings/org.eclipse.wst.common.component @@ -6,7 +6,7 @@ uses - + consumes diff --git a/restful-server-example/pom.xml b/restful-server-example/pom.xml index 4c0ed667d48..29bad8dd318 100644 --- a/restful-server-example/pom.xml +++ b/restful-server-example/pom.xml @@ -70,7 +70,6 @@ org.thymeleaf thymeleaf 2.1.3.RELEASE - true @@ -78,7 +77,6 @@ org.ebaysf.web cors-filter 1.0.1 - true servlet-api diff --git a/restful-server-example/src/main/java/ca/uhn/example/provider/OrganizationResourceProvider.java b/restful-server-example/src/main/java/ca/uhn/example/provider/OrganizationResourceProvider.java index a36a6db51c0..8553ad21dbd 100644 --- a/restful-server-example/src/main/java/ca/uhn/example/provider/OrganizationResourceProvider.java +++ b/restful-server-example/src/main/java/ca/uhn/example/provider/OrganizationResourceProvider.java @@ -50,6 +50,7 @@ public class OrganizationResourceProvider implements IResourceProvider { } MyOrganization retVal = new MyOrganization(); + retVal.setId("1"); retVal.addIdentifier("urn:example:orgs", "FooOrganization"); retVal.addAddress().addLine("123 Fake Street").setCity("Toronto"); retVal.addTelecom().setUse(ContactUseEnum.WORK).setValue("1-888-123-4567"); diff --git a/restful-server-example/src/main/java/ca/uhn/example/provider/PatientResourceProvider.java b/restful-server-example/src/main/java/ca/uhn/example/provider/PatientResourceProvider.java index feb3968e136..cce7486d6cf 100644 --- a/restful-server-example/src/main/java/ca/uhn/example/provider/PatientResourceProvider.java +++ b/restful-server-example/src/main/java/ca/uhn/example/provider/PatientResourceProvider.java @@ -30,8 +30,8 @@ import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException; import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException; /** - * This is a resource provider which stores Patient resources in memory using a HashMap. This is obviously not a production-ready solution for many reasons, but it is useful to help illustrate how to - * build a fully-functional server. + * This is a resource provider which stores Patient resources in memory using a HashMap. This is obviously not a production-ready solution for many reasons, + * but it is useful to help illustrate how to build a fully-functional server. */ public class PatientResourceProvider implements IResourceProvider { @@ -49,7 +49,10 @@ public class PatientResourceProvider implements IResourceProvider { * Constructor, which pre-populates the provider with one resource instance. */ public PatientResourceProvider() { + long resourceId = myNextId++; + Patient patient = new Patient(); + patient.setId(Long.toString(resourceId)); patient.addIdentifier(); patient.getIdentifier().get(0).setSystem(new UriDt("urn:hapitest:mrns")); patient.getIdentifier().get(0).setValue("00002"); @@ -59,7 +62,9 @@ public class PatientResourceProvider implements IResourceProvider { LinkedList list = new LinkedList(); list.add(patient); - myIdToPatientVersions.put(myNextId++, list); + + + myIdToPatientVersions.put(resourceId, list); } @@ -90,12 +95,13 @@ public class PatientResourceProvider implements IResourceProvider { Deque existingVersions = myIdToPatientVersions.get(theId); - /* - * We just use the current number of versions as the next version number - */ - IdDt version = new IdDt(existingVersions.size()); - thePatient.getResourceMetadata().put(ResourceMetadataKeyEnum.VERSION_ID, version); - + // We just use the current number of versions as the next version number + String newVersion = Integer.toString(existingVersions.size()); + + // Create an ID with the new version and assign it back to the resource + IdDt newId = new IdDt("Patient", Long.toString(theId), newVersion); + thePatient.setId(newId); + existingVersions.add(thePatient); } @@ -152,6 +158,19 @@ public class PatientResourceProvider implements IResourceProvider { return retVal; } + @Search + public List findPatientsUsingArbitraryCtriteria() { + LinkedList retVal = new LinkedList(); + + for (Deque nextPatientList : myIdToPatientVersions.values()) { + Patient nextPatient = nextPatientList.getLast(); + retVal.add(nextPatient); + } + + return retVal; + } + + /** * The getResourceType method comes from IResourceProvider, and must be overridden to indicate what type of resource this provider supplies. */