Better detection of binary content in ResponseHighlighterInterceptor

This commit is contained in:
James Agnew 2018-11-18 14:32:54 +01:00
parent fad53c6669
commit 67dbc802be
8 changed files with 308 additions and 284 deletions

View File

@ -177,6 +177,7 @@ public class ResourceTable extends BaseHasResource implements Serializable {
@OptimisticLock(excluded = true)
private Collection<ResourceIndexedCompositeStringUnique> myParamsCompositeStringUnique;
@IndexedEmbedded
@OneToMany(mappedBy = "mySourceResource", cascade = {}, fetch = FetchType.LAZY, orphanRemoval = false)
@OptimisticLock(excluded = true)
private Collection<ResourceLink> myResourceLinks;

View File

@ -129,7 +129,7 @@ public class FhirResourceDaoR4QueryCountTest extends BaseJpaR4Test {
p.getPhotoFirstRep().setCreationElement(new DateTimeType("2012")); // non-indexed field
myPatientDao.update(p).getId().toUnqualifiedVersionless();
assertEquals(4, getQueryCount().getSelect());
assertEquals(5, getQueryCount().getSelect());
assertEquals(1, getQueryCount().getInsert());
assertEquals(0, getQueryCount().getDelete());
assertEquals(1, getQueryCount().getUpdate());

View File

@ -17,7 +17,8 @@ import ca.uhn.fhir.util.StopWatch;
import ca.uhn.fhir.util.UrlUtil;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.text.StringEscapeUtils;
import org.hl7.fhir.instance.model.api.IBaseBinary;
import org.hl7.fhir.instance.model.api.IBaseResource;
import javax.servlet.ServletException;
@ -371,7 +372,7 @@ public class ResponseHighlighterInterceptor extends InterceptorAdapter {
/*
* Not binary
*/
if (!force && "Binary".equals(theRequestDetails.getResourceName())) {
if (!force && (theResponseObject.getResponseResource() instanceof IBaseBinary)) {
return super.outgoingResponse(theRequestDetails, theResponseObject, theServletRequest, theServletResponse);
}

View File

@ -1,5 +1,25 @@
package ca.uhn.fhir.rest.server.interceptor.auth;
/*-
* #%L
* HAPI FHIR - Server Framework
* %%
* Copyright (C) 2014 - 2018 University Health Network
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
public interface IAuthRuleBuilderGraphQL {
/**

View File

@ -35,9 +35,9 @@ import static org.apache.commons.lang3.StringUtils.isNotBlank;
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

View File

@ -11,6 +11,7 @@ import ca.uhn.fhir.rest.client.api.IGenericClient;
import ca.uhn.fhir.rest.client.interceptor.LoggingInterceptor;
import ca.uhn.fhir.util.PortUtil;
import ca.uhn.fhir.util.TestUtil;
import com.google.common.base.Charsets;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.CloseableHttpResponse;
@ -41,9 +42,10 @@ import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
public class OperationServerR4Test {
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(OperationServerR4Test.class);
private static final String TEXT_HTML = "text/html";
private static CloseableHttpClient ourClient;
private static FhirContext ourCtx;
private static IdType ourLastId;
private static String ourLastMethod;
private static StringType ourLastParam1;
@ -51,7 +53,6 @@ public class OperationServerR4Test {
private static List<StringType> ourLastParam3;
private static MoneyQuantity ourLastParamMoney1;
private static UnsignedIntType ourLastParamUnsignedInt1;
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(OperationServerR4Test.class);
private static int ourPort;
private static Server ourServer;
private IGenericClient myFhirClient;
@ -84,21 +85,21 @@ public class OperationServerR4Test {
List<String> opNames = toOpNames(ops);
assertThat(opNames, containsInRelativeOrder("OP_TYPE"));
// OperationDefinition def = (OperationDefinition) ops.get(opNames.indexOf("OP_TYPE")).getDefinition().getResource();
OperationDefinition def = myFhirClient.read().resource(OperationDefinition.class).withId(ops.get(opNames.indexOf("OP_TYPE")).getDefinition()).execute();
assertEquals("OP_TYPE", def.getCode());
}
/**
* See #380
*/
@Test
public void testOperationDefinition() {
OperationDefinition def = myFhirClient.read().resource(OperationDefinition.class).withId("OperationDefinition/Patient--OP_TYPE").execute();
ourLog.info(ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(def));
// @OperationParam(name="PARAM1") StringType theParam1,
// @OperationParam(name="PARAM2") Patient theParam2,
// @OperationParam(name="PARAM3", min=2, max=5) List<StringType> theParam3,
@ -109,22 +110,22 @@ public class OperationServerR4Test {
assertEquals(OperationParameterUse.IN, def.getParameter().get(0).getUse());
assertEquals(0, def.getParameter().get(0).getMin());
assertEquals("1", def.getParameter().get(0).getMax());
assertEquals("PARAM2", def.getParameter().get(1).getName());
assertEquals(OperationParameterUse.IN, def.getParameter().get(1).getUse());
assertEquals(0, def.getParameter().get(1).getMin());
assertEquals("1", def.getParameter().get(1).getMax());
assertEquals("PARAM3", def.getParameter().get(2).getName());
assertEquals(OperationParameterUse.IN, def.getParameter().get(2).getUse());
assertEquals(2, def.getParameter().get(2).getMin());
assertEquals("5", def.getParameter().get(2).getMax());
assertEquals("PARAM4", def.getParameter().get(3).getName());
assertEquals(OperationParameterUse.IN, def.getParameter().get(3).getUse());
assertEquals(1, def.getParameter().get(3).getMin());
assertEquals("*", def.getParameter().get(3).getMax());
}
private List<String> toOpNames(List<CapabilityStatement.CapabilityStatementRestResourceOperationComponent> theOps) {
@ -137,7 +138,7 @@ public class OperationServerR4Test {
@Test
public void testInstanceEverythingGet() throws Exception {
// Try with a GET
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient/123/$everything");
CloseableHttpResponse status = ourClient.execute(httpGet);
@ -149,9 +150,9 @@ public class OperationServerR4Test {
assertEquals("instance $everything", ourLastMethod);
assertThat(response, startsWith("<Bundle"));
assertEquals("Patient/123", ourLastId.toUnqualifiedVersionless().getValue());
}
@Test
public void testInstanceEverythingHapiClient() {
ourCtx.newRestfulGenericClient("http://localhost:" + ourPort).operation().onInstance(new IdType("Patient/123")).named("$everything").withParameters(new Parameters()).execute();
@ -159,7 +160,7 @@ public class OperationServerR4Test {
assertEquals("instance $everything", ourLastMethod);
assertEquals("Patient/123", ourLastId.toUnqualifiedVersionless().getValue());
}
@Test
@ -181,7 +182,7 @@ public class OperationServerR4Test {
@Test
public void testInstanceEverythingPost() throws Exception {
String inParamsStr = ourCtx.newXmlParser().encodeResourceToString(new Parameters());
// Try with a POST
HttpPost httpPost = new HttpPost("http://localhost:" + ourPort + "/Patient/123/$everything");
httpPost.setEntity(new StringEntity(inParamsStr, ContentType.create(Constants.CT_FHIR_XML, "UTF-8")));
@ -227,7 +228,7 @@ public class OperationServerR4Test {
IOUtils.closeQuietly(status);
}
}
@Test
public void testOperationOnInstance() throws Exception {
Parameters p = new Parameters();
@ -250,11 +251,11 @@ public class OperationServerR4Test {
Parameters resp = ourCtx.newXmlParser().parseResource(Parameters.class, response);
assertEquals("RET1", resp.getParameter().get(0).getName());
/*
* Against type should fail
*/
httpPost = new HttpPost("http://localhost:" + ourPort + "/Patient/$OP_INSTANCE");
httpPost.setEntity(new StringEntity(inParamsStr, ContentType.create(Constants.CT_FHIR_XML, "UTF-8")));
status = ourClient.execute(httpPost);
@ -288,7 +289,7 @@ public class OperationServerR4Test {
Parameters resp = ourCtx.newXmlParser().parseResource(Parameters.class, response);
assertEquals("RET1", resp.getParameter().get(0).getName());
}
@Test
@ -297,7 +298,7 @@ public class OperationServerR4Test {
p.addParameter().setName("PARAM1").setValue(new StringType("PARAM1val"));
p.addParameter().setName("PARAM2").setResource(new Patient().setActive(true));
String inParamsStr = ourCtx.newXmlParser().encodeResourceToString(p);
HttpPost httpPost = new HttpPost("http://localhost:" + ourPort + "/Patient/$OP_INSTANCE_OR_TYPE");
httpPost.setEntity(new StringEntity(inParamsStr, ContentType.create(Constants.CT_FHIR_XML, "UTF-8")));
CloseableHttpResponse status = ourClient.execute(httpPost);
@ -416,7 +417,7 @@ public class OperationServerR4Test {
String response = IOUtils.toString(status.getEntity().getContent(), StandardCharsets.UTF_8);
IOUtils.closeQuietly(status.getEntity().getContent());
ourLog.info(response);
ourCtx.newXmlParser().parseResource(Bundle.class, response);
}
@ -436,7 +437,7 @@ public class OperationServerR4Test {
Parameters resp = ourCtx.newXmlParser().parseResource(Parameters.class, response);
assertEquals("RET1", resp.getParameter().get(0).getName());
}
@Test
public void testOperationWithGetUsingParamsFailsWithNonPrimitive() throws Exception {
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient/$OP_TYPE?PARAM1=PARAM1val&PARAM2=foo");
@ -450,7 +451,7 @@ public class OperationServerR4Test {
assertThat(response, containsString("Can not invoke operation $OP_TYPE using HTTP GET because parameter PARAM2 is not a primitive datatype"));
}
@Test
public void testOperationWithListParam() throws Exception {
Parameters p = new Parameters();
@ -562,46 +563,30 @@ public class OperationServerR4Test {
assertEquals("read", ourLastMethod);
}
@Test
public void testReturnBinaryWithAcceptFhir() throws Exception {
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/$binaryop");
httpGet.addHeader(Constants.HEADER_ACCEPT, Constants.HEADER_ACCEPT_VALUE_XML_OR_JSON_NON_LEGACY);
try (CloseableHttpResponse status = ourClient.execute(httpGet)) {
assertEquals(200, status.getStatusLine().getStatusCode());
assertEquals("$binaryop", ourLastMethod);
@AfterClass
public static void afterClassClearContext() throws Exception {
ourServer.stop();
TestUtil.clearAllStaticFieldsForUnitTest();
assertEquals("application/fhir+xml;charset=utf-8", status.getEntity().getContentType().getValue());
assertEquals("<Binary xmlns=\"http://hl7.org/fhir\"><contentType value=\"text/html\"/><data value=\"PGh0bWw+VEFHUzwvaHRtbD4=\"/></Binary>", IOUtils.toString(status.getEntity().getContent(), Charsets.UTF_8));
}
}
@BeforeClass
public static void beforeClass() throws Exception {
ourCtx = FhirContext.forR4();
ourPort = PortUtil.findFreePort();
ourServer = new Server(ourPort);
@Test
public void testReturnBinaryWithAcceptHtml() throws Exception {
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/$binaryop");
httpGet.addHeader(Constants.HEADER_ACCEPT, TEXT_HTML);
try (CloseableHttpResponse status = ourClient.execute(httpGet)) {
assertEquals(200, status.getStatusLine().getStatusCode());
assertEquals("$binaryop", ourLastMethod);
ServletHandler proxyHandler = new ServletHandler();
RestfulServer servlet = new RestfulServer(ourCtx);
servlet.setPagingProvider(new FifoMemoryPagingProvider(10).setDefaultPageSize(2));
servlet.setFhirContext(ourCtx);
servlet.setResourceProviders(new PatientProvider());
servlet.setPlainProviders(new PlainProvider());
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 void main(String[] theValue) {
Parameters p = new Parameters();
p.addParameter().setName("start").setValue(new DateTimeType("2001-01-02"));
p.addParameter().setName("end").setValue(new DateTimeType("2015-07-10"));
String inParamsStr = FhirContext.forDstu2().newXmlParser().encodeResourceToString(p);
ourLog.info(inParamsStr.replace("\"", "\\\""));
assertEquals("text/html", status.getEntity().getContentType().getValue());
assertEquals("<html>TAGS</html>", IOUtils.toString(status.getEntity().getContent(), Charsets.UTF_8));
}
}
public static class PatientProvider implements IResourceProvider {
@ -612,12 +597,12 @@ public class OperationServerR4Test {
}
//@formatter:off
@Operation(name="$OP_INSTANCE")
@Operation(name = "$OP_INSTANCE")
public Parameters opInstance(
@IdParam IdType theId,
@OperationParam(name="PARAM1") StringType theParam1,
@OperationParam(name="PARAM2") Patient theParam2
) {
@IdParam IdType theId,
@OperationParam(name = "PARAM1") StringType theParam1,
@OperationParam(name = "PARAM2") Patient theParam2
) {
//@formatter:on
ourLastMethod = "$OP_INSTANCE";
@ -631,12 +616,12 @@ public class OperationServerR4Test {
}
//@formatter:off
@Operation(name="$OP_INSTANCE_OR_TYPE")
@Operation(name = "$OP_INSTANCE_OR_TYPE")
public Parameters opInstanceOrType(
@IdParam(optional=true) IdType theId,
@OperationParam(name="PARAM1") StringType theParam1,
@OperationParam(name="PARAM2") Patient theParam2
) {
@IdParam(optional = true) IdType theId,
@OperationParam(name = "PARAM1") StringType theParam1,
@OperationParam(name = "PARAM2") Patient theParam2
) {
//@formatter:on
ourLastMethod = "$OP_INSTANCE_OR_TYPE";
@ -650,10 +635,10 @@ public class OperationServerR4Test {
}
//@formatter:off
@Operation(name="$OP_PROFILE_DT2", idempotent=true)
@Operation(name = "$OP_PROFILE_DT2", idempotent = true)
public Bundle opProfileType(
@OperationParam(name="PARAM1") MoneyQuantity theParam1
) {
@OperationParam(name = "PARAM1") MoneyQuantity theParam1
) {
//@formatter:on
ourLastMethod = "$OP_PROFILE_DT2";
@ -665,10 +650,10 @@ public class OperationServerR4Test {
}
//@formatter:off
@Operation(name="$OP_PROFILE_DT", idempotent=true)
@Operation(name = "$OP_PROFILE_DT", idempotent = true)
public Bundle opProfileType(
@OperationParam(name="PARAM1") UnsignedIntType theParam1
) {
@OperationParam(name = "PARAM1") UnsignedIntType theParam1
) {
//@formatter:on
ourLastMethod = "$OP_PROFILE_DT";
@ -681,13 +666,13 @@ public class OperationServerR4Test {
//@formatter:off
@SuppressWarnings("unused")
@Operation(name="$OP_TYPE", idempotent=true)
@Operation(name = "$OP_TYPE", idempotent = true)
public Parameters opType(
@OperationParam(name="PARAM1") StringType theParam1,
@OperationParam(name="PARAM2") Patient theParam2,
@OperationParam(name="PARAM3", min=2, max=5) List<StringType> theParam3,
@OperationParam(name="PARAM4", min=1) List<StringType> theParam4
) {
@OperationParam(name = "PARAM1") StringType theParam1,
@OperationParam(name = "PARAM2") Patient theParam2,
@OperationParam(name = "PARAM3", min = 2, max = 5) List<StringType> theParam3,
@OperationParam(name = "PARAM4", min = 1) List<StringType> theParam4
) {
//@formatter:on
ourLastMethod = "$OP_TYPE";
@ -700,10 +685,10 @@ public class OperationServerR4Test {
}
//@formatter:off
@Operation(name="$OP_TYPE_ONLY_STRING", idempotent=true)
@Operation(name = "$OP_TYPE_ONLY_STRING", idempotent = true)
public Parameters opTypeOnlyString(
@OperationParam(name="PARAM1") StringType theParam1
) {
@OperationParam(name = "PARAM1") StringType theParam1
) {
//@formatter:on
ourLastMethod = "$OP_TYPE";
@ -715,11 +700,11 @@ public class OperationServerR4Test {
}
//@formatter:off
@Operation(name="$OP_TYPE_RET_BUNDLE")
@Operation(name = "$OP_TYPE_RET_BUNDLE")
public Bundle opTypeRetBundle(
@OperationParam(name="PARAM1") StringType theParam1,
@OperationParam(name="PARAM2") Patient theParam2
) {
@OperationParam(name = "PARAM1") StringType theParam1,
@OperationParam(name = "PARAM2") Patient theParam2
) {
//@formatter:on
ourLastMethod = "$OP_TYPE_RET_BUNDLE";
@ -731,7 +716,7 @@ public class OperationServerR4Test {
return retVal;
}
@Operation(name = "$everything", idempotent=true)
@Operation(name = "$everything", idempotent = true)
public Bundle patientEverything(@IdParam IdType thePatientId) {
ourLastMethod = "instance $everything";
ourLastId = thePatientId;
@ -754,27 +739,27 @@ public class OperationServerR4Test {
public static class PlainProvider {
//@formatter:off
@Operation(name="$OP_INSTANCE_BUNDLE_PROVIDER", idempotent=true)
@Operation(name = "$OP_INSTANCE_BUNDLE_PROVIDER", idempotent = true)
public IBundleProvider opInstanceReturnsBundleProvider() {
ourLastMethod = "$OP_INSTANCE_BUNDLE_PROVIDER";
List<IBaseResource> resources = new ArrayList<IBaseResource>();
for (int i =0; i < 100;i++) {
for (int i = 0; i < 100; i++) {
Patient p = new Patient();
p.setId("Patient/" + i);
p.addName().setFamily("Patient " + i);
resources.add(p);
}
return new SimpleBundleProvider(resources);
}
//@formatter:off
@Operation(name="$OP_SERVER")
@Operation(name = "$OP_SERVER")
public Parameters opServer(
@OperationParam(name="PARAM1") StringType theParam1,
@OperationParam(name="PARAM2") Patient theParam2
) {
@OperationParam(name = "PARAM1") StringType theParam1,
@OperationParam(name = "PARAM2") Patient theParam2
) {
//@formatter:on
ourLastMethod = "$OP_SERVER";
@ -787,10 +772,10 @@ public class OperationServerR4Test {
}
//@formatter:off
@Operation(name="$OP_SERVER_WITH_RAW_STRING")
@Operation(name = "$OP_SERVER_WITH_RAW_STRING")
public Parameters opServer(
@OperationParam(name="PARAM1") String theParam1,
@OperationParam(name="PARAM2") Patient theParam2
@OperationParam(name = "PARAM1") String theParam1,
@OperationParam(name = "PARAM2") Patient theParam2
) {
//@formatter:on
@ -803,13 +788,11 @@ public class OperationServerR4Test {
return retVal;
}
//@formatter:off
@Operation(name="$OP_SERVER_LIST_PARAM")
@Operation(name = "$OP_SERVER_LIST_PARAM")
public Parameters opServerListParam(
@OperationParam(name="PARAM2") Patient theParam2,
@OperationParam(name="PARAM3") List<StringType> theParam3
) {
//@formatter:on
@OperationParam(name = "PARAM2") Patient theParam2,
@OperationParam(name = "PARAM3") List<StringType> theParam3
) {
ourLastMethod = "$OP_SERVER_LIST_PARAM";
ourLastParam2 = theParam2;
@ -820,6 +803,60 @@ public class OperationServerR4Test {
return retVal;
}
@Operation(name = "$binaryop", idempotent = true)
public Binary binaryOp(
@OperationParam(name = "PARAM3", min = 0, max = 1) List<StringType> theParam3
) {
ourLastMethod = "$binaryop";
ourLastParam3 = theParam3;
Binary retVal = new Binary();
retVal.setContentType(TEXT_HTML);
retVal.setContent("<html>TAGS</html>".getBytes(Charsets.UTF_8));
return retVal;
}
}
@AfterClass
public static void afterClassClearContext() throws Exception {
ourServer.stop();
TestUtil.clearAllStaticFieldsForUnitTest();
}
@BeforeClass
public static void beforeClass() throws Exception {
ourCtx = FhirContext.forR4();
ourPort = PortUtil.findFreePort();
ourServer = new Server(ourPort);
ServletHandler proxyHandler = new ServletHandler();
RestfulServer servlet = new RestfulServer(ourCtx);
servlet.setPagingProvider(new FifoMemoryPagingProvider(10).setDefaultPageSize(2));
servlet.setFhirContext(ourCtx);
servlet.setResourceProviders(new PatientProvider());
servlet.setPlainProviders(new PlainProvider());
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 void main(String[] theValue) {
Parameters p = new Parameters();
p.addParameter().setName("start").setValue(new DateTimeType("2001-01-02"));
p.addParameter().setName("end").setValue(new DateTimeType("2015-07-10"));
String inParamsStr = FhirContext.forDstu2().newXmlParser().encodeResourceToString(p);
ourLog.info(inParamsStr.replace("\"", "\\\""));
}
}

View File

@ -2,17 +2,6 @@ package ca.uhn.fhir.rest.server.interceptor;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.context.api.BundleInclusionRule;
import ca.uhn.fhir.model.api.IResource;
import ca.uhn.fhir.model.dstu2.composite.HumanNameDt;
import ca.uhn.fhir.model.dstu2.composite.IdentifierDt;
import ca.uhn.fhir.model.dstu2.resource.Binary;
import ca.uhn.fhir.model.dstu2.resource.OperationOutcome;
import ca.uhn.fhir.model.dstu2.resource.OperationOutcome.Issue;
import ca.uhn.fhir.model.dstu2.resource.Organization;
import ca.uhn.fhir.model.dstu2.resource.Patient;
import ca.uhn.fhir.model.dstu2.valueset.IdentifierUseEnum;
import ca.uhn.fhir.model.primitive.IdDt;
import ca.uhn.fhir.model.primitive.UriDt;
import ca.uhn.fhir.rest.annotation.IdParam;
import ca.uhn.fhir.rest.annotation.Read;
import ca.uhn.fhir.rest.annotation.RequiredParam;
@ -28,9 +17,9 @@ import ca.uhn.fhir.rest.server.servlet.ServletRequestDetails;
import ca.uhn.fhir.util.PortUtil;
import ca.uhn.fhir.util.TestUtil;
import ca.uhn.fhir.util.UrlUtil;
import com.google.common.base.Charsets;
import com.helger.collection.iterate.ArrayEnumeration;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
@ -39,12 +28,12 @@ import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.hl7.fhir.instance.model.api.IBaseResource;
import org.hl7.fhir.r4.model.*;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.springframework.web.cors.CorsConfiguration;
import javax.servlet.http.HttpServletRequest;
@ -65,10 +54,8 @@ public class ResponseHighlightingInterceptorTest {
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(ResponseHighlightingInterceptorTest.class);
private static ResponseHighlighterInterceptor ourInterceptor = new ResponseHighlighterInterceptor();
private static CloseableHttpClient ourClient;
private static FhirContext ourCtx = FhirContext.forDstu2();
private static FhirContext ourCtx = FhirContext.forR4();
private static int ourPort;
private static Server ourServer;
private static RestfulServer ourServlet;
@Before
@ -83,28 +70,45 @@ public class ResponseHighlightingInterceptorTest {
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);
CloseableHttpResponse status = ourClient.execute(httpGet);
byte[] responseContent = IOUtils.toByteArray(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
status.close();
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);
}
/**
* Return a Binary response type - Client accepts text/html but is not a browser
*/
@Test
public void testBinaryReadHtmlResponseFromProvider() throws Exception {
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Binary/html");
httpGet.addHeader("Accept", "text/html");
CloseableHttpResponse status = ourClient.execute(httpGet);
String responseContent = IOUtils.toString(status.getEntity().getContent(), Charsets.UTF_8);
status.close();
assertEquals(200, status.getStatusLine().getStatusCode());
assertEquals("text/html", status.getFirstHeader("content-type").getValue());
assertEquals("<html>DATA</html>", responseContent);
assertEquals("Attachment;", status.getFirstHeader("Content-Disposition").getValue());
}
@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);
CloseableHttpResponse status = ourClient.execute(httpGet);
String responseContent = IOUtils.toString(status.getEntity().getContent(), StandardCharsets.UTF_8);
IOUtils.closeQuietly(status.getEntity().getContent());
status.close();
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);
assertEquals("{\"resourceType\":\"Binary\",\"id\":\"foo\",\"contentType\":\"foo\",\"data\":\"AQIDBA==\"}", responseContent);
}
@ -112,9 +116,9 @@ public class ResponseHighlightingInterceptorTest {
public void testBinaryReadAcceptMissing() throws Exception {
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Binary/foo");
HttpResponse status = ourClient.execute(httpGet);
CloseableHttpResponse status = ourClient.execute(httpGet);
byte[] responseContent = IOUtils.toByteArray(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
status.close();
assertEquals(200, status.getStatusLine().getStatusCode());
assertEquals("foo", status.getFirstHeader("content-type").getValue());
assertEquals("Attachment;", status.getFirstHeader("Content-Disposition").getValue());
@ -127,29 +131,19 @@ public class ResponseHighlightingInterceptorTest {
ResponseHighlighterInterceptor ic = ourInterceptor;
HttpServletRequest req = mock(HttpServletRequest.class);
when(req.getHeaders(Constants.HEADER_ACCEPT)).thenAnswer(new Answer<Enumeration<String>>() {
@Override
public Enumeration<String> answer(InvocationOnMock theInvocation) throws Throwable {
return new ArrayEnumeration<>("text/html,application/xhtml+xml,application/xml;q=0.9");
}
});
when(req.getHeader(Constants.HEADER_ORIGIN)).thenAnswer(new Answer<String>() {
@Override
public String answer(InvocationOnMock theInvocation) throws Throwable {
return "http://example.com";
}
});
when(req.getHeaders(Constants.HEADER_ACCEPT)).thenAnswer(theInvocation -> new ArrayEnumeration<>("text/html,application/xhtml+xml,application/xml;q=0.9"));
when(req.getHeader(Constants.HEADER_ORIGIN)).thenAnswer(theInvocation -> "http://example.com");
HttpServletResponse resp = mock(HttpServletResponse.class);
StringWriter sw = new StringWriter();
when(resp.getWriter()).thenReturn(new PrintWriter(sw));
Patient resource = new Patient();
resource.addName().addFamily("FAMILY");
resource.addName().setFamily("FAMILY");
ServletRequestDetails reqDetails = new TestServletRequestDetails();
reqDetails.setRequestType(RequestTypeEnum.GET);
HashMap<String, String[]> params = new HashMap<String, String[]>();
HashMap<String, String[]> params = new HashMap<>();
reqDetails.setParameters(params);
reqDetails.setServer(new RestfulServer(ourCtx));
reqDetails.setServletRequest(req);
@ -164,11 +158,11 @@ public class ResponseHighlightingInterceptorTest {
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient/1?_format=application/json");
httpGet.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1");
HttpResponse status = ourClient.execute(httpGet);
CloseableHttpResponse status = ourClient.execute(httpGet);
String responseContent = IOUtils.toString(status.getEntity().getContent(), StandardCharsets.UTF_8);
IOUtils.closeQuietly(status.getEntity().getContent());
status.close();
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_NEW + ";charset=utf-8", status.getFirstHeader("content-type").getValue().replace(" ", "").toLowerCase());
assertThat(responseContent, not(containsString("html")));
}
@ -177,9 +171,9 @@ public class ResponseHighlightingInterceptorTest {
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient/1?_format=application/json+fhir");
httpGet.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1");
HttpResponse status = ourClient.execute(httpGet);
CloseableHttpResponse status = ourClient.execute(httpGet);
String responseContent = IOUtils.toString(status.getEntity().getContent(), StandardCharsets.UTF_8);
IOUtils.closeQuietly(status.getEntity().getContent());
status.close();
assertEquals(200, status.getStatusLine().getStatusCode());
assertEquals(Constants.CT_FHIR_JSON + ";charset=utf-8", status.getFirstHeader("content-type").getValue().replace(" ", "").toLowerCase());
assertThat(responseContent, not(containsString("html")));
@ -190,9 +184,9 @@ public class ResponseHighlightingInterceptorTest {
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient/1?_format=" + UrlUtil.escapeUrlParam("application/json+fhir"));
httpGet.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1");
HttpResponse status = ourClient.execute(httpGet);
CloseableHttpResponse status = ourClient.execute(httpGet);
String responseContent = IOUtils.toString(status.getEntity().getContent(), StandardCharsets.UTF_8);
IOUtils.closeQuietly(status.getEntity().getContent());
status.close();
assertEquals(200, status.getStatusLine().getStatusCode());
assertEquals(Constants.CT_FHIR_JSON + ";charset=utf-8", status.getFirstHeader("content-type").getValue().replace(" ", "").toLowerCase());
assertThat(responseContent, not(containsString("html")));
@ -203,11 +197,11 @@ public class ResponseHighlightingInterceptorTest {
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient/1?_format=application/xml");
httpGet.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1");
HttpResponse status = ourClient.execute(httpGet);
String responseContent = IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
CloseableHttpResponse status = ourClient.execute(httpGet);
String responseContent = IOUtils.toString(status.getEntity().getContent(), Charsets.UTF_8);
status.close();
assertEquals(200, status.getStatusLine().getStatusCode());
assertEquals(Constants.CT_FHIR_XML + ";charset=utf-8", status.getFirstHeader("content-type").getValue().replace(" ", "").toLowerCase());
assertEquals(Constants.CT_FHIR_XML_NEW + ";charset=utf-8", status.getFirstHeader("content-type").getValue().replace(" ", "").toLowerCase());
assertThat(responseContent, not(containsString("html")));
}
@ -216,9 +210,9 @@ public class ResponseHighlightingInterceptorTest {
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient/1?_format=application/xml+fhir");
httpGet.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1");
HttpResponse status = ourClient.execute(httpGet);
String responseContent = IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
CloseableHttpResponse status = ourClient.execute(httpGet);
String responseContent = IOUtils.toString(status.getEntity().getContent(), Charsets.UTF_8);
status.close();
assertEquals(200, status.getStatusLine().getStatusCode());
assertEquals(Constants.CT_FHIR_XML + ";charset=utf-8", status.getFirstHeader("content-type").getValue().replace(" ", "").toLowerCase());
assertThat(responseContent, not(containsString("html")));
@ -229,9 +223,9 @@ public class ResponseHighlightingInterceptorTest {
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient/1?_format=" + UrlUtil.escapeUrlParam("application/xml+fhir"));
httpGet.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1");
HttpResponse status = ourClient.execute(httpGet);
String responseContent = IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
CloseableHttpResponse status = ourClient.execute(httpGet);
String responseContent = IOUtils.toString(status.getEntity().getContent(), Charsets.UTF_8);
status.close();
assertEquals(200, status.getStatusLine().getStatusCode());
assertEquals(Constants.CT_FHIR_XML + ";charset=utf-8", status.getFirstHeader("content-type").getValue().replace(" ", "").toLowerCase());
assertThat(responseContent, not(containsString("html")));
@ -242,9 +236,9 @@ public class ResponseHighlightingInterceptorTest {
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient/1?_format=html/json");
httpGet.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1");
HttpResponse status = ourClient.execute(httpGet);
CloseableHttpResponse status = ourClient.execute(httpGet);
String responseContent = IOUtils.toString(status.getEntity().getContent(), StandardCharsets.UTF_8);
IOUtils.closeQuietly(status.getEntity().getContent());
status.close();
assertEquals(200, status.getStatusLine().getStatusCode());
assertEquals("text/html;charset=utf-8", status.getFirstHeader("content-type").getValue().replace(" ", "").toLowerCase());
assertThat(responseContent, containsString("html"));
@ -259,9 +253,9 @@ public class ResponseHighlightingInterceptorTest {
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient/1?_format=" + UrlUtil.escapeUrlParam("html/json; fhirVersion=1.0"));
httpGet.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1");
HttpResponse status = ourClient.execute(httpGet);
CloseableHttpResponse status = ourClient.execute(httpGet);
String responseContent = IOUtils.toString(status.getEntity().getContent(), StandardCharsets.UTF_8);
IOUtils.closeQuietly(status.getEntity().getContent());
status.close();
assertEquals(200, status.getStatusLine().getStatusCode());
assertEquals("text/html;charset=utf-8", status.getFirstHeader("content-type").getValue().replace(" ", "").toLowerCase());
assertThat(responseContent, containsString("html"));
@ -276,9 +270,9 @@ public class ResponseHighlightingInterceptorTest {
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient/1?_format=html/xml");
httpGet.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1");
HttpResponse status = ourClient.execute(httpGet);
String responseContent = IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
CloseableHttpResponse status = ourClient.execute(httpGet);
String responseContent = IOUtils.toString(status.getEntity().getContent(), Charsets.UTF_8);
status.close();
assertEquals(200, status.getStatusLine().getStatusCode());
assertEquals("text/html;charset=utf-8", status.getFirstHeader("content-type").getValue().replace(" ", "").toLowerCase());
assertThat(responseContent, containsString("html"));
@ -291,11 +285,11 @@ public class ResponseHighlightingInterceptorTest {
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient/1?_format=json");
httpGet.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1");
HttpResponse status = ourClient.execute(httpGet);
CloseableHttpResponse status = ourClient.execute(httpGet);
String responseContent = IOUtils.toString(status.getEntity().getContent(), StandardCharsets.UTF_8);
IOUtils.closeQuietly(status.getEntity().getContent());
status.close();
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_NEW + ";charset=utf-8", status.getFirstHeader("content-type").getValue().replace(" ", "").toLowerCase());
assertThat(responseContent, not(containsString("html")));
}
@ -303,9 +297,9 @@ public class ResponseHighlightingInterceptorTest {
public void testForceResponseTime() throws Exception {
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient/1?_format=html/json");
HttpResponse status = ourClient.execute(httpGet);
CloseableHttpResponse status = ourClient.execute(httpGet);
String responseContent = IOUtils.toString(status.getEntity().getContent(), StandardCharsets.UTF_8);
IOUtils.closeQuietly(status.getEntity().getContent());
status.close();
ourLog.info(responseContent);
assertEquals(200, status.getStatusLine().getStatusCode());
assertEquals("text/html;charset=utf-8", status.getFirstHeader("content-type").getValue().replace(" ", "").toLowerCase());
@ -319,7 +313,7 @@ public class ResponseHighlightingInterceptorTest {
httpGet.addHeader("Accept", "text/html");
CloseableHttpResponse status = ourClient.execute(httpGet);
String responseContent = IOUtils.toString(status.getEntity().getContent(), StandardCharsets.UTF_8);
IOUtils.closeQuietly(status.getEntity().getContent());
status.close();
ourLog.info("Resp: {}", responseContent);
assertEquals(404, status.getStatusLine().getStatusCode());
@ -333,14 +327,14 @@ public class ResponseHighlightingInterceptorTest {
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Foobar/123");
CloseableHttpResponse status = ourClient.execute(httpGet);
String responseContent = IOUtils.toString(status.getEntity().getContent(), StandardCharsets.UTF_8);
IOUtils.closeQuietly(status.getEntity().getContent());
status.close();
ourLog.info("Resp: {}", responseContent);
assertEquals(404, status.getStatusLine().getStatusCode());
assertThat(responseContent, not(stringContainsInOrder("<span class='hlTagName'>OperationOutcome</span>", "Unknown resource type 'Foobar' - Server knows how to handle")));
assertThat(responseContent, (stringContainsInOrder("Unknown resource type 'Foobar'")));
assertThat(status.getFirstHeader("Content-Type").getValue(), containsString("application/xml+fhir"));
assertEquals(Constants.CT_FHIR_XML_NEW + ";charset=utf-8", status.getFirstHeader("content-type").getValue().replace(" ", "").toLowerCase());
}
@ -349,8 +343,8 @@ public class ResponseHighlightingInterceptorTest {
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/");
httpGet.addHeader("Accept", "text/html");
CloseableHttpResponse status = ourClient.execute(httpGet);
String responseContent = IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
String responseContent = IOUtils.toString(status.getEntity().getContent(), Charsets.UTF_8);
status.close();
ourLog.info("Resp: {}", responseContent);
assertEquals(400, status.getStatusLine().getStatusCode());
@ -364,19 +358,14 @@ public class ResponseHighlightingInterceptorTest {
ResponseHighlighterInterceptor ic = ourInterceptor;
HttpServletRequest req = mock(HttpServletRequest.class);
when(req.getHeaders(Constants.HEADER_ACCEPT)).thenAnswer(new Answer<Enumeration<String>>() {
@Override
public Enumeration<String> answer(InvocationOnMock theInvocation) throws Throwable {
return new ArrayEnumeration<String>("text/html,application/xhtml+xml,application/xml;q=0.9");
}
});
when(req.getHeaders(Constants.HEADER_ACCEPT)).thenAnswer(theInvocation -> new ArrayEnumeration<>("text/html,application/xhtml+xml,application/xml;q=0.9"));
HttpServletResponse resp = mock(HttpServletResponse.class);
StringWriter sw = new StringWriter();
when(resp.getWriter()).thenReturn(new PrintWriter(sw));
Patient resource = new Patient();
resource.addName().addFamily("FAMILY");
resource.addName().setFamily("FAMILY");
ServletRequestDetails reqDetails = new TestServletRequestDetails();
reqDetails.setRequestType(RequestTypeEnum.GET);
@ -387,7 +376,7 @@ public class ResponseHighlightingInterceptorTest {
// reqDetails.setParameters(null);
ResourceNotFoundException exception = new ResourceNotFoundException("Not found");
exception.setOperationOutcome(new OperationOutcome().addIssue(new Issue().setDiagnostics("Hello")));
exception.setOperationOutcome(new OperationOutcome().addIssue(new OperationOutcome.OperationOutcomeIssueComponent().setDiagnostics("Hello")));
assertFalse(ic.handleException(reqDetails, exception, req, resp));
@ -404,23 +393,18 @@ public class ResponseHighlightingInterceptorTest {
ResponseHighlighterInterceptor ic = ourInterceptor;
HttpServletRequest req = mock(HttpServletRequest.class);
when(req.getHeaders(Constants.HEADER_ACCEPT)).thenAnswer(new Answer<Enumeration<String>>() {
@Override
public Enumeration<String> answer(InvocationOnMock theInvocation) throws Throwable {
return new ArrayEnumeration<String>("application/xml+fhir");
}
});
when(req.getHeaders(Constants.HEADER_ACCEPT)).thenAnswer(theInvocation -> new ArrayEnumeration<>("application/xml+fhir"));
HttpServletResponse resp = mock(HttpServletResponse.class);
StringWriter sw = new StringWriter();
when(resp.getWriter()).thenReturn(new PrintWriter(sw));
Patient resource = new Patient();
resource.addName().addFamily("FAMILY");
resource.addName().setFamily("FAMILY");
ServletRequestDetails reqDetails = new TestServletRequestDetails();
reqDetails.setRequestType(RequestTypeEnum.GET);
HashMap<String, String[]> params = new HashMap<String, String[]>();
HashMap<String, String[]> params = new HashMap<>();
params.put(Constants.PARAM_FORMAT, new String[]{Constants.FORMAT_HTML});
reqDetails.setParameters(params);
reqDetails.setServer(new RestfulServer(ourCtx));
@ -438,23 +422,18 @@ public class ResponseHighlightingInterceptorTest {
ResponseHighlighterInterceptor ic = ourInterceptor;
HttpServletRequest req = mock(HttpServletRequest.class);
when(req.getHeaders(Constants.HEADER_ACCEPT)).thenAnswer(new Answer<Enumeration<String>>() {
@Override
public Enumeration<String> answer(InvocationOnMock theInvocation) throws Throwable {
return new ArrayEnumeration<String>("application/xml+fhir");
}
});
when(req.getHeaders(Constants.HEADER_ACCEPT)).thenAnswer(theInvocation -> new ArrayEnumeration<>("application/xml+fhir"));
HttpServletResponse resp = mock(HttpServletResponse.class);
StringWriter sw = new StringWriter();
when(resp.getWriter()).thenReturn(new PrintWriter(sw));
Patient resource = new Patient();
resource.addName().addFamily("FAMILY");
resource.addName().setFamily("FAMILY");
ServletRequestDetails reqDetails = new TestServletRequestDetails();
reqDetails.setRequestType(RequestTypeEnum.GET);
HashMap<String, String[]> params = new HashMap<String, String[]>();
HashMap<String, String[]> params = new HashMap<>();
params.put(Constants.PARAM_FORMAT, new String[]{Constants.CT_HTML});
reqDetails.setParameters(params);
reqDetails.setServer(new RestfulServer(ourCtx));
@ -469,23 +448,18 @@ public class ResponseHighlightingInterceptorTest {
ResponseHighlighterInterceptor ic = ourInterceptor;
HttpServletRequest req = mock(HttpServletRequest.class);
when(req.getHeaders(Constants.HEADER_ACCEPT)).thenAnswer(new Answer<Enumeration<String>>() {
@Override
public Enumeration<String> answer(InvocationOnMock theInvocation) throws Throwable {
return new ArrayEnumeration<String>("text/html,application/xhtml+xml,application/xml;q=0.9");
}
});
when(req.getHeaders(Constants.HEADER_ACCEPT)).thenAnswer(theInvocation -> new ArrayEnumeration<>("text/html,application/xhtml+xml,application/xml;q=0.9"));
HttpServletResponse resp = mock(HttpServletResponse.class);
StringWriter sw = new StringWriter();
when(resp.getWriter()).thenReturn(new PrintWriter(sw));
Patient resource = new Patient();
resource.addName().addFamily("FAMILY");
resource.addName().setFamily("FAMILY");
ServletRequestDetails reqDetails = new TestServletRequestDetails();
reqDetails.setRequestType(RequestTypeEnum.GET);
HashMap<String, String[]> params = new HashMap<String, String[]>();
HashMap<String, String[]> params = new HashMap<>();
params.put(Constants.PARAM_PRETTY, new String[]{Constants.PARAM_PRETTY_VALUE_TRUE});
params.put(Constants.PARAM_FORMAT, new String[]{Constants.CT_XML});
params.put(ResponseHighlighterInterceptor.PARAM_RAW, new String[]{ResponseHighlighterInterceptor.PARAM_RAW_TRUE});
@ -503,23 +477,18 @@ public class ResponseHighlightingInterceptorTest {
ResponseHighlighterInterceptor ic = ourInterceptor;
HttpServletRequest req = mock(HttpServletRequest.class);
when(req.getHeaders(Constants.HEADER_ACCEPT)).thenAnswer(new Answer<Enumeration<String>>() {
@Override
public Enumeration<String> answer(InvocationOnMock theInvocation) throws Throwable {
return new ArrayEnumeration<String>("text/html,application/xhtml+xml,application/xml;q=0.9");
}
});
when(req.getHeaders(Constants.HEADER_ACCEPT)).thenAnswer(theInvocation -> new ArrayEnumeration<>("text/html,application/xhtml+xml,application/xml;q=0.9"));
HttpServletResponse resp = mock(HttpServletResponse.class);
StringWriter sw = new StringWriter();
when(resp.getWriter()).thenReturn(new PrintWriter(sw));
Patient resource = new Patient();
resource.addName().addFamily("FAMILY");
resource.addName().setFamily("FAMILY");
ServletRequestDetails reqDetails = new TestServletRequestDetails();
reqDetails.setRequestType(RequestTypeEnum.GET);
reqDetails.setParameters(new HashMap<String, String[]>());
reqDetails.setParameters(new HashMap<>());
reqDetails.setServer(new RestfulServer(ourCtx));
reqDetails.setServletRequest(req);
@ -537,23 +506,18 @@ public class ResponseHighlightingInterceptorTest {
ResponseHighlighterInterceptor ic = ourInterceptor;
HttpServletRequest req = mock(HttpServletRequest.class);
when(req.getHeaders(Constants.HEADER_ACCEPT)).thenAnswer(new Answer<Enumeration<String>>() {
@Override
public Enumeration<String> answer(InvocationOnMock theInvocation) throws Throwable {
return new ArrayEnumeration<String>("text/html,application/xhtml+xml,application/xml;q=0.9");
}
});
when(req.getHeaders(Constants.HEADER_ACCEPT)).thenAnswer(theInvocation -> new ArrayEnumeration<>("text/html,application/xhtml+xml,application/xml;q=0.9"));
HttpServletResponse resp = mock(HttpServletResponse.class);
StringWriter sw = new StringWriter();
when(resp.getWriter()).thenReturn(new PrintWriter(sw));
Patient resource = new Patient();
resource.addName().addFamily("FAMILY");
resource.addName().setFamily("FAMILY");
ServletRequestDetails reqDetails = new TestServletRequestDetails();
reqDetails.setRequestType(RequestTypeEnum.GET);
HashMap<String, String[]> params = new HashMap<String, String[]>();
HashMap<String, String[]> params = new HashMap<>();
params.put(Constants.PARAM_PRETTY, new String[]{Constants.PARAM_PRETTY_VALUE_TRUE});
reqDetails.setParameters(params);
reqDetails.setServer(new RestfulServer(ourCtx));
@ -576,23 +540,18 @@ public class ResponseHighlightingInterceptorTest {
HttpServletRequest req = mock(HttpServletRequest.class);
when(req.getHeaders(Constants.HEADER_ACCEPT)).thenAnswer(new Answer<Enumeration<String>>() {
@Override
public Enumeration<String> answer(InvocationOnMock theInvocation) throws Throwable {
return new ArrayEnumeration<String>("text/html,application/xhtml+xml,application/xml;q=0.9");
}
});
when(req.getHeaders(Constants.HEADER_ACCEPT)).thenAnswer(theInvocation -> new ArrayEnumeration<>("text/html,application/xhtml+xml,application/xml;q=0.9"));
HttpServletResponse resp = mock(HttpServletResponse.class);
StringWriter sw = new StringWriter();
when(resp.getWriter()).thenReturn(new PrintWriter(sw));
Patient resource = new Patient();
resource.addName().addFamily("FAMILY");
resource.addName().setFamily("FAMILY");
ServletRequestDetails reqDetails = new TestServletRequestDetails();
reqDetails.setRequestType(RequestTypeEnum.GET);
reqDetails.setParameters(new HashMap<String, String[]>());
reqDetails.setParameters(new HashMap<>());
RestfulServer server = new RestfulServer(ourCtx);
server.setDefaultResponseEncoding(EncodingEnum.JSON);
reqDetails.setServer(server);
@ -611,23 +570,18 @@ public class ResponseHighlightingInterceptorTest {
HttpServletRequest req = mock(HttpServletRequest.class);
when(req.getHeaders(Constants.HEADER_ACCEPT)).thenAnswer(new Answer<Enumeration<String>>() {
@Override
public Enumeration<String> answer(InvocationOnMock theInvocation) throws Throwable {
return new ArrayEnumeration<String>("text/html;q=0.8,application/xhtml+xml,application/xml;q=0.9");
}
});
when(req.getHeaders(Constants.HEADER_ACCEPT)).thenAnswer(theInvocation -> new ArrayEnumeration<>("text/html;q=0.8,application/xhtml+xml,application/xml;q=0.9"));
HttpServletResponse resp = mock(HttpServletResponse.class);
StringWriter sw = new StringWriter();
when(resp.getWriter()).thenReturn(new PrintWriter(sw));
Patient resource = new Patient();
resource.addName().addFamily("FAMILY");
resource.addName().setFamily("FAMILY");
ServletRequestDetails reqDetails = new TestServletRequestDetails();
reqDetails.setRequestType(RequestTypeEnum.GET);
reqDetails.setParameters(new HashMap<String, String[]>());
reqDetails.setParameters(new HashMap<>());
RestfulServer server = new RestfulServer(ourCtx);
server.setDefaultResponseEncoding(EncodingEnum.JSON);
reqDetails.setServer(server);
@ -647,9 +601,9 @@ public class ResponseHighlightingInterceptorTest {
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient/1");
httpGet.addHeader("Accept", "text/html");
HttpResponse status = ourClient.execute(httpGet);
CloseableHttpResponse status = ourClient.execute(httpGet);
String responseContent = IOUtils.toString(status.getEntity().getContent(), StandardCharsets.UTF_8);
IOUtils.closeQuietly(status.getEntity().getContent());
status.close();
ourLog.info(responseContent);
assertEquals(200, status.getStatusLine().getStatusCode());
assertThat(responseContent, (stringContainsInOrder("<body>", "<pre>", "<div", "</pre>")));
@ -665,9 +619,9 @@ public class ResponseHighlightingInterceptorTest {
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient/1?_pretty=false");
httpGet.addHeader("Accept", "text/html");
HttpResponse status = ourClient.execute(httpGet);
CloseableHttpResponse status = ourClient.execute(httpGet);
String responseContent = IOUtils.toString(status.getEntity().getContent(), StandardCharsets.UTF_8);
IOUtils.closeQuietly(status.getEntity().getContent());
status.close();
ourLog.info(responseContent);
assertEquals(200, status.getStatusLine().getStatusCode());
assertThat(responseContent, not(stringContainsInOrder("<body>", "<pre>", "\n", "</pre>")));
@ -683,9 +637,9 @@ public class ResponseHighlightingInterceptorTest {
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient/1?_pretty=true");
httpGet.addHeader("Accept", "text/html");
HttpResponse status = ourClient.execute(httpGet);
CloseableHttpResponse status = ourClient.execute(httpGet);
String responseContent = IOUtils.toString(status.getEntity().getContent(), StandardCharsets.UTF_8);
IOUtils.closeQuietly(status.getEntity().getContent());
status.close();
ourLog.info(responseContent);
assertEquals(200, status.getStatusLine().getStatusCode());
assertThat(responseContent, (stringContainsInOrder("<body>", "<pre>", "<div", "</pre>")));
@ -697,7 +651,7 @@ public class ResponseHighlightingInterceptorTest {
httpGet.addHeader("Accept", "html");
CloseableHttpResponse status = ourClient.execute(httpGet);
String responseContent = IOUtils.toString(status.getEntity().getContent(), StandardCharsets.UTF_8);
IOUtils.closeQuietly(status.getEntity().getContent());
status.close();
ourLog.info("Resp: {}", responseContent);
assertEquals(200, status.getStatusLine().getStatusCode());
@ -711,9 +665,9 @@ public class ResponseHighlightingInterceptorTest {
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient/1?_format=html/json");
HttpResponse status = ourClient.execute(httpGet);
CloseableHttpResponse status = ourClient.execute(httpGet);
String responseContent = IOUtils.toString(status.getEntity().getContent(), StandardCharsets.UTF_8);
IOUtils.closeQuietly(status.getEntity().getContent());
status.close();
ourLog.info(responseContent);
assertEquals(200, status.getStatusLine().getStatusCode());
assertEquals("text/html;charset=utf-8", status.getFirstHeader("content-type").getValue().replace(" ", "").toLowerCase());
@ -728,9 +682,9 @@ public class ResponseHighlightingInterceptorTest {
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient/1?_format=html/json");
HttpResponse status = ourClient.execute(httpGet);
CloseableHttpResponse status = ourClient.execute(httpGet);
String responseContent = IOUtils.toString(status.getEntity().getContent(), StandardCharsets.UTF_8);
IOUtils.closeQuietly(status.getEntity().getContent());
status.close();
ourLog.info(responseContent);
assertEquals(200, status.getStatusLine().getStatusCode());
assertEquals("text/html;charset=utf-8", status.getFirstHeader("content-type").getValue().replace(" ", "").toLowerCase());
@ -745,9 +699,9 @@ public class ResponseHighlightingInterceptorTest {
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient/1?_format=html/json");
HttpResponse status = ourClient.execute(httpGet);
CloseableHttpResponse status = ourClient.execute(httpGet);
String responseContent = IOUtils.toString(status.getEntity().getContent(), StandardCharsets.UTF_8);
IOUtils.closeQuietly(status.getEntity().getContent());
status.close();
ourLog.info(responseContent);
assertEquals(200, status.getStatusLine().getStatusCode());
assertEquals("text/html;charset=utf-8", status.getFirstHeader("content-type").getValue().replace(" ", "").toLowerCase());
@ -761,9 +715,9 @@ public class ResponseHighlightingInterceptorTest {
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient/1?_format=html/json");
HttpResponse status = ourClient.execute(httpGet);
CloseableHttpResponse status = ourClient.execute(httpGet);
String responseContent = IOUtils.toString(status.getEntity().getContent(), StandardCharsets.UTF_8);
IOUtils.closeQuietly(status.getEntity().getContent());
status.close();
ourLog.info(responseContent);
assertEquals(200, status.getStatusLine().getStatusCode());
assertEquals("text/html;charset=utf-8", status.getFirstHeader("content-type").getValue().replace(" ", "").toLowerCase());
@ -780,7 +734,7 @@ public class ResponseHighlightingInterceptorTest {
public static void beforeClass() throws Exception {
ourPort = PortUtil.findFreePort();
ourLog.info("Using port: {}", ourPort);
ourServer = new Server(ourPort);
Server ourServer = new Server(ourPort);
DummyPatientResourceProvider patientProvider = new DummyPatientResourceProvider();
@ -830,16 +784,21 @@ public class ResponseHighlightingInterceptorTest {
public static class DummyBinaryResourceProvider implements IResourceProvider {
@Override
public Class<? extends IResource> getResourceType() {
public Class<Binary> getResourceType() {
return Binary.class;
}
@Read
public Binary read(@IdParam IdDt theId) {
public Binary read(@IdParam IdType theId) {
Binary retVal = new Binary();
retVal.setId("1");
retVal.setContent(new byte[]{1, 2, 3, 4});
retVal.setContentType(theId.getIdPart());
retVal.setId(theId);
if (theId.getIdPart().equals("html")) {
retVal.setContent("<html>DATA</html>".getBytes(Charsets.UTF_8));
retVal.setContentType("text/html");
}else {
retVal.setContent(new byte[]{1, 2, 3, 4});
retVal.setContentType(theId.getIdPart());
}
return retVal;
}
@ -859,13 +818,13 @@ public class ResponseHighlightingInterceptorTest {
private Patient createPatient1() {
Patient patient = new Patient();
patient.addIdentifier();
patient.getIdentifier().get(0).setUse(IdentifierUseEnum.OFFICIAL);
patient.getIdentifier().get(0).setSystem(new UriDt("urn:hapitest:mrns"));
patient.getIdentifier().get(0).setUse(Identifier.IdentifierUse.OFFICIAL);
patient.getIdentifier().get(0).setSystem("urn:hapitest:mrns");
patient.getIdentifier().get(0).setValue("00001");
patient.addName();
patient.getName().get(0).addFamily("Test");
patient.getName().get(0).setFamily("Test");
patient.getName().get(0).addGiven("PatientOne");
patient.getId().setValue("1");
patient.setId("1");
return patient;
}
@ -889,22 +848,22 @@ public class ResponseHighlightingInterceptorTest {
return Collections.singletonList(p);
}
public Map<String, Patient> getIdToPatient() {
Map<String, Patient> idToPatient = new HashMap<String, Patient>();
Map<String, Patient> getIdToPatient() {
Map<String, Patient> idToPatient = new HashMap<>();
{
Patient patient = createPatient1();
idToPatient.put("1", patient);
}
{
Patient patient = new Patient();
patient.getIdentifier().add(new IdentifierDt());
patient.getIdentifier().get(0).setUse(IdentifierUseEnum.OFFICIAL);
patient.getIdentifier().get(0).setSystem(new UriDt("urn:hapitest:mrns"));
patient.getIdentifier().add(new Identifier());
patient.getIdentifier().get(0).setUse(Identifier.IdentifierUse.OFFICIAL);
patient.getIdentifier().get(0).setSystem("urn:hapitest:mrns");
patient.getIdentifier().get(0).setValue("00002");
patient.getName().add(new HumanNameDt());
patient.getName().get(0).addFamily("Test");
patient.getName().add(new HumanName());
patient.getName().get(0).setFamily("Test");
patient.getName().get(0).addGiven("PatientTwo");
patient.getId().setValue("2");
patient.setId("2");
idToPatient.put("2", patient);
}
return idToPatient;
@ -917,10 +876,9 @@ public class ResponseHighlightingInterceptorTest {
* @return The resource
*/
@Read()
public Patient getResourceById(@IdParam IdDt theId) {
public Patient getResourceById(@IdParam IdType theId) {
String key = theId.getIdPart();
Patient retVal = getIdToPatient().get(key);
return retVal;
return getIdToPatient().get(key);
}
/**
@ -945,10 +903,10 @@ public class ResponseHighlightingInterceptorTest {
}
@Search(queryName = "searchWithWildcardRetVal")
public List<? extends IResource> searchWithWildcardRetVal() {
public List<IBaseResource> searchWithWildcardRetVal() {
Patient p = new Patient();
p.setId("1234");
p.addName().addFamily("searchWithWildcardRetVal");
p.addName().setFamily("searchWithWildcardRetVal");
return Collections.singletonList(p);
}

View File

@ -39,6 +39,13 @@
is not yet possible to specify individual resource security when
using GraphQL.
</action>
<action type="fix">
The ResponseHighlighterInterceptor now declines to handle Binary responses
provided as a response from extended operations. In other words if the
operation $foo returns a Binary resource, the ResponseHighliterInterceptor will
not provide syntax highlighting on the response. This was previously the case for
the /Binary endpoint, but not for other binary responses.
</action>
</release>
<release version="3.6.0" date="2018-11-12" description="Food">
<action type="add">