Merge pull request #489 from jkiddo/master

Added new independant overlay build for example usage and lenient handling
This commit is contained in:
James Agnew 2017-01-29 17:50:20 -05:00 committed by GitHub
commit 0223b5341c
20 changed files with 395 additions and 111 deletions

View File

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<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">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>

View File

@ -1,10 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>hapi-fhir-base-example-embedded-ws</name>
<comment></comment>
<comment>NO_M2ECLIPSE_SUPPORT: Project files created with the maven-eclipse-plugin are not supported in M2Eclipse.</comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.wst.common.project.facet.core.builder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
@ -12,6 +22,10 @@
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jem.workbench.JavaEMFNature</nature>
<nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
</natures>
</projectDescription>

View File

@ -26,25 +26,39 @@
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>3.0</version>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>com.google.inject.extensions</groupId>
<artifactId>guice-servlet</artifactId>
<version>3.0</version>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>jersey-guice</artifactId>
<version>1.18.1</version>
<version>1.19.1</version>
</dependency>
<dependency>
<groupId>org.ebaysf.web</groupId>
<artifactId>cors-filter</artifactId>
</dependency>
<dependency>
<groupId>ca.uhn.hapi.fhir</groupId>
<artifactId>hapi-fhir-structures-dstu2</artifactId>
<version>1.2</version>
<version>2.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.21</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jul-to-slf4j</artifactId>
<version>1.7.21</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
@ -56,5 +70,5 @@
</plugin>
</plugins>
</build>
</project>

View File

@ -1,18 +1,14 @@
package embedded;
import java.util.Map;
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableMap;
import javax.inject.Singleton;
import org.ebaysf.web.cors.CORSFilter;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.servlet.GuiceServletContextListener;
import com.sun.jersey.api.container.filter.GZIPContentEncodingFilter;
import com.sun.jersey.api.core.ResourceConfig;
import com.sun.jersey.guice.JerseyServletModule;
import filters.CharsetResponseFilter;
import filters.CorsResponseFilter;
public class ContextListener extends GuiceServletContextListener {
@Override
@ -22,15 +18,9 @@ public class ContextListener extends GuiceServletContextListener {
@Override
protected void configureServlets() {
final Map<String, String> params = ImmutableMap
.<String, String> builder()
.put(ResourceConfig.PROPERTY_CONTAINER_RESPONSE_FILTERS,
Joiner.on(";").join(
CharsetResponseFilter.class.getName(),
CorsResponseFilter.class.getName(),
GZIPContentEncodingFilter.class
.getName())).build();
serve("/model/*").with(FhirRestfulServlet.class, params);
bind(CORSFilter.class).in(Singleton.class);
filter("/*").through(CORSFilter.class);
serve("/model/*").with(FhirRestfulServlet.class);
}
});
}

View File

@ -6,8 +6,6 @@ import java.util.List;
import javax.inject.Singleton;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.narrative.DefaultThymeleafNarrativeGenerator;
import ca.uhn.fhir.narrative.INarrativeGenerator;
import ca.uhn.fhir.rest.server.IResourceProvider;
import ca.uhn.fhir.rest.server.RestfulServer;
import ca.uhn.fhir.rest.server.interceptor.ResponseHighlighterInterceptor;
@ -29,29 +27,10 @@ public class FhirRestfulServlet extends RestfulServer {
*/
@Override
public void initialize() {
/*
* Two resource providers are defined. Each one handles a specific type
* of resource.
*/
final List<IResourceProvider> providers = new ArrayList<IResourceProvider>();
providers.add(new SomeResourceProvider());
setResourceProviders(providers);
/*
* Use a narrative generator. This is a completely optional step, but
* can be useful as it causes HAPI to generate narratives for resources
* which don't otherwise have one.
*/
final INarrativeGenerator narrativeGen = new DefaultThymeleafNarrativeGenerator();
getFhirContext().setNarrativeGenerator(narrativeGen);
/*
* Tells HAPI to use content types which are not technically FHIR
* compliant when a browser is detected as the requesting client. This
* prevents browsers from trying to download resource responses instead
* of displaying them inline which can be handy for troubleshooting.
*/
setUseBrowserFriendlyContentTypes(true);
registerInterceptor(new ResponseHighlighterInterceptor());

View File

@ -1,4 +1,5 @@
package embedded;
import java.util.EnumSet;
import javax.servlet.DispatcherType;
@ -6,6 +7,7 @@ import javax.servlet.DispatcherType;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.DefaultServlet;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.slf4j.bridge.SLF4JBridgeHandler;
import com.google.inject.servlet.GuiceFilter;
@ -13,13 +15,18 @@ public class ServerStartup {
public static void main(final String[] args) throws Exception {
SLF4JBridgeHandler.removeHandlersForRootLogger();
SLF4JBridgeHandler.install();
final Server server = new Server(9090);
final ServletContextHandler sch = new ServletContextHandler(server, "/");
sch.addEventListener(new ContextListener());
sch.addFilter(GuiceFilter.class, "/*",
EnumSet.of(DispatcherType.REQUEST));
sch.addFilter(GuiceFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
sch.addServlet(DefaultServlet.class, "/");
server.start();
server.start();
// Service is now accessible through
// http://localhost:9090/model/Practitioner
}
}

View File

@ -4,6 +4,8 @@ import java.util.List;
import org.hl7.fhir.instance.model.api.IBaseResource;
import com.google.common.collect.Lists;
import ca.uhn.fhir.model.dstu2.resource.Practitioner;
import ca.uhn.fhir.model.primitive.StringDt;
import ca.uhn.fhir.rest.annotation.RequiredParam;
@ -21,8 +23,9 @@ public class SomeResourceProvider implements IResourceProvider {
@Search()
public List<Practitioner> findPractitionersByName(
@RequiredParam(name = Practitioner.SP_NAME) final StringDt theName) {
throw new UnprocessableEntityException(
"Please provide more than 4 characters for the name");
// throw new UnprocessableEntityException(
// "Please provide more than 4 characters for the name");
return Lists.newArrayList();
}
}

View File

@ -1,24 +0,0 @@
package filters;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import com.sun.jersey.spi.container.ContainerRequest;
import com.sun.jersey.spi.container.ContainerResponse;
import com.sun.jersey.spi.container.ContainerResponseFilter;
public class CharsetResponseFilter implements ContainerResponseFilter {
@Override
public ContainerResponse filter(final ContainerRequest request,
final ContainerResponse response) {
final MediaType contentType = response.getMediaType();
if (contentType != null) {
response.getHttpHeaders().putSingle(HttpHeaders.CONTENT_TYPE,
contentType.toString() + ";charset=UTF-8");
}
return response;
}
}

View File

@ -1,33 +0,0 @@
package filters;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
import com.sun.jersey.spi.container.ContainerRequest;
import com.sun.jersey.spi.container.ContainerResponse;
import com.sun.jersey.spi.container.ContainerResponseFilter;
public class CorsResponseFilter implements ContainerResponseFilter {
@Override
public ContainerResponse filter(final ContainerRequest req,
final ContainerResponse contResp) {
final ResponseBuilder resp = Response.fromResponse(contResp
.getResponse());
resp.header("Access-Control-Allow-Origin", "*").header(
"Access-Control-Allow-Methods", "GET, POST, OPTIONS");
final String reqHead = req
.getHeaderValue("Access-Control-Request-Headers");
if (null != reqHead && !reqHead.equals("")) {
resp.header("Access-Control-Allow-Headers", reqHead);
}
contResp.setResponse(resp.build());
return contResp;
}
}

View File

@ -37,7 +37,6 @@ import org.hl7.fhir.instance.model.api.IBaseResource;
import ca.uhn.fhir.context.ConfigurationException;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.context.FhirVersionEnum;
import ca.uhn.fhir.model.api.annotation.Description;
import ca.uhn.fhir.model.primitive.IdDt;
import ca.uhn.fhir.model.valueset.BundleTypeEnum;
@ -140,6 +139,21 @@ public class SearchMethodBinding extends BaseResourceReturningMethodBinding {
@Override
public boolean incomingServerRequestMatchesMethod(RequestDetails theRequest) {
String clientPreference = theRequest.getHeader(Constants.HEADER_PREFER);
boolean lenientHandling = false;
if(clientPreference != null)
{
String[] preferences = clientPreference.split(";");
for( String p : preferences){
if("handling:lenient".equalsIgnoreCase(p))
{
lenientHandling = true;
break;
}
}
}
if (theRequest.getId() != null && myIdParamIndex == null) {
ourLog.trace("Method {} doesn't match because ID is not null: {}", theRequest.getId());
return false;
@ -235,6 +249,9 @@ public class SearchMethodBinding extends BaseResourceReturningMethodBinding {
}
}
Set<String> keySet = theRequest.getParameters().keySet();
if(lenientHandling == true)
return true;
if (myAllowUnknownParams == false) {
for (String next : keySet) {
if (!methodParamsTemp.contains(next)) {
@ -271,7 +288,7 @@ public class SearchMethodBinding extends BaseResourceReturningMethodBinding {
}
@Override
public IBundleProvider invokeServer(IRestfulServer theServer, RequestDetails theRequest, Object[] theMethodParams) throws InvalidRequestException, InternalErrorException {
public IBundleProvider invokeServer(IRestfulServer<?> theServer, RequestDetails theRequest, Object[] theMethodParams) throws InvalidRequestException, InternalErrorException {
if (myIdParamIndex != null) {
theMethodParams[myIdParamIndex] = theRequest.getId();
}

View File

@ -61,7 +61,6 @@ import ca.uhn.fhir.model.valueset.BundleTypeEnum;
import ca.uhn.fhir.parser.IParser;
import ca.uhn.fhir.rest.api.PreferReturnEnum;
import ca.uhn.fhir.rest.api.SummaryEnum;
import ca.uhn.fhir.rest.client.apache.ApacheHttpRequest;
import ca.uhn.fhir.rest.client.api.IHttpRequest;
import ca.uhn.fhir.rest.method.ElementsParameter;
import ca.uhn.fhir.rest.method.RequestDetails;
@ -378,7 +377,7 @@ public class RestfulServerUtils {
break;
}
} catch (IllegalArgumentException e) {
ourLog.debug("Invalid {} parameger: {}", Constants.PARAM_NARRATIVE, narrative[0]);
ourLog.debug("Invalid {} parameter: {}", Constants.PARAM_NARRATIVE, narrative[0]);
}
}
}

View File

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<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">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>

View File

@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>hapi-fhir-standalone-overlay-example</name>
<comment>NO_M2ECLIPSE_SUPPORT: Project files created with the maven-eclipse-plugin are not supported in M2Eclipse.</comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.wst.common.project.facet.core.builder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jem.workbench.JavaEMFNature</nature>
<nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
</natures>
</projectDescription>

View File

@ -0,0 +1,2 @@
eclipse.preferences.version=1
encoding/<project>=UTF-8

View File

@ -0,0 +1,4 @@
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1

View File

@ -0,0 +1,79 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>ca.uhn.hapi.fhir</groupId>
<artifactId>hapi-fhir</artifactId>
<version>2.1-SNAPSHOT</version>
</parent>
<artifactId>hapi-fhir-standalone-overlay-example</artifactId>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<overlays>
<overlay>
<groupId>ca.uhn.hapi.fhir</groupId>
<artifactId>hapi-fhir-testpage-overlay</artifactId>
</overlay>
</overlays>
<warName>fhirtester</warName>
<failOnMissingWebXml>true</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>ca.uhn.hapi.fhir</groupId>
<artifactId>hapi-fhir-testpage-overlay</artifactId>
<version>2.1-SNAPSHOT</version>
<type>war</type>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>ca.uhn.hapi.fhir</groupId>
<artifactId>hapi-fhir-testpage-overlay</artifactId>
<version>2.1-SNAPSHOT</version>
<classifier>classes</classifier>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-webapp</artifactId>
</dependency>
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>com.google.inject.extensions</groupId>
<artifactId>guice-servlet</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>jersey-guice</artifactId>
<version>1.19.1</version>
</dependency>
<dependency>
<groupId>org.ebaysf.web</groupId>
<artifactId>cors-filter</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,51 @@
package embedded.example;
import javax.inject.Singleton;
import javax.servlet.ServletContextEvent;
import org.ebaysf.web.cors.CORSFilter;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.servlet.GuiceServletContextListener;
import com.sun.jersey.guice.JerseyServletModule;
public class ContextListener extends GuiceServletContextListener {
static String username;
static String password;
static String serverAddress;
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
super.contextInitialized(servletContextEvent);
username = servletContextEvent.getServletContext().getInitParameter("username") != null
? servletContextEvent.getServletContext().getInitParameter("username")
: null;
password = servletContextEvent.getServletContext().getInitParameter("password") != null
? servletContextEvent.getServletContext().getInitParameter("password")
: null;
serverAddress = servletContextEvent.getServletContext().getInitParameter("serverAddress") != null
? servletContextEvent.getServletContext().getInitParameter("serverAddress")
: null;
}
@Override
protected Injector getInjector() {
return Guice.createInjector(new JerseyServletModule() {
@Override
protected void configureServlets() {
AnnotationConfigWebApplicationContext webApp = new AnnotationConfigWebApplicationContext();
webApp.setConfigLocation(FhirTesterConfig.class.getName());
serve("/*").with(new DispatcherServlet(webApp));
bind(CORSFilter.class).in(Singleton.class);
filter("/*").through(CORSFilter.class);
}
});
}
}

View File

@ -0,0 +1,73 @@
package embedded.example;
import javax.servlet.http.HttpServletRequest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import com.google.common.base.Strings;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.context.FhirVersionEnum;
import ca.uhn.fhir.rest.client.IGenericClient;
import ca.uhn.fhir.rest.client.interceptor.BasicAuthInterceptor;
import ca.uhn.fhir.to.FhirTesterMvcConfig;
import ca.uhn.fhir.to.TesterConfig;
import ca.uhn.fhir.util.ITestingUiClientFactory;
//@formatter:off
/**
* This spring config file configures the web testing module. It serves two
* purposes: 1. It imports FhirTesterMvcConfig, which is the spring config for
* the tester itself 2. It tells the tester which server(s) to talk to, via the
* testerConfig() method below
*/
@Configuration
@Import(FhirTesterMvcConfig.class)
public class FhirTesterConfig {
/**
* This bean tells the testing webpage which servers it should configure
* itself to communicate with. In this example we configure it to talk to
* the local server, as well as one public server. If you are creating a
* project to deploy somewhere else, you might choose to only put your own
* server's address here.
*
* Note the use of the ${serverBase} variable below. This will be replaced
* with the base URL as reported by the server itself. Often for a simple
* Tomcat (or other container) installation, this will end up being
* something like "http://localhost:8080/hapi-fhir-jpaserver-example". If
* you are deploying your server to a place with a fully qualified domain
* name, you might want to use that instead of using the variable.
*/
@Bean
public TesterConfig testerConfig() {
final TesterConfig retVal = new TesterConfig();
retVal.addServer().withId("Test-Server").withFhirVersion(FhirVersionEnum.DSTU2)
.withBaseUrl(ContextListener.serverAddress).withName("FHIR Server Test Front End");
if (!Strings.isNullOrEmpty(ContextListener.username)) {
ITestingUiClientFactory clientFactory = new 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(ContextListener.username, ContextListener.password));
return client;
}
};
retVal.setClientFactory(clientFactory);
}
return retVal;
}
}
// @formatter:on

View File

@ -0,0 +1,22 @@
<?xml version="1.0"?>
<web-app>
<filter>
<filter-name>Guice Filter</filter-name>
<filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>Guice Filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>embedded.example.ContextListener</listener-class>
</listener>
<!-- <context-param> <param-name>username</param-name> <param-value>username</param-value>
</context-param> <context-param> <param-name>password</param-name> <param-value>password</param-value>
</context-param> -->
<context-param>
<param-name>serverAddress</param-name>
<param-value>http://fhirtest.uhn.ca/baseDstu2</param-value>
</context-param>
</web-app>

View File

@ -0,0 +1,14 @@
package test;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;
public class WarTester {
public static void main(String[] args) throws Exception {
final Server server = new Server(8080);
server.setHandler(new WebAppContext("target/fhirtester.war", "/"));
server.start();
}
}