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

View File

@ -12,10 +12,12 @@ public interface IFor {
IFor include(Include theIncludeManagingorganization); 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; package ca.uhn.fhir.rest.gclient;
import ca.uhn.fhir.model.api.IResource;
public interface IQuery { public interface IQuery {
IFor forResource(String theResourceName); 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; package ca.uhn.fhir.rest.gclient;
public class StringParam { public class StringParam implements IParam {
private String myParamName; private String myParamName;
@ -12,6 +12,24 @@ public class StringParam {
return new StringExactly(); 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 class EqualsExactlyCriterion implements ICriterion, ICriterionInternal {
private String myValue; 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 PARAM_TAGS = "_tags";
public static final String CHARSET_UTF_8 = "UTF-8"; public static final String CHARSET_UTF_8 = "UTF-8";
public static final String PARAM_DELETE = "_delete"; 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 { static {
Map<String, EncodingEnum> valToEncoding = new HashMap<String, EncodingEnum>(); Map<String, EncodingEnum> valToEncoding = new HashMap<String, EncodingEnum>();

View File

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

View File

@ -1,12 +1,10 @@
package ca.uhn.fhir.rest.client; package ca.uhn.fhir.rest.client;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.*;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.*;
import static org.mockito.Mockito.when;
import java.io.StringReader; import java.io.StringReader;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.List;
import org.apache.commons.io.input.ReaderInputStream; import org.apache.commons.io.input.ReaderInputStream;
import org.apache.http.HttpResponse; 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.context.FhirContext;
import ca.uhn.fhir.model.api.Bundle; 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.resource.Patient;
import ca.uhn.fhir.model.dstu.valueset.QuantityCompararatorEnum;
import ca.uhn.fhir.rest.param.QualifiedDateParam;
import ca.uhn.fhir.rest.server.Constants; import ca.uhn.fhir.rest.server.Constants;
public class GenericClientTest { public class GenericClientTest {
@ -113,16 +108,18 @@ public class GenericClientTest {
//@formatter:off //@formatter:off
Bundle response = client.search() Bundle response = client.search()
.forResource("Patient") .forResource(Patient.class)
.json() .encodedJson()
.where(Patient.PARAM_BIRTHDATE.beforeOrEquals().day("2012-01-22")) .where(Patient.PARAM_BIRTHDATE.beforeOrEquals().day("2012-01-22"))
.and(Patient.PARAM_BIRTHDATE.after().day("2011-01-01")) .and(Patient.PARAM_BIRTHDATE.after().day("2011-01-01"))
.include(Patient.INCLUDE_MANAGINGORGANIZATION) .include(Patient.INCLUDE_MANAGINGORGANIZATION)
.sort().ascending(Patient.PARAM_BIRTHDATE) .sort().ascending(Patient.PARAM_BIRTHDATE)
.sort().descending(Patient.PARAM_NAME)
.limitTo(123)
.execute(); .execute();
//@formatter:on //@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());
} }