From ef9f97a576df280fe2775038b858969b53eda41b Mon Sep 17 00:00:00 2001 From: patrick-werner Date: Wed, 20 Jun 2018 17:26:23 +0200 Subject: [PATCH 1/4] fixes #1006 --- .../src/main/java/ca/uhn/fhir/rest/api/EncodingEnum.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/api/EncodingEnum.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/api/EncodingEnum.java index 4a6e398198c..463fda14f92 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/api/EncodingEnum.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/api/EncodingEnum.java @@ -24,6 +24,7 @@ import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.parser.IParser; import org.apache.commons.lang3.ObjectUtils; +import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.Map; @@ -170,7 +171,8 @@ public enum EncodingEnum { * @see #forContentType(String) */ public static EncodingEnum forContentTypeStrict(String theContentType) { - return ourContentTypeToEncodingStrict.get(theContentType); + String[] contentTypeSplitted = theContentType.split(";"); + return ourContentTypeToEncodingStrict.get(contentTypeSplitted[0]); } /** From f470653ba4e3f9f3bc00b1edda3404393665695b Mon Sep 17 00:00:00 2001 From: patrick-werner Date: Thu, 21 Jun 2018 11:50:58 +0200 Subject: [PATCH 2/4] fixing tests --- .../java/ca/uhn/fhir/rest/api/EncodingEnum.java | 3 +++ .../ca/uhn/fhir/rest/server/BinaryDstu2Test.java | 13 +++++++------ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/api/EncodingEnum.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/api/EncodingEnum.java index 463fda14f92..96cb881264a 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/api/EncodingEnum.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/api/EncodingEnum.java @@ -171,6 +171,9 @@ public enum EncodingEnum { * @see #forContentType(String) */ public static EncodingEnum forContentTypeStrict(String theContentType) { + if (theContentType == null) { + return null; + } String[] contentTypeSplitted = theContentType.split(";"); return ourContentTypeToEncodingStrict.get(contentTypeSplitted[0]); } diff --git a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/BinaryDstu2Test.java b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/BinaryDstu2Test.java index c80a47ed911..30b66ac07ea 100644 --- a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/BinaryDstu2Test.java +++ b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/BinaryDstu2Test.java @@ -96,9 +96,9 @@ public class BinaryDstu2Test { assertArrayEquals(new byte[] { 1, 2, 3, 4 }, bin.getContent()); } - + // posts Binary directly @Test - public void testCreate() throws Exception { + public void testPostBinary() throws Exception { HttpPost http = new HttpPost("http://localhost:" + ourPort + "/Binary"); http.setEntity(new ByteArrayEntity(new byte[] { 1, 2, 3, 4 }, ContentType.create("foo/bar", "UTF-8"))); @@ -110,8 +110,9 @@ public class BinaryDstu2Test { } + // posts Binary as FHIR Resource @Test - public void testCreateWrongType() throws Exception { + public void testPostFhirBinary() throws Exception { Binary res = new Binary(); res.setContent(new byte[] { 1, 2, 3, 4 }); res.setContentType("text/plain"); @@ -123,7 +124,7 @@ public class BinaryDstu2Test { HttpResponse status = ourClient.execute(http); assertEquals(201, status.getStatusLine().getStatusCode()); - assertEquals("application/json+fhir;charset=utf-8", ourLast.getContentType().replace(" ","").toLowerCase()); + assertEquals("text/plain", ourLast.getContentType().replace(" ","").toLowerCase()); } @@ -213,11 +214,11 @@ public class BinaryDstu2Test { ourPort = PortUtil.findFreePort(); ourServer = new Server(ourPort); - ResourceProvider patientProvider = new ResourceProvider(); + ResourceProvider binaryProvider = new ResourceProvider(); ServletHandler proxyHandler = new ServletHandler(); RestfulServer servlet = new RestfulServer(ourCtx); - servlet.setResourceProviders(patientProvider); + servlet.setResourceProviders(binaryProvider); ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); From 201ba5722b84c457f69c55344ab13d7934c35d27 Mon Sep 17 00:00:00 2001 From: patrick-werner Date: Thu, 21 Jun 2018 14:07:21 +0200 Subject: [PATCH 3/4] fixing tests --- .../uhn/fhir/rest/server/BinaryDstu2Test.java | 323 +++++++++--------- 1 file changed, 166 insertions(+), 157 deletions(-) diff --git a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/BinaryDstu2Test.java b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/BinaryDstu2Test.java index 30b66ac07ea..a68e9cc2896 100644 --- a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/BinaryDstu2Test.java +++ b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/BinaryDstu2Test.java @@ -6,6 +6,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertThat; +import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.Collections; import java.util.List; @@ -13,6 +14,8 @@ import java.util.concurrent.TimeUnit; import org.apache.commons.io.IOUtils; import org.apache.http.HttpResponse; +import org.apache.http.client.config.RequestConfig; +import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.*; @@ -56,159 +59,6 @@ public class BinaryDstu2Test { TestUtil.clearAllStaticFieldsForUnitTest(); } - - @Before - public void before() { - ourLast = null; - } - - @Test - public void testReadWithExplicitTypeXml() throws Exception { - HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Binary/foo?_format=xml"); - HttpResponse status = ourClient.execute(httpGet); - String responseContent = IOUtils.toString(status.getEntity().getContent(), "UTF-8"); - IOUtils.closeQuietly(status.getEntity().getContent()); - - ourLog.info(responseContent); - - assertEquals(200, status.getStatusLine().getStatusCode()); - assertThat(status.getFirstHeader("content-type").getValue(), startsWith(Constants.CT_FHIR_XML + ";")); - - Binary bin = ourCtx.newXmlParser().parseResource(Binary.class, responseContent); - assertEquals("foo", bin.getContentType()); - assertArrayEquals(new byte[] { 1, 2, 3, 4 }, bin.getContent()); - } - - @Test - public void testReadWithExplicitTypeJson() throws Exception { - HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Binary/foo?_format=json"); - HttpResponse status = ourClient.execute(httpGet); - String responseContent = IOUtils.toString(status.getEntity().getContent(), "UTF-8"); - IOUtils.closeQuietly(status.getEntity().getContent()); - - ourLog.info(responseContent); - - assertEquals(200, status.getStatusLine().getStatusCode()); - assertThat(status.getFirstHeader("content-type").getValue(), startsWith(Constants.CT_FHIR_JSON + ";")); - - Binary bin = ourCtx.newJsonParser().parseResource(Binary.class, responseContent); - assertEquals("foo", bin.getContentType()); - assertArrayEquals(new byte[] { 1, 2, 3, 4 }, bin.getContent()); - } - - // posts Binary directly - @Test - public void testPostBinary() throws Exception { - HttpPost http = new HttpPost("http://localhost:" + ourPort + "/Binary"); - http.setEntity(new ByteArrayEntity(new byte[] { 1, 2, 3, 4 }, ContentType.create("foo/bar", "UTF-8"))); - - HttpResponse status = ourClient.execute(http); - assertEquals(201, status.getStatusLine().getStatusCode()); - - assertEquals("foo/bar; charset=UTF-8", ourLast.getContentType()); - assertArrayEquals(new byte[] { 1, 2, 3, 4 }, ourLast.getContent()); - - } - - // posts Binary as FHIR Resource - @Test - public void testPostFhirBinary() throws Exception { - Binary res = new Binary(); - res.setContent(new byte[] { 1, 2, 3, 4 }); - res.setContentType("text/plain"); - String stringContent = ourCtx.newJsonParser().encodeResourceToString(res); - - HttpPost http = new HttpPost("http://localhost:" + ourPort + "/Binary"); - http.setEntity(new StringEntity(stringContent, ContentType.create(Constants.CT_FHIR_JSON, "UTF-8"))); - - HttpResponse status = ourClient.execute(http); - assertEquals(201, status.getStatusLine().getStatusCode()); - - assertEquals("text/plain", ourLast.getContentType().replace(" ","").toLowerCase()); - - } - - @Test - public void testBinaryReadAcceptMissing() throws Exception { - HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Binary/foo"); - - HttpResponse status = ourClient.execute(httpGet); - byte[] responseContent = IOUtils.toByteArray(status.getEntity().getContent()); - IOUtils.closeQuietly(status.getEntity().getContent()); - assertEquals(200, status.getStatusLine().getStatusCode()); - assertEquals("foo", status.getFirstHeader("content-type").getValue()); - assertEquals("Attachment;", status.getFirstHeader("Content-Disposition").getValue()); - assertArrayEquals(new byte[] { 1, 2, 3, 4 }, responseContent); - - } - - @Test - public void testBinaryReadAcceptBrowser() throws Exception { - HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Binary/foo"); - httpGet.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1"); - httpGet.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); - - HttpResponse status = ourClient.execute(httpGet); - byte[] responseContent = IOUtils.toByteArray(status.getEntity().getContent()); - IOUtils.closeQuietly(status.getEntity().getContent()); - assertEquals(200, status.getStatusLine().getStatusCode()); - assertEquals("foo", status.getFirstHeader("content-type").getValue()); - assertEquals("Attachment;", status.getFirstHeader("Content-Disposition").getValue()); // This is a security requirement! - assertArrayEquals(new byte[] { 1, 2, 3, 4 }, responseContent); - } - - @Test - public void testBinaryReadAcceptFhirJson() throws Exception { - HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Binary/foo"); - httpGet.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1"); - httpGet.addHeader("Accept", Constants.CT_FHIR_JSON); - - HttpResponse status = ourClient.execute(httpGet); - String responseContent = IOUtils.toString(status.getEntity().getContent(), StandardCharsets.UTF_8); - IOUtils.closeQuietly(status.getEntity().getContent()); - assertEquals(200, status.getStatusLine().getStatusCode()); - assertEquals(Constants.CT_FHIR_JSON + ";charset=utf-8", status.getFirstHeader("content-type").getValue().replace(" ", "").toLowerCase()); - assertNull(status.getFirstHeader("Content-Disposition")); - assertEquals("{\"resourceType\":\"Binary\",\"id\":\"1\",\"contentType\":\"foo\",\"content\":\"AQIDBA==\"}", responseContent); - - } - - @Test - public void testSearchJson() throws Exception { - HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Binary?_pretty=true&_format=json"); - HttpResponse status = ourClient.execute(httpGet); - String responseContent = IOUtils.toString(status.getEntity().getContent(), StandardCharsets.UTF_8); - IOUtils.closeQuietly(status.getEntity().getContent()); - assertEquals(200, status.getStatusLine().getStatusCode()); - assertEquals(Constants.CT_FHIR_JSON + ";charset=utf-8", status.getFirstHeader("content-type").getValue().replace(" ", "").replace("UTF", "utf")); - - ourLog.info(responseContent); - - Bundle bundle = ourCtx.newJsonParser().parseResource(Bundle.class, responseContent); - Binary bin = (Binary) bundle.getEntry().get(0).getResource(); - - assertEquals("text/plain", bin.getContentType()); - assertArrayEquals(new byte[] { 1, 2, 3, 4 }, bin.getContent()); - } - - @Test - public void testSearchXml() throws Exception { - HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Binary?_pretty=true"); - HttpResponse status = ourClient.execute(httpGet); - String responseContent = IOUtils.toString(status.getEntity().getContent(), StandardCharsets.UTF_8); - IOUtils.closeQuietly(status.getEntity().getContent()); - assertEquals(200, status.getStatusLine().getStatusCode()); - assertEquals(Constants.CT_FHIR_XML + ";charset=utf-8", status.getFirstHeader("content-type").getValue().replace(" ", "").replace("UTF", "utf")); - - ourLog.info(responseContent); - - Bundle bundle = ourCtx.newXmlParser().parseResource(Bundle.class, responseContent); - Binary bin = (Binary) bundle.getEntry().get(0).getResource(); - - assertEquals("text/plain", bin.getContentType()); - assertArrayEquals(new byte[] { 1, 2, 3, 4 }, bin.getContent()); - } - @BeforeClass public static void beforeClass() throws Exception { ourPort = PortUtil.findFreePort(); @@ -226,9 +76,170 @@ public class BinaryDstu2Test { PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); - builder.setConnectionManager(connectionManager); - ourClient = builder.build(); + int timeout = 5; + RequestConfig config = RequestConfig.custom() + .setConnectTimeout(timeout * 1000) + .setConnectionRequestTimeout(timeout * 1000) + .setSocketTimeout(timeout * 1000).build(); + + builder.setConnectionManager(connectionManager); + ourClient = builder.setDefaultRequestConfig(config).build(); + + } + + @Before + public void before() { + ourLast = null; + } + + @Test + public void testReadWithExplicitTypeXml() throws Exception { + HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Binary/foo?_format=xml"); + try (CloseableHttpResponse response = ourClient.execute(httpGet)) { + String responseContent = IOUtils.toString(response.getEntity().getContent(), "UTF-8"); + IOUtils.closeQuietly(response.getEntity().getContent()); + + ourLog.info(responseContent); + + assertEquals(200, response.getStatusLine().getStatusCode()); + assertThat(response.getFirstHeader("content-type").getValue(), startsWith(Constants.CT_FHIR_XML + ";")); + + Binary bin = ourCtx.newXmlParser().parseResource(Binary.class, responseContent); + assertEquals("foo", bin.getContentType()); + assertArrayEquals(new byte[] { 1, 2, 3, 4 }, bin.getContent()); + } + } + + @Test + public void testReadWithExplicitTypeJson() throws Exception { + HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Binary/foo?_format=json"); + try (CloseableHttpResponse response = ourClient.execute(httpGet)){ + String responseContent = IOUtils.toString(response.getEntity().getContent(), "UTF-8"); + IOUtils.closeQuietly(response.getEntity().getContent()); + + ourLog.info(responseContent); + + assertEquals(200, response.getStatusLine().getStatusCode()); + assertThat(response.getFirstHeader("content-type").getValue(), startsWith(Constants.CT_FHIR_JSON + ";")); + + Binary bin = ourCtx.newJsonParser().parseResource(Binary.class, responseContent); + assertEquals("foo", bin.getContentType()); + assertArrayEquals(new byte[] { 1, 2, 3, 4 }, bin.getContent()); + } + } + + // posts Binary directly + @Test + public void testPostBinary() throws Exception { + HttpPost http = new HttpPost("http://localhost:" + ourPort + "/Binary"); + http.setEntity(new ByteArrayEntity(new byte[] { 1, 2, 3, 4 }, ContentType.create("foo/bar", "UTF-8"))); + + try (CloseableHttpResponse response = ourClient.execute(http)){ + assertEquals(201, response.getStatusLine().getStatusCode()); + + assertEquals("foo/bar; charset=UTF-8", ourLast.getContentType()); + assertArrayEquals(new byte[] { 1, 2, 3, 4 }, ourLast.getContent()); + } + } + + // posts Binary as FHIR Resource + @Test + public void testPostFhirBinary() throws Exception { + Binary res = new Binary(); + res.setContent(new byte[] { 1, 2, 3, 4 }); + res.setContentType("text/plain"); + String stringContent = ourCtx.newJsonParser().encodeResourceToString(res); + + HttpPost http = new HttpPost("http://localhost:" + ourPort + "/Binary"); + http.setEntity(new StringEntity(stringContent, ContentType.create(Constants.CT_FHIR_JSON, "UTF-8"))); + + try (CloseableHttpResponse response = ourClient.execute(http)) { + assertEquals(201, response.getStatusLine().getStatusCode()); + + assertEquals("text/plain", ourLast.getContentType().replace(" ", "").toLowerCase()); + } + } + + @Test + public void testBinaryReadAcceptMissing() throws Exception { + HttpGet http = new HttpGet("http://localhost:" + ourPort + "/Binary/foo"); + + binaryRead(http); + } + + @Test + public void testBinaryReadAcceptBrowser() throws Exception { + HttpGet http = new HttpGet("http://localhost:" + ourPort + "/Binary/foo"); + http.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1"); + http.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); + + binaryRead(http); + } + + private void binaryRead(HttpGet http) throws IOException { + try (CloseableHttpResponse status = ourClient.execute(http)) { + byte[] responseContent = IOUtils.toByteArray(status.getEntity().getContent()); + IOUtils.closeQuietly(status.getEntity().getContent()); + assertEquals(200, status.getStatusLine().getStatusCode()); + assertEquals("foo", status.getFirstHeader("content-type").getValue()); + assertEquals("Attachment;", status.getFirstHeader("Content-Disposition").getValue()); // This is a security requirement! + assertArrayEquals(new byte[]{1, 2, 3, 4}, responseContent); + } + } + + @Test + public void testBinaryReadAcceptFhirJson() throws Exception { + HttpGet http = new HttpGet("http://localhost:" + ourPort + "/Binary/foo"); + http.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1"); + http.addHeader("Accept", Constants.CT_FHIR_JSON); + + try (CloseableHttpResponse status = ourClient.execute(http)) { + String responseContent = IOUtils.toString(status.getEntity().getContent(), StandardCharsets.UTF_8); + IOUtils.closeQuietly(status.getEntity().getContent()); + assertEquals(200, status.getStatusLine().getStatusCode()); + assertEquals(Constants.CT_FHIR_JSON + ";charset=utf-8", status.getFirstHeader("content-type").getValue().replace(" ", "").toLowerCase()); + assertNull(status.getFirstHeader("Content-Disposition")); + assertEquals("{\"resourceType\":\"Binary\",\"id\":\"1\",\"contentType\":\"foo\",\"content\":\"AQIDBA==\"}", responseContent); + } + } + + @Test + public void testSearchJson() throws Exception { + HttpGet http = new HttpGet("http://localhost:" + ourPort + "/Binary?_pretty=true&_format=json"); + try (CloseableHttpResponse response = ourClient.execute(http)) { + String responseContent = IOUtils.toString(response.getEntity().getContent(), StandardCharsets.UTF_8); + IOUtils.closeQuietly(response.getEntity().getContent()); + assertEquals(200, response.getStatusLine().getStatusCode()); + assertEquals(Constants.CT_FHIR_JSON + ";charset=utf-8", response.getFirstHeader("content-type").getValue().replace(" ", "").replace("UTF", "utf")); + + ourLog.info(responseContent); + + Bundle bundle = ourCtx.newJsonParser().parseResource(Bundle.class, responseContent); + Binary bin = (Binary) bundle.getEntry().get(0).getResource(); + + assertEquals("text/plain", bin.getContentType()); + assertArrayEquals(new byte[]{1, 2, 3, 4}, bin.getContent()); + } + } + + @Test + public void testSearchXml() throws Exception { + HttpGet http = new HttpGet("http://localhost:" + ourPort + "/Binary?_pretty=true"); + try (CloseableHttpResponse response = ourClient.execute(http)) { + String responseContent = IOUtils.toString(response.getEntity().getContent(), StandardCharsets.UTF_8); + IOUtils.closeQuietly(response.getEntity().getContent()); + assertEquals(200, response.getStatusLine().getStatusCode()); + assertEquals(Constants.CT_FHIR_XML + ";charset=utf-8", response.getFirstHeader("content-type").getValue().replace(" ", "").replace("UTF", "utf")); + + ourLog.info(responseContent); + + Bundle bundle = ourCtx.newXmlParser().parseResource(Bundle.class, responseContent); + Binary bin = (Binary) bundle.getEntry().get(0).getResource(); + + assertEquals("text/plain", bin.getContentType()); + assertArrayEquals(new byte[]{1, 2, 3, 4}, bin.getContent()); + } } public static class ResourceProvider implements IResourceProvider { @@ -261,7 +272,5 @@ public class BinaryDstu2Test { retVal.setContentType("text/plain"); return Collections.singletonList(retVal); } - } - } From 8bd13134925d2b326fd123a9ad4c7c95ab5ec397 Mon Sep 17 00:00:00 2001 From: Patrick Werner Date: Thu, 21 Jun 2018 14:56:38 +0200 Subject: [PATCH 4/4] removed unused import --- .../src/main/java/ca/uhn/fhir/rest/api/EncodingEnum.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/api/EncodingEnum.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/api/EncodingEnum.java index 96cb881264a..f3593952492 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/api/EncodingEnum.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/api/EncodingEnum.java @@ -23,8 +23,6 @@ package ca.uhn.fhir.rest.api; import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.parser.IParser; import org.apache.commons.lang3.ObjectUtils; - -import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.Map;