From 1793f6d9ab9a941cae73918ebe6ea56d8a8df112 Mon Sep 17 00:00:00 2001 From: James Agnew Date: Tue, 5 Aug 2014 10:46:46 -0400 Subject: [PATCH] Fix #4- Create operation should return HTTP 200 --- hapi-fhir-base/src/changes/changes.xml | 11 +- .../uhn/fhir/rest/client/GenericClient.java | 8 +- .../BaseOutcomeReturningMethodBinding.java | 113 ++++++++---------- .../fhir/rest/client/GenericClientTest.java | 28 +++++ .../ca/uhn/fhir/rest/server/UpdateTest.java | 33 +++-- hapi-fhir-jpaserver-base/pom.xml | 10 ++ pom.xml | 2 + .../org.eclipse.wst.common.component | 2 +- 8 files changed, 128 insertions(+), 79 deletions(-) diff --git a/hapi-fhir-base/src/changes/changes.xml b/hapi-fhir-base/src/changes/changes.xml index 246ce9a2d93..2c1a4f305b5 100644 --- a/hapi-fhir-base/src/changes/changes.xml +++ b/hapi-fhir-base/src/changes/changes.xml @@ -8,7 +8,16 @@ - Allow generic client + Allow generic client ... OAUTH + + + Tester UI created double _format and _pretty param entries in searches. Thanks to Gered King of University + Health Network for reporting! + + + Create method was incorrectly returning an HTTP 204 on sucessful completion, but + should be returning an HTTP 200 per the FHIR specification. Thanks to wanghaisheng + for reporting! diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/client/GenericClient.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/client/GenericClient.java index 284bc4b72d9..e7c8695189a 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/client/GenericClient.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/client/GenericClient.java @@ -821,10 +821,10 @@ public class GenericClient extends BaseClient implements IGenericClient { public Bundle execute() { Map> params = new LinkedHashMap>(); - Map> initial = createExtraParams(); - if (initial != null) { - params.putAll(initial); - } +// Map> initial = createExtraParams(); +// if (initial != null) { +// params.putAll(initial); +// } for (ICriterionInternal next : myCriterion) { String parameterName = next.getParameterName(); diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/BaseOutcomeReturningMethodBinding.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/BaseOutcomeReturningMethodBinding.java index fa2f8cfc65e..61fe90a169f 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/BaseOutcomeReturningMethodBinding.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/BaseOutcomeReturningMethodBinding.java @@ -65,7 +65,8 @@ abstract class BaseOutcomeReturningMethodBinding extends BaseMethodBinding> theHeaders) throws IOException, BaseServerResponseException { + public MethodOutcome invokeClient(String theResponseMimeType, Reader theResponseReader, int theResponseStatusCode, Map> theHeaders) throws IOException, + BaseServerResponseException { switch (theResponseStatusCode) { case Constants.STATUS_HTTP_200_OK: case Constants.STATUS_HTTP_201_CREATED: @@ -148,26 +150,40 @@ abstract class BaseOutcomeReturningMethodBinding extends BaseMethodBinding provideAllowableRequestTypes(); /** - * Subclasses may override if the incoming request should not contain a - * resource + * Subclasses may override if the incoming request should not contain a resource */ protected boolean requestContainsResource() { return true; @@ -406,7 +397,7 @@ abstract class BaseOutcomeReturningMethodBinding extends BaseMethodBinding capt = ArgumentCaptor.forClass(HttpUriRequest.class); + when(myHttpClient.execute(capt.capture())).thenReturn(myHttpResponse); + when(myHttpResponse.getStatusLine()).thenReturn(new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), 200, "OK")); + when(myHttpResponse.getEntity().getContentType()).thenReturn(new BasicHeader("content-type", Constants.CT_FHIR_XML + "; charset=UTF-8")); + when(myHttpResponse.getEntity().getContent()).thenReturn(new ReaderInputStream(new StringReader(msg), Charset.forName("UTF-8"))); + + GenericClient client = (GenericClient) myCtx.newRestfulGenericClient("http://example.com/fhir"); + client.setPrettyPrint(true); + client.setEncoding(EncodingEnum.JSON); + + //@formatter:off + Bundle response = client.search() + .forResource(Patient.class) + .execute(); + //@formatter:on + + assertEquals("http://example.com/fhir/Patient?_format=json&_pretty=true", capt.getValue().getURI().toString()); + + } + + @SuppressWarnings("unused") @Test public void testSearchByDate() throws Exception { diff --git a/hapi-fhir-base/src/test/java/ca/uhn/fhir/rest/server/UpdateTest.java b/hapi-fhir-base/src/test/java/ca/uhn/fhir/rest/server/UpdateTest.java index 29cbe16bb14..64875fd847d 100644 --- a/hapi-fhir-base/src/test/java/ca/uhn/fhir/rest/server/UpdateTest.java +++ b/hapi-fhir-base/src/test/java/ca/uhn/fhir/rest/server/UpdateTest.java @@ -104,9 +104,10 @@ public class UpdateTest { HttpResponse status = ourClient.execute(httpPost); - assertEquals(204, status.getStatusLine().getStatusCode()); + assertEquals(200, status.getStatusLine().getStatusCode()); assertEquals("http://localhost:" + ourPort + "/DiagnosticReport/001/_history/002", status.getFirstHeader("location").getValue()); + IOUtils.closeQuietly(status.getEntity().getContent()); } @Test @@ -118,18 +119,20 @@ public class UpdateTest { HttpPut httpPost = new HttpPut("http://localhost:" + ourPort + "/DiagnosticReport/001"); httpPost.addHeader("Category", "Dog, Cat"); httpPost.setEntity(new StringEntity(new FhirContext().newXmlParser().encodeResourceToString(dr), ContentType.create(Constants.CT_FHIR_XML, "UTF-8"))); - ourClient.execute(httpPost); + CloseableHttpResponse status = ourClient.execute(httpPost); assertEquals(2, ourReportProvider.getLastTags().size()); assertEquals(new Tag("Dog"), ourReportProvider.getLastTags().get(0)); assertEquals(new Tag("Cat"), ourReportProvider.getLastTags().get(1)); + IOUtils.closeQuietly(status.getEntity().getContent()); httpPost = new HttpPut("http://localhost:" + ourPort + "/DiagnosticReport/001"); httpPost.addHeader("Category", "Dog; label=\"aa\", Cat; label=\"bb\""); httpPost.setEntity(new StringEntity(new FhirContext().newXmlParser().encodeResourceToString(dr), ContentType.create(Constants.CT_FHIR_XML, "UTF-8"))); - ourClient.execute(httpPost); + status = ourClient.execute(httpPost); assertEquals(2, ourReportProvider.getLastTags().size()); assertEquals(new Tag((String) null, "Dog", "aa"), ourReportProvider.getLastTags().get(0)); assertEquals(new Tag((String) null, "Cat", "bb"), ourReportProvider.getLastTags().get(1)); + IOUtils.closeQuietly(status.getEntity().getContent()); } @@ -142,9 +145,10 @@ public class UpdateTest { HttpPut httpPost = new HttpPut("http://localhost:" + ourPort + "/DiagnosticReport/001"); httpPost.addHeader("Category", "Dog"); httpPost.setEntity(new StringEntity(new FhirContext().newXmlParser().encodeResourceToString(dr), ContentType.create(Constants.CT_FHIR_XML, "UTF-8"))); - ourClient.execute(httpPost); + CloseableHttpResponse status = ourClient.execute(httpPost); assertEquals(1, ourReportProvider.getLastTags().size()); assertEquals(new Tag("Dog"), ourReportProvider.getLastTags().get(0)); + IOUtils.closeQuietly(status.getEntity().getContent()); } @@ -157,9 +161,10 @@ public class UpdateTest { HttpPut httpPost = new HttpPut("http://localhost:" + ourPort + "/DiagnosticReport/001"); httpPost.addHeader("Category", "Dog; scheme=\"http://foo\""); httpPost.setEntity(new StringEntity(new FhirContext().newXmlParser().encodeResourceToString(dr), ContentType.create(Constants.CT_FHIR_XML, "UTF-8"))); - ourClient.execute(httpPost); + CloseableHttpResponse status = ourClient.execute(httpPost); assertEquals(1, ourReportProvider.getLastTags().size()); assertEquals(new Tag("http://foo", "Dog", null), ourReportProvider.getLastTags().get(0)); + IOUtils.closeQuietly(status.getEntity().getContent()); httpPost = new HttpPut("http://localhost:" + ourPort + "/DiagnosticReport/001"); httpPost.addHeader("Category", "Dog; scheme=\"http://foo\";"); @@ -167,6 +172,7 @@ public class UpdateTest { ourClient.execute(httpPost); assertEquals(1, ourReportProvider.getLastTags().size()); assertEquals(new Tag("http://foo", "Dog", null), ourReportProvider.getLastTags().get(0)); + IOUtils.closeQuietly(status.getEntity().getContent()); } @@ -179,16 +185,18 @@ public class UpdateTest { HttpPut httpPost = new HttpPut("http://localhost:" + ourPort + "/DiagnosticReport/001"); httpPost.addHeader("Category", "Dog; scheme=\"http://foo\"; label=\"aaaa\""); httpPost.setEntity(new StringEntity(new FhirContext().newXmlParser().encodeResourceToString(dr), ContentType.create(Constants.CT_FHIR_XML, "UTF-8"))); - ourClient.execute(httpPost); + CloseableHttpResponse status = ourClient.execute(httpPost); assertEquals(1, ourReportProvider.getLastTags().size()); assertEquals(new Tag("http://foo", "Dog", "aaaa"), ourReportProvider.getLastTags().get(0)); + IOUtils.closeQuietly(status.getEntity().getContent()); httpPost = new HttpPut("http://localhost:" + ourPort + "/DiagnosticReport/001"); httpPost.addHeader("Category", "Dog; scheme=\"http://foo\"; label=\"aaaa\"; "); httpPost.setEntity(new StringEntity(new FhirContext().newXmlParser().encodeResourceToString(dr), ContentType.create(Constants.CT_FHIR_XML, "UTF-8"))); - ourClient.execute(httpPost); + status=ourClient.execute(httpPost); assertEquals(1, ourReportProvider.getLastTags().size()); assertEquals(new Tag("http://foo", "Dog", "aaaa"), ourReportProvider.getLastTags().get(0)); + IOUtils.closeQuietly(status.getEntity().getContent()); } @@ -208,9 +216,9 @@ public class UpdateTest { // IOUtils.toString(status.getEntity().getContent()); // ourLog.info("Response was:\n{}", responseContent); - assertEquals(204, status.getStatusLine().getStatusCode()); - assertNull(status.getEntity()); + assertEquals(200, status.getStatusLine().getStatusCode()); assertEquals("http://localhost:" + ourPort + "/DiagnosticReport/001/_history/002", status.getFirstHeader("Location").getValue()); + IOUtils.closeQuietly(status.getEntity().getContent()); } @@ -224,10 +232,11 @@ public class UpdateTest { httpPut.addHeader("Content-Location", "/Patient/001/_history/002"); httpPut.setEntity(new StringEntity(new FhirContext().newXmlParser().encodeResourceToString(patient), ContentType.create(Constants.CT_FHIR_XML, "UTF-8"))); - CloseableHttpResponse results = ourClient.execute(httpPut); - assertEquals(400, results.getStatusLine().getStatusCode()); - String responseContent = IOUtils.toString(results.getEntity().getContent()); + CloseableHttpResponse status = ourClient.execute(httpPut); + assertEquals(400, status.getStatusLine().getStatusCode()); + String responseContent = IOUtils.toString(status.getEntity().getContent()); ourLog.info("Response was:\n{}", responseContent); + IOUtils.closeQuietly(status.getEntity().getContent()); } diff --git a/hapi-fhir-jpaserver-base/pom.xml b/hapi-fhir-jpaserver-base/pom.xml index fc446926921..faf9ef27142 100644 --- a/hapi-fhir-jpaserver-base/pom.xml +++ b/hapi-fhir-jpaserver-base/pom.xml @@ -253,6 +253,16 @@ validation-api 1.1.0.Final + + javax.el + javax.el-api + 3.0.0 + + + org.glassfish + javax.el + 3.0.0 + diff --git a/pom.xml b/pom.xml index 0c14d49d111..f6609443f9b 100644 --- a/pom.xml +++ b/pom.xml @@ -142,6 +142,7 @@ + diff --git a/restful-server-example/.settings/org.eclipse.wst.common.component b/restful-server-example/.settings/org.eclipse.wst.common.component index 704fec3041b..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