From 7e27f45c9f89ad671fa91f6ffd4a4304b93ab3dc Mon Sep 17 00:00:00 2001 From: James Agnew Date: Wed, 25 Oct 2023 17:27:18 -0400 Subject: [PATCH 1/3] Servlet migration --- hapi-hl7overhttp/pom.xml | 27 +++++++--- .../encoder/Hl7OverHttpRequestEncoder.java | 4 ++ .../ca/uhn/hl7v2/hoh/llp/HohLlpWriter.java | 1 + .../hl7v2/hoh/raw/server/HohRawServlet.java | 7 ++- .../java/ca/uhn/hl7v2/hoh/util/HTTPUtils.java | 7 ++- .../hl7v2/hoh/hapi/server/HohServletTest.java | 50 ++++++++++--------- .../hoh/raw/server/HohRawServletTest.java | 10 ++-- ...CustomCertificateTlsSocketFactoryTest.java | 31 +++++++++--- pom.xml | 27 +++++++++- 9 files changed, 112 insertions(+), 52 deletions(-) diff --git a/hapi-hl7overhttp/pom.xml b/hapi-hl7overhttp/pom.xml index c6e4edbb0..c5a21612c 100644 --- a/hapi-hl7overhttp/pom.xml +++ b/hapi-hl7overhttp/pom.xml @@ -75,18 +75,29 @@ commons-lang ${commons-lang.version} + + - org.mortbay.jetty - jetty - ${jetty.version} - true + org.eclipse.jetty + jetty-server + test - org.mortbay.jetty - jetty-sslengine - ${jetty.version} - true + org.eclipse.jetty.ee10 + jetty-ee10-servlets + test + + org.eclipse.jetty.ee10 + jetty-ee10-servlet + test + + + jakarta.servlet + jakarta.servlet-api + provided + + junit junit diff --git a/hapi-hl7overhttp/src/main/java/ca/uhn/hl7v2/hoh/encoder/Hl7OverHttpRequestEncoder.java b/hapi-hl7overhttp/src/main/java/ca/uhn/hl7v2/hoh/encoder/Hl7OverHttpRequestEncoder.java index 29f366db7..05bf6183e 100644 --- a/hapi-hl7overhttp/src/main/java/ca/uhn/hl7v2/hoh/encoder/Hl7OverHttpRequestEncoder.java +++ b/hapi-hl7overhttp/src/main/java/ca/uhn/hl7v2/hoh/encoder/Hl7OverHttpRequestEncoder.java @@ -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; } diff --git a/hapi-hl7overhttp/src/main/java/ca/uhn/hl7v2/hoh/llp/HohLlpWriter.java b/hapi-hl7overhttp/src/main/java/ca/uhn/hl7v2/hoh/llp/HohLlpWriter.java index 9b21a3e5c..fd98a370d 100644 --- a/hapi-hl7overhttp/src/main/java/ca/uhn/hl7v2/hoh/llp/HohLlpWriter.java +++ b/hapi-hl7overhttp/src/main/java/ca/uhn/hl7v2/hoh/llp/HohLlpWriter.java @@ -64,6 +64,7 @@ public void setPreferredCharset(Charset thePreferredCharset) { /** * {@inheritDoc} */ + @Override public void writeMessage(String theRawMessage) throws LLPException, IOException { AbstractHl7OverHttpEncoder e; diff --git a/hapi-hl7overhttp/src/main/java/ca/uhn/hl7v2/hoh/raw/server/HohRawServlet.java b/hapi-hl7overhttp/src/main/java/ca/uhn/hl7v2/hoh/raw/server/HohRawServlet.java index 31d1b7114..2942f7608 100644 --- a/hapi-hl7overhttp/src/main/java/ca/uhn/hl7v2/hoh/raw/server/HohRawServlet.java +++ b/hapi-hl7overhttp/src/main/java/ca/uhn/hl7v2/hoh/raw/server/HohRawServlet.java @@ -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; @@ -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 { diff --git a/hapi-hl7overhttp/src/main/java/ca/uhn/hl7v2/hoh/util/HTTPUtils.java b/hapi-hl7overhttp/src/main/java/ca/uhn/hl7v2/hoh/util/HTTPUtils.java index 673f05272..8cedd8ba0 100644 --- a/hapi-hl7overhttp/src/main/java/ca/uhn/hl7v2/hoh/util/HTTPUtils.java +++ b/hapi-hl7overhttp/src/main/java/ca/uhn/hl7v2/hoh/util/HTTPUtils.java @@ -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 { diff --git a/hapi-hl7overhttp/src/test/java/ca/uhn/hl7v2/hoh/hapi/server/HohServletTest.java b/hapi-hl7overhttp/src/test/java/ca/uhn/hl7v2/hoh/hapi/server/HohServletTest.java index b5f5675be..ac03433ae 100644 --- a/hapi-hl7overhttp/src/test/java/ca/uhn/hl7v2/hoh/hapi/server/HohServletTest.java +++ b/hapi-hl7overhttp/src/test/java/ca/uhn/hl7v2/hoh/hapi/server/HohServletTest.java @@ -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; @@ -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 { @@ -60,11 +58,12 @@ 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()) { @@ -72,6 +71,10 @@ private void startServer(HohServlet theServlet) throws Exception { Thread.sleep(100); } + Connector[] connectors = myServer.getConnectors(); + assert connectors.length == 1; + myPort = ((ServerConnector) (connectors[0])).getLocalPort(); + } @Test @@ -176,7 +179,6 @@ public void testServletXml() throws Exception { @AfterClass public static void afterClass() { - // Thread.sleep(1000000); ourHapiContext.getExecutorService().shutdown(); } @@ -191,10 +193,12 @@ public static void beforeClass() { public class MyReceivingApp implements ReceivingApplication { + @Override public boolean canProcess(Message theMessage) { return true; } + @Override public Message processMessage(Message theMessage, Map theMetadata) throws ReceivingApplicationException, HL7Exception { myMessage = theMessage.encode(); diff --git a/hapi-hl7overhttp/src/test/java/ca/uhn/hl7v2/hoh/raw/server/HohRawServletTest.java b/hapi-hl7overhttp/src/test/java/ca/uhn/hl7v2/hoh/raw/server/HohRawServletTest.java index e88dd1fc5..cae95d63d 100644 --- a/hapi-hl7overhttp/src/test/java/ca/uhn/hl7v2/hoh/raw/server/HohRawServletTest.java +++ b/hapi-hl7overhttp/src/test/java/ca/uhn/hl7v2/hoh/raw/server/HohRawServletTest.java @@ -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; @@ -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)) { @@ -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); @@ -92,6 +93,7 @@ public void before() throws Exception { myResponse = null; } + @Override public IResponseSendable messageReceived(IReceivable theMessage) throws MessageProcessingException { myMessage = theMessage.getMessage(); diff --git a/hapi-hl7overhttp/src/test/java/ca/uhn/hl7v2/hoh/sockets/CustomCertificateTlsSocketFactoryTest.java b/hapi-hl7overhttp/src/test/java/ca/uhn/hl7v2/hoh/sockets/CustomCertificateTlsSocketFactoryTest.java index e1a27260a..179bc42da 100644 --- a/hapi-hl7overhttp/src/test/java/ca/uhn/hl7v2/hoh/sockets/CustomCertificateTlsSocketFactoryTest.java +++ b/hapi-hl7overhttp/src/test/java/ca/uhn/hl7v2/hoh/sockets/CustomCertificateTlsSocketFactoryTest.java @@ -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; @@ -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(); } diff --git a/pom.xml b/pom.xml index cca48f0eb..93eef5fa5 100644 --- a/pom.xml +++ b/pom.xml @@ -177,6 +177,31 @@ + + + + org.eclipse.jetty + jetty-server + ${jetty_version} + + + org.eclipse.jetty.ee10 + jetty-ee10-servlets + ${jetty_version} + + + org.eclipse.jetty.ee10 + jetty-ee10-servlet + ${jetty_version} + + + jakarta.servlet + jakarta.servlet-api + 6.0.0 + + + + hapi-bom hapi-base @@ -665,7 +690,7 @@ 1.1.1 2.2 4.8.1 - 6.1.26 + 12.0.2 4.12 1.2.17 1.7.30 From b014a976d999403e2d2d01679331124901f9bd54 Mon Sep 17 00:00:00 2001 From: James Agnew Date: Sat, 28 Oct 2023 09:11:49 -0400 Subject: [PATCH 2/3] Servlet fixes --- hapi-examples/pom.xml | 31 +- .../examples/hoh/CustomCertificateServer.java | 32 +- ...eHl7OverHttpServletWithOneApplication.java | 5 +- .../examples/hoh/JettyBasedRawServer.java | 11 +- .../hl7v2/examples/hoh/SignatureClient.java | 10 +- .../hl7v2/examples/hoh/SignatureServlet.java | 5 +- hapi-hl7overhttp/doc_hapi.html | 1 + .../encoder/Hl7OverHttpRequestEncoder.java | 2 +- .../llp/Hl7OverHttpLowerLayerProtocol.java | 333 +++--- .../ca/uhn/hl7v2/hoh/llp/HohLlpWriter.java | 6 +- .../hl7v2/hoh/hapi/server/HohServletTest.java | 4 + .../hoh/raw/server/HohRawServletTest.java | 1041 +++++++++-------- 12 files changed, 771 insertions(+), 710 deletions(-) diff --git a/hapi-examples/pom.xml b/hapi-examples/pom.xml index 87829a15d..44ea64083 100644 --- a/hapi-examples/pom.xml +++ b/hapi-examples/pom.xml @@ -69,14 +69,31 @@ javax.mail mail 1.4.1 - - - org.mortbay.jetty - jetty-embedded - 6.1.26 - true + + + + + org.eclipse.jetty + jetty-server + + + org.eclipse.jetty.ee10 + jetty-ee10-servlets + + + org.eclipse.jetty.ee10 + jetty-ee10-servlet + + + jakarta.servlet + jakarta.servlet-api + provided - + + org.eclipse.jetty.ee10 + jetty-ee10-servlet + + diff --git a/hapi-examples/src/main/java/ca/uhn/hl7v2/examples/hoh/CustomCertificateServer.java b/hapi-examples/src/main/java/ca/uhn/hl7v2/examples/hoh/CustomCertificateServer.java index 70a382c20..2fb772506 100644 --- a/hapi-examples/src/main/java/ca/uhn/hl7v2/examples/hoh/CustomCertificateServer.java +++ b/hapi-examples/src/main/java/ca/uhn/hl7v2/examples/hoh/CustomCertificateServer.java @@ -1,8 +1,5 @@ package ca.uhn.hl7v2.examples.hoh; -import org.mortbay.jetty.Server; -import org.mortbay.jetty.security.SslSelectChannelConnector; - import ca.uhn.hl7v2.DefaultHapiContext; import ca.uhn.hl7v2.HapiContext; import ca.uhn.hl7v2.app.HL7Service; @@ -11,6 +8,14 @@ import ca.uhn.hl7v2.hoh.util.HapiSocketTlsFactoryWrapper; import ca.uhn.hl7v2.hoh.util.ServerRoleEnum; import ca.uhn.hl7v2.llp.LowerLayerProtocol; +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; public class CustomCertificateServer { @@ -50,13 +55,22 @@ public static void main(String[] args) throws Exception { // Create a Jetty Server Server s = new Server(); -SslSelectChannelConnector ssl = new SslSelectChannelConnector(); -ssl.setKeystore("src/test/resources/keystore.jks"); -ssl.setPassword("changeit"); -ssl.setKeyPassword("changeit"); -ssl.setPort(443); +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(443); +sslConnector.setIdleTimeout(50000); + +s.setConnectors(new Connector[]{ sslConnector }); -s.addConnector(ssl); s.start(); // END SNIPPET: server } diff --git a/hapi-examples/src/main/java/ca/uhn/hl7v2/examples/hoh/ExampleHl7OverHttpServletWithOneApplication.java b/hapi-examples/src/main/java/ca/uhn/hl7v2/examples/hoh/ExampleHl7OverHttpServletWithOneApplication.java index 489778870..b9d56fe90 100644 --- a/hapi-examples/src/main/java/ca/uhn/hl7v2/examples/hoh/ExampleHl7OverHttpServletWithOneApplication.java +++ b/hapi-examples/src/main/java/ca/uhn/hl7v2/examples/hoh/ExampleHl7OverHttpServletWithOneApplication.java @@ -3,13 +3,12 @@ import java.io.IOException; import java.util.Map; -import javax.servlet.ServletConfig; - import ca.uhn.hl7v2.HL7Exception; import ca.uhn.hl7v2.hoh.hapi.server.HohServlet; import ca.uhn.hl7v2.model.Message; import ca.uhn.hl7v2.protocol.ReceivingApplication; import ca.uhn.hl7v2.protocol.ReceivingApplicationException; +import jakarta.servlet.ServletConfig; /** * Example servlet implementation which receives HL7 messages @@ -44,6 +43,7 @@ private static class MyApplication implements ReceivingApplication * @param theMetadata A map containing additional information about * the message, where it came from, etc. */ + @Override public Message processMessage(Message theMessage, Map theMetadata) throws ReceivingApplicationException, HL7Exception { System.out.println("Received message:\n" + theMessage.encode()); @@ -73,6 +73,7 @@ public Message processMessage(Message theMessage, Map theMetadat /** * {@inheritDoc} */ + @Override public boolean canProcess(Message theMessage) { return true; } diff --git a/hapi-examples/src/main/java/ca/uhn/hl7v2/examples/hoh/JettyBasedRawServer.java b/hapi-examples/src/main/java/ca/uhn/hl7v2/examples/hoh/JettyBasedRawServer.java index 7c7ac3343..0a6e8bd9a 100644 --- a/hapi-examples/src/main/java/ca/uhn/hl7v2/examples/hoh/JettyBasedRawServer.java +++ b/hapi-examples/src/main/java/ca/uhn/hl7v2/examples/hoh/JettyBasedRawServer.java @@ -1,10 +1,9 @@ package ca.uhn.hl7v2.examples.hoh; -import javax.servlet.Servlet; - -import org.mortbay.jetty.Server; -import org.mortbay.jetty.servlet.Context; -import org.mortbay.jetty.servlet.ServletHolder; +import jakarta.servlet.Servlet; +import org.eclipse.jetty.ee10.servlet.ServletContextHandler; +import org.eclipse.jetty.ee10.servlet.ServletHolder; +import org.eclipse.jetty.server.Server; public class JettyBasedRawServer { @@ -20,7 +19,7 @@ public static void main(String[] args) throws Exception { // Create a Jetty server instance Server server = new Server(port); -Context context = new Context(server, "/Hl7Listener", Context.SESSIONS); +ServletContextHandler context = new ServletContextHandler("/Hl7Listener"); Servlet servlet = new ExampleRawHl7OverHttpServlet(); /* diff --git a/hapi-examples/src/main/java/ca/uhn/hl7v2/examples/hoh/SignatureClient.java b/hapi-examples/src/main/java/ca/uhn/hl7v2/examples/hoh/SignatureClient.java index a3c6e5397..e1d6e7d66 100644 --- a/hapi-examples/src/main/java/ca/uhn/hl7v2/examples/hoh/SignatureClient.java +++ b/hapi-examples/src/main/java/ca/uhn/hl7v2/examples/hoh/SignatureClient.java @@ -55,8 +55,14 @@ public static void main(String[] args) throws KeyStoreException, NoSuchAlgorithm } // START SNIPPET: llp +// Create the ConnectionHub +String host = "localhost"; +int port = 8080; +boolean tls = false; + Hl7OverHttpLowerLayerProtocol llp; llp = new Hl7OverHttpLowerLayerProtocol(ServerRoleEnum.CLIENT); +llp.setHost(host); //Create a message signer BouncyCastleCmsMessageSigner signer = new BouncyCastleCmsMessageSigner(); @@ -65,10 +71,6 @@ public static void main(String[] args) throws KeyStoreException, NoSuchAlgorithm signer.setAliasPassword("key_password"); llp.setSigner(signer); -// Create the ConnectionHub -String host = "localhost"; -int port = 8080; -boolean tls = false; DefaultHapiContext ctx = new DefaultHapiContext(); ctx.setLowerLayerProtocol(llp); diff --git a/hapi-examples/src/main/java/ca/uhn/hl7v2/examples/hoh/SignatureServlet.java b/hapi-examples/src/main/java/ca/uhn/hl7v2/examples/hoh/SignatureServlet.java index bbb0bc494..0514ebe53 100644 --- a/hapi-examples/src/main/java/ca/uhn/hl7v2/examples/hoh/SignatureServlet.java +++ b/hapi-examples/src/main/java/ca/uhn/hl7v2/examples/hoh/SignatureServlet.java @@ -1,11 +1,10 @@ package ca.uhn.hl7v2.examples.hoh; -import javax.servlet.ServletConfig; -import javax.servlet.ServletException; - import ca.uhn.hl7v2.hoh.hapi.server.HohServlet; import ca.uhn.hl7v2.hoh.sign.BouncyCastleCmsMessageSigner; import ca.uhn.hl7v2.hoh.util.KeystoreUtils; +import jakarta.servlet.ServletConfig; +import jakarta.servlet.ServletException; //START SNIPPET: servlet public class SignatureServlet extends HohServlet { diff --git a/hapi-hl7overhttp/doc_hapi.html b/hapi-hl7overhttp/doc_hapi.html index 846d26e54..8d1f58c7e 100644 --- a/hapi-hl7overhttp/doc_hapi.html +++ b/hapi-hl7overhttp/doc_hapi.html @@ -494,6 +494,7 @@

Drop-In LLP Replacement

// Create an HoH LLP for the context LowerLayerProtocol llp = new Hl7OverHttpLowerLayerProtocol(ServerRoleEnum.CLIENT); +llp.setHost("localhost"); ctx.setLowerLayerProtocol(llp); // Use the LLP in a HapiContext to get a client connection diff --git a/hapi-hl7overhttp/src/main/java/ca/uhn/hl7v2/hoh/encoder/Hl7OverHttpRequestEncoder.java b/hapi-hl7overhttp/src/main/java/ca/uhn/hl7v2/hoh/encoder/Hl7OverHttpRequestEncoder.java index 05bf6183e..e69770c6b 100644 --- a/hapi-hl7overhttp/src/main/java/ca/uhn/hl7v2/hoh/encoder/Hl7OverHttpRequestEncoder.java +++ b/hapi-hl7overhttp/src/main/java/ca/uhn/hl7v2/hoh/encoder/Hl7OverHttpRequestEncoder.java @@ -50,10 +50,10 @@ protected void addSpecificHeaders() { } else { ourLog.warn("Host has been set, but port has not"); } + getHeaders().put("Host", hostBuilder.toString()); } else { ourLog.warn("Host has not been set"); } - getHeaders().put("Host", hostBuilder.toString()); } @Override diff --git a/hapi-hl7overhttp/src/main/java/ca/uhn/hl7v2/hoh/llp/Hl7OverHttpLowerLayerProtocol.java b/hapi-hl7overhttp/src/main/java/ca/uhn/hl7v2/hoh/llp/Hl7OverHttpLowerLayerProtocol.java index 31c0a702f..a7a3e0277 100644 --- a/hapi-hl7overhttp/src/main/java/ca/uhn/hl7v2/hoh/llp/Hl7OverHttpLowerLayerProtocol.java +++ b/hapi-hl7overhttp/src/main/java/ca/uhn/hl7v2/hoh/llp/Hl7OverHttpLowerLayerProtocol.java @@ -1,10 +1,5 @@ package ca.uhn.hl7v2.hoh.llp; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.nio.charset.Charset; - import ca.uhn.hl7v2.app.TwoPortService; import ca.uhn.hl7v2.hoh.api.IAuthorizationClientCallback; import ca.uhn.hl7v2.hoh.api.IAuthorizationServerCallback; @@ -15,12 +10,17 @@ import ca.uhn.hl7v2.llp.LLPException; import ca.uhn.hl7v2.llp.LowerLayerProtocol; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.nio.charset.Charset; + /** *

* LowerLayerProtocol implementation which use the HL7 over HTTP specification * as a transport layer. *

- * + * *

* Note that this implementation has limitations: *

    @@ -30,160 +30,169 @@ */ public class Hl7OverHttpLowerLayerProtocol extends LowerLayerProtocol { - private IAuthorizationClientCallback myAuthorizationClientCallback; - private IAuthorizationServerCallback myAuthorizationServerCallback; - private HohLlpReader myNextReader; - private HohLlpWriter myNextWriter; - private final ServerRoleEnum myRole; - private ISigner mySigner; - private String myUriPath = "/"; - private Charset myPreferredCharset; - - public Hl7OverHttpLowerLayerProtocol(ServerRoleEnum theRole) { - myRole = theRole; - - if (myRole == null) { - throw new NullPointerException("Role can not be null"); - } - } - /** - * @return the authorizationClientCallback - */ - IAuthorizationClientCallback getAuthorizationClientCallback() { - return myAuthorizationClientCallback; - } - - /** - * Provide the authorization callback, if any - */ - IAuthorizationServerCallback getAuthorizationServerCallback() { - return myAuthorizationServerCallback; - } - - /** - * {@inheritDoc} - */ - @Override - public HL7Reader getReader(InputStream theArg0) throws LLPException { - if (myNextReader == null && myNextWriter != null) { - myNextWriter = null; - throw new LLPException("Hl7OverHttpLowerLayerProtocol can not be used with a multi socket implementation"); - } - prepareReadersIfNeeded(); - HohLlpReader retVal = myNextReader; - try { - retVal.setInputStream(theArg0); - } catch (IOException e) { - throw new LLPException("Failed to set stream: " + e.getMessage(), e); - } - - myNextReader = null; - return retVal; - } - - /** - * Returns the server role this protocol implementation is being used for - */ - public ServerRoleEnum getRole() { - return myRole; - } - - /** - * @return The signature profile signer - * @see #setSigner(ISigner) - */ - ISigner getSigner() { - return mySigner; - } - - /** - * @return The URI to use for this LLP implementation - * @see #setUriPath(String) - */ - public String getUriPath() { - return myUriPath; - } - - /** - * {@inheritDoc} - */ - @Override - public HL7Writer getWriter(OutputStream theArg0) throws LLPException { - if (myNextReader != null && myNextWriter == null) { - myNextReader = null; - throw new LLPException("Hl7OverHttpLowerLayerProtocol can not be used with a multi socket implementation"); - } - prepareReadersIfNeeded(); - HohLlpWriter retVal = myNextWriter; - retVal.setPreferredCharset(myPreferredCharset); - try { - retVal.setOutputStream(theArg0); - } catch (IOException e) { - throw new LLPException("Failed to set stream: " + e.getMessage(), e); - } - - myNextWriter = null; - return retVal; - } - - private void prepareReadersIfNeeded() { - if (myNextReader == null && myNextWriter == null) { - myNextReader = new HohLlpReader(this); - myNextWriter = new HohLlpWriter(this); - myNextReader.setWriter(myNextWriter); - } - } - - /** - * Provides an authorization callback for authorizing incoming requests. This method - * must only be called of this LLP instance is in {@link ServerRoleEnum#SERVER SERVER} mode. - */ - public void setAuthorizationCallback(IAuthorizationClientCallback theAuthorizationClientCallback) { - if (myRole == ServerRoleEnum.SERVER) { - throw new IllegalStateException("This LLP implementation is in CLIENT mode, so it can not use an authorization callback"); - } - myAuthorizationClientCallback = theAuthorizationClientCallback; - } - - /** - * Provides an authorization callback for authorizing incoming requests. This method - * must only be called of this LLP instance is in {@link ServerRoleEnum#SERVER SERVER} mode. - */ - public void setAuthorizationCallback(IAuthorizationServerCallback theAuthorizationCallback) { - if (myRole == ServerRoleEnum.CLIENT) { - throw new IllegalStateException("This LLP implementation is in CLIENT mode, so it can not use an authorization callback"); - } - myAuthorizationServerCallback = theAuthorizationCallback; - } - - /** - * @param theSigner The signature profile signer - */ - public void setSigner(ISigner theSigner) { - mySigner = theSigner; - } - - /** - * The URI path to use for this protocol. The URI path is the portion of the address (URL) which - * is being accessed which designates the location on the host (i.e. the "path"). By - * default this is set to "/" - * - * @param theUriPath the uri to set - */ - public void setUriPath(String theUriPath) { - myUriPath = theUriPath; - } - - /** - * Sets the charset which will be used for any initiated outgoing messages. What this - * means is that if a message is sent as a response (e.g. an ACK) using this LLP, - * the LLP will ignore the charset provided by this method and will attempt to use - * the charset used in the original incoming message. On the other hand, if a new - * outgoing message is transmitted using this LLP (i.e. not an ACK), the charset - * specified here will be used. - */ - public void setPreferredCharset(Charset thePreferredCharset) { - myPreferredCharset = thePreferredCharset; - } - + private final ServerRoleEnum myRole; + private IAuthorizationClientCallback myAuthorizationClientCallback; + private IAuthorizationServerCallback myAuthorizationServerCallback; + private HohLlpReader myNextReader; + private HohLlpWriter myNextWriter; + private ISigner mySigner; + private String myUriPath = "/"; + private Charset myPreferredCharset; + private String myHost; + + public Hl7OverHttpLowerLayerProtocol(ServerRoleEnum theRole) { + myRole = theRole; + + if (myRole == null) { + throw new NullPointerException("Role can not be null"); + } + } + + /** + * @return the authorizationClientCallback + */ + IAuthorizationClientCallback getAuthorizationClientCallback() { + return myAuthorizationClientCallback; + } + + /** + * Provide the authorization callback, if any + */ + IAuthorizationServerCallback getAuthorizationServerCallback() { + return myAuthorizationServerCallback; + } + + /** + * {@inheritDoc} + */ + @Override + public HL7Reader getReader(InputStream theArg0) throws LLPException { + if (myNextReader == null && myNextWriter != null) { + myNextWriter = null; + throw new LLPException("Hl7OverHttpLowerLayerProtocol can not be used with a multi socket implementation"); + } + prepareReadersIfNeeded(); + HohLlpReader retVal = myNextReader; + try { + retVal.setInputStream(theArg0); + } catch (IOException e) { + throw new LLPException("Failed to set stream: " + e.getMessage(), e); + } + + myNextReader = null; + return retVal; + } + + /** + * Returns the server role this protocol implementation is being used for + */ + public ServerRoleEnum getRole() { + return myRole; + } + + /** + * @return The signature profile signer + * @see #setSigner(ISigner) + */ + ISigner getSigner() { + return mySigner; + } + + /** + * @param theSigner The signature profile signer + */ + public void setSigner(ISigner theSigner) { + mySigner = theSigner; + } + + /** + * @return The URI to use for this LLP implementation + * @see #setUriPath(String) + */ + public String getUriPath() { + return myUriPath; + } + + /** + * The URI path to use for this protocol. The URI path is the portion of the address (URL) which + * is being accessed which designates the location on the host (i.e. the "path"). By + * default this is set to "/" + * + * @param theUriPath the uri to set + */ + public void setUriPath(String theUriPath) { + myUriPath = theUriPath; + } + + public String getHost() { + return myHost; + } + + public void setHost(String thehost) { + myHost = thehost; + } + + /** + * {@inheritDoc} + */ + @Override + public HL7Writer getWriter(OutputStream theArg0) throws LLPException { + if (myNextReader != null && myNextWriter == null) { + myNextReader = null; + throw new LLPException("Hl7OverHttpLowerLayerProtocol can not be used with a multi socket implementation"); + } + prepareReadersIfNeeded(); + HohLlpWriter retVal = myNextWriter; + retVal.setPreferredCharset(myPreferredCharset); + try { + retVal.setOutputStream(theArg0); + } catch (IOException e) { + throw new LLPException("Failed to set stream: " + e.getMessage(), e); + } + + myNextWriter = null; + return retVal; + } + + private void prepareReadersIfNeeded() { + if (myNextReader == null && myNextWriter == null) { + myNextReader = new HohLlpReader(this); + myNextWriter = new HohLlpWriter(this); + myNextReader.setWriter(myNextWriter); + } + } + + /** + * Provides an authorization callback for authorizing incoming requests. This method + * must only be called of this LLP instance is in {@link ServerRoleEnum#SERVER SERVER} mode. + */ + public void setAuthorizationCallback(IAuthorizationClientCallback theAuthorizationClientCallback) { + if (myRole == ServerRoleEnum.SERVER) { + throw new IllegalStateException("This LLP implementation is in CLIENT mode, so it can not use an authorization callback"); + } + myAuthorizationClientCallback = theAuthorizationClientCallback; + } + + /** + * Provides an authorization callback for authorizing incoming requests. This method + * must only be called of this LLP instance is in {@link ServerRoleEnum#SERVER SERVER} mode. + */ + public void setAuthorizationCallback(IAuthorizationServerCallback theAuthorizationCallback) { + if (myRole == ServerRoleEnum.CLIENT) { + throw new IllegalStateException("This LLP implementation is in CLIENT mode, so it can not use an authorization callback"); + } + myAuthorizationServerCallback = theAuthorizationCallback; + } + + /** + * Sets the charset which will be used for any initiated outgoing messages. What this + * means is that if a message is sent as a response (e.g. an ACK) using this LLP, + * the LLP will ignore the charset provided by this method and will attempt to use + * the charset used in the original incoming message. On the other hand, if a new + * outgoing message is transmitted using this LLP (i.e. not an ACK), the charset + * specified here will be used. + */ + public void setPreferredCharset(Charset thePreferredCharset) { + myPreferredCharset = thePreferredCharset; + } } diff --git a/hapi-hl7overhttp/src/main/java/ca/uhn/hl7v2/hoh/llp/HohLlpWriter.java b/hapi-hl7overhttp/src/main/java/ca/uhn/hl7v2/hoh/llp/HohLlpWriter.java index fd98a370d..d69c39541 100644 --- a/hapi-hl7overhttp/src/main/java/ca/uhn/hl7v2/hoh/llp/HohLlpWriter.java +++ b/hapi-hl7overhttp/src/main/java/ca/uhn/hl7v2/hoh/llp/HohLlpWriter.java @@ -69,7 +69,11 @@ public void writeMessage(String theRawMessage) throws LLPException, IOException AbstractHl7OverHttpEncoder e; if (myProtocol.getRole() == ServerRoleEnum.CLIENT) { - e = new Hl7OverHttpRequestEncoder(); + Hl7OverHttpRequestEncoder encoder = new Hl7OverHttpRequestEncoder(); + if (myProtocol.getHost() != null) { + encoder.setHost(myProtocol.getHost()); + } + e = encoder; if (myProtocol.getAuthorizationClientCallback() != null) { e.setUsername(myProtocol.getAuthorizationClientCallback().provideUsername(myProtocol.getUriPath())); e.setPassword(myProtocol.getAuthorizationClientCallback().providePassword(myProtocol.getUriPath())); diff --git a/hapi-hl7overhttp/src/test/java/ca/uhn/hl7v2/hoh/hapi/server/HohServletTest.java b/hapi-hl7overhttp/src/test/java/ca/uhn/hl7v2/hoh/hapi/server/HohServletTest.java index ac03433ae..5f03f9a8b 100644 --- a/hapi-hl7overhttp/src/test/java/ca/uhn/hl7v2/hoh/hapi/server/HohServletTest.java +++ b/hapi-hl7overhttp/src/test/java/ca/uhn/hl7v2/hoh/hapi/server/HohServletTest.java @@ -89,6 +89,7 @@ public void testServlet() throws Exception { msg.getPID().getPid5_PatientName(0).getXpn1_FamilyName().getFn1_Surname().setValue("I♥HAPI"); // needs // utf-8 Hl7OverHttpLowerLayerProtocol llp = new Hl7OverHttpLowerLayerProtocol(ServerRoleEnum.CLIENT); + llp.setHost("localhost"); llp.setPreferredCharset(StandardCharsets.UTF_8); Connection conn = ourConnectionHub.attach("localhost", myPort, PipeParser.getInstanceWithNoValidation(), llp, false); Message response; @@ -116,6 +117,7 @@ public void testServletAE() throws Exception { // utf-8 myResponse = msg.generateACK(AcknowledgmentCode.AE, new HL7Exception("dsfasfs")); Hl7OverHttpLowerLayerProtocol llp = new Hl7OverHttpLowerLayerProtocol(ServerRoleEnum.CLIENT); + llp.setHost("localhost"); llp.setPreferredCharset(StandardCharsets.UTF_8); Connection conn = ourConnectionHub.attach("localhost", myPort, PipeParser.getInstanceWithNoValidation(), llp, false); Message response = conn.getInitiator().sendAndReceive(msg); @@ -141,6 +143,7 @@ public void testServletAR() throws Exception { // utf-8 myResponse = msg.generateACK(AcknowledgmentCode.AR, new HL7Exception("dsfasfs")); Hl7OverHttpLowerLayerProtocol llp = new Hl7OverHttpLowerLayerProtocol(ServerRoleEnum.CLIENT); + llp.setHost("localhost"); llp.setPreferredCharset(StandardCharsets.UTF_8); Connection conn = ourConnectionHub.attach("localhost", myPort, PipeParser.getInstanceWithNoValidation(), llp, false); Message response = conn.getInitiator().sendAndReceive(msg); @@ -165,6 +168,7 @@ public void testServletXml() throws Exception { msg.getPID().getPid5_PatientName(0).getXpn1_FamilyName().getFn1_Surname().setValue("I♥HAPI"); // needs // utf-8 Hl7OverHttpLowerLayerProtocol llp = new Hl7OverHttpLowerLayerProtocol(ServerRoleEnum.CLIENT); + llp.setHost("localhost"); llp.setPreferredCharset(StandardCharsets.UTF_8); Connection conn = ourConnectionHub.attach("localhost", myPort, DefaultXMLParser.getInstanceWithNoValidation(), llp, false); Message response = conn.getInitiator().sendAndReceive(msg); diff --git a/hapi-hl7overhttp/src/test/java/ca/uhn/hl7v2/hoh/raw/server/HohRawServletTest.java b/hapi-hl7overhttp/src/test/java/ca/uhn/hl7v2/hoh/raw/server/HohRawServletTest.java index cae95d63d..4b8cb40af 100644 --- a/hapi-hl7overhttp/src/test/java/ca/uhn/hl7v2/hoh/raw/server/HohRawServletTest.java +++ b/hapi-hl7overhttp/src/test/java/ca/uhn/hl7v2/hoh/raw/server/HohRawServletTest.java @@ -1,26 +1,5 @@ package ca.uhn.hl7v2.hoh.raw.server; -import static org.junit.Assert.*; - -import java.io.BufferedReader; -import java.io.DataOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.net.HttpURLConnection; -import java.net.URL; -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 ca.uhn.hl7v2.AcknowledgmentCode; import ca.uhn.hl7v2.DefaultHapiContext; import ca.uhn.hl7v2.HL7Exception; @@ -38,506 +17,538 @@ import ca.uhn.hl7v2.hoh.util.StringUtils; import ca.uhn.hl7v2.model.v25.message.ADT_A05; import ca.uhn.hl7v2.parser.DefaultXMLParser; -import ca.uhn.hl7v2.parser.EncodingNotSupportedException; import ca.uhn.hl7v2.parser.GenericParser; +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.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import java.io.BufferedReader; +import java.io.DataOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.net.URL; +import java.nio.charset.Charset; +import java.util.Map.Entry; + +import static org.junit.Assert.assertEquals; public class HohRawServletTest implements IAuthorizationServerCallback, IMessageHandler { - private static DefaultHapiContext ourHapiContext; - private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(HohRawServletTest.class); - private String myExpectedPassword; - private String myExpectedUri; - private String myExpectedUsername; - private String myMessage; - private String myResponse; - private int myPort; - private Server myServer; - - @After - 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)) { - return false; - } - if (!StringUtils.equals(myExpectedUsername, theUsername)) { - return false; - } - return StringUtils.equals(myExpectedPassword, thePassword); - } - return true; - - } - - @Before - public void before() throws Exception { - myPort = RandomServerPortProvider.findFreePort(); - myServer = new Server(myPort); - ServletContextHandler context = new ServletContextHandler(); - HohRawServlet servlet = new HohRawServlet(); - servlet.setAuthorizationCallback(this); - servlet.setMessageHandler(this); - context.addServlet(new ServletHolder(servlet), "/*"); - - myServer.start(); - - while (myServer.isStarting()) { - ourLog.info("Waiting for server to start..."); - Thread.sleep(100); - } - - myResponse = null; - } - - @Override - public IResponseSendable messageReceived(IReceivable theMessage) throws MessageProcessingException { - - myMessage = theMessage.getMessage(); - try { - if (myResponse == null) { - myResponse = GenericParser.getInstanceWithNoValidation().parse(myMessage).generateACK().encode(); - } - return new RawSendable(myResponse); - } catch (IOException | HL7Exception e) { - throw new MessageProcessingException(e); - } + private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(HohRawServletTest.class); + private static DefaultHapiContext ourHapiContext; + private String myExpectedPassword; + private String myExpectedUri; + private String myExpectedUsername; + private String myMessage; + private String myResponse; + private int myPort; + private Server myServer; + + @After + 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)) { + return false; + } + if (!StringUtils.equals(myExpectedUsername, theUsername)) { + return false; + } + return StringUtils.equals(myExpectedPassword, thePassword); + } + return true; + + } + + @Before + public void before() throws Exception { + myServer = new Server(0); + ServletContextHandler context = new ServletContextHandler(); + HohRawServlet servlet = new HohRawServlet(); + servlet.setAuthorizationCallback(this); + servlet.setMessageHandler(this); + context.addServlet(new ServletHolder(servlet), "/*"); + + myServer.setHandler(context); + 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(); + + myResponse = null; + } + + @Override + public IResponseSendable messageReceived(IReceivable theMessage) throws MessageProcessingException { + + myMessage = theMessage.getMessage(); + try { + if (myResponse == null) { + myResponse = GenericParser.getInstanceWithNoValidation().parse(myMessage).generateACK().encode(); + } + return new RawSendable(myResponse); + } catch (IOException | HL7Exception e) { + throw new MessageProcessingException(e); + } + + } + + @Test + public void testSuccessWhenRequestHasNoCharsetSpecified() throws Exception { + + String message = // - + "MSH|^~\\&|||||200803051508||ADT^A31|2|P|2.5\r" + // - + "EVN||200803051509\r" + // - + "PID|||ZZZZZZ83M64Z148R^^^SSN^SSN^^20070103\r"; // - + ADT_A05 msg = new ADT_A05(); + msg.parse(message); + + Hl7OverHttpLowerLayerProtocol llp = new Hl7OverHttpLowerLayerProtocol(ServerRoleEnum.CLIENT); + String charsetName = "ISO-8859-1"; + llp.setPreferredCharset(Charset.forName(charsetName)); + llp.setHost("localhost"); + + Hl7OverHttpRequestEncoder enc = new Hl7OverHttpRequestEncoder(); + enc.setCharset(Charset.forName(charsetName)); + enc.setUsername("hello"); + enc.setPassword("world"); + enc.setMessage(message); + enc.setHost("localhost"); + enc.encode(); + + String urlString = "http://localhost:" + myPort + "/"; + ourLog.info("URL: {}", urlString); + URL url = new URL(urlString); + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + conn.setRequestMethod("POST"); + + conn.setUseCaches(false); + conn.setDoInput(true); + conn.setDoOutput(true); + + for (Entry next : enc.getHeaders().entrySet()) { + if (next.getKey().toLowerCase().equals("content-type")) { + conn.setRequestProperty(next.getKey(), "application/hl7-v2"); + } else { + conn.setRequestProperty(next.getKey(), next.getValue()); + } + } + + DataOutputStream wr = new DataOutputStream(conn.getOutputStream()); + wr.write(enc.getData()); + wr.flush(); + + // Get Response + InputStream is; + try { + is = conn.getInputStream(); + } catch (IOException e) { + if (conn.getResponseCode() == 400 || conn.getResponseCode() == 500) { + is = conn.getErrorStream(); + } else { + throw e; + } + } + BufferedReader rd = new BufferedReader(new InputStreamReader(is)); + String line; + StringBuilder response = new StringBuilder(); + while ((line = rd.readLine()) != null) { + response.append(line); + response.append('\r'); + } + String responseString = response.toString(); + ourLog.info("Response:\n{}", responseString); + + assertEquals(EncodingStyle.ER7.getContentType(), conn.getHeaderField("Content-Type").replaceAll(";.*", "")); + assertEquals("utf-8", conn.getHeaderField("Content-Type").replaceAll(".*;.*charset=", "")); + assertEquals(200, conn.getResponseCode()); + assertEquals(message, myMessage); + assertEquals(myResponse, responseString); + } + + @Test + public void testServlet() throws Exception { + + String message = // - + "MSH|^~\\&|||||200803051508||ADT^A31|2|P|2.5\r" + // - + "EVN||200803051509\r" + // - + "PID|||ZZZZZZ83M64Z148R^^^SSN^SSN^^20070103\r"; // - + ADT_A05 msg = new ADT_A05(); + msg.parse(message); + + Hl7OverHttpLowerLayerProtocol llp = new Hl7OverHttpLowerLayerProtocol(ServerRoleEnum.CLIENT); + String charsetName = "ISO-8859-2"; + llp.setPreferredCharset(Charset.forName(charsetName)); + llp.setHost("localhost"); + + Hl7OverHttpRequestEncoder enc = new Hl7OverHttpRequestEncoder(); + enc.setCharset(Charset.forName(charsetName)); + enc.setUsername("hello"); + enc.setPassword("world"); + enc.setMessage(message); + enc.encode(); + + String urlString = "http://localhost:" + myPort + "/"; + ourLog.info("URL: {}", urlString); + URL url = new URL(urlString); + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + conn.setRequestMethod("POST"); + + conn.setUseCaches(false); + conn.setDoInput(true); + conn.setDoOutput(true); + + for (Entry next : enc.getHeaders().entrySet()) { + conn.setRequestProperty(next.getKey(), next.getValue()); + } + + DataOutputStream wr = new DataOutputStream(conn.getOutputStream()); + wr.write(enc.getData()); + wr.flush(); + + // Get Response + InputStream is; + try { + is = conn.getInputStream(); + } catch (IOException e) { + if (conn.getResponseCode() == 400 || conn.getResponseCode() == 500) { + is = conn.getErrorStream(); + } else { + throw e; + } + } + BufferedReader rd = new BufferedReader(new InputStreamReader(is)); + String line; + StringBuilder response = new StringBuilder(); + while ((line = rd.readLine()) != null) { + response.append(line); + response.append('\r'); + } + String responseString = response.toString(); + ourLog.info("Response:\n{}", responseString); + + assertEquals(EncodingStyle.ER7.getContentType(), conn.getHeaderField("Content-Type").replaceAll(";.*", "")); + assertEquals(charsetName, conn.getHeaderField("Content-Type").replaceAll(".*;.*charset=", "")); + assertEquals(200, conn.getResponseCode()); + assertEquals(message, myMessage); + assertEquals(myResponse, responseString); + + } + + @Test + public void testLargeMessage() throws Exception { + + StringBuilder b = new StringBuilder(); + for (int a = 0; a < 100000; a++) { + b.append('a'); + } + + String message = // - + "MSH|^~\\&|||||200803051508||ADT^A31|2|P|2.5\r" + // - + "EVN||200803051509\r" + // - + "PID|||" + b + "^^^SSN^SSN^^20070103\r"; // - + ADT_A05 msg = new ADT_A05(); + msg.parse(message); + + Hl7OverHttpLowerLayerProtocol llp = new Hl7OverHttpLowerLayerProtocol(ServerRoleEnum.CLIENT); + String charsetName = "ISO-8859-2"; + llp.setPreferredCharset(Charset.forName(charsetName)); + llp.setHost("localhost"); + + Hl7OverHttpRequestEncoder enc = new Hl7OverHttpRequestEncoder(); + enc.setCharset(Charset.forName(charsetName)); + enc.setUsername("hello"); + enc.setPassword("world"); + enc.setMessage(message); + enc.encode(); + + String urlString = "http://localhost:" + myPort + "/"; + ourLog.info("URL: {}", urlString); + URL url = new URL(urlString); + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + conn.setRequestMethod("POST"); + + conn.setUseCaches(false); + conn.setDoInput(true); + conn.setDoOutput(true); + + for (Entry next : enc.getHeaders().entrySet()) { + conn.setRequestProperty(next.getKey(), next.getValue()); + } + + DataOutputStream wr = new DataOutputStream(conn.getOutputStream()); + wr.write(enc.getData()); + wr.flush(); + + // Get Response + InputStream is; + try { + is = conn.getInputStream(); + } catch (IOException e) { + if (conn.getResponseCode() == 400 || conn.getResponseCode() == 500) { + is = conn.getErrorStream(); + } else { + throw e; + } + } + BufferedReader rd = new BufferedReader(new InputStreamReader(is)); + String line; + StringBuilder response = new StringBuilder(); + while ((line = rd.readLine()) != null) { + response.append(line); + response.append('\r'); + } + String responseString = response.toString(); + ourLog.info("Response:\n{}", responseString); + + assertEquals(EncodingStyle.ER7.getContentType(), conn.getHeaderField("Content-Type").replaceAll(";.*", "")); + assertEquals(charsetName, conn.getHeaderField("Content-Type").replaceAll(".*;.*charset=", "")); + assertEquals(200, conn.getResponseCode()); + assertEquals(message, myMessage); + assertEquals(myResponse, responseString); + + } + + @Test + public void testServletAR() throws Exception { + + String message = // - + "MSH|^~\\&|||||200803051508||ADT^A31|2|P|2.5\r" + // - + "EVN||200803051509\r" + // - + "PID|||ZZZZZZ83M64Z148R^^^SSN^SSN^^20070103\r"; // - + ADT_A05 msg = new ADT_A05(); + msg.parse(message); + + myResponse = msg.generateACK(AcknowledgmentCode.AR, new HL7Exception("ewrerwe")).encode(); + + Hl7OverHttpLowerLayerProtocol llp = new Hl7OverHttpLowerLayerProtocol(ServerRoleEnum.CLIENT); + String charsetName = "ISO-8859-2"; + llp.setPreferredCharset(Charset.forName(charsetName)); + llp.setHost("localhost"); + + Hl7OverHttpRequestEncoder enc = new Hl7OverHttpRequestEncoder(); + enc.setCharset(Charset.forName(charsetName)); + enc.setUsername("hello"); + enc.setPassword("world"); + enc.setMessage(message); + enc.encode(); + + String urlString = "http://localhost:" + myPort + "/"; + ourLog.info("URL: {}", urlString); + URL url = new URL(urlString); + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + conn.setRequestMethod("POST"); + + conn.setUseCaches(false); + conn.setDoInput(true); + conn.setDoOutput(true); + for (Entry next : enc.getHeaders().entrySet()) { + conn.setRequestProperty(next.getKey(), next.getValue()); + } + + DataOutputStream wr = new DataOutputStream(conn.getOutputStream()); + wr.write(enc.getData()); + wr.flush(); + + // Get Response + InputStream is; + try { + is = conn.getInputStream(); + } catch (IOException e) { + if (conn.getResponseCode() == 400 || conn.getResponseCode() == 500) { + is = conn.getErrorStream(); + } else { + throw e; + } + } + BufferedReader rd = new BufferedReader(new InputStreamReader(is)); + String line; + StringBuilder response = new StringBuilder(); + while ((line = rd.readLine()) != null) { + response.append(line); + response.append('\r'); + } + String responseString = response.toString(); + ourLog.info("Response:\n{}", responseString); + + assertEquals(EncodingStyle.ER7.getContentType(), conn.getHeaderField("Content-Type").replaceAll(";.*", "")); + assertEquals(charsetName, conn.getHeaderField("Content-Type").replaceAll(".*;.*charset=", "")); + assertEquals(200, conn.getResponseCode()); + assertEquals(message, myMessage); + assertEquals(myResponse, responseString); + + } + + @Test + public void testServletAE() throws Exception { + + String message = // - + "MSH|^~\\&|||||200803051508||ADT^A31|2|P|2.5\r" + // - + "EVN||200803051509\r" + // - + "PID|||ZZZZZZ83M64Z148R^^^SSN^SSN^^20070103\r"; // - + ADT_A05 msg = new ADT_A05(); + msg.parse(message); + + myResponse = msg.generateACK(AcknowledgmentCode.AE, new HL7Exception("ewrerwe")).encode(); + + Hl7OverHttpLowerLayerProtocol llp = new Hl7OverHttpLowerLayerProtocol(ServerRoleEnum.CLIENT); + String charsetName = "ISO-8859-2"; + llp.setPreferredCharset(Charset.forName(charsetName)); + llp.setHost("localhost"); + + Hl7OverHttpRequestEncoder enc = new Hl7OverHttpRequestEncoder(); + enc.setCharset(Charset.forName(charsetName)); + enc.setUsername("hello"); + enc.setPassword("world"); + enc.setMessage(message); + enc.encode(); + + String urlString = "http://localhost:" + myPort + "/"; + ourLog.info("URL: {}", urlString); + URL url = new URL(urlString); + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + conn.setRequestMethod("POST"); + + conn.setUseCaches(false); + conn.setDoInput(true); + conn.setDoOutput(true); + for (Entry next : enc.getHeaders().entrySet()) { + conn.setRequestProperty(next.getKey(), next.getValue()); + } + + DataOutputStream wr = new DataOutputStream(conn.getOutputStream()); + wr.write(enc.getData()); + wr.flush(); + + // Get Response + InputStream is; + try { + is = conn.getInputStream(); + } catch (IOException e) { + if (conn.getResponseCode() == 400 || conn.getResponseCode() == 500) { + is = conn.getErrorStream(); + } else { + throw e; + } + } + BufferedReader rd = new BufferedReader(new InputStreamReader(is)); + String line; + StringBuilder response = new StringBuilder(); + while ((line = rd.readLine()) != null) { + response.append(line); + response.append('\r'); + } + String responseString = response.toString(); + ourLog.info("Response:\n{}", responseString); + + assertEquals(EncodingStyle.ER7.getContentType(), conn.getHeaderField("Content-Type").replaceAll(";.*", "")); + assertEquals(charsetName, conn.getHeaderField("Content-Type").replaceAll(".*;.*charset=", "")); + assertEquals(200, conn.getResponseCode()); + assertEquals(message, myMessage); + assertEquals(myResponse, responseString); + + } + + @Test + public void testServletSimpleXml() throws Exception { + + String message = // - + "MSH|^~\\&|||||200803051508||ADT^A31|2|P|2.5\r" + // - + "EVN||200803051509\r" + // - + "PID|||ZZZZZZ83M64Z148R^^^SSN^SSN^^20070103\r"; // - + ADT_A05 msg = new ADT_A05(); + msg.parse(message); + + message = DefaultXMLParser.getInstanceWithNoValidation().encode(msg); + + Hl7OverHttpLowerLayerProtocol llp = new Hl7OverHttpLowerLayerProtocol(ServerRoleEnum.CLIENT); + String charsetName = "ISO-8859-2"; + llp.setPreferredCharset(Charset.forName(charsetName)); + llp.setHost("localhost"); + + Hl7OverHttpRequestEncoder enc = new Hl7OverHttpRequestEncoder(); + enc.setCharset(Charset.forName(charsetName)); + enc.setUsername("hello"); + enc.setPassword("world"); + enc.setMessage(message); + enc.encode(); + + String urlString = "http://localhost:" + myPort + "/"; + ourLog.info("URL: {}", urlString); + URL url = new URL(urlString); + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + conn.setRequestMethod("POST"); + + conn.setUseCaches(false); + conn.setDoInput(true); + conn.setDoOutput(true); + for (Entry next : enc.getHeaders().entrySet()) { + conn.setRequestProperty(next.getKey(), next.getValue()); + } + + DataOutputStream wr = new DataOutputStream(conn.getOutputStream()); + wr.write(enc.getData()); + wr.flush(); + + // Get Response + InputStream is; + try { + is = conn.getInputStream(); + } catch (IOException e) { + if (conn.getResponseCode() == 400 || conn.getResponseCode() == 500) { + is = conn.getErrorStream(); + } else { + throw e; + } + } + BufferedReader rd = new BufferedReader(new InputStreamReader(is)); + String line; + StringBuilder response = new StringBuilder(); + while ((line = rd.readLine()) != null) { + response.append(line); + response.append('\r'); + } + String responseString = response.toString(); + ourLog.info("Response:\n{}", responseString); + + assertEquals(EncodingStyle.XML.getContentType(), conn.getHeaderField("Content-Type").replaceAll(";.*", "")); + assertEquals(charsetName, conn.getHeaderField("Content-Type").replaceAll(".*;.*charset=", "")); + assertEquals(200, conn.getResponseCode()); + assertEquals(message, myMessage); + assertEquals(myResponse.replaceAll("([\\r\\n])+", " "), responseString.replaceAll("([\\r\\n])+", " ")); } - @Test - public void testSuccessWhenRequestHasNoCharsetSpecified() throws Exception { - - String message = // - - "MSH|^~\\&|||||200803051508||ADT^A31|2|P|2.5\r" + // - - "EVN||200803051509\r" + // - - "PID|||ZZZZZZ83M64Z148R^^^SSN^SSN^^20070103\r"; // - - ADT_A05 msg = new ADT_A05(); - msg.parse(message); - - Hl7OverHttpLowerLayerProtocol llp = new Hl7OverHttpLowerLayerProtocol(ServerRoleEnum.CLIENT); - String charsetName = "ISO-8859-1"; - llp.setPreferredCharset(Charset.forName(charsetName)); - - Hl7OverHttpRequestEncoder enc = new Hl7OverHttpRequestEncoder(); - enc.setCharset(Charset.forName(charsetName)); - enc.setUsername("hello"); - enc.setPassword("world"); - enc.setMessage(message); - enc.encode(); - - String urlString = "http://localhost:" + myPort + "/"; - ourLog.info("URL: {}", urlString); - URL url = new URL(urlString); - HttpURLConnection conn = (HttpURLConnection) url.openConnection(); - conn.setRequestMethod("POST"); - - conn.setUseCaches(false); - conn.setDoInput(true); - conn.setDoOutput(true); - - for (Entry next : enc.getHeaders().entrySet()) { - if (next.getKey().toLowerCase().equals("content-type")) { - conn.setRequestProperty(next.getKey(), "application/hl7-v2"); - } else { - conn.setRequestProperty(next.getKey(), next.getValue()); - } - } - - DataOutputStream wr = new DataOutputStream(conn.getOutputStream()); - wr.write(enc.getData()); - wr.flush(); - - // Get Response - InputStream is; - try { - is = conn.getInputStream(); - } catch (IOException e) { - if (conn.getResponseCode() == 400 || conn.getResponseCode() == 500) { - is = conn.getErrorStream(); - } else { - throw e; - } - } - BufferedReader rd = new BufferedReader(new InputStreamReader(is)); - String line; - StringBuilder response = new StringBuilder(); - while ((line = rd.readLine()) != null) { - response.append(line); - response.append('\r'); - } - String responseString = response.toString(); - ourLog.info("Response:\n{}", responseString); - - assertEquals(EncodingStyle.ER7.getContentType(), conn.getHeaderField("Content-Type").replaceAll(";.*", "")); - assertEquals("UTF-8", conn.getHeaderField("Content-Type").replaceAll(".*;.*charset=", "")); - assertEquals(200, conn.getResponseCode()); - assertEquals(message, myMessage); - assertEquals(myResponse, responseString); - } - - @Test - public void testServlet() throws Exception { - - String message = // - - "MSH|^~\\&|||||200803051508||ADT^A31|2|P|2.5\r" + // - - "EVN||200803051509\r" + // - - "PID|||ZZZZZZ83M64Z148R^^^SSN^SSN^^20070103\r"; // - - ADT_A05 msg = new ADT_A05(); - msg.parse(message); - - Hl7OverHttpLowerLayerProtocol llp = new Hl7OverHttpLowerLayerProtocol(ServerRoleEnum.CLIENT); - String charsetName = "ISO-8859-2"; - llp.setPreferredCharset(Charset.forName(charsetName)); - - Hl7OverHttpRequestEncoder enc = new Hl7OverHttpRequestEncoder(); - enc.setCharset(Charset.forName(charsetName)); - enc.setUsername("hello"); - enc.setPassword("world"); - enc.setMessage(message); - enc.encode(); - - String urlString = "http://localhost:" + myPort + "/"; - ourLog.info("URL: {}", urlString); - URL url = new URL(urlString); - HttpURLConnection conn = (HttpURLConnection) url.openConnection(); - conn.setRequestMethod("POST"); - - conn.setUseCaches(false); - conn.setDoInput(true); - conn.setDoOutput(true); - - for (Entry next : enc.getHeaders().entrySet()) { - conn.setRequestProperty(next.getKey(), next.getValue()); - } - - DataOutputStream wr = new DataOutputStream(conn.getOutputStream()); - wr.write(enc.getData()); - wr.flush(); - - // Get Response - InputStream is; - try { - is = conn.getInputStream(); - } catch (IOException e) { - if (conn.getResponseCode() == 400 || conn.getResponseCode() == 500) { - is = conn.getErrorStream(); - } else { - throw e; - } - } - BufferedReader rd = new BufferedReader(new InputStreamReader(is)); - String line; - StringBuilder response = new StringBuilder(); - while ((line = rd.readLine()) != null) { - response.append(line); - response.append('\r'); - } - String responseString = response.toString(); - ourLog.info("Response:\n{}", responseString); - - assertEquals(EncodingStyle.ER7.getContentType(), conn.getHeaderField("Content-Type").replaceAll(";.*", "")); - assertEquals(charsetName, conn.getHeaderField("Content-Type").replaceAll(".*;.*charset=", "")); - assertEquals(200, conn.getResponseCode()); - assertEquals(message, myMessage); - assertEquals(myResponse, responseString); - - } - - @Test - public void testLargeMessage() throws Exception { - - StringBuilder b= new StringBuilder(); - for (int a = 0; a < 100000; a++) { - b.append('a'); - } - - String message = // - - "MSH|^~\\&|||||200803051508||ADT^A31|2|P|2.5\r" + // - - "EVN||200803051509\r" + // - - "PID|||" + b + "^^^SSN^SSN^^20070103\r"; // - - ADT_A05 msg = new ADT_A05(); - msg.parse(message); - - Hl7OverHttpLowerLayerProtocol llp = new Hl7OverHttpLowerLayerProtocol(ServerRoleEnum.CLIENT); - String charsetName = "ISO-8859-2"; - llp.setPreferredCharset(Charset.forName(charsetName)); - - Hl7OverHttpRequestEncoder enc = new Hl7OverHttpRequestEncoder(); - enc.setCharset(Charset.forName(charsetName)); - enc.setUsername("hello"); - enc.setPassword("world"); - enc.setMessage(message); - enc.encode(); - - String urlString = "http://localhost:" + myPort + "/"; - ourLog.info("URL: {}", urlString); - URL url = new URL(urlString); - HttpURLConnection conn = (HttpURLConnection) url.openConnection(); - conn.setRequestMethod("POST"); - - conn.setUseCaches(false); - conn.setDoInput(true); - conn.setDoOutput(true); - - for (Entry next : enc.getHeaders().entrySet()) { - conn.setRequestProperty(next.getKey(), next.getValue()); - } - - DataOutputStream wr = new DataOutputStream(conn.getOutputStream()); - wr.write(enc.getData()); - wr.flush(); - - // Get Response - InputStream is; - try { - is = conn.getInputStream(); - } catch (IOException e) { - if (conn.getResponseCode() == 400 || conn.getResponseCode() == 500) { - is = conn.getErrorStream(); - } else { - throw e; - } - } - BufferedReader rd = new BufferedReader(new InputStreamReader(is)); - String line; - StringBuilder response = new StringBuilder(); - while ((line = rd.readLine()) != null) { - response.append(line); - response.append('\r'); - } - String responseString = response.toString(); - ourLog.info("Response:\n{}", responseString); - - assertEquals(EncodingStyle.ER7.getContentType(), conn.getHeaderField("Content-Type").replaceAll(";.*", "")); - assertEquals(charsetName, conn.getHeaderField("Content-Type").replaceAll(".*;.*charset=", "")); - assertEquals(200, conn.getResponseCode()); - assertEquals(message, myMessage); - assertEquals(myResponse, responseString); - - } - - @Test - public void testServletAR() throws Exception { - - String message = // - - "MSH|^~\\&|||||200803051508||ADT^A31|2|P|2.5\r" + // - - "EVN||200803051509\r" + // - - "PID|||ZZZZZZ83M64Z148R^^^SSN^SSN^^20070103\r"; // - - ADT_A05 msg = new ADT_A05(); - msg.parse(message); - - myResponse = msg.generateACK(AcknowledgmentCode.AR, new HL7Exception("ewrerwe")).encode(); - - Hl7OverHttpLowerLayerProtocol llp = new Hl7OverHttpLowerLayerProtocol(ServerRoleEnum.CLIENT); - String charsetName = "ISO-8859-2"; - llp.setPreferredCharset(Charset.forName(charsetName)); - - Hl7OverHttpRequestEncoder enc = new Hl7OverHttpRequestEncoder(); - enc.setCharset(Charset.forName(charsetName)); - enc.setUsername("hello"); - enc.setPassword("world"); - enc.setMessage(message); - enc.encode(); - - String urlString = "http://localhost:" + myPort + "/"; - ourLog.info("URL: {}", urlString); - URL url = new URL(urlString); - HttpURLConnection conn = (HttpURLConnection) url.openConnection(); - conn.setRequestMethod("POST"); - - conn.setUseCaches(false); - conn.setDoInput(true); - conn.setDoOutput(true); - for (Entry next : enc.getHeaders().entrySet()) { - conn.setRequestProperty(next.getKey(), next.getValue()); - } - - DataOutputStream wr = new DataOutputStream(conn.getOutputStream()); - wr.write(enc.getData()); - wr.flush(); - - // Get Response - InputStream is; - try { - is = conn.getInputStream(); - } catch (IOException e) { - if (conn.getResponseCode() == 400 || conn.getResponseCode() == 500) { - is = conn.getErrorStream(); - } else { - throw e; - } - } - BufferedReader rd = new BufferedReader(new InputStreamReader(is)); - String line; - StringBuilder response = new StringBuilder(); - while ((line = rd.readLine()) != null) { - response.append(line); - response.append('\r'); - } - String responseString = response.toString(); - ourLog.info("Response:\n{}", responseString); - - assertEquals(EncodingStyle.ER7.getContentType(), conn.getHeaderField("Content-Type").replaceAll(";.*", "")); - assertEquals(charsetName, conn.getHeaderField("Content-Type").replaceAll(".*;.*charset=", "")); - assertEquals(200, conn.getResponseCode()); - assertEquals(message, myMessage); - assertEquals(myResponse, responseString); - - } - - @Test - public void testServletAE() throws Exception { - - String message = // - - "MSH|^~\\&|||||200803051508||ADT^A31|2|P|2.5\r" + // - - "EVN||200803051509\r" + // - - "PID|||ZZZZZZ83M64Z148R^^^SSN^SSN^^20070103\r"; // - - ADT_A05 msg = new ADT_A05(); - msg.parse(message); - - myResponse = msg.generateACK(AcknowledgmentCode.AE, new HL7Exception("ewrerwe")).encode(); - - Hl7OverHttpLowerLayerProtocol llp = new Hl7OverHttpLowerLayerProtocol(ServerRoleEnum.CLIENT); - String charsetName = "ISO-8859-2"; - llp.setPreferredCharset(Charset.forName(charsetName)); - - Hl7OverHttpRequestEncoder enc = new Hl7OverHttpRequestEncoder(); - enc.setCharset(Charset.forName(charsetName)); - enc.setUsername("hello"); - enc.setPassword("world"); - enc.setMessage(message); - enc.encode(); - - String urlString = "http://localhost:" + myPort + "/"; - ourLog.info("URL: {}", urlString); - URL url = new URL(urlString); - HttpURLConnection conn = (HttpURLConnection) url.openConnection(); - conn.setRequestMethod("POST"); - - conn.setUseCaches(false); - conn.setDoInput(true); - conn.setDoOutput(true); - for (Entry next : enc.getHeaders().entrySet()) { - conn.setRequestProperty(next.getKey(), next.getValue()); - } - - DataOutputStream wr = new DataOutputStream(conn.getOutputStream()); - wr.write(enc.getData()); - wr.flush(); - - // Get Response - InputStream is; - try { - is = conn.getInputStream(); - } catch (IOException e) { - if (conn.getResponseCode() == 400 || conn.getResponseCode() == 500) { - is = conn.getErrorStream(); - } else { - throw e; - } - } - BufferedReader rd = new BufferedReader(new InputStreamReader(is)); - String line; - StringBuilder response = new StringBuilder(); - while ((line = rd.readLine()) != null) { - response.append(line); - response.append('\r'); - } - String responseString = response.toString(); - ourLog.info("Response:\n{}", responseString); - - assertEquals(EncodingStyle.ER7.getContentType(), conn.getHeaderField("Content-Type").replaceAll(";.*", "")); - assertEquals(charsetName, conn.getHeaderField("Content-Type").replaceAll(".*;.*charset=", "")); - assertEquals(200, conn.getResponseCode()); - assertEquals(message, myMessage); - assertEquals(myResponse, responseString); - - } - - @Test - public void testServletSimpleXml() throws Exception { - - String message = // - - "MSH|^~\\&|||||200803051508||ADT^A31|2|P|2.5\r" + // - - "EVN||200803051509\r" + // - - "PID|||ZZZZZZ83M64Z148R^^^SSN^SSN^^20070103\r"; // - - ADT_A05 msg = new ADT_A05(); - msg.parse(message); - - message = DefaultXMLParser.getInstanceWithNoValidation().encode(msg); - - Hl7OverHttpLowerLayerProtocol llp = new Hl7OverHttpLowerLayerProtocol(ServerRoleEnum.CLIENT); - String charsetName = "ISO-8859-2"; - llp.setPreferredCharset(Charset.forName(charsetName)); - - Hl7OverHttpRequestEncoder enc = new Hl7OverHttpRequestEncoder(); - enc.setCharset(Charset.forName(charsetName)); - enc.setUsername("hello"); - enc.setPassword("world"); - enc.setMessage(message); - enc.encode(); - - String urlString = "http://localhost:" + myPort + "/"; - ourLog.info("URL: {}", urlString); - URL url = new URL(urlString); - HttpURLConnection conn = (HttpURLConnection) url.openConnection(); - conn.setRequestMethod("POST"); - - conn.setUseCaches(false); - conn.setDoInput(true); - conn.setDoOutput(true); - for (Entry next : enc.getHeaders().entrySet()) { - conn.setRequestProperty(next.getKey(), next.getValue()); - } - - DataOutputStream wr = new DataOutputStream(conn.getOutputStream()); - wr.write(enc.getData()); - wr.flush(); - - // Get Response - InputStream is; - try { - is = conn.getInputStream(); - } catch (IOException e) { - if (conn.getResponseCode() == 400 || conn.getResponseCode() == 500) { - is = conn.getErrorStream(); - } else { - throw e; - } - } - BufferedReader rd = new BufferedReader(new InputStreamReader(is)); - String line; - StringBuilder response = new StringBuilder(); - while ((line = rd.readLine()) != null) { - response.append(line); - response.append('\r'); - } - String responseString = response.toString(); - ourLog.info("Response:\n{}", responseString); - - assertEquals(EncodingStyle.XML.getContentType(), conn.getHeaderField("Content-Type").replaceAll(";.*", "")); - assertEquals(charsetName, conn.getHeaderField("Content-Type").replaceAll(".*;.*charset=", "")); - assertEquals(200, conn.getResponseCode()); - assertEquals(message, myMessage); - assertEquals(myResponse.replaceAll("([\\r\\n])+", " "), responseString.replaceAll("([\\r\\n])+", " ")); - - } - - @AfterClass - public static void afterClass() { - // Thread.sleep(1000000); - ourHapiContext.getExecutorService().shutdown(); - } - - @BeforeClass - public static void beforeClass() { - System.setProperty("DEBUG", "true"); - - ourHapiContext = new DefaultHapiContext(); - } + @AfterClass + public static void afterClass() { + // Thread.sleep(1000000); + ourHapiContext.getExecutorService().shutdown(); + } + + @BeforeClass + public static void beforeClass() { + System.setProperty("DEBUG", "true"); + + ourHapiContext = new DefaultHapiContext(); + } } From d8b76773b78a336b9ce597097bbaab8b603c8fd9 Mon Sep 17 00:00:00 2001 From: James Agnew Date: Mon, 30 Oct 2023 13:39:47 -0400 Subject: [PATCH 3/3] Prepare for 2.5 --- hapi-base/pom.xml | 2 +- hapi-bom/pom.xml | 2 +- hapi-dist/pom.xml | 2 +- hapi-examples/pom.xml | 2 +- hapi-hl7overhttp/pom.xml | 2 +- hapi-osgi-base/pom.xml | 2 +- hapi-sourcegen/pom.xml | 2 +- hapi-structures-superstructures/pom.xml | 2 +- hapi-structures-v21/pom.xml | 2 +- hapi-structures-v22/pom.xml | 2 +- hapi-structures-v23/pom.xml | 2 +- hapi-structures-v231-alternatenames/pom.xml | 2 +- hapi-structures-v231/pom.xml | 2 +- hapi-structures-v24/pom.xml | 2 +- hapi-structures-v25/pom.xml | 2 +- hapi-structures-v251/pom.xml | 2 +- hapi-structures-v26/pom.xml | 2 +- hapi-structures-v27/pom.xml | 2 +- hapi-structures-v28/pom.xml | 2 +- hapi-structures-v281/pom.xml | 2 +- hapi-test/pom.xml | 2 +- hapi-testmindeps/pom.xml | 2 +- hapi-testnodeps/pom.xml | 2 +- hapi-testpanel/pom.xml | 2 +- pom.xml | 2 +- 25 files changed, 25 insertions(+), 25 deletions(-) diff --git a/hapi-base/pom.xml b/hapi-base/pom.xml index c55981884..cee11d3b5 100644 --- a/hapi-base/pom.xml +++ b/hapi-base/pom.xml @@ -2,7 +2,7 @@ hapi ca.uhn.hapi - 2.4 + 2.5 ../pom.xml 4.0.0 diff --git a/hapi-bom/pom.xml b/hapi-bom/pom.xml index c86b3a770..47b6be5bd 100644 --- a/hapi-bom/pom.xml +++ b/hapi-bom/pom.xml @@ -8,7 +8,7 @@ hapi ca.uhn.hapi - 2.4 + 2.5 diff --git a/hapi-dist/pom.xml b/hapi-dist/pom.xml index 702d7251d..e4889b5c6 100644 --- a/hapi-dist/pom.xml +++ b/hapi-dist/pom.xml @@ -4,7 +4,7 @@ hapi ca.uhn.hapi - 2.4 + 2.5 ../pom.xml diff --git a/hapi-examples/pom.xml b/hapi-examples/pom.xml index 66f2c05ef..2ff1a2c1b 100644 --- a/hapi-examples/pom.xml +++ b/hapi-examples/pom.xml @@ -3,7 +3,7 @@ hapi ca.uhn.hapi - 2.4 + 2.5 ../pom.xml diff --git a/hapi-hl7overhttp/pom.xml b/hapi-hl7overhttp/pom.xml index 2234df0e9..096668b0f 100644 --- a/hapi-hl7overhttp/pom.xml +++ b/hapi-hl7overhttp/pom.xml @@ -2,7 +2,7 @@ hapi ca.uhn.hapi - 2.4 + 2.5 ../pom.xml 4.0.0 diff --git a/hapi-osgi-base/pom.xml b/hapi-osgi-base/pom.xml index d5ae1ef22..7cab67034 100644 --- a/hapi-osgi-base/pom.xml +++ b/hapi-osgi-base/pom.xml @@ -3,7 +3,7 @@ hapi ca.uhn.hapi - 2.4 + 2.5 ../pom.xml diff --git a/hapi-sourcegen/pom.xml b/hapi-sourcegen/pom.xml index 281d758ca..941c19b20 100644 --- a/hapi-sourcegen/pom.xml +++ b/hapi-sourcegen/pom.xml @@ -3,7 +3,7 @@ hapi ca.uhn.hapi - 2.4 + 2.5 ../pom.xml diff --git a/hapi-structures-superstructures/pom.xml b/hapi-structures-superstructures/pom.xml index 06da8487e..4a1df5122 100644 --- a/hapi-structures-superstructures/pom.xml +++ b/hapi-structures-superstructures/pom.xml @@ -3,7 +3,7 @@ hapi ca.uhn.hapi - 2.4 + 2.5 ../pom.xml diff --git a/hapi-structures-v21/pom.xml b/hapi-structures-v21/pom.xml index a89189998..4062a4757 100644 --- a/hapi-structures-v21/pom.xml +++ b/hapi-structures-v21/pom.xml @@ -3,7 +3,7 @@ hapi ca.uhn.hapi - 2.4 + 2.5 ../pom.xml diff --git a/hapi-structures-v22/pom.xml b/hapi-structures-v22/pom.xml index 1321af480..c1bed8dc7 100644 --- a/hapi-structures-v22/pom.xml +++ b/hapi-structures-v22/pom.xml @@ -3,7 +3,7 @@ hapi ca.uhn.hapi - 2.4 + 2.5 ../pom.xml diff --git a/hapi-structures-v23/pom.xml b/hapi-structures-v23/pom.xml index 9575933d2..de4e4699e 100644 --- a/hapi-structures-v23/pom.xml +++ b/hapi-structures-v23/pom.xml @@ -3,7 +3,7 @@ hapi ca.uhn.hapi - 2.4 + 2.5 ../pom.xml diff --git a/hapi-structures-v231-alternatenames/pom.xml b/hapi-structures-v231-alternatenames/pom.xml index 316b6d242..f3a40b482 100644 --- a/hapi-structures-v231-alternatenames/pom.xml +++ b/hapi-structures-v231-alternatenames/pom.xml @@ -4,7 +4,7 @@ hapi ca.uhn.hapi - 2.4 + 2.5 ../pom.xml diff --git a/hapi-structures-v231/pom.xml b/hapi-structures-v231/pom.xml index 2cb425a9b..b03150a35 100644 --- a/hapi-structures-v231/pom.xml +++ b/hapi-structures-v231/pom.xml @@ -4,7 +4,7 @@ hapi ca.uhn.hapi - 2.4 + 2.5 ../pom.xml diff --git a/hapi-structures-v24/pom.xml b/hapi-structures-v24/pom.xml index a73855aed..59d0266d0 100644 --- a/hapi-structures-v24/pom.xml +++ b/hapi-structures-v24/pom.xml @@ -3,7 +3,7 @@ hapi ca.uhn.hapi - 2.4 + 2.5 ../pom.xml diff --git a/hapi-structures-v25/pom.xml b/hapi-structures-v25/pom.xml index bf440edad..1787c39d2 100644 --- a/hapi-structures-v25/pom.xml +++ b/hapi-structures-v25/pom.xml @@ -3,7 +3,7 @@ hapi ca.uhn.hapi - 2.4 + 2.5 ../pom.xml diff --git a/hapi-structures-v251/pom.xml b/hapi-structures-v251/pom.xml index a040d3e47..b91df76e4 100644 --- a/hapi-structures-v251/pom.xml +++ b/hapi-structures-v251/pom.xml @@ -3,7 +3,7 @@ hapi ca.uhn.hapi - 2.4 + 2.5 ../pom.xml diff --git a/hapi-structures-v26/pom.xml b/hapi-structures-v26/pom.xml index 1ec1c6e95..2030c97ac 100644 --- a/hapi-structures-v26/pom.xml +++ b/hapi-structures-v26/pom.xml @@ -4,7 +4,7 @@ hapi ca.uhn.hapi - 2.4 + 2.5 ../pom.xml diff --git a/hapi-structures-v27/pom.xml b/hapi-structures-v27/pom.xml index d1fd36efe..df5d7d2bd 100644 --- a/hapi-structures-v27/pom.xml +++ b/hapi-structures-v27/pom.xml @@ -4,7 +4,7 @@ hapi ca.uhn.hapi - 2.4 + 2.5 ../pom.xml diff --git a/hapi-structures-v28/pom.xml b/hapi-structures-v28/pom.xml index 52b52990c..89e95fde6 100644 --- a/hapi-structures-v28/pom.xml +++ b/hapi-structures-v28/pom.xml @@ -4,7 +4,7 @@ hapi ca.uhn.hapi - 2.4 + 2.5 ../pom.xml diff --git a/hapi-structures-v281/pom.xml b/hapi-structures-v281/pom.xml index b37188eee..f032f4243 100644 --- a/hapi-structures-v281/pom.xml +++ b/hapi-structures-v281/pom.xml @@ -4,7 +4,7 @@ hapi ca.uhn.hapi - 2.4 + 2.5 ../pom.xml diff --git a/hapi-test/pom.xml b/hapi-test/pom.xml index 2b4919f07..a6776499e 100644 --- a/hapi-test/pom.xml +++ b/hapi-test/pom.xml @@ -3,7 +3,7 @@ hapi ca.uhn.hapi - 2.4 + 2.5 ../pom.xml diff --git a/hapi-testmindeps/pom.xml b/hapi-testmindeps/pom.xml index e399cf52d..5ca92b738 100644 --- a/hapi-testmindeps/pom.xml +++ b/hapi-testmindeps/pom.xml @@ -3,7 +3,7 @@ hapi ca.uhn.hapi - 2.4 + 2.5 ../pom.xml diff --git a/hapi-testnodeps/pom.xml b/hapi-testnodeps/pom.xml index 40652bcf2..11109541b 100644 --- a/hapi-testnodeps/pom.xml +++ b/hapi-testnodeps/pom.xml @@ -3,7 +3,7 @@ hapi ca.uhn.hapi - 2.4 + 2.5 ../pom.xml diff --git a/hapi-testpanel/pom.xml b/hapi-testpanel/pom.xml index 8dd7717a5..0318dfaf9 100644 --- a/hapi-testpanel/pom.xml +++ b/hapi-testpanel/pom.xml @@ -2,7 +2,7 @@ hapi ca.uhn.hapi - 2.4 + 2.5 ../pom.xml 4.0.0 diff --git a/pom.xml b/pom.xml index 967dba42c..80bfd25d5 100644 --- a/pom.xml +++ b/pom.xml @@ -12,7 +12,7 @@ ca.uhn.hapi hapi pom - 2.4 + 2.5 HAPI - The Open Source Java HL7 Parser and API http://hl7api.sourceforge.net/