Skip to content

Commit

Permalink
Include implementation URL in generated capabilitystatement
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesagnew committed Jun 3, 2018
1 parent 30dd3c0 commit 9db3be8
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 116 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,13 @@ public Conformance getServerConformance(HttpServletRequest theRequest) {
// effort since the parser
// needs to be modified to actually allow it

retVal.getImplementation().setDescription(myServerConfiguration.getImplementationDescription());
ServletContext servletContext = (ServletContext) (theRequest == null ? null : theRequest.getAttribute(RestfulServer.SERVLET_CONTEXT_ATTRIBUTE));
String serverBase = myServerConfiguration.getServerAddressStrategy().determineServerBase(servletContext, theRequest);
retVal
.getImplementation()
.setUrl(serverBase)
.setDescription(myServerConfiguration.getImplementationDescription());

retVal.setKind(ConformanceStatementKind.INSTANCE);
retVal.getSoftware().setName(myServerConfiguration.getServerName());
retVal.getSoftware().setVersion(myServerConfiguration.getServerVersion());
Expand All @@ -203,11 +209,9 @@ public Conformance getServerConformance(HttpServletRequest theRequest) {
String resourceName = nextEntry.getKey();
RuntimeResourceDefinition def = myServerConfiguration.getFhirContext().getResourceDefinition(resourceName);
resource.getTypeElement().setValue(def.getName());
ServletContext servletContext = (ServletContext) (theRequest == null ? null : theRequest.getAttribute(RestfulServer.SERVLET_CONTEXT_ATTRIBUTE));
String serverBase = myServerConfiguration.getServerAddressStrategy().determineServerBase(servletContext, theRequest);
resource.getProfile().setReference((def.getResourceProfile(serverBase)));

TreeSet<String> includes = new TreeSet<String>();
TreeSet<String> includes = new TreeSet<>();

// Map<String, Conformance.RestResourceSearchParam> nameToSearchParam = new HashMap<String,
// Conformance.RestResourceSearchParam>();
Expand Down Expand Up @@ -273,7 +277,7 @@ public Conformance getServerConformance(HttpServletRequest theRequest) {
}
}

Collections.sort(resource.getInteraction(), new Comparator<ResourceInteractionComponent>() {
resource.getInteraction().sort(new Comparator<ResourceInteractionComponent>() {
@Override
public int compare(ResourceInteractionComponent theO1, ResourceInteractionComponent theO2) {
TypeRestfulInteraction o1 = theO1.getCode();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,13 @@ public Conformance getServerConformance(HttpServletRequest theRequest) {
retVal.setAcceptUnknown(UnknownContentCodeEnum.UNKNOWN_EXTENSIONS); // TODO: make this configurable - this is a fairly big effort since the parser
// needs to be modified to actually allow it

retVal.getImplementation().setDescription(myServerConfiguration.getImplementationDescription());
ServletContext servletContext = (ServletContext) (theRequest == null ? null : theRequest.getAttribute(RestfulServer.SERVLET_CONTEXT_ATTRIBUTE));
String serverBase = myServerConfiguration.getServerAddressStrategy().determineServerBase(servletContext, theRequest);
retVal
.getImplementation()
.setUrl(serverBase)
.setDescription(myServerConfiguration.getImplementationDescription());

retVal.setKind(ConformanceStatementKindEnum.INSTANCE);
retVal.getSoftware().setName(myServerConfiguration.getServerName());
retVal.getSoftware().setVersion(myServerConfiguration.getServerVersion());
Expand All @@ -189,8 +195,6 @@ public Conformance getServerConformance(HttpServletRequest theRequest) {
String resourceName = nextEntry.getKey();
RuntimeResourceDefinition def = myServerConfiguration.getFhirContext().getResourceDefinition(resourceName);
resource.getTypeElement().setValue(def.getName());
ServletContext servletContext = (ServletContext) (theRequest == null ? null : theRequest.getAttribute(RestfulServer.SERVLET_CONTEXT_ATTRIBUTE));
String serverBase = myServerConfiguration.getServerAddressStrategy().determineServerBase(servletContext, theRequest);
resource.getProfile().setReference(new IdDt(def.getResourceProfile(serverBase)));

TreeSet<String> includes = new TreeSet<String>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,14 @@ public CapabilityStatement getServerConformance(HttpServletRequest theRequest) {
// effort since the parser
// needs to be modified to actually allow it

retVal.getImplementation().setDescription(myServerConfiguration.getImplementationDescription());
retVal.setKind(CapabilityStatementKind.INSTANCE);
ServletContext servletContext = (ServletContext) (theRequest == null ? null : theRequest.getAttribute(RestfulServer.SERVLET_CONTEXT_ATTRIBUTE));
String serverBase = myServerConfiguration.getServerAddressStrategy().determineServerBase(servletContext, theRequest);
retVal
.getImplementation()
.setUrl(serverBase)
.setDescription(myServerConfiguration.getImplementationDescription());

retVal.setKind(CapabilityStatementKind.INSTANCE);
retVal.getSoftware().setName(myServerConfiguration.getServerName());
retVal.getSoftware().setVersion(myServerConfiguration.getServerVersion());
retVal.addFormat(Constants.CT_FHIR_XML_NEW);
Expand All @@ -201,11 +207,9 @@ public CapabilityStatement getServerConformance(HttpServletRequest theRequest) {
String resourceName = nextEntry.getKey();
RuntimeResourceDefinition def = myServerConfiguration.getFhirContext().getResourceDefinition(resourceName);
resource.getTypeElement().setValue(def.getName());
ServletContext servletContext = (ServletContext) (theRequest == null ? null : theRequest.getAttribute(RestfulServer.SERVLET_CONTEXT_ATTRIBUTE));
String serverBase = myServerConfiguration.getServerAddressStrategy().determineServerBase(servletContext, theRequest);
resource.getProfile().setReference((def.getResourceProfile(serverBase)));

TreeSet<String> includes = new TreeSet<String>();
TreeSet<String> includes = new TreeSet<>();

// Map<String, CapabilityStatement.RestResourceSearchParam> nameToSearchParam = new HashMap<String,
// CapabilityStatement.RestResourceSearchParam>();
Expand Down Expand Up @@ -271,23 +275,23 @@ public CapabilityStatement getServerConformance(HttpServletRequest theRequest) {
}
}

Collections.sort(resource.getInteraction(), new Comparator<ResourceInteractionComponent>() {
@Override
public int compare(ResourceInteractionComponent theO1, ResourceInteractionComponent theO2) {
TypeRestfulInteraction o1 = theO1.getCode();
TypeRestfulInteraction o2 = theO2.getCode();
if (o1 == null && o2 == null) {
return 0;
}
if (o1 == null) {
return 1;
}
if (o2 == null) {
return -1;
}
return o1.ordinal() - o2.ordinal();
}
});
resource.getInteraction().sort(new Comparator<ResourceInteractionComponent>() {
@Override
public int compare(ResourceInteractionComponent theO1, ResourceInteractionComponent theO2) {
TypeRestfulInteraction o1 = theO1.getCode();
TypeRestfulInteraction o2 = theO2.getCode();
if (o1 == null && o2 == null) {
return 0;
}
if (o1 == null) {
return 1;
}
if (o2 == null) {
return -1;
}
return o1.ordinal() - o2.ordinal();
}
});

}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package ca.uhn.fhir.rest.server;

import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.stringContainsInOrder;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;

import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.concurrent.TimeUnit;

import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.context.FhirVersionEnum;
import ca.uhn.fhir.rest.annotation.OptionalParam;
import ca.uhn.fhir.rest.annotation.ResourceParam;
import ca.uhn.fhir.rest.annotation.Search;
import ca.uhn.fhir.rest.annotation.Validate;
import ca.uhn.fhir.rest.api.MethodOutcome;
import ca.uhn.fhir.rest.param.StringParam;
import ca.uhn.fhir.util.PortUtil;
import ca.uhn.fhir.util.TestUtil;
import ca.uhn.fhir.util.VersionUtil;
import org.apache.commons.io.IOUtils;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
Expand All @@ -22,64 +23,27 @@
import org.eclipse.jetty.servlet.ServletHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.hl7.fhir.dstu3.hapi.rest.server.ServerCapabilityStatementProvider;
import org.hl7.fhir.dstu3.model.CapabilityStatement;
import org.hl7.fhir.dstu3.model.Patient;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.context.FhirVersionEnum;
import ca.uhn.fhir.rest.annotation.OptionalParam;
import ca.uhn.fhir.rest.annotation.ResourceParam;
import ca.uhn.fhir.rest.annotation.Search;
import ca.uhn.fhir.rest.annotation.Validate;
import ca.uhn.fhir.rest.api.MethodOutcome;
import ca.uhn.fhir.rest.param.StringParam;
import ca.uhn.fhir.util.PortUtil;
import ca.uhn.fhir.util.TestUtil;
import ca.uhn.fhir.util.VersionUtil;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.concurrent.TimeUnit;

import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;

public class MetadataCapabilityStatementDstu3Test {

private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(MetadataCapabilityStatementDstu3Test.class);
private static CloseableHttpClient ourClient;
private static FhirContext ourCtx = FhirContext.forDstu3();
private static int ourPort;
private static Server ourServer;
private static RestfulServer ourServlet;
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(MetadataCapabilityStatementDstu3Test.class);

@Test
public void testSummary() throws Exception {
String output;

// With
HttpRequestBase httpPost = new HttpGet("http://localhost:" + ourPort + "/metadata?_summary=true&_pretty=true");
CloseableHttpResponse status = ourClient.execute(httpPost);
try {
output = IOUtils.toString(status.getEntity().getContent(), StandardCharsets.UTF_8);
assertEquals(200, status.getStatusLine().getStatusCode());
ourLog.info(output);
assertThat(output, containsString("<CapabilityStatement"));
assertThat(output, stringContainsInOrder("<meta>", "SUBSETTED", "</meta>"));
assertThat(output, not(stringContainsInOrder("searchParam")));
} finally {
IOUtils.closeQuietly(status.getEntity().getContent());
}

// Without
httpPost = new HttpGet("http://localhost:" + ourPort + "/metadata?_pretty=true");
status = ourClient.execute(httpPost);
try {
output = IOUtils.toString(status.getEntity().getContent(), StandardCharsets.UTF_8);
assertEquals(200, status.getStatusLine().getStatusCode());
ourLog.info(output);
assertThat(output, containsString("<CapabilityStatement"));
assertThat(output, not(stringContainsInOrder("<meta>", "SUBSETTED", "</meta>")));
assertThat(output, stringContainsInOrder("searchParam"));
} finally {
IOUtils.closeQuietly(status.getEntity().getContent());
}
}

@Test
public void testElements() throws Exception {
Expand Down Expand Up @@ -109,7 +73,7 @@ public void testHttpMethods() throws Exception {
assertEquals(200, status.getStatusLine().getStatusCode());
assertThat(output, containsString("<CapabilityStatement"));
assertEquals("HAPI FHIR " + VersionUtil.getVersion() + " REST Server (FHIR Server; FHIR " + FhirVersionEnum.DSTU3.getFhirVersionString() + "/DSTU3)",
status.getFirstHeader("X-Powered-By").getValue());
status.getFirstHeader("X-Powered-By").getValue());
} finally {
IOUtils.closeQuietly(status.getEntity().getContent());
}
Expand All @@ -120,8 +84,8 @@ public void testHttpMethods() throws Exception {
output = IOUtils.toString(status.getEntity().getContent(), StandardCharsets.UTF_8);
assertEquals(405, status.getStatusLine().getStatusCode());
assertEquals(
"<OperationOutcome xmlns=\"http://hl7.org/fhir\"><issue><severity value=\"error\"/><code value=\"processing\"/><diagnostics value=\"/metadata request must use HTTP GET\"/></issue></OperationOutcome>",
output);
"<OperationOutcome xmlns=\"http://hl7.org/fhir\"><issue><severity value=\"error\"/><code value=\"processing\"/><diagnostics value=\"/metadata request must use HTTP GET\"/></issue></OperationOutcome>",
output);
} finally {
IOUtils.closeQuietly(status.getEntity().getContent());
}
Expand All @@ -140,6 +104,57 @@ public void testHttpMethods() throws Exception {
}
}

@Test
public void testResponseContainsBaseUrl() throws Exception {
String output;

HttpRequestBase httpPost = new HttpGet("http://localhost:" + ourPort + "/metadata?_format=json");
CloseableHttpResponse status = ourClient.execute(httpPost);
try {
output = IOUtils.toString(status.getEntity().getContent(), StandardCharsets.UTF_8);
assertEquals(200, status.getStatusLine().getStatusCode());
ourLog.info(output);
CapabilityStatement cs = ourCtx.newJsonParser().parseResource(CapabilityStatement.class, output);

assertEquals("http://localhost:" + ourPort + "/", cs.getImplementation().getUrl());
} finally {
IOUtils.closeQuietly(status.getEntity().getContent());
}
}

@Test
public void testSummary() throws Exception {
String output;

// With
HttpRequestBase httpPost = new HttpGet("http://localhost:" + ourPort + "/metadata?_summary=true&_pretty=true");
CloseableHttpResponse status = ourClient.execute(httpPost);
try {
output = IOUtils.toString(status.getEntity().getContent(), StandardCharsets.UTF_8);
assertEquals(200, status.getStatusLine().getStatusCode());
ourLog.info(output);
assertThat(output, containsString("<CapabilityStatement"));
assertThat(output, stringContainsInOrder("<meta>", "SUBSETTED", "</meta>"));
assertThat(output, not(stringContainsInOrder("searchParam")));
} finally {
IOUtils.closeQuietly(status.getEntity().getContent());
}

// Without
httpPost = new HttpGet("http://localhost:" + ourPort + "/metadata?_pretty=true");
status = ourClient.execute(httpPost);
try {
output = IOUtils.toString(status.getEntity().getContent(), StandardCharsets.UTF_8);
assertEquals(200, status.getStatusLine().getStatusCode());
ourLog.info(output);
assertThat(output, containsString("<CapabilityStatement"));
assertThat(output, not(stringContainsInOrder("<meta>", "SUBSETTED", "</meta>")));
assertThat(output, stringContainsInOrder("searchParam"));
} finally {
IOUtils.closeQuietly(status.getEntity().getContent());
}
}

@AfterClass
public static void afterClassClearContext() throws Exception {
ourServer.stop();
Expand All @@ -156,7 +171,7 @@ public static void beforeClass() throws Exception {
ServletHandler proxyHandler = new ServletHandler();
ourServlet = new RestfulServer(ourCtx);
ourServlet.setResourceProviders(patientProvider);

ourServlet.setServerConformanceProvider(new ServerCapabilityStatementProvider(ourServlet));

ServletHolder servletHolder = new ServletHolder(ourServlet);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,13 @@ public Conformance getServerConformance(HttpServletRequest theRequest) {
retVal.setAcceptUnknown(UnknownContentCode.EXTENSIONS); // TODO: make this configurable - this is a fairly big effort since the parser
// needs to be modified to actually allow it

retVal.getImplementation().setDescription(myServerConfiguration.getImplementationDescription());
ServletContext servletContext = (ServletContext) (theRequest == null ? null : theRequest.getAttribute(RestfulServer.SERVLET_CONTEXT_ATTRIBUTE));
String serverBase = myServerConfiguration.getServerAddressStrategy().determineServerBase(servletContext, theRequest);
retVal
.getImplementation()
.setUrl(serverBase)
.setDescription(myServerConfiguration.getImplementationDescription());

retVal.setKind(ConformanceStatementKind.INSTANCE);
retVal.getSoftware().setName(myServerConfiguration.getServerName());
retVal.getSoftware().setVersion(myServerConfiguration.getServerVersion());
Expand All @@ -193,11 +199,9 @@ public Conformance getServerConformance(HttpServletRequest theRequest) {
String resourceName = nextEntry.getKey();
RuntimeResourceDefinition def = myServerConfiguration.getFhirContext().getResourceDefinition(resourceName);
resource.getTypeElement().setValue(def.getName());
ServletContext servletContext = (ServletContext) (theRequest == null ? null : theRequest.getAttribute(RestfulServer.SERVLET_CONTEXT_ATTRIBUTE));
String serverBase = myServerConfiguration.getServerAddressStrategy().determineServerBase(servletContext, theRequest);
resource.getProfile().setReference((def.getResourceProfile(serverBase)));

TreeSet<String> includes = new TreeSet<String>();
TreeSet<String> includes = new TreeSet<>();

// Map<String, Conformance.RestResourceSearchParam> nameToSearchParam =
// new HashMap<String,
Expand Down Expand Up @@ -262,7 +266,7 @@ public Conformance getServerConformance(HttpServletRequest theRequest) {
}
}

Collections.sort(resource.getInteraction(), new Comparator<ResourceInteractionComponent>() {
resource.getInteraction().sort(new Comparator<ResourceInteractionComponent>() {
@Override
public int compare(ResourceInteractionComponent theO1, ResourceInteractionComponent theO2) {
TypeRestfulInteraction o1 = theO1.getCode();
Expand Down
Loading

0 comments on commit 9db3be8

Please sign in to comment.