Fix #162 - Allow web testing UI to configure the client

This commit is contained in:
James Agnew 2015-04-23 17:52:51 -04:00
parent 8072ca1bc1
commit 7db7097e25
7 changed files with 104 additions and 1 deletions

View File

@ -0,0 +1,23 @@
package example;
import javax.servlet.http.HttpServletRequest;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.rest.client.IGenericClient;
import ca.uhn.fhir.rest.client.interceptor.BasicAuthInterceptor;
import ca.uhn.fhir.util.ITestingUiClientFactory;
public class AuthorizingTesterUiClientFactory implements ITestingUiClientFactory {
@Override
public IGenericClient newClient(FhirContext theFhirContext, HttpServletRequest theRequest, String theServerBaseUrl) {
// Create a client
IGenericClient client = theFhirContext.newRestfulGenericClient(theServerBaseUrl);
// Register an interceptor which adds credentials
client.registerInterceptor(new BasicAuthInterceptor("someusername", "somepassword"));
return client;
}
}

View File

@ -0,0 +1,19 @@
package ca.uhn.fhir.util;
import javax.servlet.http.HttpServletRequest;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.rest.client.IGenericClient;
/**
* This interface isn't used by hapi-fhir-base, but is used by the
* <a href="http://jamesagnew.github.io/hapi-fhir/doc_server_tester.html">Web Testing UI</a>
*/
public interface ITestingUiClientFactory {
/**
* Instantiate a new client
*/
IGenericClient newClient(FhirContext theFhirContext, HttpServletRequest theRequest, String theServerBaseUrl);
}

View File

@ -9,15 +9,21 @@ import org.apache.commons.lang3.Validate;
import org.springframework.beans.factory.annotation.Required;
import ca.uhn.fhir.context.FhirVersionEnum;
import ca.uhn.fhir.util.ITestingUiClientFactory;
public class TesterConfig {
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(TesterConfig.class);
public static final String SYSPROP_FORCE_SERVERS = "ca.uhn.fhir.to.TesterConfig_SYSPROP_FORCE_SERVERS";
private LinkedHashMap<String, String> myIdToServerName = new LinkedHashMap<String, String>();
private ITestingUiClientFactory myClientFactory;
private LinkedHashMap<String, FhirVersionEnum> myIdToFhirVersion = new LinkedHashMap<String, FhirVersionEnum>();
private LinkedHashMap<String, String> myIdToServerBase = new LinkedHashMap<String, String>();
private LinkedHashMap<String, String> myIdToServerName = new LinkedHashMap<String, String>();
public ITestingUiClientFactory getClientFactory() {
return myClientFactory;
}
public boolean getDebugTemplatesMode() {
return true;
@ -35,6 +41,10 @@ public class TesterConfig {
return myIdToServerName;
}
public void setClientFactory(ITestingUiClientFactory theClientFactory) {
myClientFactory = theClientFactory;
}
@Required
public void setServers(List<String> theServers) {
List<String> servers = theServers;

View File

@ -13,6 +13,7 @@ import ca.uhn.fhir.context.FhirVersionEnum;
import ca.uhn.fhir.parser.IParser;
import ca.uhn.fhir.rest.client.GenericClient;
import ca.uhn.fhir.rest.client.IClientInterceptor;
import ca.uhn.fhir.rest.client.ServerValidationModeEnum;
import ca.uhn.fhir.rest.server.EncodingEnum;
import ca.uhn.fhir.rest.server.IncomingRequestAddressStrategy;
import ca.uhn.fhir.to.Controller;
@ -110,6 +111,8 @@ public class HomeRequest {
}
public GenericClient newClient(HttpServletRequest theRequest, FhirContext theContext, TesterConfig theConfig, Controller.CaptureInterceptor theInterceptor) {
theContext.getRestfulClientFactory().setServerValidationModeEnum(ServerValidationModeEnum.NEVER);
GenericClient retVal = (GenericClient) theContext.newRestfulGenericClient(getServerBase(theRequest, theConfig));
retVal.setKeepResponses(true);

View File

@ -21,6 +21,11 @@
<value>example , DSTU1 , Restful Server Example , http://localhost:8080/fhir</value>
</list>
</property>
<!--
Add a property for the client factory if one is needed
<property name="clientFactory"><bean class="com.example.AuthorizingTesterUiClientFactory"/></property>
-->
</bean>
<!-- A FhirContext bean is also required -->

View File

@ -144,6 +144,12 @@
JPA server (uhnfhirtest.uhn.ca) sometimes included an empty
"text" element in Bundles being returned.
</action>
<action type="add" issue="162">
Add a framework for the Web Tester UI to allow its internal FHIR client to
be configured (e.g. to add an authorization interceptor so that it adds
credentials to client requests it makes). Thanks to Harsha Kumara for
the suggestion!
</action>
</release>
<release version="0.9" date="2015-Mar-14">
<action type="add">

View File

@ -170,6 +170,43 @@
</subsection>
</section>
<section name="Authorization">
<p>
The testing UI uses its own client to talk to your FHIR server. In other words, there are no
special "hooks" which the tested uses to retrieve data from your server, it acts as an HTTP client
just like any other client.
</p>
<p>
This does mean that if your server has any authorization requirements, you will need to configure the
tester UI to meet those requirements. For example, if your server has been configured to require
a HTTP Basic Auth header (e.g. <code>Authorization: Basic VVNFUjpQQVNT</code>) you need to
configure the tester UI to send those credentials across when it is acting as
a FHIR client.
</p>
<p>
This is done by providing your own implementation of the <code>ITestingUiClientFactory</code>
interface. This interface takes in some details about the incoming request and produces
a client.
</p>
<p>
The following example shows an implementation of the client factory which registers
an authorization interceptor with hardcoded credentials.
</p>
<macro name="snippet">
<param name="file" value="restful-server-example/src/main/webapp/WEB-INF/hapi-fhir-tester-config.xml" />
</macro>
<p>
This client factory is then registered with the TesterConfig in the <code>hapi-fhir-tester-config.xml</code>
file, as shown above.
</p>
</section>
</body>