|
From: Davies, B. <Bri...@Mc...> - 2016-02-19 20:50:06
|
I am trying to test sending a message using a HAPI http client using the following configuration. The server starts and the message is sent but
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import ca.uhn.hl7v2.hoh.hapi.client.HohClientSimple;
import ca.uhn.hl7v2.hoh.sockets.TlsSocketFactory;
import ca.uhn.hl7v2.parser.PipeParser;
@Component
@PropertySource(value = {"classpath:connection.properties" })
public class HttpClientConfig {
@Autowired
private Environment env;
@Bean
public HohClientSimple httpClient() {
HohClientSimple client = new HohClientSimple(env.getProperty("http.host"), Integer.parseInt(env.getProperty("http.port")), env.getProperty("http.uri.path"),PipeParser.getInstanceWithNoValidation());
client.setSocketFactory(new TlsSocketFactory());
return client;
}
}
import java.io.IOException;
import java.util.Map;
import ca.uhn.hl7v2.AcknowledgmentCode;
import ca.uhn.hl7v2.HL7Exception;
import ca.uhn.hl7v2.model.Message;
import ca.uhn.hl7v2.protocol.ReceivingApplication;
import ca.uhn.hl7v2.protocol.ReceivingApplicationException;
public class MessageHandler implements ReceivingApplication {
@Override
public Message processMessage(Message theMessage, Map<String, Object> theMetadata)
throws ReceivingApplicationException, HL7Exception {
Message response = null;
try {
response = theMessage.generateACK();
} catch (IOException e) {
e.printStackTrace();
}
return response;
}
@Override
public boolean canProcess(Message theMessage) {
return true;
}
}
import java.io.IOException;
import javax.servlet.Servlet;
import org.eclipse.jetty.server.Server;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
import ca.uhn.hl7v2.DefaultHapiContext;
import ca.uhn.hl7v2.HL7Exception;
import ca.uhn.hl7v2.app.Connection;
import ca.uhn.hl7v2.hoh.api.DecodeException;
import ca.uhn.hl7v2.hoh.api.EncodeException;
import ca.uhn.hl7v2.hoh.api.ISendable;
import ca.uhn.hl7v2.hoh.hapi.api.MessageSendable;
import ca.uhn.hl7v2.hoh.hapi.client.HohClientSimple;
import ca.uhn.hl7v2.hoh.llp.Hl7OverHttpLowerLayerProtocol;
import ca.uhn.hl7v2.hoh.util.ServerRoleEnum;
import ca.uhn.hl7v2.llp.LLPException;
import ca.uhn.hl7v2.llp.LowerLayerProtocol;
import ca.uhn.hl7v2.model.DataTypeException;
import ca.uhn.hl7v2.model.Message;
import ca.uhn.hl7v2.model.v25.message.ADT_A01;
import ca.uhn.hl7v2.model.v25.message.ADT_A38;
import ca.uhn.hl7v2.model.v25.segment.DB1;
import ca.uhn.hl7v2.model.v25.segment.MSH;
import ca.uhn.hl7v2.model.v25.segment.SFT;
import ca.uhn.hl7v2.parser.EncodingNotSupportedException;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {HttpClientConfig.class},loader = AnnotationConfigContextLoader.class)
public class SendingRequestTest {
@Autowired
private HohClientSimple httpClient;
@Before
public void initServer() throws Exception{
Server server = new Server(5050);
ServletContextHandler handler = new ServletContextHandler(server, "/testURL");
Servlet servlet=new HttpMessageReceiver();
handler.addServlet(new ServletHolder(servlet), "/appContext");
server.start();
System.out.println("Server started!!");
}
@Test
public void testSendReceive(){
try {
ADT_A38 adt=new ADT_A38();
MSH msh1=adt.getMSH();
msh1.getMsh1_FieldSeparator().setValue("|");
msh1.getMsh2_EncodingCharacters().setValue("^~\\&");
SFT sft=adt.getSFT();
adt.insertSFT(sft, 0);
DB1 db1=adt.getDB1();
adt.insertDB1(db1, 0);
ISendable sendable = new MessageSendable(adt);
//Message message=createTestMessage();
httpClient.sendAndReceiveMessage(sendable);
} catch (DataTypeException e) {
e.printStackTrace();
System.out.println("datatype problem");
} catch (EncodingNotSupportedException e) {
e.printStackTrace();
System.out.println("encode not supported problem");
System.exit(1);
} catch (HL7Exception e) {
e.printStackTrace();
} catch (DecodeException e) {
e.printStackTrace();
System.out.println("decode problem");
System.exit(1);
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
} catch (EncodeException e) {
e.printStackTrace();
System.out.println("encode problem, not that it's not supported");
}
}
}
I get the following error:
Feb 19, 2016 1:14:25 PM org.springframework.context.support.GenericApplicati onContext prepareRefresh
INFO: Refreshing org.springframework.context.support.GenericApplicationConte xt@3b733273: startup date [Fri Feb 19 13:14:25 CST 2016]; root of context hierar chy
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further de tails.
SendingRequestTest > testSendReceive STANDARD_OUT
Server started!!
SendingRequestTest > testSendReceive STANDARD_ERR OR
ca.uhn.hl7v2.hoh.api.NonHl7ResponseException: Invalid Content-Type: text/html l
at ca.uhn.hl7v2.hoh.encoder.AbstractHl7OverHttpDecoder.doReadContentsFro mInputStreamAndDecode(AbstractHl7OverHttpDecoder.java:233)
at ca.uhn.hl7v2.hoh.encoder.AbstractHl7OverHttpDecoder.readHeadersAndCon tentsFromInputStreamAndDecode(AbstractHl7OverHttpDecoder.java:541)
at ca.uhn.hl7v2.hoh.raw.client.AbstractRawClient.doSendAndReceiveInterna l(AbstractRawClient.java:159)
....
|