Added new example

This commit is contained in:
Jens Kristian Villadsen 2016-11-09 11:38:18 +01:00
parent 0a93db572e
commit fab75f1add
15 changed files with 357 additions and 48 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,30 @@
<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>
</dependencies>
<build>
<plugins>
<plugin>
@ -56,5 +61,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;
@ -16,10 +17,12 @@ public class ServerStartup {
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

@ -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();
}
}