Update the examples
This commit is contained in:
@@ -1,18 +1,5 @@
|
|||||||
|
|
||||||
package ca.uhn.fhir.jpa.demo;
|
package ca.uhn.fhir.jpa.demo;
|
||||||
|
|
||||||
import java.util.Collection;
|
|
||||||
import java.util.List;
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
|
|
||||||
import javax.servlet.ServletException;
|
|
||||||
|
|
||||||
import org.hl7.fhir.dstu3.model.Bundle;
|
|
||||||
import org.hl7.fhir.dstu3.model.Meta;
|
|
||||||
import org.springframework.web.context.ContextLoaderListener;
|
|
||||||
import org.springframework.web.context.WebApplicationContext;
|
|
||||||
|
|
||||||
import ca.uhn.fhir.context.FhirContext;
|
import ca.uhn.fhir.context.FhirContext;
|
||||||
import ca.uhn.fhir.context.FhirVersionEnum;
|
import ca.uhn.fhir.context.FhirVersionEnum;
|
||||||
import ca.uhn.fhir.jpa.dao.DaoConfig;
|
import ca.uhn.fhir.jpa.dao.DaoConfig;
|
||||||
@@ -21,139 +8,166 @@ import ca.uhn.fhir.jpa.provider.JpaConformanceProviderDstu2;
|
|||||||
import ca.uhn.fhir.jpa.provider.JpaSystemProviderDstu2;
|
import ca.uhn.fhir.jpa.provider.JpaSystemProviderDstu2;
|
||||||
import ca.uhn.fhir.jpa.provider.dstu3.JpaConformanceProviderDstu3;
|
import ca.uhn.fhir.jpa.provider.dstu3.JpaConformanceProviderDstu3;
|
||||||
import ca.uhn.fhir.jpa.provider.dstu3.JpaSystemProviderDstu3;
|
import ca.uhn.fhir.jpa.provider.dstu3.JpaSystemProviderDstu3;
|
||||||
|
import ca.uhn.fhir.jpa.provider.dstu3.TerminologyUploaderProviderDstu3;
|
||||||
import ca.uhn.fhir.jpa.search.DatabaseBackedPagingProvider;
|
import ca.uhn.fhir.jpa.search.DatabaseBackedPagingProvider;
|
||||||
|
import ca.uhn.fhir.jpa.subscription.resthook.SubscriptionRestHookInterceptor;
|
||||||
import ca.uhn.fhir.model.dstu2.composite.MetaDt;
|
import ca.uhn.fhir.model.dstu2.composite.MetaDt;
|
||||||
import ca.uhn.fhir.narrative.DefaultThymeleafNarrativeGenerator;
|
import ca.uhn.fhir.narrative.DefaultThymeleafNarrativeGenerator;
|
||||||
import ca.uhn.fhir.rest.api.EncodingEnum;
|
import ca.uhn.fhir.rest.api.EncodingEnum;
|
||||||
import ca.uhn.fhir.rest.server.*;
|
import ca.uhn.fhir.rest.server.ETagSupportEnum;
|
||||||
|
import ca.uhn.fhir.rest.server.HardcodedServerAddressStrategy;
|
||||||
|
import ca.uhn.fhir.rest.server.IResourceProvider;
|
||||||
|
import ca.uhn.fhir.rest.server.RestfulServer;
|
||||||
import ca.uhn.fhir.rest.server.interceptor.IServerInterceptor;
|
import ca.uhn.fhir.rest.server.interceptor.IServerInterceptor;
|
||||||
|
import org.hl7.fhir.dstu3.model.Bundle;
|
||||||
|
import org.hl7.fhir.dstu3.model.Meta;
|
||||||
|
import org.springframework.web.context.ContextLoaderListener;
|
||||||
|
import org.springframework.web.context.WebApplicationContext;
|
||||||
|
|
||||||
|
import javax.servlet.ServletException;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public class JpaServerDemo extends RestfulServer {
|
public class JpaServerDemo extends RestfulServer {
|
||||||
|
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
private WebApplicationContext myAppCtx;
|
private WebApplicationContext myAppCtx;
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
@Override
|
@Override
|
||||||
protected void initialize() throws ServletException {
|
protected void initialize() throws ServletException {
|
||||||
super.initialize();
|
super.initialize();
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* We want to support FHIR DSTU2 format. This means that the server
|
* We want to support FHIR DSTU2 format. This means that the server
|
||||||
* will use the DSTU2 bundle format and other DSTU2 encoding changes.
|
* will use the DSTU2 bundle format and other DSTU2 encoding changes.
|
||||||
*
|
*
|
||||||
* If you want to use DSTU1 instead, change the following line, and change the 2 occurrences of dstu2 in web.xml to dstu1
|
* If you want to use DSTU1 instead, change the following line, and change the 2 occurrences of dstu2 in web.xml to dstu1
|
||||||
*/
|
*/
|
||||||
FhirVersionEnum fhirVersion = FhirVersionEnum.DSTU3;
|
FhirVersionEnum fhirVersion = FhirVersionEnum.DSTU3;
|
||||||
setFhirContext(new FhirContext(fhirVersion));
|
setFhirContext(new FhirContext(fhirVersion));
|
||||||
|
|
||||||
// Get the spring context from the web container (it's declared in web.xml)
|
// Get the spring context from the web container (it's declared in web.xml)
|
||||||
myAppCtx = ContextLoaderListener.getCurrentWebApplicationContext();
|
myAppCtx = ContextLoaderListener.getCurrentWebApplicationContext();
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* The BaseJavaConfigDstu2.java class is a spring configuration
|
* The BaseJavaConfigDstu2.java class is a spring configuration
|
||||||
* file which is automatically generated as a part of hapi-fhir-jpaserver-base and
|
* file which is automatically generated as a part of hapi-fhir-jpaserver-base and
|
||||||
* contains bean definitions for a resource provider for each resource type
|
* contains bean definitions for a resource provider for each resource type
|
||||||
*/
|
*/
|
||||||
String resourceProviderBeanName;
|
String resourceProviderBeanName;
|
||||||
if (fhirVersion == FhirVersionEnum.DSTU2) {
|
if (fhirVersion == FhirVersionEnum.DSTU2) {
|
||||||
resourceProviderBeanName = "myResourceProvidersDstu2";
|
resourceProviderBeanName = "myResourceProvidersDstu2";
|
||||||
} else if (fhirVersion == FhirVersionEnum.DSTU3) {
|
} else if (fhirVersion == FhirVersionEnum.DSTU3) {
|
||||||
resourceProviderBeanName = "myResourceProvidersDstu3";
|
resourceProviderBeanName = "myResourceProvidersDstu3";
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalStateException();
|
throw new IllegalStateException();
|
||||||
}
|
}
|
||||||
List<IResourceProvider> beans = myAppCtx.getBean(resourceProviderBeanName, List.class);
|
List<IResourceProvider> beans = myAppCtx.getBean(resourceProviderBeanName, List.class);
|
||||||
setResourceProviders(beans);
|
setResourceProviders(beans);
|
||||||
|
|
||||||
/*
|
|
||||||
* The system provider implements non-resource-type methods, such as
|
|
||||||
* transaction, and global history.
|
|
||||||
*/
|
|
||||||
Object systemProvider;
|
|
||||||
if (fhirVersion == FhirVersionEnum.DSTU2) {
|
|
||||||
systemProvider = myAppCtx.getBean("mySystemProviderDstu2", JpaSystemProviderDstu2.class);
|
|
||||||
} else if (fhirVersion == FhirVersionEnum.DSTU3) {
|
|
||||||
systemProvider = myAppCtx.getBean("mySystemProviderDstu3", JpaSystemProviderDstu3.class);
|
|
||||||
} else {
|
|
||||||
throw new IllegalStateException();
|
|
||||||
}
|
|
||||||
setPlainProviders(systemProvider);
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* The conformance provider exports the supported resources, search parameters, etc for
|
* The system provider implements non-resource-type methods, such as
|
||||||
* this server. The JPA version adds resource counts to the exported statement, so it
|
* transaction, and global history.
|
||||||
* is a nice addition.
|
*/
|
||||||
*/
|
Object systemProvider;
|
||||||
if (fhirVersion == FhirVersionEnum.DSTU2) {
|
if (fhirVersion == FhirVersionEnum.DSTU2) {
|
||||||
IFhirSystemDao<ca.uhn.fhir.model.dstu2.resource.Bundle, MetaDt> systemDao = myAppCtx.getBean("mySystemDaoDstu2", IFhirSystemDao.class);
|
systemProvider = myAppCtx.getBean("mySystemProviderDstu2", JpaSystemProviderDstu2.class);
|
||||||
JpaConformanceProviderDstu2 confProvider = new JpaConformanceProviderDstu2(this, systemDao,
|
} else if (fhirVersion == FhirVersionEnum.DSTU3) {
|
||||||
myAppCtx.getBean(DaoConfig.class));
|
systemProvider = myAppCtx.getBean("mySystemProviderDstu3", JpaSystemProviderDstu3.class);
|
||||||
confProvider.setImplementationDescription("Example Server");
|
} else {
|
||||||
setServerConformanceProvider(confProvider);
|
throw new IllegalStateException();
|
||||||
} else if (fhirVersion == FhirVersionEnum.DSTU3) {
|
}
|
||||||
IFhirSystemDao<Bundle, Meta> systemDao = myAppCtx.getBean("mySystemDaoDstu3", IFhirSystemDao.class);
|
setPlainProviders(systemProvider);
|
||||||
JpaConformanceProviderDstu3 confProvider = new JpaConformanceProviderDstu3(this, systemDao,
|
|
||||||
myAppCtx.getBean(DaoConfig.class));
|
|
||||||
confProvider.setImplementationDescription("Example Server");
|
|
||||||
setServerConformanceProvider(confProvider);
|
|
||||||
} else {
|
|
||||||
throw new IllegalStateException();
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Enable ETag Support (this is already the default)
|
* The conformance provider exports the supported resources, search parameters, etc for
|
||||||
*/
|
* this server. The JPA version adds resource counts to the exported statement, so it
|
||||||
setETagSupport(ETagSupportEnum.ENABLED);
|
* is a nice addition.
|
||||||
|
*/
|
||||||
|
if (fhirVersion == FhirVersionEnum.DSTU2) {
|
||||||
|
IFhirSystemDao<ca.uhn.fhir.model.dstu2.resource.Bundle, MetaDt> systemDao = myAppCtx.getBean("mySystemDaoDstu2", IFhirSystemDao.class);
|
||||||
|
JpaConformanceProviderDstu2 confProvider = new JpaConformanceProviderDstu2(this, systemDao,
|
||||||
|
myAppCtx.getBean(DaoConfig.class));
|
||||||
|
confProvider.setImplementationDescription("Example Server");
|
||||||
|
setServerConformanceProvider(confProvider);
|
||||||
|
} else if (fhirVersion == FhirVersionEnum.DSTU3) {
|
||||||
|
IFhirSystemDao<Bundle, Meta> systemDao = myAppCtx.getBean("mySystemDaoDstu3", IFhirSystemDao.class);
|
||||||
|
JpaConformanceProviderDstu3 confProvider = new JpaConformanceProviderDstu3(this, systemDao,
|
||||||
|
myAppCtx.getBean(DaoConfig.class));
|
||||||
|
confProvider.setImplementationDescription("Example Server");
|
||||||
|
setServerConformanceProvider(confProvider);
|
||||||
|
} else {
|
||||||
|
throw new IllegalStateException();
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* This server tries to dynamically generate narratives
|
* Enable ETag Support (this is already the default)
|
||||||
*/
|
*/
|
||||||
FhirContext ctx = getFhirContext();
|
setETagSupport(ETagSupportEnum.ENABLED);
|
||||||
ctx.setNarrativeGenerator(new DefaultThymeleafNarrativeGenerator());
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Default to JSON and pretty printing
|
* This server tries to dynamically generate narratives
|
||||||
*/
|
*/
|
||||||
setDefaultPrettyPrint(true);
|
FhirContext ctx = getFhirContext();
|
||||||
setDefaultResponseEncoding(EncodingEnum.JSON);
|
ctx.setNarrativeGenerator(new DefaultThymeleafNarrativeGenerator());
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* -- New in HAPI FHIR 1.5 --
|
* Default to JSON and pretty printing
|
||||||
* This configures the server to page search results to and from
|
*/
|
||||||
* the database, instead of only paging them to memory. This may mean
|
setDefaultPrettyPrint(true);
|
||||||
* a performance hit when performing searches that return lots of results,
|
setDefaultResponseEncoding(EncodingEnum.JSON);
|
||||||
* but makes the server much more scalable.
|
|
||||||
*/
|
|
||||||
setPagingProvider(myAppCtx.getBean(DatabaseBackedPagingProvider.class));
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Load interceptors for the server from Spring (these are defined in FhirServerConfig.java)
|
* -- New in HAPI FHIR 1.5 --
|
||||||
*/
|
* This configures the server to page search results to and from
|
||||||
Collection<IServerInterceptor> interceptorBeans = myAppCtx.getBeansOfType(IServerInterceptor.class).values();
|
* the database, instead of only paging them to memory. This may mean
|
||||||
for (IServerInterceptor interceptor : interceptorBeans) {
|
* a performance hit when performing searches that return lots of results,
|
||||||
this.registerInterceptor(interceptor);
|
* but makes the server much more scalable.
|
||||||
}
|
*/
|
||||||
|
setPagingProvider(myAppCtx.getBean(DatabaseBackedPagingProvider.class));
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* If you are hosting this server at a specific DNS name, the server will try to
|
* Load interceptors for the server from Spring (these are defined in FhirServerConfig.java)
|
||||||
* figure out the FHIR base URL based on what the web container tells it, but
|
*/
|
||||||
* this doesn't always work. If you are setting links in your search bundles that
|
Collection<IServerInterceptor> interceptorBeans = myAppCtx.getBeansOfType(IServerInterceptor.class).values();
|
||||||
* just refer to "localhost", you might want to use a server address strategy:
|
for (IServerInterceptor interceptor : interceptorBeans) {
|
||||||
*/
|
this.registerInterceptor(interceptor);
|
||||||
//setServerAddressStrategy(new HardcodedServerAddressStrategy("http://mydomain.com/fhir/baseDstu2"));
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* If you are using DSTU3+, you may want to add a terminology uploader, which allows
|
* If you are hosting this server at a specific DNS name, the server will try to
|
||||||
* uploading of external terminologies such as Snomed CT. Note that this uploader
|
* figure out the FHIR base URL based on what the web container tells it, but
|
||||||
* does not have any security attached (any anonymous user may use it by default)
|
* this doesn't always work. If you are setting links in your search bundles that
|
||||||
* so it is a potential security vulnerability. Consider using an AuthorizationInterceptor
|
* just refer to "localhost", you might want to use a server address strategy:
|
||||||
* with this feature.
|
*/
|
||||||
*/
|
if (false) { // <-- DISABLED RIGHT NOW
|
||||||
//if (fhirVersion == FhirVersionEnum.DSTU3) {
|
setServerAddressStrategy(new HardcodedServerAddressStrategy("http://mydomain.com/fhir/baseDstu3"));
|
||||||
// registerProvider(myAppCtx.getBean(TerminologyUploaderProviderDstu3.class));
|
}
|
||||||
//}
|
|
||||||
}
|
/*
|
||||||
|
* If you are using DSTU3+, you may want to add a terminology uploader, which allows
|
||||||
|
* uploading of external terminologies such as Snomed CT. Note that this uploader
|
||||||
|
* does not have any security attached (any anonymous user may use it by default)
|
||||||
|
* so it is a potential security vulnerability. Consider using an AuthorizationInterceptor
|
||||||
|
* with this feature.
|
||||||
|
*/
|
||||||
|
if (false) { // <-- DISABLED RIGHT NOW
|
||||||
|
if (fhirVersion == FhirVersionEnum.DSTU3) {
|
||||||
|
registerProvider(myAppCtx.getBean(TerminologyUploaderProviderDstu3.class));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* If you want to support subscriptions, you will want to register interceptors for
|
||||||
|
* any type of subscriptions you want to support.
|
||||||
|
*/
|
||||||
|
if (false) { // <-- DISABLED RIGHT NOW
|
||||||
|
SubscriptionRestHookInterceptor restHookInterceptor = myAppCtx.getBean(SubscriptionRestHookInterceptor.class);
|
||||||
|
registerInterceptor(restHookInterceptor);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user