From 8354f4b565eed18346cfe443b71fe07d06ac9afc Mon Sep 17 00:00:00 2001 From: James Agnew Date: Fri, 5 Aug 2016 18:39:44 -0400 Subject: [PATCH] Use correct accept header --- .../fhir/rest/server/RestfulServerUtils.java | 2 +- hapi-fhir-client-okhttp/.classpath | 32 +++++++++++++++++++ hapi-fhir-client-okhttp/.project | 31 ++++++++++++++++++ .../org.eclipse.core.resources.prefs | 2 ++ .../.settings/org.eclipse.m2e.core.prefs | 4 +++ .../okhttp/client/OkHttpRestfulClient.java | 15 ++------- .../rest/client/GenericClientDstu3Test.java | 2 ++ 7 files changed, 74 insertions(+), 14 deletions(-) create mode 100644 hapi-fhir-client-okhttp/.classpath create mode 100644 hapi-fhir-client-okhttp/.project create mode 100644 hapi-fhir-client-okhttp/.settings/org.eclipse.core.resources.prefs create mode 100644 hapi-fhir-client-okhttp/.settings/org.eclipse.m2e.core.prefs diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/server/RestfulServerUtils.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/server/RestfulServerUtils.java index 235fec59a62..cf84a81940c 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/server/RestfulServerUtils.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/server/RestfulServerUtils.java @@ -779,7 +779,7 @@ public class RestfulServerUtils { public static void addAcceptHeaderToRequest(EncodingEnum theEncoding, IHttpRequest theHttpRequest, FhirContext theContext) { if (theEncoding == null) { - if (!theContext.getVersion().getVersion().isNewerThan(FhirVersionEnum.DSTU2)) { + if (theContext.getVersion().getVersion().isNewerThan(FhirVersionEnum.DSTU2) == false) { theHttpRequest.addHeader(Constants.HEADER_ACCEPT, Constants.HEADER_ACCEPT_VALUE_XML_OR_JSON_LEGACY); } else { theHttpRequest.addHeader(Constants.HEADER_ACCEPT, Constants.HEADER_ACCEPT_VALUE_XML_OR_JSON_NON_LEGACY); diff --git a/hapi-fhir-client-okhttp/.classpath b/hapi-fhir-client-okhttp/.classpath new file mode 100644 index 00000000000..a5f3e22af20 --- /dev/null +++ b/hapi-fhir-client-okhttp/.classpath @@ -0,0 +1,32 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/hapi-fhir-client-okhttp/.project b/hapi-fhir-client-okhttp/.project new file mode 100644 index 00000000000..43baea7b073 --- /dev/null +++ b/hapi-fhir-client-okhttp/.project @@ -0,0 +1,31 @@ + + + hapi-fhir-client-okhttp + NO_M2ECLIPSE_SUPPORT: Project files created with the maven-eclipse-plugin are not supported in M2Eclipse. + + + + + org.eclipse.wst.common.project.facet.core.builder + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.jem.workbench.JavaEMFNature + org.eclipse.wst.common.modulecore.ModuleCoreNature + org.eclipse.m2e.core.maven2Nature + org.eclipse.jdt.core.javanature + org.eclipse.wst.common.project.facet.core.nature + + diff --git a/hapi-fhir-client-okhttp/.settings/org.eclipse.core.resources.prefs b/hapi-fhir-client-okhttp/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 00000000000..99f26c0203a --- /dev/null +++ b/hapi-fhir-client-okhttp/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +encoding/=UTF-8 diff --git a/hapi-fhir-client-okhttp/.settings/org.eclipse.m2e.core.prefs b/hapi-fhir-client-okhttp/.settings/org.eclipse.m2e.core.prefs new file mode 100644 index 00000000000..f897a7f1cb2 --- /dev/null +++ b/hapi-fhir-client-okhttp/.settings/org.eclipse.m2e.core.prefs @@ -0,0 +1,4 @@ +activeProfiles= +eclipse.preferences.version=1 +resolveWorkspaceProjects=true +version=1 diff --git a/hapi-fhir-client-okhttp/src/main/java/ca/uhn/fhir/okhttp/client/OkHttpRestfulClient.java b/hapi-fhir-client-okhttp/src/main/java/ca/uhn/fhir/okhttp/client/OkHttpRestfulClient.java index 0d469f72527..7862eaa8ae9 100644 --- a/hapi-fhir-client-okhttp/src/main/java/ca/uhn/fhir/okhttp/client/OkHttpRestfulClient.java +++ b/hapi-fhir-client-okhttp/src/main/java/ca/uhn/fhir/okhttp/client/OkHttpRestfulClient.java @@ -28,6 +28,7 @@ import ca.uhn.fhir.rest.client.api.IHttpClient; import ca.uhn.fhir.rest.client.api.IHttpRequest; import ca.uhn.fhir.rest.server.Constants; import ca.uhn.fhir.rest.server.EncodingEnum; +import ca.uhn.fhir.rest.server.RestfulServerUtils; import okhttp3.*; import okhttp3.internal.Version; import org.hl7.fhir.instance.model.api.IBaseBinary; @@ -125,7 +126,7 @@ public class OkHttpRestfulClient implements IHttpClient { addUserAgentHeader(theHttpRequest, theContext); addAcceptCharsetHeader(theHttpRequest); - addAcceptHeader(theHttpRequest, theEncoding); + RestfulServerUtils.addAcceptHeaderToRequest(theEncoding, theHttpRequest, theContext); addIfNoneExistHeader(theHttpRequest); } @@ -137,18 +138,6 @@ public class OkHttpRestfulClient implements IHttpClient { theHttpRequest.addHeader("Accept-Charset", "utf-8"); } - private void addAcceptHeader(OkHttpRestfulRequest theHttpRequest, EncodingEnum theEncoding) { - Request.Builder builder = theHttpRequest.getRequest(); - - if (theEncoding == null) { - builder.addHeader(Constants.HEADER_ACCEPT, Constants.HEADER_ACCEPT_VALUE_XML_OR_JSON_LEGACY); - } else if (theEncoding == EncodingEnum.JSON) { - builder.addHeader(Constants.HEADER_ACCEPT, Constants.CT_FHIR_JSON); - } else if (theEncoding == EncodingEnum.XML) { - builder.addHeader(Constants.HEADER_ACCEPT, Constants.CT_FHIR_XML); - } - } - private void addIfNoneExistHeader(IHttpRequest result) { if (myIfNoneExistParams != null) { addIfNoneExistHeaderFromParams(result, myIfNoneExistParams); diff --git a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/client/GenericClientDstu3Test.java b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/client/GenericClientDstu3Test.java index 9745148593f..02f654c769c 100644 --- a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/client/GenericClientDstu3Test.java +++ b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/client/GenericClientDstu3Test.java @@ -396,6 +396,8 @@ public class GenericClientDstu3Test { assertEquals(myAnswerCount, capt.getAllValues().size()); assertEquals("http://example.com/fhir/Patient", capt.getAllValues().get(0).getURI().toASCIIString()); + assertEquals(Constants.CT_FHIR_XML, capt.getAllValues().get(0).getFirstHeader("content-type").getValue().replaceAll(";.*", "")); + assertEquals("http://foo.com/base/Patient/222/_history/3", capt.getAllValues().get(1).getURI().toASCIIString()); }