use of org.apache.camel.component.mllp.MllpWriteException in project camel by apache.
the class MllpSocketWriter method writeEnvelopedPayload.
public void writeEnvelopedPayload(byte[] hl7MessageBytes, byte[] hl7AcknowledgementBytes) throws MllpException {
if (socket == null) {
final String errorMessage = "Socket is null";
if (isAcknowledgementWriter()) {
throw new MllpAcknowledgementDeliveryException(errorMessage, hl7MessageBytes, hl7AcknowledgementBytes);
} else {
throw new MllpWriteException(errorMessage, hl7MessageBytes);
}
} else if (!socket.isConnected()) {
final String errorMessage = "Socket is not connected";
if (isAcknowledgementWriter()) {
throw new MllpAcknowledgementDeliveryException(errorMessage, hl7MessageBytes, hl7AcknowledgementBytes);
} else {
throw new MllpWriteException(errorMessage, hl7MessageBytes);
}
} else if (socket.isClosed()) {
final String errorMessage = "Socket is closed";
if (isAcknowledgementWriter()) {
throw new MllpAcknowledgementDeliveryException(errorMessage, hl7MessageBytes, hl7AcknowledgementBytes);
} else {
throw new MllpWriteException(errorMessage, hl7MessageBytes);
}
}
OutputStream socketOutputStream = null;
try {
socketOutputStream = socket.getOutputStream();
} catch (IOException e) {
final String errorMessage = "Failed to retrieve the OutputStream from the Socket";
if (isAcknowledgementWriter()) {
throw new MllpAcknowledgementDeliveryException(errorMessage, hl7MessageBytes, hl7AcknowledgementBytes);
} else {
throw new MllpWriteException(errorMessage, hl7MessageBytes, hl7AcknowledgementBytes);
}
}
try {
socketOutputStream.write(START_OF_BLOCK);
} catch (IOException e) {
final String errorMessage = "Failed to write the START_OF_BLOCK to the Socket's OutputStream";
if (isAcknowledgementWriter()) {
throw new MllpAcknowledgementDeliveryException(errorMessage, hl7MessageBytes, hl7AcknowledgementBytes);
} else {
throw new MllpWriteException(errorMessage, hl7MessageBytes, hl7AcknowledgementBytes);
}
}
if (isAcknowledgementWriter()) {
if (hl7AcknowledgementBytes == null) {
log.warn("HL7 Acknowledgement payload is null - sending empty MLLP payload");
} else if (hl7AcknowledgementBytes.length <= 0) {
log.warn("HL7 Acknowledgement payload is empty - sending empty MLLP payload");
} else {
try {
socketOutputStream.write(hl7AcknowledgementBytes);
} catch (IOException ioEx) {
throw new MllpAcknowledgementDeliveryException("Failed to write the HL7 Acknowledgement payload to the Socket's OutputStream", hl7MessageBytes, hl7AcknowledgementBytes, ioEx);
}
}
} else {
if (hl7MessageBytes == null) {
log.warn("HL7 Message payload is null - sending empty MLLP payload");
} else if (hl7MessageBytes.length <= 0) {
log.warn("HL7 Message payload is empty - sending empty MLLP payload");
} else {
try {
socketOutputStream.write(hl7MessageBytes);
} catch (IOException ioEx) {
throw new MllpWriteException("Failed to write the HL7 Message payload to the Socket's OutputStream", hl7MessageBytes, hl7AcknowledgementBytes, ioEx);
}
}
}
try {
socketOutputStream.write(PAYLOAD_TERMINATOR);
socketOutputStream.flush();
} catch (IOException e) {
final String errorMessage = "Failed to write the END_OF_BLOCK and END_OF_DATA to the Socket's OutputStream";
if (isAcknowledgementWriter()) {
throw new MllpAcknowledgementDeliveryException(errorMessage, hl7MessageBytes, hl7AcknowledgementBytes);
} else {
throw new MllpWriteException(errorMessage, hl7MessageBytes, hl7AcknowledgementBytes);
}
}
}
use of org.apache.camel.component.mllp.MllpWriteException in project camel by apache.
the class MllpBufferedSocketWriter method writeEnvelopedPayload.
@Override
public void writeEnvelopedPayload(byte[] hl7MessageBytes, byte[] hl7AcknowledgementBytes) throws MllpException {
if (socket == null) {
final String errorMessage = "Socket is null";
if (isAcknowledgementWriter()) {
throw new MllpAcknowledgementDeliveryException(errorMessage, hl7MessageBytes, hl7AcknowledgementBytes);
} else {
throw new MllpWriteException(errorMessage, hl7MessageBytes);
}
} else if (!socket.isConnected()) {
final String errorMessage = "Socket is not connected";
if (isAcknowledgementWriter()) {
throw new MllpAcknowledgementDeliveryException(errorMessage, hl7MessageBytes, hl7AcknowledgementBytes);
} else {
throw new MllpWriteException(errorMessage, hl7MessageBytes);
}
} else if (socket.isClosed()) {
final String errorMessage = "Socket is closed";
if (isAcknowledgementWriter()) {
throw new MllpAcknowledgementDeliveryException(errorMessage, hl7MessageBytes, hl7AcknowledgementBytes);
} else {
throw new MllpWriteException(errorMessage, hl7MessageBytes);
}
}
OutputStream socketOutputStream = null;
try {
socketOutputStream = socket.getOutputStream();
} catch (IOException e) {
final String errorMessage = "Failed to retrieve the OutputStream from the Socket";
if (isAcknowledgementWriter()) {
throw new MllpAcknowledgementDeliveryException(errorMessage, hl7MessageBytes, hl7AcknowledgementBytes);
} else {
throw new MllpWriteException(errorMessage, hl7MessageBytes, hl7AcknowledgementBytes);
}
}
outputBuffer.write(START_OF_BLOCK);
if (isAcknowledgementWriter()) {
if (hl7AcknowledgementBytes == null) {
log.warn("HL7 Acknowledgement payload is null - sending empty MLLP payload");
} else if (hl7AcknowledgementBytes.length <= 0) {
log.warn("HL7 Acknowledgement payload is empty - sending empty MLLP payload");
} else {
outputBuffer.write(hl7AcknowledgementBytes, 0, hl7AcknowledgementBytes.length);
}
} else {
if (hl7MessageBytes == null) {
log.warn("HL7 Message payload is null - sending empty MLLP payload");
} else if (hl7MessageBytes.length <= 0) {
log.warn("HL7 Message payload is empty - sending empty MLLP payload");
} else {
outputBuffer.write(hl7MessageBytes, 0, hl7MessageBytes.length);
}
}
outputBuffer.write(END_OF_BLOCK);
outputBuffer.write(END_OF_DATA);
try {
outputBuffer.writeTo(socketOutputStream);
socketOutputStream.flush();
} catch (IOException e) {
final String errorMessage = "Failed to write the MLLP payload to the Socket's OutputStream";
if (isAcknowledgementWriter()) {
throw new MllpAcknowledgementDeliveryException(errorMessage, hl7MessageBytes, hl7AcknowledgementBytes);
} else {
throw new MllpWriteException(errorMessage, hl7MessageBytes);
}
} finally {
outputBuffer.reset();
}
}
Aggregations