Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for websockets #20

Merged
merged 2 commits into from
May 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ mvn jetty:run

Then, browse to the following link to use the server:

[http:https://localhost:8080/](http:https://localhost:8080/)
[http:https://localhost:8080/hapi-fhir-jpaserver/](http:https://localhost:8080/hapi-fhir-jpaserver/)

# Deploying to a Container

Expand Down Expand Up @@ -74,3 +74,14 @@ FLUSH PRIVILEGES;
* hibernate.dialect=org.hibernate.dialect.MySQL5Dialect

It is important to use MySQL5Dialect when using MySQL version 5+.

# Enabling Subscriptions

The server may be configured with subscription support by enabling properties in the [hapi.properties](https://github.com/hapifhir/hapi-fhir-jpaserver-starter/blob/master/src/main/resources/hapi.properties) file:

* `subscription.resthook.enabled` - Enables REST Hook subscriptions, where the server will make an outgoing connection to a remote REST server

* `subscription.email.enabled` - Enables email subscriptions. Note that you must also provide the connection details for a usable SMTP server.

* `subscription.websocket.enabled` - Enables websocket subscriptions. With this enabled, your server will accept incoming websocket connections on the following URL (this example uses the default context path and port, you may need to tweak depending on your deployment environment): [ws:https://localhost:8080/hapi-fhir-jpaserver/websocket](ws:https://localhost:8080/hapi-fhir-jpaserver/websocket)

12 changes: 2 additions & 10 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<parent>
<groupId>ca.uhn.hapi.fhir</groupId>
<artifactId>hapi-fhir</artifactId>
<version>3.7.0</version>
<version>3.8.0-SNAPSHOT</version>
</parent>

<groupId>ca.uhn.hapi.fhir.demo</groupId>
Expand Down Expand Up @@ -129,14 +129,6 @@
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derbynet</artifactId>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derbyclient</artifactId>
</dependency>

<!-- The following dependencies are only needed for automated unit tests, you do not neccesarily need them to run the example. -->
<dependency>
Expand Down Expand Up @@ -204,7 +196,7 @@
<version>9.4.8.v20180619</version>
<configuration>
<webApp>
<contextPath>/</contextPath>
<contextPath>/hapi-fhir-jpaserver</contextPath>
<allowDuplicateFragmentNames>true</allowDuplicateFragmentNames>
</webApp>
</configuration>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public DatabaseBackedPagingProvider databaseBackedPagingProvider() {
@Bean()
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean retVal = super.entityManagerFactory();
retVal.setPersistenceUnitName(HapiProperties.getPersistenceUnitName());
retVal.setPersistenceUnitName("HAPI_PU");

try {
retVal.setDataSource(myDataSource);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public DatabaseBackedPagingProvider databaseBackedPagingProvider() {
@Bean()
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean retVal = super.entityManagerFactory();
retVal.setPersistenceUnitName(HapiProperties.getPersistenceUnitName());
retVal.setPersistenceUnitName("HAPI_PU");

try {
retVal.setDataSource(myDataSource);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public DatabaseBackedPagingProvider databaseBackedPagingProvider() {
@Bean()
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean retVal = super.entityManagerFactory();
retVal.setPersistenceUnitName(HapiProperties.getPersistenceUnitName());
retVal.setPersistenceUnitName("HAPI_PU");

try {
retVal.setDataSource(myDataSource);
Expand Down
10 changes: 0 additions & 10 deletions src/main/java/ca/uhn/fhir/jpa/starter/HapiProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ public class HapiProperties {
static final String LOGGER_NAME = "logger.name";
static final String MAX_FETCH_SIZE = "max_fetch_size";
static final String MAX_PAGE_SIZE = "max_page_size";
static final String PERSISTENCE_UNIT_NAME = "persistence_unit_name";
static final String SERVER_ADDRESS = "server_address";
static final String SERVER_BASE = "server.base";
static final String SERVER_ID = "server.id";
static final String SERVER_NAME = "server.name";
static final String SUBSCRIPTION_EMAIL_ENABLED = "subscription.email.enabled";
Expand Down Expand Up @@ -201,10 +199,6 @@ public static Integer getMaximumFetchSize() {
return HapiProperties.getIntegerProperty(MAX_FETCH_SIZE, Integer.MAX_VALUE);
}

public static String getPersistenceUnitName() {
return HapiProperties.getProperty(PERSISTENCE_UNIT_NAME, "HAPI_PU");
}

public static String getLoggerName() {
return HapiProperties.getProperty(LOGGER_NAME, "fhirtest.access");
}
Expand Down Expand Up @@ -269,10 +263,6 @@ public static String getCorsAllowedOrigin() {
return HapiProperties.getProperty(CORS_ALLOWED_ORIGIN, "*");
}

public static String getServerBase() {
return HapiProperties.getProperty(SERVER_BASE, "/fhir");
}

public static String getServerName() {
return HapiProperties.getProperty(SERVER_NAME, "Local Tester");
}
Expand Down
14 changes: 8 additions & 6 deletions src/main/resources/hapi.properties
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@

# Adjust this to set the version of FHIR supported by this server. See
# FhirVersionEnum for a list of available constants.
# FhirVersionEnum for a list of available constants. Example values include
# DSTU2, DSTU3, R4.
fhir_version=DSTU3

# This is the address that the FHIR server will report as its own address.
# If this server will be deployed (for example) to an internet accessible
# server, put the DNS name of that server here.
server_address=http:https://localhost:8080/fhir/

# This is the context path for the FHIR endpoint. If this is changed, the
# setting above should also be changed.
server.base=/fhir
#
# Note that this is also the address that the hapi-fhir-testpage-overlay
# (the web UI similar to the one at http:https://hapi.fhir.org) will use to
# connect internally to the FHIR server, so this also needs to be a name
# accessible from the server itself.
server_address=http:https://localhost:8080/hapi-fhir-jpaserver/fhir/

default_encoding=JSON
etag_support=ENABLED
Expand Down
16 changes: 5 additions & 11 deletions src/test/java/ca/uhn/fhir/jpa/starter/ExampleServerDstu2IT.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
import org.junit.BeforeClass;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;

import static org.junit.Assert.assertEquals;
Expand All @@ -33,14 +31,13 @@ public class ExampleServerDstu2IT {
HapiProperties.forceReload();
HapiProperties.setProperty(HapiProperties.FHIR_VERSION, "DSTU2");
HapiProperties.setProperty(HapiProperties.DATASOURCE_URL, "jdbc:derby:memory:dbr2;create=true");
HapiProperties.setProperty(HapiProperties.TEST_PORT, Integer.toString(PortUtil.findFreePort()));
ourCtx = FhirContext.forDstu2();
ourPort = HapiProperties.getTestPort();
ourPort = PortUtil.findFreePort();
}

@Test
public void testCreateAndRead() throws IOException {
ourLog.info("Base URL is: http:https://localhost:" + ourPort + HapiProperties.getServerBase());
public void testCreateAndRead() {
ourLog.info("Base URL is: " + HapiProperties.getServerAddress());
String methodName = "testCreateResourceConditional";

Patient pt = new Patient();
Expand All @@ -62,13 +59,10 @@ public static void beforeClass() throws Exception {

ourLog.info("Project base path is: {}", path);

if (ourPort == 0) {
ourPort = RandomServerPortProvider.findFreePort();
}
ourServer = new Server(ourPort);

WebAppContext webAppContext = new WebAppContext();
webAppContext.setContextPath("/");
webAppContext.setContextPath("/hapi-fhir-jpaserver");
webAppContext.setDescriptor(path + "/src/main/webapp/WEB-INF/web.xml");
webAppContext.setResourceBase(path + "/target/hapi-fhir-jpaserver-starter");
webAppContext.setParentLoaderPriority(true);
Expand All @@ -78,7 +72,7 @@ public static void beforeClass() throws Exception {

ourCtx.getRestfulClientFactory().setServerValidationMode(ServerValidationModeEnum.NEVER);
ourCtx.getRestfulClientFactory().setSocketTimeout(1200 * 1000);
ourServerBase = "http:https://localhost:" + ourPort + HapiProperties.getServerBase();
ourServerBase = "http:https://localhost:" + ourPort + "/hapi-fhir-jpaserver/fhir/";
ourClient = ourCtx.newRestfulGenericClient(ourServerBase);
ourClient.registerInterceptor(new LoggingInterceptor(true));
}
Expand Down
13 changes: 6 additions & 7 deletions src/test/java/ca/uhn/fhir/jpa/starter/ExampleServerDstu3IT.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,14 @@ public class ExampleServerDstu3IT {
HapiProperties.forceReload();
HapiProperties.setProperty(HapiProperties.FHIR_VERSION, "DSTU3");
HapiProperties.setProperty(HapiProperties.DATASOURCE_URL, "jdbc:derby:memory:dbr3;create=true");
HapiProperties.setProperty(HapiProperties.TEST_PORT, Integer.toString(PortUtil.findFreePort()));
HapiProperties.setProperty(HapiProperties.SUBSCRIPTION_WEBSOCKET_ENABLED, "true");
ourCtx = FhirContext.forDstu3();
ourPort = HapiProperties.getTestPort();
ourPort = PortUtil.findFreePort();
}

@Test
public void testCreateAndRead() throws IOException {
ourLog.info("Base URL is: http:https://localhost:" + ourPort + HapiProperties.getServerBase());
public void testCreateAndRead() {
ourLog.info("Base URL is: " + HapiProperties.getServerAddress());
String methodName = "testCreateResourceConditional";

Patient pt = new Patient();
Expand Down Expand Up @@ -92,7 +91,7 @@ public void testWebsocketSubscription() throws Exception {
SocketImplementation mySocketImplementation = new SocketImplementation(mySubscriptionId.getIdPart(), EncodingEnum.JSON);

myWebSocketClient.start();
URI echoUri = new URI("ws:https://localhost:" + ourPort + "/websocket");
URI echoUri = new URI("ws:https://localhost:" + ourPort + "/hapi-fhir-jpaserver/websocket");
ClientUpgradeRequest request = new ClientUpgradeRequest();
ourLog.info("Connecting to : {}", echoUri);
Future<Session> connection = myWebSocketClient.connect(mySocketImplementation, echoUri, request);
Expand Down Expand Up @@ -138,7 +137,7 @@ public static void beforeClass() throws Exception {
ourServer = new Server(ourPort);

WebAppContext webAppContext = new WebAppContext();
webAppContext.setContextPath("/");
webAppContext.setContextPath("/hapi-fhir-jpaserver");
webAppContext.setDescriptor(path + "/src/main/webapp/WEB-INF/web.xml");
webAppContext.setResourceBase(path + "/target/hapi-fhir-jpaserver-starter");
webAppContext.setParentLoaderPriority(true);
Expand All @@ -148,7 +147,7 @@ public static void beforeClass() throws Exception {

ourCtx.getRestfulClientFactory().setServerValidationMode(ServerValidationModeEnum.NEVER);
ourCtx.getRestfulClientFactory().setSocketTimeout(1200 * 1000);
ourServerBase = "http:https://localhost:" + ourPort + HapiProperties.getServerBase();
ourServerBase = "http:https://localhost:" + ourPort + "/hapi-fhir-jpaserver/fhir/";
ourClient = ourCtx.newRestfulGenericClient(ourServerBase);
ourClient.registerInterceptor(new LoggingInterceptor(true));
}
Expand Down
16 changes: 8 additions & 8 deletions src/test/java/ca/uhn/fhir/jpa/starter/ExampleServerR4IT.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.junit.BeforeClass;
import org.junit.Test;

import java.io.IOException;
import java.net.URI;
import java.nio.file.Paths;
import java.util.concurrent.Future;
Expand All @@ -45,15 +44,14 @@ public class ExampleServerR4IT {
HapiProperties.forceReload();
HapiProperties.setProperty(HapiProperties.DATASOURCE_URL, "jdbc:derby:memory:dbr4;create=true");
HapiProperties.setProperty(HapiProperties.FHIR_VERSION, "R4");
HapiProperties.setProperty(HapiProperties.TEST_PORT, Integer.toString(PortUtil.findFreePort()));
HapiProperties.setProperty(HapiProperties.SUBSCRIPTION_WEBSOCKET_ENABLED, "true");
ourCtx = FhirContext.forR4();
ourPort = HapiProperties.getTestPort();
ourPort = PortUtil.findFreePort();
}

@Test
public void testCreateAndRead() throws IOException {
ourLog.info("Base URL is: http:https://localhost:" + ourPort + HapiProperties.getServerBase());
public void testCreateAndRead() {
ourLog.info("Base URL is: " + HapiProperties.getServerAddress());
String methodName = "testCreateResourceConditional";

Patient pt = new Patient();
Expand Down Expand Up @@ -93,7 +91,7 @@ public void testWebsocketSubscription() throws Exception {
SocketImplementation mySocketImplementation = new SocketImplementation(mySubscriptionId.getIdPart(), EncodingEnum.JSON);

myWebSocketClient.start();
URI echoUri = new URI("ws:https://localhost:" + ourPort + "/websocket");
URI echoUri = new URI("ws:https://localhost:" + ourPort + "/hapi-fhir-jpaserver/websocket");
ClientUpgradeRequest request = new ClientUpgradeRequest();
ourLog.info("Connecting to : {}", echoUri);
Future<Session> connection = myWebSocketClient.connect(mySocketImplementation, echoUri, request);
Expand Down Expand Up @@ -139,7 +137,7 @@ public static void beforeClass() throws Exception {
ourServer = new Server(ourPort);

WebAppContext webAppContext = new WebAppContext();
webAppContext.setContextPath("/");
webAppContext.setContextPath("/hapi-fhir-jpaserver");
webAppContext.setDisplayName("HAPI FHIR");
webAppContext.setDescriptor(path + "/src/main/webapp/WEB-INF/web.xml");
webAppContext.setResourceBase(path + "/target/hapi-fhir-jpaserver-starter");
Expand All @@ -150,7 +148,9 @@ public static void beforeClass() throws Exception {

ourCtx.getRestfulClientFactory().setServerValidationMode(ServerValidationModeEnum.NEVER);
ourCtx.getRestfulClientFactory().setSocketTimeout(1200 * 1000);
ourServerBase = "http:https://localhost:" + ourPort + HapiProperties.getServerBase();
ourServerBase = HapiProperties.getServerAddress();
ourServerBase = "http:https://localhost:" + ourPort + "/hapi-fhir-jpaserver/fhir/";

ourClient = ourCtx.newRestfulGenericClient(ourServerBase);
ourClient.registerInterceptor(new LoggingInterceptor(true));
}
Expand Down