Skip to content

Commit

Permalink
Servlet migration
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesagnew committed Oct 25, 2023
1 parent 2241e2f commit 7e27f45
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 52 deletions.
27 changes: 19 additions & 8 deletions hapi-hl7overhttp/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,18 +75,29 @@
<artifactId>commons-lang</artifactId>
<version>${commons-lang.version}</version>
</dependency>

<!-- Jetty -->
<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty</artifactId>
<version>${jetty.version}</version>
<optional>true</optional>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-sslengine</artifactId>
<version>${jetty.version}</version>
<optional>true</optional>
<groupId>org.eclipse.jetty.ee10</groupId>
<artifactId>jetty-ee10-servlets</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.ee10</groupId>
<artifactId>jetty-ee10-servlet</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ public class Hl7OverHttpRequestEncoder extends AbstractHl7OverHttpEncoder {
private String myHost;
private int myPort;

public Hl7OverHttpRequestEncoder() {
super();
}

public void setHost(String theHost) {
myHost = theHost;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public void setPreferredCharset(Charset thePreferredCharset) {
/**
* {@inheritDoc}
*/
@Override
public void writeMessage(String theRawMessage) throws LLPException, IOException {

AbstractHl7OverHttpEncoder e;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
import java.util.Enumeration;
import java.util.LinkedHashMap;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ca.uhn.hl7v2.hoh.api.DecodeException;
import ca.uhn.hl7v2.hoh.api.IAuthorizationServerCallback;
import ca.uhn.hl7v2.hoh.api.IMessageHandler;
Expand All @@ -21,6 +17,9 @@
import ca.uhn.hl7v2.hoh.sign.ISigner;
import ca.uhn.hl7v2.hoh.sign.SignatureVerificationException;
import ca.uhn.hl7v2.hoh.util.HTTPUtils;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

public class HohRawServlet extends HttpServlet {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package ca.uhn.hl7v2.hoh.util;

import ca.uhn.hl7v2.hoh.util.repackage.Base64;
import jakarta.servlet.ServletOutputStream;

import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

import javax.servlet.ServletOutputStream;

import ca.uhn.hl7v2.hoh.util.repackage.Base64;


public class HTTPUtils {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,11 @@
package ca.uhn.hl7v2.hoh.hapi.server;

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.concurrent.TimeUnit;


import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.servlet.Context;
import org.mortbay.jetty.servlet.ServletHolder;

import ca.uhn.hl7v2.AcknowledgmentCode;
import ca.uhn.hl7v2.DefaultHapiContext;
import ca.uhn.hl7v2.HL7Exception;
import ca.uhn.hl7v2.app.Connection;
import ca.uhn.hl7v2.app.ConnectionHub;
import ca.uhn.hl7v2.hoh.llp.Hl7OverHttpLowerLayerProtocol;
import ca.uhn.hl7v2.hoh.util.RandomServerPortProvider;
import ca.uhn.hl7v2.hoh.util.ServerRoleEnum;
import ca.uhn.hl7v2.model.Message;
import ca.uhn.hl7v2.model.v25.message.ACK;
Expand All @@ -33,6 +15,22 @@
import ca.uhn.hl7v2.protocol.ReceivingApplication;
import ca.uhn.hl7v2.protocol.ReceivingApplicationException;
import ca.uhn.hl7v2.validation.impl.ValidationContextFactory;
import org.eclipse.jetty.ee10.servlet.ServletContextHandler;
import org.eclipse.jetty.ee10.servlet.ServletHolder;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public class HohServletTest {

Expand Down Expand Up @@ -60,18 +58,23 @@ public void before() {
}

private void startServer(HohServlet theServlet) throws Exception {
myPort = RandomServerPortProvider.findFreePort();
myServer = new Server(myPort);
Context context = new Context(myServer, "/", Context.SESSIONS);
context.addServlet(new ServletHolder(theServlet), "/*");
myServer = new Server(0);

ServletContextHandler handler = new ServletContextHandler();
handler.addServlet(new ServletHolder(theServlet), "/*");

myServer.setHandler(handler);
myServer.start();

while (myServer.isStarting()) {
ourLog.info("Waiting for server to start...");
Thread.sleep(100);
}

Connector[] connectors = myServer.getConnectors();
assert connectors.length == 1;
myPort = ((ServerConnector) (connectors[0])).getLocalPort();

}

@Test
Expand Down Expand Up @@ -176,7 +179,6 @@ public void testServletXml() throws Exception {

@AfterClass
public static void afterClass() {
// Thread.sleep(1000000);
ourHapiContext.getExecutorService().shutdown();
}

Expand All @@ -191,10 +193,12 @@ public static void beforeClass() {

public class MyReceivingApp implements ReceivingApplication<Message> {

@Override
public boolean canProcess(Message theMessage) {
return true;
}

@Override
public Message processMessage(Message theMessage, Map<String, Object> theMetadata) throws ReceivingApplicationException, HL7Exception {
myMessage = theMessage.encode();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
import java.nio.charset.Charset;
import java.util.Map.Entry;

import org.eclipse.jetty.ee10.servlet.ServletContextHandler;
import org.eclipse.jetty.ee10.servlet.ServletHolder;
import org.eclipse.jetty.server.Server;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.servlet.Context;
import org.mortbay.jetty.servlet.ServletHolder;

import ca.uhn.hl7v2.AcknowledgmentCode;
import ca.uhn.hl7v2.DefaultHapiContext;
Expand Down Expand Up @@ -58,6 +58,7 @@ public void after() throws Exception {
myServer.stop();
}

@Override
public boolean authorize(String theUri, String theUsername, String thePassword) {
if (myExpectedUsername != null) {
if (!StringUtils.equals(myExpectedUri, theUri)) {
Expand All @@ -76,7 +77,7 @@ public boolean authorize(String theUri, String theUsername, String thePassword)
public void before() throws Exception {
myPort = RandomServerPortProvider.findFreePort();
myServer = new Server(myPort);
Context context = new Context(myServer, "/", Context.SESSIONS);
ServletContextHandler context = new ServletContextHandler();
HohRawServlet servlet = new HohRawServlet();
servlet.setAuthorizationCallback(this);
servlet.setMessageHandler(this);
Expand All @@ -92,6 +93,7 @@ public void before() throws Exception {
myResponse = null;
}

@Override
public IResponseSendable<String> messageReceived(IReceivable<String> theMessage) throws MessageProcessingException {

myMessage = theMessage.getMessage();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,16 @@
import javax.net.ssl.SSLServerSocket;

import ca.uhn.hl7v2.hoh.util.RandomServerPortProvider;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.HttpConfiguration;
import org.eclipse.jetty.server.HttpConnectionFactory;
import org.eclipse.jetty.server.SecureRequestCustomizer;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.SslConnectionFactory;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.junit.Before;
import org.junit.Test;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.security.SslSelectChannelConnector;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
Expand Down Expand Up @@ -104,13 +110,22 @@ public static void main(String[] args) throws Exception {

Server s = new Server();

SslSelectChannelConnector ssl = new SslSelectChannelConnector();
ssl.setKeystore("src/test/resources/keystore.jks");
ssl.setPassword("changeit");
ssl.setKeyPassword("changeit");
ssl.setPort(60647);
HttpConfiguration https = new HttpConfiguration();
https.addCustomizer(new SecureRequestCustomizer());

SslContextFactory.Server ssl = new SslContextFactory.Server();
ssl.setKeyStorePath("src/test/resources/keystore.jks");
ssl.setKeyStorePassword("changeit");
ssl.setKeyManagerPassword("changeit");

ServerConnector sslConnector = new ServerConnector(s,
new SslConnectionFactory(ssl, "http/1.1"),
new HttpConnectionFactory(https));
sslConnector.setPort(60647);
sslConnector.setIdleTimeout(50000);

s.setConnectors(new Connector[]{ sslConnector });

s.addConnector(ssl);
s.start();
}

Expand Down
27 changes: 26 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,31 @@
<dependencies>
</dependencies>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>${jetty_version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.ee10</groupId>
<artifactId>jetty-ee10-servlets</artifactId>
<version>${jetty_version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.ee10</groupId>
<artifactId>jetty-ee10-servlet</artifactId>
<version>${jetty_version}</version>
</dependency>
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>6.0.0</version>
</dependency>
</dependencies>
</dependencyManagement>

<modules>
<module>hapi-bom</module>
<module>hapi-base</module>
Expand Down Expand Up @@ -665,7 +690,7 @@
<geronimo.jms.spec.version>1.1.1</geronimo.jms.spec.version>
<hamcrest.version>2.2</hamcrest.version>
<mockito_version>4.8.1</mockito_version>
<jetty.version>6.1.26</jetty.version> <!-- !! -->
<jetty_version>12.0.2</jetty_version>
<junit.version>4.12</junit.version>
<log4j.version>1.2.17</log4j.version> <!-- !! -->
<slf4j.version>1.7.30</slf4j.version>
Expand Down

0 comments on commit 7e27f45

Please sign in to comment.