From b68d07b546ed3b1cc0ba3308e10c67e4a2e1a5d1 Mon Sep 17 00:00:00 2001 From: jamesagnew Date: Wed, 4 Mar 2015 08:44:31 -0500 Subject: [PATCH] Work on operation support in client --- .../java/example/GenericClientExample.java | 31 ++- examples/src/main/java/logback.xml | 16 ++ .../java/ca/uhn/fhir/parser/JsonParser.java | 7 +- .../java/ca/uhn/fhir/parser/ParserState.java | 10 +- .../java/ca/uhn/fhir/parser/XmlParser.java | 10 +- .../uhn/fhir/rest/client/GenericClient.java | 78 ++++++- .../uhn/fhir/rest/client/IGenericClient.java | 2 - .../ca/uhn/fhir/rest/gclient/IOperation.java | 2 +- .../fhir/rest/gclient/IOperationUnnamed.java | 8 + .../fhir/rest/gclient/IOperationUntyped.java | 9 + .../gclient/IOperationUntypedWithInput.java | 7 + .../BaseHttpClientInvocationWithContents.java | 18 +- ...turningMethodBindingWithResourceParam.java | 8 +- .../rest/method/HistoryMethodBinding.java | 208 +++++++++--------- .../rest/method/HttpPostClientInvocation.java | 3 +- .../rest/method/OperationMethodBinding.java | 31 +++ .../fhir/rest/method/ReadMethodBinding.java | 8 +- .../fhir/rest/server/RestfulServerUtils.java | 6 +- .../fhir/instance/model/api/IBaseBinary.java} | 12 +- .../instance/model/api/IBaseParameters.java | 7 + hapi-fhir-jpaserver-base/.classpath | 2 +- ....eclipse.wst.common.project.facet.core.xml | 2 +- .../uhn/fhir/model/dev/resource/Binary.java | 5 +- .../uhn/fhir/model/dstu/resource/Binary.java | 6 +- .../model/dstu2/ModelInstantiationTest.java | 4 +- .../rest/client/GenericClientTestDstu2.java | 36 ++- .../org/hl7/fhir/instance/model/Binary.java | 18 +- .../uhn/fhir/model/ModelInheritanceTest.java | 176 ++++++++------- .../src/main/resources/vm/resource.vm | 7 +- 29 files changed, 471 insertions(+), 266 deletions(-) create mode 100644 examples/src/main/java/logback.xml create mode 100644 hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/gclient/IOperationUnnamed.java create mode 100644 hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/gclient/IOperationUntyped.java create mode 100644 hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/gclient/IOperationUntypedWithInput.java create mode 100644 hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/OperationMethodBinding.java rename hapi-fhir-base/src/main/java/{ca/uhn/fhir/model/base/resource/BaseBinary.java => org/hl7/fhir/instance/model/api/IBaseBinary.java} (72%) create mode 100644 hapi-fhir-base/src/main/java/org/hl7/fhir/instance/model/api/IBaseParameters.java diff --git a/examples/src/main/java/example/GenericClientExample.java b/examples/src/main/java/example/GenericClientExample.java index 14e904ae0bc..e1795079b9c 100644 --- a/examples/src/main/java/example/GenericClientExample.java +++ b/examples/src/main/java/example/GenericClientExample.java @@ -3,6 +3,8 @@ package example; import java.util.ArrayList; import java.util.List; +import org.omg.Dynamic.Parameter; + import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.model.api.Bundle; import ca.uhn.fhir.model.api.IResource; @@ -12,11 +14,17 @@ import ca.uhn.fhir.model.dstu2.valueset.AdministrativeGenderEnum; import ca.uhn.fhir.model.dstu2.resource.Observation; import ca.uhn.fhir.model.dstu2.resource.OperationOutcome; import ca.uhn.fhir.model.dstu2.resource.Organization; +import ca.uhn.fhir.model.dstu2.resource.Parameters; import ca.uhn.fhir.model.dstu2.resource.Patient; +import ca.uhn.fhir.model.dstu2.resource.ValueSet; +import ca.uhn.fhir.model.primitive.CodeDt; +import ca.uhn.fhir.model.primitive.DateDt; import ca.uhn.fhir.model.primitive.IdDt; import ca.uhn.fhir.model.primitive.InstantDt; +import ca.uhn.fhir.model.primitive.UriDt; import ca.uhn.fhir.rest.api.MethodOutcome; import ca.uhn.fhir.rest.client.IGenericClient; +import ca.uhn.fhir.rest.client.interceptor.LoggingInterceptor; import ca.uhn.fhir.rest.method.SearchStyleEnum; import ca.uhn.fhir.rest.server.exceptions.PreconditionFailedException; @@ -359,7 +367,28 @@ public class GenericClientExample { } public static void main(String[] args) { - fluentSearch(); + operation(); + } + + @SuppressWarnings("unused") + private static void operation() { + // START SNIPPET: operation + // Create a client to talk to the HeathIntersections server + FhirContext ctx = FhirContext.forDstu2(); + IGenericClient client = ctx.newRestfulGenericClient("http://fhir-dev.healthintersections.com.au/open"); + client.registerInterceptor(new LoggingInterceptor(true)); + + Parameters inParams = new Parameters(); + inParams.addParameter().setName("start").setValue(new DateDt("2001-01-01")); + inParams.addParameter().setName("end").setValue(new DateDt("2015-03-01")); + + Parameters outParams = client + .operation() + .ofInstance(new IdDt("Patient", "1")) + .named("$everything") + .withParameters(inParams) + .execute(); + // END SNIPPET: operation } } diff --git a/examples/src/main/java/logback.xml b/examples/src/main/java/logback.xml new file mode 100644 index 00000000000..1c8de9da344 --- /dev/null +++ b/examples/src/main/java/logback.xml @@ -0,0 +1,16 @@ + + + + + INFO + + + %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} [%file:%line] %msg%n + + + + + + + + \ No newline at end of file diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/JsonParser.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/JsonParser.java index 2ef153675eb..f558ab39697 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/JsonParser.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/JsonParser.java @@ -51,11 +51,13 @@ import javax.json.stream.JsonParsingException; import ca.uhn.fhir.model.base.composite.BaseCodingDt; import ca.uhn.fhir.model.primitive.*; + import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.Validate; import org.hl7.fhir.instance.model.IBase; import org.hl7.fhir.instance.model.IBaseResource; import org.hl7.fhir.instance.model.IPrimitiveType; +import org.hl7.fhir.instance.model.api.IBaseBinary; import org.hl7.fhir.instance.model.api.IAnyResource; import org.hl7.fhir.instance.model.api.IBaseBooleanDatatype; import org.hl7.fhir.instance.model.api.IBaseDatatype; @@ -92,7 +94,6 @@ import ca.uhn.fhir.model.api.annotation.Child; import ca.uhn.fhir.model.base.composite.BaseContainedDt; import ca.uhn.fhir.model.base.composite.BaseNarrativeDt; import ca.uhn.fhir.model.base.composite.BaseResourceReferenceDt; -import ca.uhn.fhir.model.base.resource.BaseBinary; import ca.uhn.fhir.model.primitive.DecimalDt; import ca.uhn.fhir.model.primitive.IdDt; import ca.uhn.fhir.model.primitive.IntegerDt; @@ -693,8 +694,8 @@ public class JsonParser extends BaseParser implements IParser { } } - if (theResource instanceof BaseBinary) { - BaseBinary bin = (BaseBinary) theResource; + if (theResource instanceof IBaseBinary) { + IBaseBinary bin = (IBaseBinary) theResource; theEventWriter.write("contentType", bin.getContentType()); theEventWriter.write("content", bin.getContentAsBase64()); } else { diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/ParserState.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/ParserState.java index 7c3c8aeaed9..d8141f62bbe 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/ParserState.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/ParserState.java @@ -38,6 +38,7 @@ import org.hl7.fhir.instance.model.IBase; import org.hl7.fhir.instance.model.IBaseResource; import org.hl7.fhir.instance.model.ICompositeType; import org.hl7.fhir.instance.model.IPrimitiveType; +import org.hl7.fhir.instance.model.api.IBaseBinary; import org.hl7.fhir.instance.model.api.IAnyResource; import org.hl7.fhir.instance.model.api.IBaseDatatype; import org.hl7.fhir.instance.model.api.IBaseElement; @@ -74,7 +75,6 @@ import ca.uhn.fhir.model.api.ResourceMetadataKeyEnum; import ca.uhn.fhir.model.api.Tag; import ca.uhn.fhir.model.api.TagList; import ca.uhn.fhir.model.base.composite.BaseResourceReferenceDt; -import ca.uhn.fhir.model.base.resource.BaseBinary; import ca.uhn.fhir.model.base.resource.ResourceMetadataMap; import ca.uhn.fhir.model.primitive.IdDt; import ca.uhn.fhir.model.primitive.InstantDt; @@ -886,10 +886,10 @@ class ParserState { private static final int SUBSTATE_CONTENT = 2; private static final int SUBSTATE_CT = 1; private String myData; - private BaseBinary myInstance; + private IBaseBinary myInstance; private int mySubState = 0; - public BinaryResourceStateForDstu1(PreResourceState thePreResourceState, BaseBinary theInstance) { + public BinaryResourceStateForDstu1(PreResourceState thePreResourceState, IBaseBinary theInstance) { super(thePreResourceState); myInstance = theInstance; } @@ -900,7 +900,7 @@ class ParserState { if (myInstance instanceof IIdentifiableElement) { ((IIdentifiableElement) myInstance).setElementSpecificId((theValue)); } else { - (myInstance).setId(new IdDt(theValue)); + ((IResource)myInstance).setId(new IdDt(theValue)); } } else if ("contentType".equals(theName)) { myInstance.setContentType(theValue); @@ -1923,7 +1923,7 @@ class ParserState { String resourceName = def.getName(); if ("Binary".equals(resourceName) && myContext.getVersion().getVersion() == FhirVersionEnum.DSTU1) { - push(new BinaryResourceStateForDstu1(getRootPreResourceState(), (BaseBinary) myInstance)); + push(new BinaryResourceStateForDstu1(getRootPreResourceState(), (IBaseBinary) myInstance)); } else if (myInstance instanceof IResource) { push(new ResourceStateHapi(getRootPreResourceState(), def, (IResource) myInstance)); } else { diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/XmlParser.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/XmlParser.java index 70522e9d753..0805d7e5928 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/XmlParser.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/XmlParser.java @@ -49,6 +49,7 @@ import org.apache.commons.lang3.StringUtils; import org.hl7.fhir.instance.model.IBase; import org.hl7.fhir.instance.model.IBaseResource; import org.hl7.fhir.instance.model.IPrimitiveType; +import org.hl7.fhir.instance.model.api.IBaseBinary; import org.hl7.fhir.instance.model.api.IAnyResource; import org.hl7.fhir.instance.model.api.IBaseDatatype; import org.hl7.fhir.instance.model.api.IBaseExtension; @@ -79,7 +80,6 @@ import ca.uhn.fhir.model.base.composite.BaseCodingDt; import ca.uhn.fhir.model.base.composite.BaseContainedDt; import ca.uhn.fhir.model.base.composite.BaseNarrativeDt; import ca.uhn.fhir.model.base.composite.BaseResourceReferenceDt; -import ca.uhn.fhir.model.base.resource.BaseBinary; import ca.uhn.fhir.model.primitive.IdDt; import ca.uhn.fhir.model.primitive.InstantDt; import ca.uhn.fhir.model.primitive.StringDt; @@ -766,8 +766,8 @@ public class XmlParser extends BaseParser implements IParser { theEventWriter.writeEndElement(); } - if (theResource instanceof BaseBinary) { - BaseBinary bin = (BaseBinary) theResource; + if (theResource instanceof IBaseBinary) { + IBaseBinary bin = (IBaseBinary) theResource; writeOptionalTagWithValue(theEventWriter, "contentType", bin.getContentType()); writeOptionalTagWithValue(theEventWriter, "content", bin.getContentAsBase64()); } else { @@ -781,8 +781,8 @@ public class XmlParser extends BaseParser implements IParser { theEventWriter.writeAttribute("id", theResourceId); } - if (theResource instanceof BaseBinary) { - BaseBinary bin = (BaseBinary) theResource; + if (theResource instanceof IBaseBinary) { + IBaseBinary bin = (IBaseBinary) theResource; if (bin.getContentType() != null) { theEventWriter.writeAttribute("contentType", bin.getContentType()); } diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/client/GenericClient.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/client/GenericClient.java index b6ed2c1aab0..5606e311a87 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/client/GenericClient.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/client/GenericClient.java @@ -20,8 +20,7 @@ package ca.uhn.fhir.rest.client; * #L% */ -import static org.apache.commons.lang3.StringUtils.isBlank; -import static org.apache.commons.lang3.StringUtils.isNotBlank; +import static org.apache.commons.lang3.StringUtils.*; import java.io.IOException; import java.io.Reader; @@ -39,6 +38,7 @@ import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpRequestBase; import org.hl7.fhir.instance.model.IBaseResource; import org.hl7.fhir.instance.model.api.IBaseBundle; +import org.hl7.fhir.instance.model.api.IBaseParameters; import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.context.FhirVersionEnum; @@ -76,6 +76,9 @@ import ca.uhn.fhir.rest.gclient.IHistory; import ca.uhn.fhir.rest.gclient.IHistoryTyped; import ca.uhn.fhir.rest.gclient.IHistoryUntyped; import ca.uhn.fhir.rest.gclient.IOperation; +import ca.uhn.fhir.rest.gclient.IOperationUnnamed; +import ca.uhn.fhir.rest.gclient.IOperationUntyped; +import ca.uhn.fhir.rest.gclient.IOperationUntypedWithInput; import ca.uhn.fhir.rest.gclient.IParam; import ca.uhn.fhir.rest.gclient.IQuery; import ca.uhn.fhir.rest.gclient.IRead; @@ -95,9 +98,11 @@ import ca.uhn.fhir.rest.method.DeleteMethodBinding; import ca.uhn.fhir.rest.method.HistoryMethodBinding; import ca.uhn.fhir.rest.method.HttpDeleteClientInvocation; import ca.uhn.fhir.rest.method.HttpGetClientInvocation; +import ca.uhn.fhir.rest.method.HttpPostClientInvocation; import ca.uhn.fhir.rest.method.HttpSimpleGetClientInvocation; import ca.uhn.fhir.rest.method.IClientResponseHandler; import ca.uhn.fhir.rest.method.MethodUtil; +import ca.uhn.fhir.rest.method.OperationMethodBinding; import ca.uhn.fhir.rest.method.ReadMethodBinding; import ca.uhn.fhir.rest.method.SearchMethodBinding; import ca.uhn.fhir.rest.method.SearchStyleEnum; @@ -117,6 +122,72 @@ import ca.uhn.fhir.util.ICallable; */ public class GenericClient extends BaseClient implements IGenericClient { + @SuppressWarnings("rawtypes") + public class OperationInternal extends BaseClientExecutable implements IOperation, IOperationUnnamed, IOperationUntyped, IOperationUntypedWithInput { + + private IdDt myId; + private Class myType; + private IBaseParameters myParameters; + private String myOperationName; + + @Override + public IOperationUnnamed ofServer() { + return this; + } + + @Override + public IOperationUnnamed ofType(Class theResourceType) { + myType = theResourceType; + return this; + } + + @Override + public IOperationUnnamed ofInstance(IdDt theId) { + myId = theId; + return this; + } + + @SuppressWarnings({ "unchecked" }) + @Override + public IOperationUntypedWithInput withParameters(IBaseParameters theParameters) { + Validate.notNull(theParameters, "theParameters can not be null"); + myParameters = theParameters; + return this; + } + + @SuppressWarnings("unchecked") + @Override + public Object execute() { + String resourceName; + String id; + if (myType != null) { + resourceName = myContext.getResourceDefinition(myType).getName(); + id = null; + } else if (myId != null) { + resourceName = myId.getResourceType(); + id = myId.getIdPart(); + } else { + resourceName = null; + id = null; + } + + HttpPostClientInvocation invocation = OperationMethodBinding.createOperationInvocation(myContext, resourceName, id, myOperationName, myParameters); + + IClientResponseHandler handler; + handler = new ResourceResponseHandler(myParameters.getClass(), null); + + return invoke(null, handler, invocation); + } + + @Override + public IOperationUntyped named(String theName) { + Validate.notBlank(theName, "theName can not be null"); + myOperationName =theName; + return this; + } + + } + private static final String I18N_CANNOT_DETEMINE_RESOURCE_TYPE = "ca.uhn.fhir.rest.client.GenericClient.cannotDetermineResourceTypeFromUri"; private static final String I18N_INCOMPLETE_URI_FOR_READ = "ca.uhn.fhir.rest.client.GenericClient.incompleteUriForRead"; @@ -340,8 +411,7 @@ public class GenericClient extends BaseClient implements IGenericClient { @Override public IOperation operation() { - // TODO Auto-generated method stub - return null; + return new OperationInternal(); } @Override diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/client/IGenericClient.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/client/IGenericClient.java index cb34846e28b..1855f25dfa9 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/client/IGenericClient.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/client/IGenericClient.java @@ -34,9 +34,7 @@ import ca.uhn.fhir.model.primitive.IdDt; import ca.uhn.fhir.model.primitive.UriDt; import ca.uhn.fhir.rest.api.MethodOutcome; import ca.uhn.fhir.rest.client.api.IRestfulClient; -import ca.uhn.fhir.rest.gclient.IClientExecutable; import ca.uhn.fhir.rest.gclient.ICreate; -import ca.uhn.fhir.rest.gclient.ICreateTyped; import ca.uhn.fhir.rest.gclient.IDelete; import ca.uhn.fhir.rest.gclient.IGetPage; import ca.uhn.fhir.rest.gclient.IGetTags; diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/gclient/IOperation.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/gclient/IOperation.java index bf6c01d96ed..26f11238383 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/gclient/IOperation.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/gclient/IOperation.java @@ -1,6 +1,6 @@ package ca.uhn.fhir.rest.gclient; -public interface IOperation { +public interface IOperation extends IBaseOn { diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/gclient/IOperationUnnamed.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/gclient/IOperationUnnamed.java new file mode 100644 index 00000000000..189c8f6b45f --- /dev/null +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/gclient/IOperationUnnamed.java @@ -0,0 +1,8 @@ +package ca.uhn.fhir.rest.gclient; + + +public interface IOperationUnnamed { + + IOperationUntyped named(String theName); + +} diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/gclient/IOperationUntyped.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/gclient/IOperationUntyped.java new file mode 100644 index 00000000000..858bbfafcc6 --- /dev/null +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/gclient/IOperationUntyped.java @@ -0,0 +1,9 @@ +package ca.uhn.fhir.rest.gclient; + +import org.hl7.fhir.instance.model.api.IBaseParameters; + +public interface IOperationUntyped { + + IOperationUntypedWithInput withParameters(T theParameters); + +} diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/gclient/IOperationUntypedWithInput.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/gclient/IOperationUntypedWithInput.java new file mode 100644 index 00000000000..35601867b8d --- /dev/null +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/gclient/IOperationUntypedWithInput.java @@ -0,0 +1,7 @@ +package ca.uhn.fhir.rest.gclient; + +import org.hl7.fhir.instance.model.api.IBaseParameters; + +public interface IOperationUntypedWithInput extends IClientExecutable, T> { + +} diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/BaseHttpClientInvocationWithContents.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/BaseHttpClientInvocationWithContents.java index 3d8d527f8c8..376dde07cf4 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/BaseHttpClientInvocationWithContents.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/BaseHttpClientInvocationWithContents.java @@ -20,10 +20,6 @@ package ca.uhn.fhir.rest.method; * #L% */ -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.List; @@ -36,16 +32,14 @@ import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpRequestBase; import org.apache.http.entity.AbstractHttpEntity; import org.apache.http.entity.ByteArrayEntity; -import org.apache.http.entity.ContentType; -import org.apache.http.entity.StringEntity; import org.apache.http.message.BasicNameValuePair; import org.hl7.fhir.instance.model.IBaseResource; +import org.hl7.fhir.instance.model.api.IBaseBinary; import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.model.api.Bundle; import ca.uhn.fhir.model.api.IResource; import ca.uhn.fhir.model.api.TagList; -import ca.uhn.fhir.model.base.resource.BaseBinary; import ca.uhn.fhir.model.valueset.BundleTypeEnum; import ca.uhn.fhir.parser.DataFormatException; import ca.uhn.fhir.parser.IParser; @@ -69,7 +63,7 @@ abstract class BaseHttpClientInvocationWithContents extends BaseHttpClientInvoca private Map> myIfNoneExistParams; private String myIfNoneExistString; private Map> myParams; - private final IResource myResource; + private final IBaseResource myResource; private final List myResources; private final TagList myTagList; private final String myUrlPath; @@ -85,7 +79,7 @@ abstract class BaseHttpClientInvocationWithContents extends BaseHttpClientInvoca myBundleType = null; } - public BaseHttpClientInvocationWithContents(FhirContext theContext, IResource theResource, Map> theParams, String... theUrlPath) { + public BaseHttpClientInvocationWithContents(FhirContext theContext, IBaseResource theResource, Map> theParams, String... theUrlPath) { myContext = theContext; myResource = theResource; myTagList = null; @@ -98,7 +92,7 @@ abstract class BaseHttpClientInvocationWithContents extends BaseHttpClientInvoca myBundleType = null; } - public BaseHttpClientInvocationWithContents(FhirContext theContext, IResource theResource, String theUrlPath) { + public BaseHttpClientInvocationWithContents(FhirContext theContext, IBaseResource theResource, String theUrlPath) { super(); myContext = theContext; myResource = theResource; @@ -209,8 +203,8 @@ abstract class BaseHttpClientInvocationWithContents extends BaseHttpClientInvoca appendExtraParamsWithQuestionMark(theExtraParams, url, url.indexOf("?") == -1); - if (myResource != null && BaseBinary.class.isAssignableFrom(myResource.getClass())) { - BaseBinary binary = (BaseBinary) myResource; + if (myResource != null && IBaseBinary.class.isAssignableFrom(myResource.getClass())) { + IBaseBinary binary = (IBaseBinary) myResource; /* * Note: Be careful about changing which constructor we use for ByteArrayEntity, diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/BaseOutcomeReturningMethodBindingWithResourceParam.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/BaseOutcomeReturningMethodBindingWithResourceParam.java index f1f150babe3..f8244a18fec 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/BaseOutcomeReturningMethodBindingWithResourceParam.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/BaseOutcomeReturningMethodBindingWithResourceParam.java @@ -26,11 +26,11 @@ import java.lang.reflect.Modifier; import org.apache.commons.io.IOUtils; import org.hl7.fhir.instance.model.IBaseResource; +import org.hl7.fhir.instance.model.api.IBaseBinary; import ca.uhn.fhir.context.ConfigurationException; import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.model.api.IResource; -import ca.uhn.fhir.model.base.resource.BaseBinary; import ca.uhn.fhir.rest.annotation.ResourceParam; import ca.uhn.fhir.rest.client.BaseHttpClientInvocation; import ca.uhn.fhir.rest.param.ResourceParameter; @@ -64,7 +64,7 @@ abstract class BaseOutcomeReturningMethodBindingWithResourceParam extends BaseOu providerResourceType = ((IResourceProvider) theProvider).getResourceType(); } - if (BaseBinary.class.isAssignableFrom(providerResourceType)) { + if (IBaseBinary.class.isAssignableFrom(providerResourceType)) { myBinary = true; } @@ -99,11 +99,11 @@ abstract class BaseOutcomeReturningMethodBindingWithResourceParam extends BaseOu String ct = theRequest.getServletRequest().getHeader(Constants.HEADER_CONTENT_TYPE); byte[] contents = IOUtils.toByteArray(theRequest.getServletRequest().getInputStream()); - BaseBinary binary = (BaseBinary) getContext().getResourceDefinition("Binary").newInstance(); + IBaseBinary binary = (IBaseBinary) getContext().getResourceDefinition("Binary").newInstance(); binary.setContentType(ct); binary.setContent(contents); - return binary; + return (IResource) binary; } else { return super.parseIncomingServerResource(theRequest); } diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/HistoryMethodBinding.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/HistoryMethodBinding.java index 13ad5d3a5a6..3fef1a1e52e 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/HistoryMethodBinding.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/HistoryMethodBinding.java @@ -96,6 +96,11 @@ public class HistoryMethodBinding extends BaseResourceReturningMethodBinding { return myResourceOperationType; } + @Override + protected BundleTypeEnum getResponseBundleType() { + return BundleTypeEnum.HISTORY; + } + @Override public ReturnTypeEnum getReturnType() { return ReturnTypeEnum.BUNDLE; @@ -106,110 +111,6 @@ public class HistoryMethodBinding extends BaseResourceReturningMethodBinding { return mySystemOperationType; } - @Override - public BaseHttpClientInvocation invokeClient(Object[] theArgs) throws InternalErrorException { - IdDt id = null; - String resourceName = myResourceName; - if (myIdParamIndex != null) { - id = (IdDt) theArgs[myIdParamIndex]; - if (id == null || isBlank(id.getValue())) { - throw new NullPointerException("ID can not be null"); - } - } - - String historyId = id != null ? id.getIdPart() : null; - HttpGetClientInvocation retVal = createHistoryInvocation(resourceName, historyId, null, null); - - if (theArgs != null) { - for (int idx = 0; idx < theArgs.length; idx++) { - IParameter nextParam = getParameters().get(idx); - nextParam.translateClientArgumentIntoQueryArgument(getContext(), theArgs[idx], retVal.getParameters()); - } - } - - return retVal; - } - - @Override - protected BundleTypeEnum getResponseBundleType() { - return BundleTypeEnum.HISTORY; - } - - public static HttpGetClientInvocation createHistoryInvocation(String theResourceName, String theId, BaseDateTimeDt theSince, Integer theLimit) { - StringBuilder b = new StringBuilder(); - if (theResourceName != null) { - b.append(theResourceName); - if (isNotBlank(theId)) { - b.append('/'); - b.append(theId); - } - } - if (b.length() > 0) { - b.append('/'); - } - b.append(Constants.PARAM_HISTORY); - - boolean haveParam = false; - if (theSince != null && !theSince.isEmpty()) { - haveParam = true; - b.append('?').append(Constants.PARAM_SINCE).append('=').append(theSince.getValueAsString()); - } - if (theLimit != null) { - b.append(haveParam ? '&' : '?'); - b.append(Constants.PARAM_COUNT).append('=').append(theLimit); - } - - HttpGetClientInvocation retVal = new HttpGetClientInvocation(b.toString()); - return retVal; - } - - @Override - public IBundleProvider invokeServer(RequestDetails theRequest, Object[] theMethodParams) throws InvalidRequestException, InternalErrorException { - if (myIdParamIndex != null) { - theMethodParams[myIdParamIndex] = theRequest.getId(); - } - - Object response = invokeServerMethod(theMethodParams); - - final IBundleProvider resources = toResourceList(response); - - /* - * We wrap the response so we can verify that it has the ID and version set, - * as is the contract for history - */ - return new IBundleProvider() { - - @Override - public int size() { - return resources.size(); - } - - @Override - public List getResources(int theFromIndex, int theToIndex) { - List retVal = resources.getResources(theFromIndex, theToIndex); - int index = theFromIndex; - for (IResource nextResource : retVal) { - if (nextResource.getId() == null || isBlank(nextResource.getId().getIdPart())) { - throw new InternalErrorException("Server provided resource at index " + index + " with no ID set (using IResource#setId(IdDt))"); - } - if (isBlank(nextResource.getId().getVersionIdPart())) { - IdDt versionId = (IdDt) ResourceMetadataKeyEnum.VERSION_ID.get(nextResource); - if (versionId == null || versionId.isEmpty()) { - throw new InternalErrorException("Server provided resource at index " + index + " with no Version ID set (using IResource#setId(IdDt))"); - } - } - index++; - } - return retVal; - } - - @Override - public InstantDt getPublished() { - return resources.getPublished(); - } - }; - } - // ObjectUtils.equals is replaced by a JDK7 method.. @SuppressWarnings("deprecation") @Override @@ -238,6 +139,105 @@ public class HistoryMethodBinding extends BaseResourceReturningMethodBinding { return true; } + + @Override + public BaseHttpClientInvocation invokeClient(Object[] theArgs) throws InternalErrorException { + IdDt id = null; + String resourceName = myResourceName; + if (myIdParamIndex != null) { + id = (IdDt) theArgs[myIdParamIndex]; + if (id == null || isBlank(id.getValue())) { + throw new NullPointerException("ID can not be null"); + } + } + + String historyId = id != null ? id.getIdPart() : null; + HttpGetClientInvocation retVal = createHistoryInvocation(resourceName, historyId, null, null); + + if (theArgs != null) { + for (int idx = 0; idx < theArgs.length; idx++) { + IParameter nextParam = getParameters().get(idx); + nextParam.translateClientArgumentIntoQueryArgument(getContext(), theArgs[idx], retVal.getParameters()); + } + } + + return retVal; + } + + @Override + public IBundleProvider invokeServer(RequestDetails theRequest, Object[] theMethodParams) throws InvalidRequestException, InternalErrorException { + if (myIdParamIndex != null) { + theMethodParams[myIdParamIndex] = theRequest.getId(); + } + + Object response = invokeServerMethod(theMethodParams); + + final IBundleProvider resources = toResourceList(response); + + /* + * We wrap the response so we can verify that it has the ID and version set, + * as is the contract for history + */ + return new IBundleProvider() { + + @Override + public InstantDt getPublished() { + return resources.getPublished(); + } + + @Override + public List getResources(int theFromIndex, int theToIndex) { + List retVal = resources.getResources(theFromIndex, theToIndex); + int index = theFromIndex; + for (IResource nextResource : retVal) { + if (nextResource.getId() == null || isBlank(nextResource.getId().getIdPart())) { + throw new InternalErrorException("Server provided resource at index " + index + " with no ID set (using IResource#setId(IdDt))"); + } + if (isBlank(nextResource.getId().getVersionIdPart())) { + IdDt versionId = (IdDt) ResourceMetadataKeyEnum.VERSION_ID.get(nextResource); + if (versionId == null || versionId.isEmpty()) { + throw new InternalErrorException("Server provided resource at index " + index + " with no Version ID set (using IResource#setId(IdDt))"); + } + } + index++; + } + return retVal; + } + + @Override + public int size() { + return resources.size(); + } + }; + } + + public static HttpGetClientInvocation createHistoryInvocation(String theResourceName, String theId, BaseDateTimeDt theSince, Integer theLimit) { + StringBuilder b = new StringBuilder(); + if (theResourceName != null) { + b.append(theResourceName); + if (isNotBlank(theId)) { + b.append('/'); + b.append(theId); + } + } + if (b.length() > 0) { + b.append('/'); + } + b.append(Constants.PARAM_HISTORY); + + boolean haveParam = false; + if (theSince != null && !theSince.isEmpty()) { + haveParam = true; + b.append('?').append(Constants.PARAM_SINCE).append('=').append(theSince.getValueAsString()); + } + if (theLimit != null) { + b.append(haveParam ? '&' : '?'); + b.append(Constants.PARAM_COUNT).append('=').append(theLimit); + } + + HttpGetClientInvocation retVal = new HttpGetClientInvocation(b.toString()); + return retVal; + } private static Class toReturnType(Method theMethod, Object theProvider) { if (theProvider instanceof IResourceProvider) { diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/HttpPostClientInvocation.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/HttpPostClientInvocation.java index 8dcb990f43e..76863184c06 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/HttpPostClientInvocation.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/HttpPostClientInvocation.java @@ -25,6 +25,7 @@ import java.util.Map; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.AbstractHttpEntity; +import org.hl7.fhir.instance.model.IBaseResource; import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.model.api.Bundle; @@ -35,7 +36,7 @@ import ca.uhn.fhir.model.valueset.BundleTypeEnum; public class HttpPostClientInvocation extends BaseHttpClientInvocationWithContents { - public HttpPostClientInvocation(FhirContext theContext, IResource theResource, String theUrlExtension) { + public HttpPostClientInvocation(FhirContext theContext, IBaseResource theResource, String theUrlExtension) { super(theContext, theResource, theUrlExtension); } diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/OperationMethodBinding.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/OperationMethodBinding.java new file mode 100644 index 00000000000..9a36fd91b30 --- /dev/null +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/OperationMethodBinding.java @@ -0,0 +1,31 @@ +package ca.uhn.fhir.rest.method; + +import static org.apache.commons.lang3.StringUtils.*; + +import org.hl7.fhir.instance.model.api.IBaseParameters; + +import ca.uhn.fhir.context.FhirContext; + +public class OperationMethodBinding { + + public static HttpPostClientInvocation createOperationInvocation(FhirContext theContext, String theResourceName, String theId, String theOperationName, IBaseParameters theInput) { + StringBuilder b = new StringBuilder(); + if (theResourceName != null) { + b.append(theResourceName); + if (isNotBlank(theId)) { + b.append('/'); + b.append(theId); + } + } + if (b.length() > 0) { + b.append('/'); + } + if (!theOperationName.startsWith("$")) { + b.append("$"); + } + b.append(theOperationName); + + return new HttpPostClientInvocation(theContext, theInput, b.toString()); + } + +} diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/ReadMethodBinding.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/ReadMethodBinding.java index bbf8a813530..9055a7ef732 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/ReadMethodBinding.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/ReadMethodBinding.java @@ -32,12 +32,12 @@ import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.Validate; import org.hl7.fhir.instance.model.IBaseResource; +import org.hl7.fhir.instance.model.api.IBaseBinary; import ca.uhn.fhir.context.ConfigurationException; import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.model.api.Bundle; import ca.uhn.fhir.model.api.IResource; -import ca.uhn.fhir.model.base.resource.BaseBinary; import ca.uhn.fhir.model.dstu.valueset.RestfulOperationSystemEnum; import ca.uhn.fhir.model.dstu.valueset.RestfulOperationTypeEnum; import ca.uhn.fhir.model.primitive.IdDt; @@ -174,19 +174,19 @@ public class ReadMethodBinding extends BaseResourceReturningMethodBinding implem public Object invokeClient(String theResponseMimeType, InputStream theResponseReader, int theResponseStatusCode, Map> theHeaders) throws IOException, BaseServerResponseException { byte[] contents = IOUtils.toByteArray(theResponseReader); - BaseBinary resource = (BaseBinary) getContext().getResourceDefinition("Binary").newInstance(); + IBaseBinary resource = (IBaseBinary) getContext().getResourceDefinition("Binary").newInstance(); resource.setContentType(theResponseMimeType); resource.setContent(contents); switch (getMethodReturnType()) { case BUNDLE: - return Bundle.withSingleResource(resource); + return Bundle.withSingleResource((IResource) resource); case LIST_OF_RESOURCES: return Collections.singletonList(resource); case RESOURCE: return resource; case BUNDLE_PROVIDER: - return new SimpleBundleProvider(resource); + return new SimpleBundleProvider((IResource) resource); } throw new IllegalStateException("" + getMethodReturnType()); // should not happen 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 516c7700348..a43fad59d38 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 @@ -18,6 +18,7 @@ import javax.servlet.http.HttpServletResponse; import org.apache.commons.lang3.StringUtils; import org.apache.http.client.utils.DateUtils; +import org.hl7.fhir.instance.model.api.IBaseBinary; import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.context.RuntimeResourceDefinition; @@ -26,7 +27,6 @@ import ca.uhn.fhir.model.api.IResource; import ca.uhn.fhir.model.api.ResourceMetadataKeyEnum; import ca.uhn.fhir.model.api.Tag; import ca.uhn.fhir.model.api.TagList; -import ca.uhn.fhir.model.base.resource.BaseBinary; import ca.uhn.fhir.model.primitive.IdDt; import ca.uhn.fhir.model.primitive.InstantDt; import ca.uhn.fhir.parser.IParser; @@ -73,8 +73,8 @@ public class RestfulServerUtils { } } - if (theResource instanceof BaseBinary && theResponseEncoding == null) { - BaseBinary bin = (BaseBinary) theResource; + if (theResource instanceof IBaseBinary && theResponseEncoding == null) { + IBaseBinary bin = (IBaseBinary) theResource; if (isNotBlank(bin.getContentType())) { theHttpResponse.setContentType(bin.getContentType()); } else { diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/model/base/resource/BaseBinary.java b/hapi-fhir-base/src/main/java/org/hl7/fhir/instance/model/api/IBaseBinary.java similarity index 72% rename from hapi-fhir-base/src/main/java/ca/uhn/fhir/model/base/resource/BaseBinary.java rename to hapi-fhir-base/src/main/java/org/hl7/fhir/instance/model/api/IBaseBinary.java index 2625ede0dfd..481fd4b823c 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/model/base/resource/BaseBinary.java +++ b/hapi-fhir-base/src/main/java/org/hl7/fhir/instance/model/api/IBaseBinary.java @@ -1,4 +1,4 @@ -package ca.uhn.fhir.model.base.resource; +package org.hl7.fhir.instance.model.api; /* * #%L @@ -20,9 +20,9 @@ package ca.uhn.fhir.model.base.resource; * #L% */ -import ca.uhn.fhir.model.api.IResource; +import org.hl7.fhir.instance.model.IBaseResource; -public interface BaseBinary extends IResource { +public interface IBaseBinary extends IBaseResource { byte[] getContent(); @@ -30,9 +30,9 @@ public interface BaseBinary extends IResource { String getContentType(); - BaseBinary setContent(byte[] theContent); + IBaseBinary setContent(byte[] theContent); - BaseBinary setContentAsBase64(String theContent); + IBaseBinary setContentAsBase64(String theContent); - BaseBinary setContentType(String theContentType); + IBaseBinary setContentType(String theContentType); } diff --git a/hapi-fhir-base/src/main/java/org/hl7/fhir/instance/model/api/IBaseParameters.java b/hapi-fhir-base/src/main/java/org/hl7/fhir/instance/model/api/IBaseParameters.java new file mode 100644 index 00000000000..4a004f556c8 --- /dev/null +++ b/hapi-fhir-base/src/main/java/org/hl7/fhir/instance/model/api/IBaseParameters.java @@ -0,0 +1,7 @@ +package org.hl7.fhir.instance.model.api; + +import org.hl7.fhir.instance.model.IBaseResource; + +public interface IBaseParameters extends IBaseResource { + +} diff --git a/hapi-fhir-jpaserver-base/.classpath b/hapi-fhir-jpaserver-base/.classpath index f9148a1a7ac..9ebe1a6b158 100644 --- a/hapi-fhir-jpaserver-base/.classpath +++ b/hapi-fhir-jpaserver-base/.classpath @@ -27,7 +27,7 @@ - + diff --git a/hapi-fhir-jpaserver-base/.settings/org.eclipse.wst.common.project.facet.core.xml b/hapi-fhir-jpaserver-base/.settings/org.eclipse.wst.common.project.facet.core.xml index 4f92af543f1..5c9bd7532ab 100644 --- a/hapi-fhir-jpaserver-base/.settings/org.eclipse.wst.common.project.facet.core.xml +++ b/hapi-fhir-jpaserver-base/.settings/org.eclipse.wst.common.project.facet.core.xml @@ -1,5 +1,5 @@ - + diff --git a/hapi-fhir-structures-dev/src/main/java/ca/uhn/fhir/model/dev/resource/Binary.java b/hapi-fhir-structures-dev/src/main/java/ca/uhn/fhir/model/dev/resource/Binary.java index 0de2de69892..fafebcce5e4 100644 --- a/hapi-fhir-structures-dev/src/main/java/ca/uhn/fhir/model/dev/resource/Binary.java +++ b/hapi-fhir-structures-dev/src/main/java/ca/uhn/fhir/model/dev/resource/Binary.java @@ -23,17 +23,18 @@ package ca.uhn.fhir.model.dev.resource; import java.util.Collections; import java.util.List; +import org.hl7.fhir.instance.model.api.IBaseBinary; + import ca.uhn.fhir.context.FhirVersionEnum; import ca.uhn.fhir.model.api.IElement; import ca.uhn.fhir.model.api.annotation.Child; import ca.uhn.fhir.model.api.annotation.ResourceDef; -import ca.uhn.fhir.model.base.resource.BaseBinary; import ca.uhn.fhir.model.primitive.Base64BinaryDt; import ca.uhn.fhir.model.primitive.StringDt; import ca.uhn.fhir.util.ElementUtil; @ResourceDef(name = "Binary", profile = "http://hl7.org/fhir/profiles/Binary", id = "binary") -public class Binary extends BaseResource implements BaseBinary { +public class Binary extends BaseResource implements IBaseBinary { @Child(name = "content", order = 1) private Base64BinaryDt myContent = new Base64BinaryDt(); diff --git a/hapi-fhir-structures-dstu/src/main/java/ca/uhn/fhir/model/dstu/resource/Binary.java b/hapi-fhir-structures-dstu/src/main/java/ca/uhn/fhir/model/dstu/resource/Binary.java index 5952d174290..bf639b549c9 100644 --- a/hapi-fhir-structures-dstu/src/main/java/ca/uhn/fhir/model/dstu/resource/Binary.java +++ b/hapi-fhir-structures-dstu/src/main/java/ca/uhn/fhir/model/dstu/resource/Binary.java @@ -23,18 +23,18 @@ package ca.uhn.fhir.model.dstu.resource; import java.util.Collections; import java.util.List; +import org.hl7.fhir.instance.model.api.IBaseBinary; + import ca.uhn.fhir.context.FhirVersionEnum; import ca.uhn.fhir.model.api.IElement; -import ca.uhn.fhir.model.api.IResource; import ca.uhn.fhir.model.api.annotation.Child; import ca.uhn.fhir.model.api.annotation.ResourceDef; -import ca.uhn.fhir.model.base.resource.BaseBinary; import ca.uhn.fhir.model.primitive.Base64BinaryDt; import ca.uhn.fhir.model.primitive.StringDt; import ca.uhn.fhir.util.ElementUtil; @ResourceDef(name = "Binary", profile = "http://hl7.org/fhir/profiles/Binary", id = "binary") -public class Binary extends BaseResource implements BaseBinary { +public class Binary extends BaseResource implements IBaseBinary { @Child(name = "content", order = 1) private Base64BinaryDt myContent = new Base64BinaryDt(); diff --git a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/model/dstu2/ModelInstantiationTest.java b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/model/dstu2/ModelInstantiationTest.java index 2dd82298fd2..f68d1776deb 100644 --- a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/model/dstu2/ModelInstantiationTest.java +++ b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/model/dstu2/ModelInstantiationTest.java @@ -4,12 +4,12 @@ import static org.junit.Assert.*; import java.util.Properties; +import org.hl7.fhir.instance.model.api.IBaseBinary; import org.junit.Test; import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.context.RuntimeResourceDefinition; import ca.uhn.fhir.model.api.IResource; -import ca.uhn.fhir.model.base.resource.BaseBinary; import ca.uhn.fhir.model.dstu2.resource.Binary; public class ModelInstantiationTest { @@ -18,7 +18,7 @@ public class ModelInstantiationTest { @Test public void testBinaryIsBaseBinary() { - assertTrue(BaseBinary.class.isAssignableFrom(Binary.class)); + assertTrue(IBaseBinary.class.isAssignableFrom(Binary.class)); } @SuppressWarnings("unchecked") diff --git a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/client/GenericClientTestDstu2.java b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/client/GenericClientTestDstu2.java index 06590c5ac78..45a9035c4dc 100644 --- a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/client/GenericClientTestDstu2.java +++ b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/client/GenericClientTestDstu2.java @@ -38,6 +38,7 @@ import ca.uhn.fhir.model.dstu2.resource.Parameters; import ca.uhn.fhir.model.dstu2.resource.Patient; import ca.uhn.fhir.model.primitive.IdDt; import ca.uhn.fhir.model.primitive.StringDt; +import ca.uhn.fhir.parser.IParser; import ca.uhn.fhir.rest.server.Constants; import ca.uhn.fhir.rest.server.EncodingEnum; @@ -79,7 +80,7 @@ public class GenericClientTestDstu2 { //@formatter:on return msg; } - + @Test public void testHistory() throws Exception { @@ -93,7 +94,8 @@ public class GenericClientTestDstu2 { @Override public InputStream answer(InvocationOnMock theInvocation) throws Throwable { return new ReaderInputStream(new StringReader(msg), Charset.forName("UTF-8")); - }}); + } + }); IGenericClient client = ourCtx.newRestfulGenericClient("http://example.com/fhir"); @@ -121,7 +123,7 @@ public class GenericClientTestDstu2 { assertEquals("http://example.com/fhir/Patient/_history", capt.getAllValues().get(idx).getURI().toString()); assertEquals(1, response.getEntry().size()); idx++; - + //@formatter:off response = client .history() @@ -134,7 +136,6 @@ public class GenericClientTestDstu2 { idx++; } - @Test public void testSearchByString() throws Exception { String msg = "{\"resourceType\":\"Bundle\",\"id\":null,\"base\":\"http://localhost:57931/fhir/contextDev\",\"total\":1,\"link\":[{\"relation\":\"self\",\"url\":\"http://localhost:57931/fhir/contextDev/Patient?identifier=urn%3AMultiFhirVersionTest%7CtestSubmitPatient01&_format=json\"}],\"entry\":[{\"resource\":{\"resourceType\":\"Patient\",\"id\":\"1\",\"meta\":{\"versionId\":\"1\",\"lastUpdated\":\"2014-12-20T18:41:29.706-05:00\"},\"identifier\":[{\"system\":\"urn:MultiFhirVersionTest\",\"value\":\"testSubmitPatient01\"}]}}]}"; @@ -160,29 +161,42 @@ public class GenericClientTestDstu2 { } @Test - public void testOperationWithListOfParameters() throws Exception { + public void testOperationWithListOfParameterResponse() throws Exception { + IParser p = ourCtx.newXmlParser(); + Parameters inParams = new Parameters(); inParams.addParameter().setValue(new StringDt("STRINGVALIN1")); inParams.addParameter().setValue(new StringDt("STRINGVALIN2")); - String reqString = ourCtx.newXmlParser().encodeResourceToString(inParams); + String reqString = p.encodeResourceToString(inParams); Parameters outParams = new Parameters(); outParams.addParameter().setValue(new StringDt("STRINGVALOUT1")); outParams.addParameter().setValue(new StringDt("STRINGVALOUT2")); - final String respString = ourCtx.newXmlParser().encodeResourceToString(outParams); - + final String respString = p.encodeResourceToString(outParams); + ArgumentCaptor capt = ArgumentCaptor.forClass(HttpUriRequest.class); when(myHttpClient.execute(capt.capture())).thenReturn(myHttpResponse); when(myHttpResponse.getStatusLine()).thenReturn(new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), 200, "OK")); - when(myHttpResponse.getEntity().getContentType()).thenReturn(new BasicHeader("content-type", Constants.CT_FHIR_JSON + "; charset=UTF-8")); + when(myHttpResponse.getEntity().getContentType()).thenReturn(new BasicHeader("content-type", Constants.CT_FHIR_XML + "; charset=UTF-8")); when(myHttpResponse.getEntity().getContent()).thenAnswer(new Answer() { @Override public ReaderInputStream answer(InvocationOnMock theInvocation) throws Throwable { - return new ReaderInputStream(new StringReader(respString), Charset.forName("UTF-8")); }}); + return new ReaderInputStream(new StringReader(respString), Charset.forName("UTF-8")); + } + }); IGenericClient client = ourCtx.newRestfulGenericClient("http://example.com/fhir"); -// client.operation().onServer().withParameters(inParams); + int idx = 0; + + Parameters resp = client.operation().ofServer().named("$SOMEOPERATION").withParameters(inParams).execute(); + assertEquals(respString, p.encodeResourceToString(resp)); + assertEquals(1, capt.getAllValues().get(idx).getHeaders(Constants.HEADER_CONTENT_TYPE).length); + assertEquals(EncodingEnum.XML.getResourceContentType() + Constants.HEADER_SUFFIX_CT_UTF_8, capt.getAllValues().get(idx).getFirstHeader(Constants.HEADER_CONTENT_TYPE).getValue()); + assertEquals(extractBody(capt, idx), reqString); + assertEquals("POST", capt.getAllValues().get(idx).getRequestLine().getMethod()); + idx++; + } @Test diff --git a/hapi-fhir-structures-hl7org-dstu2/src/main/java/org/hl7/fhir/instance/model/Binary.java b/hapi-fhir-structures-hl7org-dstu2/src/main/java/org/hl7/fhir/instance/model/Binary.java index 718db72ffc7..0d3e4c858ab 100644 --- a/hapi-fhir-structures-hl7org-dstu2/src/main/java/org/hl7/fhir/instance/model/Binary.java +++ b/hapi-fhir-structures-hl7org-dstu2/src/main/java/org/hl7/fhir/instance/model/Binary.java @@ -39,11 +39,12 @@ import org.hl7.fhir.instance.model.annotations.SearchParamDefinition; import org.hl7.fhir.instance.model.annotations.Block; import org.hl7.fhir.instance.model.annotations.Child; import org.hl7.fhir.instance.model.annotations.Description; +import org.hl7.fhir.instance.model.api.IBaseBinary; /** * A binary resource can contain any content, whether text, image, pdf, zip archive, etc. */ @ResourceDef(name="Binary", profile="http://hl7.org/fhir/Profile/Binary") -public class Binary extends Resource { +public class Binary extends Resource implements IBaseBinary { /** * MimeType of the binary content represented as a standard MimeType (BCP 13). @@ -212,5 +213,20 @@ public class Binary extends Resource { @SearchParamDefinition(name="contenttype", path="Binary.contentType", description="MimeType of the binary content", type="token" ) public static final String SP_CONTENTTYPE = "contenttype"; +@Override +public String getContentAsBase64() { + return getContentElement().getValueAsString(); +} + +@Override +public Binary setContentAsBase64(String theContent) { + if (theContent != null) { + getContentElement().setValueAsString(theContent); + } else { + setContent(null); + } + return this; +} + } diff --git a/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/model/ModelInheritanceTest.java b/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/model/ModelInheritanceTest.java index 32b078bbb7d..85340e98add 100644 --- a/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/model/ModelInheritanceTest.java +++ b/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/model/ModelInheritanceTest.java @@ -1,12 +1,11 @@ package ca.uhn.fhir.model; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; +import static org.junit.Assert.*; import org.hl7.fhir.instance.model.Address; import org.hl7.fhir.instance.model.BackboneElement; import org.hl7.fhir.instance.model.Base; +import org.hl7.fhir.instance.model.Binary; import org.hl7.fhir.instance.model.BooleanType; import org.hl7.fhir.instance.model.Bundle; import org.hl7.fhir.instance.model.Coding; @@ -19,6 +18,7 @@ import org.hl7.fhir.instance.model.ICompositeType; import org.hl7.fhir.instance.model.IPrimitiveType; import org.hl7.fhir.instance.model.IdType; import org.hl7.fhir.instance.model.Identifier; +import org.hl7.fhir.instance.model.Identifier.IdentifierUseEnumFactory; import org.hl7.fhir.instance.model.IntegerType; import org.hl7.fhir.instance.model.Meta; import org.hl7.fhir.instance.model.Narrative; @@ -27,11 +27,11 @@ import org.hl7.fhir.instance.model.Reference; import org.hl7.fhir.instance.model.Resource; import org.hl7.fhir.instance.model.Timing; import org.hl7.fhir.instance.model.Type; -import org.hl7.fhir.instance.model.Identifier.IdentifierUseEnumFactory; import org.hl7.fhir.instance.model.annotations.Block; import org.hl7.fhir.instance.model.annotations.Child; import org.hl7.fhir.instance.model.api.IAnyResource; import org.hl7.fhir.instance.model.api.IBackboneElement; +import org.hl7.fhir.instance.model.api.IBaseBinary; import org.hl7.fhir.instance.model.api.IBaseBooleanDatatype; import org.hl7.fhir.instance.model.api.IBaseBundle; import org.hl7.fhir.instance.model.api.IBaseDatatype; @@ -49,8 +49,7 @@ import org.hl7.fhir.instance.model.api.IReference; import org.junit.Test; public class ModelInheritanceTest { - - /** + /* *
      * Other changes:
      *
@@ -71,11 +70,6 @@ public class ModelInheritanceTest {
      * 
*/ - @Test - public void testType() { - assertTrue(IBaseDatatype.class.isAssignableFrom(Type.class)); - } - /** * This one should apply to all composite types */ @@ -84,76 +78,6 @@ public class ModelInheritanceTest { assertTrue(ICompositeType.class.isAssignableFrom(Address.class)); } - @Test - public void testBase() { - assertTrue(IBase.class.isAssignableFrom(Base.class)); - } - - public void testIdentifierUse() throws Exception { - Child child = Identifier.class.getField("use").getAnnotation(Child.class); - assertEquals(IdentifierUseEnumFactory.class, child.enumFactory()); - } - - - /** - * Should be "implements IBaseExtension" - */ - @Test - public void testExtension() { - assertTrue(IBaseExtension.class.isAssignableFrom(Extension.class)); - assertTrue(IBaseHasExtensions.class.isAssignableFrom(Extension.class)); - } - - @Test - public void testNarrative() { - assertTrue(INarrative.class.isAssignableFrom(Narrative.class)); - } - - @Test - public void testBooleanType() { - assertTrue(IBaseBooleanDatatype.class.isAssignableFrom(BooleanType.class)); - } - - @Test - public void testDecimalType() { - assertTrue(IBaseDecimalDatatype.class.isAssignableFrom(DecimalType.class)); - } - - @Test - public void testIntegerType() { - assertTrue(IBaseIntegerDatatype.class.isAssignableFrom(IntegerType.class)); - } - - @Test - public void testPrimitiveType() { - assertTrue(IPrimitiveType.class.isAssignableFrom(PrimitiveType.class)); - assertTrue(IBaseHasExtensions.class.isAssignableFrom(PrimitiveType.class)); - } - - @Test - public void testResource() { - assertTrue(IAnyResource.class.isAssignableFrom(Resource.class)); - } - - @Test - public void testBundle() { - assertTrue(IBaseBundle.class.isAssignableFrom(Bundle.class)); - } - - public void testIdType() { - assertTrue(IIdType.class.isAssignableFrom(IdType.class)); - } - - @Test - public void testReference() { - assertTrue(IReference.class.isAssignableFrom(Reference.class)); - } - - @Test - public void testMeta() { - assertTrue(IMetaType.class.isAssignableFrom(Meta.class)); - } - @Test public void testBackboneElement() { assertTrue(IBackboneElement.class.isAssignableFrom(BackboneElement.class)); @@ -162,8 +86,36 @@ public class ModelInheritanceTest { } @Test - public void testElement() { - assertTrue(IBaseHasExtensions.class.isAssignableFrom(Element.class)); + public void testBase() { + assertTrue(IBase.class.isAssignableFrom(Base.class)); + } + + + @Test + public void testBinary() { + assertTrue(IBaseBinary.class.isAssignableFrom(Binary.class)); + } + + + @Test + public void testBooleanType() { + assertTrue(IBaseBooleanDatatype.class.isAssignableFrom(BooleanType.class)); + } + + + @Test + public void testBundle() { + assertTrue(IBaseBundle.class.isAssignableFrom(Bundle.class)); + } + + @Test + public void testCoding() { + assertTrue(ICoding.class.isAssignableFrom(Coding.class)); + } + + @Test + public void testDecimalType() { + assertTrue(IBaseDecimalDatatype.class.isAssignableFrom(DecimalType.class)); } @Test @@ -173,8 +125,57 @@ public class ModelInheritanceTest { } @Test - public void testCoding() { - assertTrue(ICoding.class.isAssignableFrom(Coding.class)); + public void testElement() { + assertTrue(IBaseHasExtensions.class.isAssignableFrom(Element.class)); + } + + /** + * Should be "implements IBaseExtension" + */ + @Test + public void testExtension() { + assertTrue(IBaseExtension.class.isAssignableFrom(Extension.class)); + assertTrue(IBaseHasExtensions.class.isAssignableFrom(Extension.class)); + } + + public void testIdentifierUse() throws Exception { + Child child = Identifier.class.getField("use").getAnnotation(Child.class); + assertEquals(IdentifierUseEnumFactory.class, child.enumFactory()); + } + + public void testIdType() { + assertTrue(IIdType.class.isAssignableFrom(IdType.class)); + } + + @Test + public void testIntegerType() { + assertTrue(IBaseIntegerDatatype.class.isAssignableFrom(IntegerType.class)); + } + + @Test + public void testMeta() { + assertTrue(IMetaType.class.isAssignableFrom(Meta.class)); + } + + @Test + public void testNarrative() { + assertTrue(INarrative.class.isAssignableFrom(Narrative.class)); + } + + @Test + public void testPrimitiveType() { + assertTrue(IPrimitiveType.class.isAssignableFrom(PrimitiveType.class)); + assertTrue(IBaseHasExtensions.class.isAssignableFrom(PrimitiveType.class)); + } + + @Test + public void testReference() { + assertTrue(IReference.class.isAssignableFrom(Reference.class)); + } + + @Test + public void testResource() { + assertTrue(IAnyResource.class.isAssignableFrom(Resource.class)); } @Test @@ -183,4 +184,9 @@ public class ModelInheritanceTest { assertNotNull(Timing.TimingRepeatComponent.class.getAnnotation(Block.class)); } + @Test + public void testType() { + assertTrue(IBaseDatatype.class.isAssignableFrom(Type.class)); + } + } diff --git a/hapi-tinder-plugin/src/main/resources/vm/resource.vm b/hapi-tinder-plugin/src/main/resources/vm/resource.vm index 6daa7a427a5..888601d81b7 100644 --- a/hapi-tinder-plugin/src/main/resources/vm/resource.vm +++ b/hapi-tinder-plugin/src/main/resources/vm/resource.vm @@ -41,11 +41,8 @@ import ${import}; @ResourceDef(name="${elementName}", profile="${profile}", id="${id}") public class ${className} extends ca.uhn.fhir.model.${version}.resource.BaseResource implements #{if}( ${className}=="OperationOutcome" || ${className}=="Conformance" || ${className}=="SecurityEvent" ) ca.uhn.fhir.model.base.resource.Base${className} #{else} IResource #{end} -#if ( ${className} == "Bundle" ) - , org.hl7.fhir.instance.model.api.IBaseBundle -#end -#if ( ${className}=="Binary" ) - , ca.uhn.fhir.model.base.resource.Base${className} +#if ( ${className} == "Bundle" || ${className} == "Parameters" || ${className} == "Binary" ) + , org.hl7.fhir.instance.model.api.IBase${className} #end {