From 67640791296edeeca954a1dddd4ddd869a30a73d Mon Sep 17 00:00:00 2001 From: James Date: Mon, 22 May 2017 16:15:51 -0400 Subject: [PATCH] Bugfixes to the testpage overlay --- .../client/apache/ApacheHttpResponse.java | 16 +++ .../fhir/rest/client/api/IHttpResponse.java | 101 +++++++++--------- .../ca/uhn/fhir/cli/RunServerCommand.java | 9 +- .../okhttp/client/OkHttpRestfulResponse.java | 5 + .../java/ca/uhn/fhir/to/BaseController.java | 2 +- .../to/model/BufferResponseInterceptor.java | 21 ++++ .../ca/uhn/fhir/to/model/HomeRequest.java | 2 + 7 files changed, 105 insertions(+), 51 deletions(-) create mode 100644 hapi-fhir-testpage-overlay/src/main/java/ca/uhn/fhir/to/model/BufferResponseInterceptor.java diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/client/apache/ApacheHttpResponse.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/client/apache/ApacheHttpResponse.java index 9dc17f33068..ed6763cdfe4 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/client/apache/ApacheHttpResponse.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/client/apache/ApacheHttpResponse.java @@ -1,5 +1,7 @@ package ca.uhn.fhir.rest.client.apache; +import static org.hamcrest.Matchers.array; + /* * #%L * HAPI FHIR - Core Library @@ -28,6 +30,7 @@ import java.io.Reader; import java.io.StringReader; import java.nio.charset.Charset; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -136,6 +139,19 @@ public class ApacheHttpResponse implements IHttpResponse { return headers; } + @Override + public List getHeaders(String theName) { + Header[] headers = myResponse.getHeaders(theName); + if (headers == null) { + headers = new Header[0]; + } + List retVal = new ArrayList(); + for (Header next : headers) { + retVal.add(next.getValue()); + } + return retVal; + } + @Override public String getMimeType() { ContentType ct = ContentType.get(myResponse.getEntity()); diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/client/api/IHttpResponse.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/client/api/IHttpResponse.java index d7a84aed89e..a72c6c1fb96 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/client/api/IHttpResponse.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/client/api/IHttpResponse.java @@ -32,52 +32,10 @@ import java.util.Map; public interface IHttpResponse { /** - * Get the status code associated with the response. - * - * @return the response status code. + * @deprecated This method was deprecated in HAPI FHIR 2.2 because its name has a typo. Use {@link #bufferEntity()} instead. */ - public int getStatus(); - - /** - * @return the native response, depending on the client library used - */ - Object getResponse(); - - /** - * Extracts {@code Content-Type} value from the response exactly as - * specified by the {@code Content-Type} header. Returns {@code null} - * if not specified. - */ - public String getMimeType(); - - /** - * Get map of the response headers and corresponding string values. - * - * @return response headers as a map header keys and they values. - */ - public Map> getAllHeaders(); - - /** - * Get the response status information reason phrase associated with the response. - * - * @return the reason phrase. - */ - public String getStatusInfo(); - - /** - * Returna reader for the response entity - */ - public Reader createReader() throws IOException; - - /** - * Read the message entity input stream as an InputStream. - */ - public InputStream readEntity() throws IOException; - - /** - * Close the response - */ - public void close(); + @Deprecated + void bufferEntitity() throws IOException; /** * Buffer the message entity data. @@ -105,9 +63,56 @@ public interface IHttpResponse { void bufferEntity() throws IOException; /** - * @deprecated This method was deprecated in HAPI FHIR 2.2 because its name has a typo. Use {@link #bufferEntity()} instead. + * Close the response */ - @Deprecated - void bufferEntitity() throws IOException; + public void close(); + + /** + * Returna reader for the response entity + */ + public Reader createReader() throws IOException; + + /** + * Get map of the response headers and corresponding string values. + * + * @return response headers as a map header keys and they values. + */ + public Map> getAllHeaders(); + + /** + * Return all headers in the response with the given type + */ + public List getHeaders(String theName); + + /** + * Extracts {@code Content-Type} value from the response exactly as + * specified by the {@code Content-Type} header. Returns {@code null} + * if not specified. + */ + public String getMimeType(); + + /** + * @return the native response, depending on the client library used + */ + Object getResponse(); + + /** + * Get the status code associated with the response. + * + * @return the response status code. + */ + public int getStatus(); + + /** + * Get the response status information reason phrase associated with the response. + * + * @return the reason phrase. + */ + public String getStatusInfo(); + + /** + * Read the message entity input stream as an InputStream. + */ + public InputStream readEntity() throws IOException; } diff --git a/hapi-fhir-cli/hapi-fhir-cli-app/src/main/java/ca/uhn/fhir/cli/RunServerCommand.java b/hapi-fhir-cli/hapi-fhir-cli-app/src/main/java/ca/uhn/fhir/cli/RunServerCommand.java index a9f9aff9a17..2e70ad9ad9e 100644 --- a/hapi-fhir-cli/hapi-fhir-cli-app/src/main/java/ca/uhn/fhir/cli/RunServerCommand.java +++ b/hapi-fhir-cli/hapi-fhir-cli-app/src/main/java/ca/uhn/fhir/cli/RunServerCommand.java @@ -27,7 +27,7 @@ import ca.uhn.fhir.jpa.demo.FhirServerConfigDstu3; public class RunServerCommand extends BaseCommand { - private static final String DISABLE_REFERENTIAL_INTEGRITY = "disable-referential-integrity"; + private static final String OPTION_DISABLE_REFERENTIAL_INTEGRITY = "disable-referential-integrity"; private static final String OPTION_LOWMEM = "lowmem"; private static final String OPTION_ALLOW_EXTERNAL_REFS = "allow-external-refs"; private static final int DEFAULT_PORT = 8080; @@ -50,7 +50,7 @@ public class RunServerCommand extends BaseCommand { options.addOption(OPTION_P, "port", true, "The port to listen on (default is " + DEFAULT_PORT + ")"); options.addOption(null, OPTION_LOWMEM, false, "If this flag is set, the server will operate in low memory mode (some features disabled)"); options.addOption(null, OPTION_ALLOW_EXTERNAL_REFS, false, "If this flag is set, the server will allow resources to be persisted contaning external resource references"); - options.addOption(null, DISABLE_REFERENTIAL_INTEGRITY, false, "If this flag is set, the server will not enforce referential integrity"); + options.addOption(null, OPTION_DISABLE_REFERENTIAL_INTEGRITY, false, "If this flag is set, the server will not enforce referential integrity"); return options; } @@ -76,6 +76,11 @@ public class RunServerCommand extends BaseCommand { ContextHolder.setAllowExternalRefs(true); } + if (theCommandLine.hasOption(OPTION_DISABLE_REFERENTIAL_INTEGRITY)) { + ourLog.info("Server is configured to not enforce referential integrity"); + ContextHolder.setDisableReferentialIntegrity(true); + } + ContextHolder.setCtx(getSpecVersionContext(theCommandLine)); ourLog.info("Preparing HAPI FHIR JPA server on port {}", myPort); diff --git a/hapi-fhir-client-okhttp/src/main/java/ca/uhn/fhir/okhttp/client/OkHttpRestfulResponse.java b/hapi-fhir-client-okhttp/src/main/java/ca/uhn/fhir/okhttp/client/OkHttpRestfulResponse.java index caddea159d4..81e5dc236f2 100644 --- a/hapi-fhir-client-okhttp/src/main/java/ca/uhn/fhir/okhttp/client/OkHttpRestfulResponse.java +++ b/hapi-fhir-client-okhttp/src/main/java/ca/uhn/fhir/okhttp/client/OkHttpRestfulResponse.java @@ -86,6 +86,11 @@ public class OkHttpRestfulResponse implements IHttpResponse { return myResponse.headers().toMultimap(); } + @Override + public List getHeaders(String theName) { + return myResponse.headers(theName); + } + @Override public String getMimeType() { String contentType = myResponse.header(Constants.HEADER_CONTENT_TYPE); diff --git a/hapi-fhir-testpage-overlay/src/main/java/ca/uhn/fhir/to/BaseController.java b/hapi-fhir-testpage-overlay/src/main/java/ca/uhn/fhir/to/BaseController.java index 63668e0134f..35ab412e234 100644 --- a/hapi-fhir-testpage-overlay/src/main/java/ca/uhn/fhir/to/BaseController.java +++ b/hapi-fhir-testpage-overlay/src/main/java/ca/uhn/fhir/to/BaseController.java @@ -598,7 +598,7 @@ public class BaseController { lastResponse.bufferEntity(); resultBody = IOUtils.toString(lastResponse.readEntity(), Constants.CHARSET_UTF8); - List ctStrings = lastResponse.getAllHeaders().get(Constants.HEADER_CONTENT_TYPE); + List ctStrings = lastResponse.getHeaders(Constants.HEADER_CONTENT_TYPE); if (ctStrings != null && ctStrings.isEmpty() == false) { ct = ContentType.parse(ctStrings.get(0)); mimeType = ct.getMimeType(); diff --git a/hapi-fhir-testpage-overlay/src/main/java/ca/uhn/fhir/to/model/BufferResponseInterceptor.java b/hapi-fhir-testpage-overlay/src/main/java/ca/uhn/fhir/to/model/BufferResponseInterceptor.java new file mode 100644 index 00000000000..0ae6852f1f7 --- /dev/null +++ b/hapi-fhir-testpage-overlay/src/main/java/ca/uhn/fhir/to/model/BufferResponseInterceptor.java @@ -0,0 +1,21 @@ +package ca.uhn.fhir.to.model; + +import java.io.IOException; + +import ca.uhn.fhir.rest.client.IClientInterceptor; +import ca.uhn.fhir.rest.client.api.IHttpRequest; +import ca.uhn.fhir.rest.client.api.IHttpResponse; + +public class BufferResponseInterceptor implements IClientInterceptor { + + @Override + public void interceptRequest(IHttpRequest theRequest) { + // nothing + } + + @Override + public void interceptResponse(IHttpResponse theResponse) throws IOException { + theResponse.bufferEntity(); + } + +} diff --git a/hapi-fhir-testpage-overlay/src/main/java/ca/uhn/fhir/to/model/HomeRequest.java b/hapi-fhir-testpage-overlay/src/main/java/ca/uhn/fhir/to/model/HomeRequest.java index 432f6a07436..0811e674221 100644 --- a/hapi-fhir-testpage-overlay/src/main/java/ca/uhn/fhir/to/model/HomeRequest.java +++ b/hapi-fhir-testpage-overlay/src/main/java/ca/uhn/fhir/to/model/HomeRequest.java @@ -137,6 +137,8 @@ public class HomeRequest { } else { retVal = (GenericClient) theContext.newRestfulGenericClient(getServerBase(theRequest, theConfig)); } + + retVal.registerInterceptor(new BufferResponseInterceptor()); retVal.setKeepResponses(true);