Merge pull request #1007 from jamesagnew/1006-Writing-structured-Binary

1006 writing structured binary
This commit is contained in:
Patrick Werner 2018-06-21 18:03:14 +02:00 committed by GitHub
commit b1e09fcdf7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 127 additions and 114 deletions

View File

@ -23,7 +23,6 @@ package ca.uhn.fhir.rest.api;
import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.parser.IParser; import ca.uhn.fhir.parser.IParser;
import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.ObjectUtils;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -170,7 +169,11 @@ public enum EncodingEnum {
* @see #forContentType(String) * @see #forContentType(String)
*/ */
public static EncodingEnum forContentTypeStrict(String theContentType) { public static EncodingEnum forContentTypeStrict(String theContentType) {
return ourContentTypeToEncodingStrict.get(theContentType); if (theContentType == null) {
return null;
}
String[] contentTypeSplitted = theContentType.split(";");
return ourContentTypeToEncodingStrict.get(contentTypeSplitted[0]);
} }
/** /**

View File

@ -6,6 +6,7 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
import java.io.IOException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
@ -13,6 +14,8 @@ import java.util.concurrent.TimeUnit;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse; 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.HttpGet;
import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.*; import org.apache.http.entity.*;
@ -56,6 +59,34 @@ public class BinaryDstu2Test {
TestUtil.clearAllStaticFieldsForUnitTest(); TestUtil.clearAllStaticFieldsForUnitTest();
} }
@BeforeClass
public static void beforeClass() throws Exception {
ourPort = PortUtil.findFreePort();
ourServer = new Server(ourPort);
ResourceProvider binaryProvider = new ResourceProvider();
ServletHandler proxyHandler = new ServletHandler();
RestfulServer servlet = new RestfulServer(ourCtx);
servlet.setResourceProviders(binaryProvider);
ServletHolder servletHolder = new ServletHolder(servlet);
proxyHandler.addServletWithMapping(servletHolder, "/*");
ourServer.setHandler(proxyHandler);
ourServer.start();
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS);
HttpClientBuilder builder = HttpClientBuilder.create();
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 @Before
public void before() { public void before() {
@ -65,53 +96,56 @@ public class BinaryDstu2Test {
@Test @Test
public void testReadWithExplicitTypeXml() throws Exception { public void testReadWithExplicitTypeXml() throws Exception {
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Binary/foo?_format=xml"); HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Binary/foo?_format=xml");
HttpResponse status = ourClient.execute(httpGet); try (CloseableHttpResponse response = ourClient.execute(httpGet)) {
String responseContent = IOUtils.toString(status.getEntity().getContent(), "UTF-8"); String responseContent = IOUtils.toString(response.getEntity().getContent(), "UTF-8");
IOUtils.closeQuietly(status.getEntity().getContent()); IOUtils.closeQuietly(response.getEntity().getContent());
ourLog.info(responseContent); ourLog.info(responseContent);
assertEquals(200, status.getStatusLine().getStatusCode()); assertEquals(200, response.getStatusLine().getStatusCode());
assertThat(status.getFirstHeader("content-type").getValue(), startsWith(Constants.CT_FHIR_XML + ";")); assertThat(response.getFirstHeader("content-type").getValue(), startsWith(Constants.CT_FHIR_XML + ";"));
Binary bin = ourCtx.newXmlParser().parseResource(Binary.class, responseContent); Binary bin = ourCtx.newXmlParser().parseResource(Binary.class, responseContent);
assertEquals("foo", bin.getContentType()); assertEquals("foo", bin.getContentType());
assertArrayEquals(new byte[] { 1, 2, 3, 4 }, bin.getContent()); assertArrayEquals(new byte[] { 1, 2, 3, 4 }, bin.getContent());
} }
}
@Test @Test
public void testReadWithExplicitTypeJson() throws Exception { public void testReadWithExplicitTypeJson() throws Exception {
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Binary/foo?_format=json"); HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Binary/foo?_format=json");
HttpResponse status = ourClient.execute(httpGet); try (CloseableHttpResponse response = ourClient.execute(httpGet)){
String responseContent = IOUtils.toString(status.getEntity().getContent(), "UTF-8"); String responseContent = IOUtils.toString(response.getEntity().getContent(), "UTF-8");
IOUtils.closeQuietly(status.getEntity().getContent()); IOUtils.closeQuietly(response.getEntity().getContent());
ourLog.info(responseContent); ourLog.info(responseContent);
assertEquals(200, status.getStatusLine().getStatusCode()); assertEquals(200, response.getStatusLine().getStatusCode());
assertThat(status.getFirstHeader("content-type").getValue(), startsWith(Constants.CT_FHIR_JSON + ";")); assertThat(response.getFirstHeader("content-type").getValue(), startsWith(Constants.CT_FHIR_JSON + ";"));
Binary bin = ourCtx.newJsonParser().parseResource(Binary.class, responseContent); Binary bin = ourCtx.newJsonParser().parseResource(Binary.class, responseContent);
assertEquals("foo", bin.getContentType()); assertEquals("foo", bin.getContentType());
assertArrayEquals(new byte[] { 1, 2, 3, 4 }, bin.getContent()); assertArrayEquals(new byte[] { 1, 2, 3, 4 }, bin.getContent());
} }
}
// posts Binary directly
@Test @Test
public void testCreate() throws Exception { public void testPostBinary() throws Exception {
HttpPost http = new HttpPost("http://localhost:" + ourPort + "/Binary"); HttpPost http = new HttpPost("http://localhost:" + ourPort + "/Binary");
http.setEntity(new ByteArrayEntity(new byte[] { 1, 2, 3, 4 }, ContentType.create("foo/bar", "UTF-8"))); http.setEntity(new ByteArrayEntity(new byte[] { 1, 2, 3, 4 }, ContentType.create("foo/bar", "UTF-8")));
HttpResponse status = ourClient.execute(http); try (CloseableHttpResponse response = ourClient.execute(http)){
assertEquals(201, status.getStatusLine().getStatusCode()); assertEquals(201, response.getStatusLine().getStatusCode());
assertEquals("foo/bar; charset=UTF-8", ourLast.getContentType()); assertEquals("foo/bar; charset=UTF-8", ourLast.getContentType());
assertArrayEquals(new byte[] { 1, 2, 3, 4 }, ourLast.getContent()); assertArrayEquals(new byte[] { 1, 2, 3, 4 }, ourLast.getContent());
}
} }
// posts Binary as FHIR Resource
@Test @Test
public void testCreateWrongType() throws Exception { public void testPostFhirBinary() throws Exception {
Binary res = new Binary(); Binary res = new Binary();
res.setContent(new byte[] { 1, 2, 3, 4 }); res.setContent(new byte[] { 1, 2, 3, 4 });
res.setContentType("text/plain"); res.setContentType("text/plain");
@ -120,34 +154,31 @@ public class BinaryDstu2Test {
HttpPost http = new HttpPost("http://localhost:" + ourPort + "/Binary"); HttpPost http = new HttpPost("http://localhost:" + ourPort + "/Binary");
http.setEntity(new StringEntity(stringContent, ContentType.create(Constants.CT_FHIR_JSON, "UTF-8"))); http.setEntity(new StringEntity(stringContent, ContentType.create(Constants.CT_FHIR_JSON, "UTF-8")));
HttpResponse status = ourClient.execute(http); try (CloseableHttpResponse response = ourClient.execute(http)) {
assertEquals(201, status.getStatusLine().getStatusCode()); assertEquals(201, response.getStatusLine().getStatusCode());
assertEquals("application/json+fhir;charset=utf-8", ourLast.getContentType().replace(" ","").toLowerCase());
assertEquals("text/plain", ourLast.getContentType().replace(" ", "").toLowerCase());
}
} }
@Test @Test
public void testBinaryReadAcceptMissing() throws Exception { public void testBinaryReadAcceptMissing() throws Exception {
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Binary/foo"); HttpGet http = 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);
binaryRead(http);
} }
@Test @Test
public void testBinaryReadAcceptBrowser() throws Exception { public void testBinaryReadAcceptBrowser() throws Exception {
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Binary/foo"); HttpGet http = 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"); http.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"); http.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
HttpResponse status = ourClient.execute(httpGet); binaryRead(http);
}
private void binaryRead(HttpGet http) throws IOException {
try (CloseableHttpResponse status = ourClient.execute(http)) {
byte[] responseContent = IOUtils.toByteArray(status.getEntity().getContent()); byte[] responseContent = IOUtils.toByteArray(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent()); IOUtils.closeQuietly(status.getEntity().getContent());
assertEquals(200, status.getStatusLine().getStatusCode()); assertEquals(200, status.getStatusLine().getStatusCode());
@ -155,31 +186,32 @@ public class BinaryDstu2Test {
assertEquals("Attachment;", status.getFirstHeader("Content-Disposition").getValue()); // This is a security requirement! assertEquals("Attachment;", status.getFirstHeader("Content-Disposition").getValue()); // This is a security requirement!
assertArrayEquals(new byte[]{1, 2, 3, 4}, responseContent); assertArrayEquals(new byte[]{1, 2, 3, 4}, responseContent);
} }
}
@Test @Test
public void testBinaryReadAcceptFhirJson() throws Exception { public void testBinaryReadAcceptFhirJson() throws Exception {
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Binary/foo"); HttpGet http = 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"); http.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); http.addHeader("Accept", Constants.CT_FHIR_JSON);
HttpResponse status = ourClient.execute(httpGet); try (CloseableHttpResponse status = ourClient.execute(http)) {
String responseContent = IOUtils.toString(status.getEntity().getContent(), StandardCharsets.UTF_8); String responseContent = IOUtils.toString(status.getEntity().getContent(), StandardCharsets.UTF_8);
IOUtils.closeQuietly(status.getEntity().getContent()); IOUtils.closeQuietly(status.getEntity().getContent());
assertEquals(200, status.getStatusLine().getStatusCode()); assertEquals(200, status.getStatusLine().getStatusCode());
assertEquals(Constants.CT_FHIR_JSON + ";charset=utf-8", status.getFirstHeader("content-type").getValue().replace(" ", "").toLowerCase()); assertEquals(Constants.CT_FHIR_JSON + ";charset=utf-8", status.getFirstHeader("content-type").getValue().replace(" ", "").toLowerCase());
assertNull(status.getFirstHeader("Content-Disposition")); assertNull(status.getFirstHeader("Content-Disposition"));
assertEquals("{\"resourceType\":\"Binary\",\"id\":\"1\",\"contentType\":\"foo\",\"content\":\"AQIDBA==\"}", responseContent); assertEquals("{\"resourceType\":\"Binary\",\"id\":\"1\",\"contentType\":\"foo\",\"content\":\"AQIDBA==\"}", responseContent);
}
} }
@Test @Test
public void testSearchJson() throws Exception { public void testSearchJson() throws Exception {
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Binary?_pretty=true&_format=json"); HttpGet http = new HttpGet("http://localhost:" + ourPort + "/Binary?_pretty=true&_format=json");
HttpResponse status = ourClient.execute(httpGet); try (CloseableHttpResponse response = ourClient.execute(http)) {
String responseContent = IOUtils.toString(status.getEntity().getContent(), StandardCharsets.UTF_8); String responseContent = IOUtils.toString(response.getEntity().getContent(), StandardCharsets.UTF_8);
IOUtils.closeQuietly(status.getEntity().getContent()); IOUtils.closeQuietly(response.getEntity().getContent());
assertEquals(200, status.getStatusLine().getStatusCode()); assertEquals(200, response.getStatusLine().getStatusCode());
assertEquals(Constants.CT_FHIR_JSON + ";charset=utf-8", status.getFirstHeader("content-type").getValue().replace(" ", "").replace("UTF", "utf")); assertEquals(Constants.CT_FHIR_JSON + ";charset=utf-8", response.getFirstHeader("content-type").getValue().replace(" ", "").replace("UTF", "utf"));
ourLog.info(responseContent); ourLog.info(responseContent);
@ -189,15 +221,16 @@ public class BinaryDstu2Test {
assertEquals("text/plain", bin.getContentType()); assertEquals("text/plain", bin.getContentType());
assertArrayEquals(new byte[]{1, 2, 3, 4}, bin.getContent()); assertArrayEquals(new byte[]{1, 2, 3, 4}, bin.getContent());
} }
}
@Test @Test
public void testSearchXml() throws Exception { public void testSearchXml() throws Exception {
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Binary?_pretty=true"); HttpGet http = new HttpGet("http://localhost:" + ourPort + "/Binary?_pretty=true");
HttpResponse status = ourClient.execute(httpGet); try (CloseableHttpResponse response = ourClient.execute(http)) {
String responseContent = IOUtils.toString(status.getEntity().getContent(), StandardCharsets.UTF_8); String responseContent = IOUtils.toString(response.getEntity().getContent(), StandardCharsets.UTF_8);
IOUtils.closeQuietly(status.getEntity().getContent()); IOUtils.closeQuietly(response.getEntity().getContent());
assertEquals(200, status.getStatusLine().getStatusCode()); assertEquals(200, response.getStatusLine().getStatusCode());
assertEquals(Constants.CT_FHIR_XML + ";charset=utf-8", status.getFirstHeader("content-type").getValue().replace(" ", "").replace("UTF", "utf")); assertEquals(Constants.CT_FHIR_XML + ";charset=utf-8", response.getFirstHeader("content-type").getValue().replace(" ", "").replace("UTF", "utf"));
ourLog.info(responseContent); ourLog.info(responseContent);
@ -207,27 +240,6 @@ public class BinaryDstu2Test {
assertEquals("text/plain", bin.getContentType()); assertEquals("text/plain", bin.getContentType());
assertArrayEquals(new byte[]{1, 2, 3, 4}, bin.getContent()); assertArrayEquals(new byte[]{1, 2, 3, 4}, bin.getContent());
} }
@BeforeClass
public static void beforeClass() throws Exception {
ourPort = PortUtil.findFreePort();
ourServer = new Server(ourPort);
ResourceProvider patientProvider = new ResourceProvider();
ServletHandler proxyHandler = new ServletHandler();
RestfulServer servlet = new RestfulServer(ourCtx);
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();
} }
public static class ResourceProvider implements IResourceProvider { public static class ResourceProvider implements IResourceProvider {
@ -260,7 +272,5 @@ public class BinaryDstu2Test {
retVal.setContentType("text/plain"); retVal.setContentType("text/plain");
return Collections.singletonList(retVal); return Collections.singletonList(retVal);
} }
} }
} }