Add a test for #107

This commit is contained in:
jamesagnew 2015-02-13 08:26:21 -05:00
parent ec5f7afa5e
commit 083a0baf79
8 changed files with 216 additions and 11 deletions

View File

@ -0,0 +1,49 @@
package example;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.AuthenticationStrategy;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.ProxyAuthenticationStrategy;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.model.dstu2.resource.Patient;
import ca.uhn.fhir.model.primitive.IdDt;
import ca.uhn.fhir.rest.client.IGenericClient;
public class HttpProxy {
public static void main(String[] args) {
final String authUser = "username";
final String authPassword = "password";
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope("10.10.10.10", 8080),
new UsernamePasswordCredentials(authUser, authPassword));
HttpHost myProxy = new HttpHost("10.10.10.10", 8080);
HttpClientBuilder clientBuilder = HttpClientBuilder.create();
clientBuilder
.setProxy(myProxy)
.setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy())
.setDefaultCredentialsProvider(credsProvider)
.disableCookieManagement();
CloseableHttpClient httpClient = clientBuilder.build();
FhirContext ctx = new FhirContext();
String serverBase = "http://spark.furore.com/fhir/";
ctx.getRestfulClientFactory().setHttpClient(httpClient);
IGenericClient client = ctx.newRestfulGenericClient(serverBase);
IdDt id = new IdDt("Patient", "123");
client.read(Patient.class, id);
}
}

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.6">
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7">
<attributes>
<attribute name="owner.project.facets" value="java"/>
</attributes>

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.6"/>
<installed facet="java" version="1.7"/>
</faceted-project>

View File

@ -0,0 +1,150 @@
package ca.uhn.fhir.rest.client;
import static org.junit.Assert.*;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.collections.EnumerationUtils;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.ProxyAuthenticationStrategy;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.ContextHandlerCollection;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.junit.BeforeClass;
import org.junit.Test;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.model.api.IResource;
import ca.uhn.fhir.model.dstu.resource.Patient;
import ca.uhn.fhir.model.primitive.IdDt;
import ca.uhn.fhir.rest.annotation.IdParam;
import ca.uhn.fhir.rest.annotation.Read;
import ca.uhn.fhir.rest.server.IResourceProvider;
import ca.uhn.fhir.rest.server.RestfulServer;
import ca.uhn.fhir.util.PortUtil;
public class HttpProxyTest {
private static FhirContext ourCtx;
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(HttpProxyTest.class);
private static HttpServletRequest ourRequest;
private boolean myFirstRequest;
private String myAuthHeader;
@SuppressWarnings("serial")
@Test
public void testProxiedRequest() throws Exception {
int port = PortUtil.findFreePort();
Server server = new Server(port);
myFirstRequest = true;
myAuthHeader = null;
RestfulServer restServer = new RestfulServer() {
@Override
protected void doGet(HttpServletRequest theRequest, HttpServletResponse theResponse) throws ServletException, IOException {
if (myFirstRequest) {
theResponse.addHeader("Proxy-Authenticate", "Basic realm=\"some_realm\"");
theResponse.setStatus(407);
theResponse.getWriter().close();
myFirstRequest = false;
return;
}
String auth = theRequest.getHeader("Proxy-Authorization");
if (auth != null) {
myAuthHeader = auth;
}
super.doGet(theRequest, theResponse);
}
};
restServer.setFhirContext(ourCtx);
restServer.setResourceProviders(new PatientResourceProvider());
// ServletHandler proxyHandler = new ServletHandler();
ServletHolder servletHolder = new ServletHolder(restServer);
ServletContextHandler ch = new ServletContextHandler();
ch.setContextPath("/rootctx/rcp2");
ch.addServlet(servletHolder, "/fhirctx/fcp2/*");
ContextHandlerCollection contexts = new ContextHandlerCollection();
server.setHandler(contexts);
server.setHandler(ch);
server.start();
try {
final String authUser = "username";
final String authPassword = "password";
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope("127.0.0.1", port), new UsernamePasswordCredentials(authUser, authPassword));
HttpHost myProxy = new HttpHost("127.0.0.1", port);
//@formatter:off
HttpClientBuilder clientBuilder = HttpClientBuilder.create();
clientBuilder
.setProxy(myProxy)
.setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy())
.setDefaultCredentialsProvider(credsProvider)
.disableCookieManagement();
CloseableHttpClient httpClient = clientBuilder.build();
//@formatter:on
ourCtx.getRestfulClientFactory().setHttpClient(httpClient);
String baseUri = "http://99.99.99.99:" + port + "/rootctx/rcp2/fhirctx/fcp2";
IGenericClient client = ourCtx.newRestfulGenericClient(baseUri);
IdDt id = new IdDt("Patient", "123");
client.read(Patient.class, id);
assertEquals("Basic dXNlcm5hbWU6cGFzc3dvcmQ=", myAuthHeader);
} finally {
server.stop();
}
}
@BeforeClass
public static void beforeClass() throws Exception {
ourCtx = new FhirContext();
}
public static class PatientResourceProvider implements IResourceProvider {
@Override
public Class<? extends IResource> getResourceType() {
return Patient.class;
}
@Read
public Patient read(@IdParam IdDt theId, HttpServletRequest theRequest) {
Patient retVal = new Patient();
retVal.setId(theId);
ourRequest = theRequest;
ourLog.info(EnumerationUtils.toList(ourRequest.getHeaderNames()).toString());
ourLog.info("Proxy-Connection: " + EnumerationUtils.toList(ourRequest.getHeaders("Proxy-Connection")));
ourLog.info("Host: " + EnumerationUtils.toList(ourRequest.getHeaders("Host")));
ourLog.info("User-Agent: " + EnumerationUtils.toList(ourRequest.getHeaders("User-Agent")));
return retVal;
}
}
}

View File

@ -393,7 +393,9 @@
var pres = elements[i].getElementsByTagName("pre");
for (var j = 0; j < pres.length; j++) {
var pre = pres[j];
if (pre.innerHTML.match(/\/\*/)) {
if (pre.innerHTML.match(/^\s*\&lt\;/)) {
pre.className = 'brush: xml';
} else if (pre.innerHTML.match(/\/\*/)) {
pre.className = 'brush: java';
} else if (pre.innerHTML.match(/^\/\//)) {
pre.className = 'brush: java';

View File

@ -109,11 +109,12 @@ a[name]:before {
tt {
white-space: pre;
color: #448;
color: #846;
margin-bottom: 5px;
margin-top: 10px;
padding: 2px;
border: 1px solid #AAA;
background-color: #F0F0F0;
}
h1,h2,h3,h4,h5 {

View File

@ -22,12 +22,13 @@
<logo name="Built with Maven 3" href="http://maven.apache.org" img="./images/maven-logo-mini.png" />
</poweredBy>
<publishDate position="bottom" format="yyyy-MM-dd" />
<version position="bottom" />
<publishDate position="right" format="yyyy-MM-dd" />
<version position="right" />
<!-- <version position="left" /> -->
<body>
<head>
<!-- Syntax Highlighter -->
<!--
@ -51,7 +52,7 @@
</head>
<breadcrumbs>
<item name="HAPI FHIR" href="/" />
<item name="HAPI FHIR" href="./index.html" />
</breadcrumbs>
<links>
@ -71,8 +72,8 @@
<item name="Test Server" href="http://fhirtest.uhn.ca"/>
</menu>
<menu name="Documentation" inherit="top">
<item name="Introduction" href="./doc_intro.html" >
<menu name="Documentation" inherit="top" >
<item name="Introduction" href="./doc_intro.html">
<item name="DSTU2 Support (new)" href="./doc_dstu2.html" />
</item>
@ -157,6 +158,9 @@
</sections>
-->
</index>
<doc_cors>
<shortTitle>CORS</shortTitle>
</doc_cors>
</pages>
<theme>bootswatch-united</theme>
<toc>sidebar</toc>

View File

@ -1,13 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<document xmlns="http://maven.apache.org/XDOC/2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/XDOC/2.0 http://maven.apache.org/xsd/xdoc-2.0.xsd">
<properties>
<title>RESTful Operations - HAPI FHIR</title>
<author email="jamesagnew@users.sourceforge.net">James Agnew</author>
</properties>
<body>
<section name="Implementing Resource Provider Operations">
<p>