public void writeTo(MessageContext msgCtxt,
OMOutputFormat format,
OutputStream out,
boolean preserve) throws AxisFault {
if (log.isDebugEnabled()) {
log.debug("Start writing the message to OutputStream");
}
// Check whther the message to be written is a fault message
if (msgCtxt.getFLOW() == MessageContext.OUT_FAULT_FLOW
|| msgCtxt.getEnvelope().hasFault()) {
try {
SOAPFault fault = msgCtxt.getEnvelope().getBody().getFault();
String hessianFaultDetail = "";
String hessianFaultMessage = "";
String hessianFaultCode = "500";
if (fault.getDetail() != null) {
hessianFaultDetail = fault.getDetail().getText();
}
if (fault.getReason() != null) {
hessianFaultMessage = fault.getReason().getText();
}
if (fault.getCode() != null) {
hessianFaultCode = fault.getCode().getText();
}
BufferedOutputStream faultOutStream = new BufferedOutputStream(out);
HessianUtils.writeFault(
hessianFaultCode, hessianFaultMessage, hessianFaultDetail, faultOutStream);
faultOutStream.flush();
faultOutStream.close();
} catch (IOException e) {
handleException("Unalbe to write the fault as a hessian message", e);
}
// if the message is not a fault extract the Hessian bytes and write it to the wire
} else {
OMText hessianOMText = null;
OMElement omElement = msgCtxt.getEnvelope().getBody().getFirstElement();
Iterator it = omElement.getChildren();
while (it.hasNext()) {
OMNode hessianElement = (OMNode) it.next();
if (hessianElement instanceof OMText) {
OMText tempNode = (OMText) hessianElement;
if (tempNode.getDataHandler() != null && ((DataHandler) tempNode
.getDataHandler()).getDataSource() instanceof SynapseBinaryDataSource) {
hessianOMText = tempNode;
}
}
}
if (hessianOMText != null) {
try {
SynapseBinaryDataSource synapseBinaryDataSource = (SynapseBinaryDataSource) (
(DataHandler) hessianOMText.getDataHandler()).getDataSource();
InputStream inputStream = synapseBinaryDataSource.getInputStream();
BufferedOutputStream outputStream = new BufferedOutputStream(out);
byte[] buffer = new byte[1024];
int byteCount;
while ((byteCount=inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, byteCount);
}
inputStream.close();
outputStream.flush();
outputStream.close();
} catch (IOException e) {
handleException("Couldn't get the bytes from the HessianDataSource", e);
}
} else {
handleException("Unable to find the hessian content in the payload");
}
}
if (log.isDebugEnabled()) {
log.debug("Writing message as a hessian message is successful");
}
}
Extract Hessian bytes from the received SOAP message and write it onto the wire |