Add setEncoding and setPrettyPrint methods to generic client, and add

documentation for #113
This commit is contained in:
James Agnew 2015-03-19 12:16:04 +01:00
parent 95d5503a9a
commit 6020368162
11 changed files with 101 additions and 40 deletions

View File

@ -50,6 +50,7 @@ import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.model.base.resource.BaseOperationOutcome;
import ca.uhn.fhir.parser.DataFormatException;
import ca.uhn.fhir.parser.IParser;
import ca.uhn.fhir.rest.client.api.IRestfulClient;
import ca.uhn.fhir.rest.client.exceptions.FhirClientConnectionException;
import ca.uhn.fhir.rest.method.IClientResponseHandler;
import ca.uhn.fhir.rest.method.IClientResponseHandlerHandlesBinary;
@ -57,21 +58,20 @@ import ca.uhn.fhir.rest.server.Constants;
import ca.uhn.fhir.rest.server.EncodingEnum;
import ca.uhn.fhir.rest.server.exceptions.BaseServerResponseException;
public abstract class BaseClient {
public abstract class BaseClient implements IRestfulClient {
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(BaseClient.class);
private final HttpClient myClient;
private boolean myDontValidateConformance;
private EncodingEnum myEncoding = null; // default unspecified (will be XML)
private final RestfulClientFactory myFactory;
private List<IClientInterceptor> myInterceptors = new ArrayList<IClientInterceptor>();
private boolean myKeepResponses = false;
private HttpResponse myLastResponse;
private String myLastResponseBody;
private Boolean myPrettyPrint = false;
private final RestfulClientFactory myFactory;
private final String myUrlBase;
private boolean myDontValidateConformance;
BaseClient(HttpClient theClient, String theUrlBase, RestfulClientFactory theFactory) {
super();
myClient = theClient;
@ -103,6 +103,14 @@ public abstract class BaseClient {
return myEncoding;
}
/**
* {@inheritDoc}
*/
@Override
public HttpClient getHttpClient() {
return myClient;
}
/**
* For now, this is a part of the internal API of HAPI - Use with caution as this method may change!
*/
@ -117,14 +125,26 @@ public abstract class BaseClient {
return myLastResponseBody;
}
String getServerBase() {
/**
* Returns the pretty print flag, which is a request to the server for it to return "pretty printed" responses. Note that this is currently a non-standard flag (_pretty) which is supported only by
* HAPI based servers (and any other servers which might implement it).
*/
public Boolean getPrettyPrint() {
return myPrettyPrint;
}
/**
* {@inheritDoc}
*/
@Override
public String getServerBase() {
return myUrlBase;
}
public String getUrlBase() {
return myUrlBase;
}
<T> T invokeClient(FhirContext theContext, IClientResponseHandler<T> binding, BaseHttpClientInvocation clientInvocation) {
return invokeClient(theContext, binding, clientInvocation, false);
}
@ -132,15 +152,6 @@ public abstract class BaseClient {
<T> T invokeClient(FhirContext theContext, IClientResponseHandler<T> binding, BaseHttpClientInvocation clientInvocation, boolean theLogRequestAndResponse) {
return invokeClient(theContext, binding, clientInvocation, null, null, theLogRequestAndResponse);
}
/**
* This method is an internal part of the HAPI API andmay change, use with caution. If you
* want to disable the loading of conformance statements, use {@link IRestfulClientFactory#setServerValidationModeEnum(ServerValidationModeEnum)}
*/
public void setDontValidateConformance(boolean theDontValidateConformance) {
myDontValidateConformance = theDontValidateConformance;
}
<T> T invokeClient(FhirContext theContext, IClientResponseHandler<T> binding, BaseHttpClientInvocation clientInvocation, EncodingEnum theEncoding, Boolean thePrettyPrint, boolean theLogRequestAndResponse) {
@ -335,14 +346,6 @@ public abstract class BaseClient {
return Boolean.TRUE.equals(myPrettyPrint);
}
/**
* Returns the pretty print flag, which is a request to the server for it to return "pretty printed" responses. Note that this is currently a non-standard flag (_pretty) which is supported only by
* HAPI based servers (and any other servers which might implement it).
*/
public Boolean getPrettyPrint() {
return myPrettyPrint;
}
private void keepResponseAndLogIt(boolean theLogRequestAndResponse, HttpResponse response, String responseString) {
if (myKeepResponses) {
myLastResponse = response;
@ -359,19 +362,27 @@ public abstract class BaseClient {
ourLog.trace("FHIR response:\n{}\n{}", response, responseString);
}
}
public void registerInterceptor(IClientInterceptor theInterceptor) {
Validate.notNull(theInterceptor, "Interceptor can not be null");
myInterceptors.add(theInterceptor);
}
/**
* Sets the encoding that will be used on requests. Default is <code>null</code>, which means the client will not explicitly request an encoding. (This is standard behaviour according to the FHIR
* specification)
* This method is an internal part of the HAPI API andmay change, use with caution. If you
* want to disable the loading of conformance statements, use {@link IRestfulClientFactory#setServerValidationModeEnum(ServerValidationModeEnum)}
*/
public BaseClient setEncoding(EncodingEnum theEncoding) {
public void setDontValidateConformance(boolean theDontValidateConformance) {
myDontValidateConformance = theDontValidateConformance;
}
/**
* Sets the encoding that will be used on requests. Default is <code>null</code>, which means the client will not explicitly request an encoding. (This is perfectly acceptable behaviour according to the FHIR
* specification. In this case, the server will choose which encoding to return, and the client can handle either XML or JSON)
*/
public void setEncoding(EncodingEnum theEncoding) {
myEncoding = theEncoding;
return this;
// return this;
}
/**
@ -399,9 +410,9 @@ public abstract class BaseClient {
* Sets the pretty print flag, which is a request to the server for it to return "pretty printed" responses. Note that this is currently a non-standard flag (_pretty) which is supported only by
* HAPI based servers (and any other servers which might implement it).
*/
public BaseClient setPrettyPrint(Boolean thePrettyPrint) {
public void setPrettyPrint(Boolean thePrettyPrint) {
myPrettyPrint = thePrettyPrint;
return this;
// return this;
}
public void unregisterInterceptor(IClientInterceptor theInterceptor) {

View File

@ -71,4 +71,9 @@ class ClientInvocationHandler extends BaseClient implements InvocationHandler {
throw new UnsupportedOperationException("The method '" + theMethod.getName() + "' in type " + theMethod.getDeclaringClass().getSimpleName() + " has no handler. Did you forget to annotate it with a RESTful method annotation?");
}
@Override
public FhirContext getFhirContext() {
return myContext;
}
}

View File

@ -161,6 +161,12 @@ public class GenericClient extends BaseClient implements IGenericClient {
return new CreateInternal();
}
@Override
public FhirContext getFhirContext() {
return myContext;
}
@Override
public MethodOutcome create(IResource theResource) {
BaseHttpClientInvocation invocation = MethodUtil.createCreateInvocation(theResource, myContext);

View File

@ -45,7 +45,7 @@ import ca.uhn.fhir.rest.gclient.ITransaction;
import ca.uhn.fhir.rest.gclient.IUntypedQuery;
import ca.uhn.fhir.rest.gclient.IUpdate;
public interface IGenericClient {
public interface IGenericClient extends IRestfulClient {
/**
* Retrieves and returns the server conformance statement

View File

@ -53,12 +53,12 @@ public interface IRestfulClient {
/**
* Specifies that the client should request that the server respond with "pretty printing"
* enabled. Note that this is a non-standard parameter, so it may only
* work against HAPI based servers.
* enabled. Note that this is a non-standard parameter, not all servers will
* support it.
*
* @param thePrettyPrint The pretty print flag to use in the request (default is <code>false</code>)
*/
void setPrettyPrint(boolean thePrettyPrint);
void setPrettyPrint(Boolean thePrettyPrint);
/**
* Base URL for the server, with no trailing "/"

View File

@ -27,7 +27,7 @@
<attribute name="org.eclipse.jst.component.nondependency" value=""/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7">
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6">
<attributes>
<attribute name="owner.project.facets" value="java"/>
</attributes>

View File

@ -3,4 +3,6 @@ encoding//src/main/java=UTF-8
encoding//src/main/resources=UTF-8
encoding//src/test/java=UTF-8
encoding//src/test/resources=UTF-8
encoding//target/generated-resources/tinder=UTF-8
encoding//target/generated-sources/tinder=UTF-8
encoding/<project>=UTF-8

View File

@ -7,9 +7,9 @@ org.eclipse.jdt.core.compiler.annotation.nullable=org.eclipse.jdt.annotation.Nul
org.eclipse.jdt.core.compiler.annotation.nullanalysis=disabled
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.7
org.eclipse.jdt.core.compiler.compliance=1.6
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
@ -97,4 +97,4 @@ org.eclipse.jdt.core.compiler.problem.unusedPrivateMember=warning
org.eclipse.jdt.core.compiler.problem.unusedTypeParameter=ignore
org.eclipse.jdt.core.compiler.problem.unusedWarningToken=warning
org.eclipse.jdt.core.compiler.problem.varargsArgumentNeedCast=warning
org.eclipse.jdt.core.compiler.source=1.7
org.eclipse.jdt.core.compiler.source=1.6

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<faceted-project>
<installed facet="jst.utility" version="1.0"/>
<installed facet="java" version="1.7"/>
<installed facet="java" version="1.6"/>
</faceted-project>

View File

@ -413,6 +413,32 @@ public class GenericClientTest {
}
@Test
public void testSetDefaultEncoding() throws Exception {
String msg = ourCtx.newJsonParser().encodeResourceToString(new Patient());
ArgumentCaptor<HttpUriRequest> 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().getContent()).thenReturn(new ReaderInputStream(new StringReader(msg), Charset.forName("UTF-8")));
// Header[] headers = new Header[] { new BasicHeader(Constants.HEADER_LAST_MODIFIED, "Wed, 15 Nov 1995 04:58:08 GMT"),
// new BasicHeader(Constants.HEADER_CONTENT_LOCATION, "http://foo.com/Patient/123/_history/2333"),
// new BasicHeader(Constants.HEADER_CATEGORY, "http://foo/tagdefinition.html; scheme=\"http://hl7.org/fhir/tag\"; label=\"Some tag\"") };
// when(myHttpResponse.getAllHeaders()).thenReturn(headers);
IGenericClient client = ourCtx.newRestfulGenericClient("http://example.com/fhir");
(client).setEncoding(EncodingEnum.JSON);
int count = 0;
client.read(Patient.class, new IdDt("Patient/1234"));
assertEquals("http://example.com/fhir/Patient/1234?_format=json", capt.getAllValues().get(count).getURI().toString());
count++;
}
@Test
public void testReadFluent() throws Exception {

View File

@ -19,6 +19,17 @@
Add support for "profile" and "tag" elements in the resource Meta block
when parsing DSTU2 structures.
</action>
<action type="fix" issue="113">
When a user manually creates the list of contained resources in a resource,
the encoder fails to encode any resources that don't have a '#' at the
start of their ID. This is unintuitive, so we now assume that '123' means '#123'.
Thanks to myungchoi for reporting and providing a test case!
</action>
<action type="add">
Add methods for setting the default encoding (XML/JSON) and
oretty print behaviour in the Fluent Client. Thanks to Stackoverflow
user ewall for the idea.
</action>
</release>
<release version="0.9" date="2015-Mar-14">
<action type="add">