Get fluent client working

This commit is contained in:
jamesagnew 2014-05-08 07:57:11 -04:00
parent de926434b8
commit 2b683b8f02
11 changed files with 292 additions and 175 deletions

View File

@ -267,7 +267,7 @@ public class Patient extends BaseResource implements IResource {
public static final DateParam PARAM_BIRTHDATE = new DateParam(SP_BIRTHDATE);
public static final Include INCLUDE_MANAGINGORGANIZATION = new Include("managingOrganization");
public static final Include INCLUDE_MANAGINGORGANIZATION = new Include("Patient.managingOrganization");
@Child(name="identifier", type=IdentifierDt.class, order=0, min=0, max=Child.MAX_UNLIMITED)

View File

@ -23,7 +23,6 @@ package ca.uhn.fhir.rest.client;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@ -41,11 +40,12 @@ import ca.uhn.fhir.model.dstu.resource.Conformance;
import ca.uhn.fhir.model.primitive.IdDt;
import ca.uhn.fhir.parser.IParser;
import ca.uhn.fhir.rest.api.MethodOutcome;
import ca.uhn.fhir.rest.gclient.DateParam;
import ca.uhn.fhir.rest.gclient.ICriterion;
import ca.uhn.fhir.rest.gclient.ICriterionInternal;
import ca.uhn.fhir.rest.gclient.IFor;
import ca.uhn.fhir.rest.gclient.IParam;
import ca.uhn.fhir.rest.gclient.IQuery;
import ca.uhn.fhir.rest.gclient.ISort;
import ca.uhn.fhir.rest.gclient.Include;
import ca.uhn.fhir.rest.method.BaseOutcomeReturningMethodBinding;
import ca.uhn.fhir.rest.method.ConformanceMethodBinding;
@ -57,12 +57,14 @@ import ca.uhn.fhir.rest.method.ReadMethodBinding;
import ca.uhn.fhir.rest.method.SearchMethodBinding;
import ca.uhn.fhir.rest.method.UpdateMethodBinding;
import ca.uhn.fhir.rest.method.ValidateMethodBinding;
import ca.uhn.fhir.rest.server.Constants;
import ca.uhn.fhir.rest.server.EncodingEnum;
import ca.uhn.fhir.rest.server.exceptions.BaseServerResponseException;
public class GenericClient extends BaseClient implements IGenericClient {
private FhirContext myContext;
private HttpRequestBase myLastRequest;
/**
@ -74,10 +76,94 @@ public class GenericClient extends BaseClient implements IGenericClient {
myContext = theContext;
}
@Override
public Conformance conformance() {
GetClientInvocation invocation = ConformanceMethodBinding.createConformanceInvocation();
if (isKeepResponses()) {
myLastRequest = invocation.asHttpRequest(getServerBase(), createExtraParams(), getEncoding());
}
IClientResponseHandler binding = new IClientResponseHandler() {
@Override
public Object invokeClient(String theResponseMimeType, Reader theResponseReader, int theResponseStatusCode, Map<String, List<String>> theHeaders) throws IOException, BaseServerResponseException {
EncodingEnum respType = EncodingEnum.forContentType(theResponseMimeType);
IParser parser = respType.newParser(myContext);
return parser.parseResource(Conformance.class, theResponseReader);
}
};
Conformance resp = (Conformance) invokeClient(binding, invocation);
return resp;
}
@Override
public MethodOutcome create(IResource theResource) {
BaseClientInvocation invocation = CreateMethodBinding.createCreateInvocation(theResource, myContext);
if (isKeepResponses()) {
myLastRequest = invocation.asHttpRequest(getServerBase(), createExtraParams(), getEncoding());
}
RuntimeResourceDefinition def = myContext.getResourceDefinition(theResource);
final String resourceName = def.getName();
IClientResponseHandler binding = new IClientResponseHandler() {
@Override
public Object invokeClient(String theResponseMimeType, Reader theResponseReader, int theResponseStatusCode, Map<String, List<String>> theHeaders) throws IOException, BaseServerResponseException {
MethodOutcome response = BaseOutcomeReturningMethodBinding.process2xxResponse(myContext, resourceName, theResponseStatusCode, theResponseMimeType, theResponseReader, theHeaders);
return response;
}
};
MethodOutcome resp = (MethodOutcome) invokeClient(binding, invocation);
return resp;
}
@Override
public MethodOutcome delete(final Class<? extends IResource> theType, IdDt theId) {
DeleteClientInvocation invocation = DeleteMethodBinding.createDeleteInvocation(toResourceName(theType), theId);
if (isKeepResponses()) {
myLastRequest = invocation.asHttpRequest(getServerBase(), createExtraParams(), getEncoding());
}
final String resourceName = myContext.getResourceDefinition(theType).getName();
IClientResponseHandler binding = new IClientResponseHandler() {
@Override
public Object invokeClient(String theResponseMimeType, Reader theResponseReader, int theResponseStatusCode, Map<String, List<String>> theHeaders) throws IOException, BaseServerResponseException {
MethodOutcome response = BaseOutcomeReturningMethodBinding.process2xxResponse(myContext, resourceName, theResponseStatusCode, theResponseMimeType, theResponseReader, theHeaders);
return response;
}
};
MethodOutcome resp = (MethodOutcome) invokeClient(binding, invocation);
return resp;
}
public HttpRequestBase getLastRequest() {
return myLastRequest;
}
@Override
public <T extends IResource> Bundle history(final Class<T> theType, IdDt theIdDt) {
GetClientInvocation invocation = HistoryMethodBinding.createHistoryInvocation(toResourceName(theType), theIdDt);
if (isKeepResponses()) {
myLastRequest = invocation.asHttpRequest(getServerBase(), createExtraParams(), getEncoding());
}
IClientResponseHandler binding = new IClientResponseHandler() {
@Override
public Object invokeClient(String theResponseMimeType, Reader theResponseReader, int theResponseStatusCode, Map<String, List<String>> theHeaders) throws IOException, BaseServerResponseException {
EncodingEnum respType = EncodingEnum.forContentType(theResponseMimeType);
IParser parser = respType.newParser(myContext);
return parser.parseBundle(theType, theResponseReader);
}
};
Bundle resp = (Bundle) invokeClient(binding, invocation);
return resp;
}
@Override
public <T extends IResource> T read(final Class<T> theType, IdDt theId) {
GetClientInvocation invocation = ReadMethodBinding.createReadInvocation(theId, toResourceName(theType));
@ -100,56 +186,8 @@ public class GenericClient extends BaseClient implements IGenericClient {
}
@Override
public MethodOutcome delete(final Class<? extends IResource> theType, IdDt theId) {
DeleteClientInvocation invocation = DeleteMethodBinding.createDeleteInvocation(toResourceName(theType), theId);
if (isKeepResponses()) {
myLastRequest = invocation.asHttpRequest(getServerBase(), createExtraParams(), getEncoding());
}
final String resourceName = myContext.getResourceDefinition(theType).getName();
IClientResponseHandler binding = new IClientResponseHandler() {
@Override
public Object invokeClient(String theResponseMimeType, Reader theResponseReader, int theResponseStatusCode, Map<String, List<String>> theHeaders) throws IOException, BaseServerResponseException {
MethodOutcome response = BaseOutcomeReturningMethodBinding.process2xxResponse(myContext, resourceName, theResponseStatusCode, theResponseMimeType, theResponseReader, theHeaders);
return response;
}
};
MethodOutcome resp = (MethodOutcome) invokeClient(binding, invocation);
return resp;
}
/**
* For now, this is a part of the internal API of HAPI - Use with caution as
* this method may change!
*/
public void setLastRequest(HttpRequestBase theLastRequest) {
myLastRequest = theLastRequest;
}
private String toResourceName(Class<? extends IResource> theType) {
return myContext.getResourceDefinition(theType).getName();
}
@Override
public <T extends IResource> T vread(final Class<T> theType, IdDt theId, IdDt theVersionId) {
GetClientInvocation invocation = ReadMethodBinding.createVReadInvocation(theId, theVersionId, toResourceName(theType));
if (isKeepResponses()) {
myLastRequest = invocation.asHttpRequest(getServerBase(), createExtraParams(), getEncoding());
}
IClientResponseHandler binding = new IClientResponseHandler() {
@Override
public Object invokeClient(String theResponseMimeType, Reader theResponseReader, int theResponseStatusCode, Map<String, List<String>> theHeaders) throws IOException, BaseServerResponseException {
EncodingEnum respType = EncodingEnum.forContentType(theResponseMimeType);
IParser parser = respType.newParser(myContext);
return parser.parseResource(theType, theResponseReader);
}
};
@SuppressWarnings("unchecked")
T resp = (T) invokeClient(binding, invocation);
return resp;
public IQuery search() {
return new QueryInternal();
}
@Override
@ -181,27 +219,12 @@ public class GenericClient extends BaseClient implements IGenericClient {
return resp;
}
@Override
public MethodOutcome create(IResource theResource) {
BaseClientInvocation invocation = CreateMethodBinding.createCreateInvocation(theResource, myContext);
if (isKeepResponses()) {
myLastRequest = invocation.asHttpRequest(getServerBase(), createExtraParams(), getEncoding());
}
RuntimeResourceDefinition def = myContext.getResourceDefinition(theResource);
final String resourceName = def.getName();
IClientResponseHandler binding = new IClientResponseHandler() {
@Override
public Object invokeClient(String theResponseMimeType, Reader theResponseReader, int theResponseStatusCode, Map<String, List<String>> theHeaders) throws IOException, BaseServerResponseException {
MethodOutcome response = BaseOutcomeReturningMethodBinding.process2xxResponse(myContext, resourceName, theResponseStatusCode, theResponseMimeType, theResponseReader, theHeaders);
return response;
}
};
MethodOutcome resp = (MethodOutcome) invokeClient(binding, invocation);
return resp;
/**
* For now, this is a part of the internal API of HAPI - Use with caution as
* this method may change!
*/
public void setLastRequest(HttpRequestBase theLastRequest) {
myLastRequest = theLastRequest;
}
@Override
@ -249,8 +272,8 @@ public class GenericClient extends BaseClient implements IGenericClient {
}
@Override
public <T extends IResource> Bundle history(final Class<T> theType, IdDt theIdDt) {
GetClientInvocation invocation = HistoryMethodBinding.createHistoryInvocation(toResourceName(theType), theIdDt);
public <T extends IResource> T vread(final Class<T> theType, IdDt theId, IdDt theVersionId) {
GetClientInvocation invocation = ReadMethodBinding.createVReadInvocation(theId, theVersionId, toResourceName(theType));
if (isKeepResponses()) {
myLastRequest = invocation.asHttpRequest(getServerBase(), createExtraParams(), getEncoding());
}
@ -260,57 +283,39 @@ public class GenericClient extends BaseClient implements IGenericClient {
public Object invokeClient(String theResponseMimeType, Reader theResponseReader, int theResponseStatusCode, Map<String, List<String>> theHeaders) throws IOException, BaseServerResponseException {
EncodingEnum respType = EncodingEnum.forContentType(theResponseMimeType);
IParser parser = respType.newParser(myContext);
return parser.parseBundle(theType, theResponseReader);
return parser.parseResource(theType, theResponseReader);
}
};
Bundle resp = (Bundle) invokeClient(binding, invocation);
return resp;
}
@Override
public Conformance conformance() {
GetClientInvocation invocation = ConformanceMethodBinding.createConformanceInvocation();
if (isKeepResponses()) {
myLastRequest = invocation.asHttpRequest(getServerBase(), createExtraParams(), getEncoding());
}
IClientResponseHandler binding = new IClientResponseHandler() {
@Override
public Object invokeClient(String theResponseMimeType, Reader theResponseReader, int theResponseStatusCode, Map<String, List<String>> theHeaders) throws IOException, BaseServerResponseException {
EncodingEnum respType = EncodingEnum.forContentType(theResponseMimeType);
IParser parser = respType.newParser(myContext);
return parser.parseResource(Conformance.class, theResponseReader);
}
};
Conformance resp = (Conformance) invokeClient(binding, invocation);
@SuppressWarnings("unchecked")
T resp = (T) invokeClient(binding, invocation);
return resp;
}
@Override
public IQuery search() {
return new QueryInternal();
private String toResourceName(Class<? extends IResource> theType) {
return myContext.getResourceDefinition(theType).getName();
}
public class ForInternal implements IFor {
private class ForInternal implements IFor {
private Class<? extends IResource> myResourceType;
private List<ICriterionInternal> myCriterion = new ArrayList<ICriterionInternal>();
private List<Include> myInclude = new ArrayList<Include>();
private EncodingEnum myParamEncoding;
private Integer myParamLimit;
private String myResourceName;
public ForInternal(String theResourceName) {
myResourceType = myContext.getResourceDefinition(theResourceName).getImplementingClass();
myResourceName = theResourceName;
}
private Class<? extends IResource> myResourceType;
private List<SortInternal> mySort = new ArrayList<SortInternal>();
public ForInternal(Class<? extends IResource> theResourceType) {
myResourceType = theResourceType;
myResourceName = myContext.getResourceDefinition(theResourceType).getName();
}
public ForInternal(String theResourceName) {
myResourceType = myContext.getResourceDefinition(theResourceName).getImplementingClass();
myResourceName = theResourceName;
}
@Override
public IFor and(ICriterion theCriterion) {
myCriterion.add((ICriterionInternal) theCriterion);
@ -318,20 +323,27 @@ public class GenericClient extends BaseClient implements IGenericClient {
}
@Override
public IFor where(ICriterion theCriterion) {
myCriterion.add((ICriterionInternal) theCriterion);
public IFor encodedJson() {
myParamEncoding = EncodingEnum.JSON;
return this;
}
@Override
public IFor encodedXml() {
myParamEncoding = EncodingEnum.XML;
return null;
}
@Override
public Bundle execute() {
StringBuilder b = new StringBuilder();
b.append(getServerBase());
b.append('/');
b.append(myResourceType);
b.append('?');
Map<String, List<String>> params=new HashMap<String, List<String>>();
Map<String, List<String>> params = new LinkedHashMap<String, List<String>>();
for (ICriterionInternal next : myCriterion) {
String parameterName = next.getParameterName();
String parameterValue = next.getParameterValue();
@ -339,7 +351,19 @@ public class GenericClient extends BaseClient implements IGenericClient {
}
for (Include next : myInclude) {
addParam(params, "_include", next.getInclude());
addParam(params, Constants.PARAM_INCLUDE, next.getInclude());
}
for (SortInternal next : mySort) {
addParam(params, next.getParamName(), next.getParamValue());
}
if (myParamEncoding != null) {
addParam(params, Constants.PARAM_FORMAT, myParamEncoding.getFormatContentType());
}
if (myParamLimit != null) {
addParam(params, Constants.PARAM_COUNT, Integer.toString(myParamLimit));
}
GetClientInvocation invocation = new GetClientInvocation(params, myResourceName);
@ -358,14 +382,7 @@ public class GenericClient extends BaseClient implements IGenericClient {
Bundle resp = (Bundle) invokeClient(binding, invocation);
return resp;
}
private void addParam(Map<String, List<String>> params, String parameterName, String parameterValue) {
if (!params.containsKey(parameterName)) {
params.put(parameterName, new ArrayList<String>());
}
params.get(parameterName).add(parameterValue);
}
@Override
@ -375,33 +392,90 @@ public class GenericClient extends BaseClient implements IGenericClient {
}
@Override
public IFor json() {
// TODO Auto-generated method stub
return null;
public IFor limitTo(int theLimitTo) {
if (theLimitTo > 0) {
myParamLimit = theLimitTo;
} else {
myParamLimit = null;
}
return this;
}
@Override
public IFor sort() {
// TODO Auto-generated method stub
return null;
public ISort sort() {
SortInternal retVal = new SortInternal(this);
mySort.add(retVal);
return retVal;
}
@Override
public IFor ascending(DateParam theParamBirthdate) {
// TODO Auto-generated method stub
return null;
public IFor where(ICriterion theCriterion) {
myCriterion.add((ICriterionInternal) theCriterion);
return this;
}
private void addParam(Map<String, List<String>> params, String parameterName, String parameterValue) {
if (!params.containsKey(parameterName)) {
params.put(parameterName, new ArrayList<String>());
}
params.get(parameterName).add(parameterValue);
}
}
public class QueryInternal implements IQuery {
private class QueryInternal implements IQuery {
@Override
public IFor forResource(String theResourceName) {
return new ForInternal(theResourceName);
}
@Override
public IFor forResource(Class<? extends IResource> theResourceType) {
return new ForInternal(theResourceType);
}
}
private class SortInternal implements ISort {
private ForInternal myFor;
private String myParamName;
private String myParamValue;
public SortInternal(ForInternal theFor) {
myFor = theFor;
}
@Override
public IFor ascending(IParam theParam) {
myParamName = Constants.PARAM_SORT_ASC;
myParamValue = theParam.getParamName();
return myFor;
}
@Override
public IFor defaultOrder(IParam theParam) {
myParamName = Constants.PARAM_SORT;
myParamValue = theParam.getParamName();
return myFor;
}
@Override
public IFor descending(IParam theParam) {
myParamName = Constants.PARAM_SORT_DESC;
myParamValue = theParam.getParamName();
return myFor;
}
public String getParamName() {
return myParamName;
}
public String getParamValue() {
return myParamValue;
}
}
}

View File

@ -5,10 +5,15 @@ import java.util.Date;
import ca.uhn.fhir.model.api.TemporalPrecisionEnum;
import ca.uhn.fhir.model.primitive.DateTimeDt;
public class DateParam {
public class DateParam implements IParam {
private String myParamName;
@Override
public String getParamName() {
return myParamName;
}
public DateParam(String theParamName) {
myParamName = theParamName;
}

View File

@ -12,10 +12,12 @@ public interface IFor {
IFor include(Include theIncludeManagingorganization);
IFor json();
IFor encodedJson();
IFor sort();
IFor encodedXml();
IFor ascending(DateParam theParamBirthdate);
ISort sort();
IFor limitTo(int theLimitTo);
}

View File

@ -0,0 +1,7 @@
package ca.uhn.fhir.rest.gclient;
public interface IParam {
String getParamName();
}

View File

@ -1,8 +1,12 @@
package ca.uhn.fhir.rest.gclient;
import ca.uhn.fhir.model.api.IResource;
public interface IQuery {
IFor forResource(String theResourceName);
IFor forResource(Class<? extends IResource> theClass);
}

View File

@ -0,0 +1,11 @@
package ca.uhn.fhir.rest.gclient;
public interface ISort {
IFor ascending(IParam theParam);
IFor defaultOrder(IParam theParam);
IFor descending(IParam theParam);
}

View File

@ -1,6 +1,6 @@
package ca.uhn.fhir.rest.gclient;
public class StringParam {
public class StringParam implements IParam {
private String myParamName;
@ -12,6 +12,24 @@ public class StringParam {
return new StringExactly();
}
@Override
public String getParamName() {
return myParamName;
}
public interface IStringExactly {
ICriterion value(String theValue);
}
public class StringExactly implements IStringExactly {
@Override
public ICriterion value(String theValue) {
return new EqualsExactlyCriterion(theValue);
}
}
private class EqualsExactlyCriterion implements ICriterion, ICriterionInternal {
private String myValue;
@ -32,17 +50,4 @@ public class StringParam {
}
public interface IStringExactly {
ICriterion value(String theValue);
}
public class StringExactly implements IStringExactly {
@Override
public ICriterion value(String theValue) {
return new EqualsExactlyCriterion(theValue);
}
}
}

View File

@ -71,6 +71,9 @@ public class Constants {
public static final String PARAM_TAGS = "_tags";
public static final String CHARSET_UTF_8 = "UTF-8";
public static final String PARAM_DELETE = "_delete";
public static final String FORMAT_XML = "xml";
public static final String FORMAT_JSON = "json";
public static final String PARAM_INCLUDE = "_include";
static {
Map<String, EncodingEnum> valToEncoding = new HashMap<String, EncodingEnum>();

View File

@ -27,55 +27,61 @@ import ca.uhn.fhir.parser.IParser;
public enum EncodingEnum {
XML(Constants.CT_FHIR_XML, Constants.CT_ATOM_XML, Constants.CT_XML) {
XML(Constants.CT_FHIR_XML, Constants.CT_ATOM_XML, Constants.CT_XML, Constants.FORMAT_XML) {
@Override
public IParser newParser(FhirContext theContext) {
return theContext.newXmlParser();
}
},
JSON(Constants.CT_FHIR_JSON, Constants.CT_FHIR_JSON, Constants.CT_JSON) {
JSON(Constants.CT_FHIR_JSON, Constants.CT_FHIR_JSON, Constants.CT_JSON, Constants.FORMAT_JSON) {
@Override
public IParser newParser(FhirContext theContext) {
return theContext.newJsonParser();
}
}
;
private static HashMap<String, EncodingEnum> ourContentTypeToEncoding;
static {
ourContentTypeToEncoding = new HashMap<String, EncodingEnum>();
for (EncodingEnum next: values()) {
for (EncodingEnum next : values()) {
ourContentTypeToEncoding.put(next.getBundleContentType(), next);
ourContentTypeToEncoding.put(next.getResourceContentType(), next);
ourContentTypeToEncoding.put(next.getBrowserFriendlyBundleContentType(), next);
}
/*
* These are wrong, but we add them just to be tolerant
* of other people's mistakes
* These are wrong, but we add them just to be tolerant of other
* people's mistakes
*/
ourContentTypeToEncoding.put("application/json", JSON);
ourContentTypeToEncoding.put("application/xml", XML);
ourContentTypeToEncoding.put("text/json", JSON);
ourContentTypeToEncoding.put("text/xml", XML);
}
private String myResourceContentType;
private String myBundleContentType;
private String myBrowserFriendlyContentType;
EncodingEnum(String theResourceContentType, String theBundleContentType, String theBrowserFriendlyContentType) {
private String myFormatContentType;
EncodingEnum(String theResourceContentType, String theBundleContentType, String theBrowserFriendlyContentType, String theFormatContentType) {
myResourceContentType = theResourceContentType;
myBundleContentType = theBundleContentType;
myBrowserFriendlyContentType = theBrowserFriendlyContentType;
myFormatContentType = theFormatContentType;
}
public String getRequestContentType() {
return myFormatContentType;
}
public abstract IParser newParser(FhirContext theContext);
public String getBundleContentType() {
return myBundleContentType;
}
@ -92,5 +98,8 @@ public enum EncodingEnum {
return ourContentTypeToEncoding.get(theContentType);
}
public String getFormatContentType() {
return myFormatContentType;
}
}

View File

@ -1,12 +1,10 @@
package ca.uhn.fhir.rest.client;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import java.io.StringReader;
import java.nio.charset.Charset;
import java.util.List;
import org.apache.commons.io.input.ReaderInputStream;
import org.apache.http.HttpResponse;
@ -22,10 +20,7 @@ import org.mockito.internal.stubbing.defaultanswers.ReturnsDeepStubs;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.model.api.Bundle;
import ca.uhn.fhir.model.dstu.resource.Conformance;
import ca.uhn.fhir.model.dstu.resource.Patient;
import ca.uhn.fhir.model.dstu.valueset.QuantityCompararatorEnum;
import ca.uhn.fhir.rest.param.QualifiedDateParam;
import ca.uhn.fhir.rest.server.Constants;
public class GenericClientTest {
@ -113,16 +108,18 @@ public class GenericClientTest {
//@formatter:off
Bundle response = client.search()
.forResource("Patient")
.json()
.forResource(Patient.class)
.encodedJson()
.where(Patient.PARAM_BIRTHDATE.beforeOrEquals().day("2012-01-22"))
.and(Patient.PARAM_BIRTHDATE.after().day("2011-01-01"))
.include(Patient.INCLUDE_MANAGINGORGANIZATION)
.sort().ascending(Patient.PARAM_BIRTHDATE)
.sort().descending(Patient.PARAM_NAME)
.limitTo(123)
.execute();
//@formatter:on
assertEquals("http://foo/Patient?birthdate=%3C%3D2012-01-22&birthdate=%3E2011-01-01&_include=Patient.managingOrganization", capt.getValue().getURI().toString());
assertEquals("http://foo/Patient?birthdate=%3C%3D2012-01-22&birthdate=%3E2011-01-01&_include=Patient.managingOrganization&_sort%3Aasc=birthdate&_sort%3Adesc=name&_format=json&_count=123", capt.getValue().getURI().toString());
}