final XMLStreamWriter writer = getTransformer().getOutputFactory()
.createXMLStreamWriter(out, message.getEncoding());
message.getSerializer().writeMessage(message, writer, context);
writer.close();
} catch (XMLStreamException e) {
throw new XFireException("Error closing output stream", e);
}
return;
}
} else {
try {
DeliveryChannel channel = ((JbiTransport) getTransport()).getContext().getDeliveryChannel();
MessageExchangeFactory factory = channel.createExchangeFactory();
URI mep = null;
if (context.getExchange().getOperation().getOutputMessage() != null) {
mep = MessageExchangeSupport.IN_OUT;
} else if (context.getExchange().getOperation().getFaults().size() > 0) {
mep = MessageExchangeSupport.ROBUST_IN_ONLY;
} else {
mep = MessageExchangeSupport.IN_ONLY;
}
MessageExchange me = factory.createExchange(mep);
me.setInterfaceName((QName) context.getService().getProperty(JBI_INTERFACE_NAME));
me.setOperation(context.getExchange().getOperation().getQName());
me.setService((QName) context.getService().getProperty(JBI_SERVICE_NAME));
me.setEndpoint((ServiceEndpoint) context.getService().getProperty(JBI_ENDPOINT));
NormalizedMessage msg = me.createMessage();
me.setMessage(msg, "in");
if (Boolean.TRUE.equals(context.getService().getProperty(JBI_SECURITY_PROPAGATATION))) {
MessageExchange oldMe = JBIContext.getMessageExchange();
NormalizedMessage oldMsg = (oldMe != null) ? oldMe.getMessage("in") : null;
if (oldMsg != null) {
msg.setSecuritySubject(oldMsg.getSecuritySubject());
}
}
me.setOperation(context.getExchange().getOperation().getQName());
msg.setContent(getContent(context, message));
if (!channel.sendSync(me)) {
throw new XFireException("Unable to send jbi exchange: sendSync returned false");
}
if (me.getStatus() == ExchangeStatus.ERROR) {
if (me.getError() != null) {
throw new XFireFault(me.getError(), XFireFault.RECEIVER);
} else {
throw new XFireFault("Unkown Error", XFireFault.RECEIVER);
}
} else if (me.getStatus() == ExchangeStatus.ACTIVE) {
if (me.getFault() != null) {
JDOMResult result = new JDOMResult();
String str = getTransformer().contentToString(me.getFault());
getTransformer().toResult(new StringSource(str), result);
Element e = result.getDocument().getRootElement();
e = (Element) e.clone();
me.setStatus(ExchangeStatus.DONE);
channel.send(me);
XFireFault xfireFault = new XFireFault(str, XFireFault.RECEIVER);
xfireFault.getDetail().addContent(e);
throw xfireFault;
} else if (me.getMessage("out") != null) {
Source outSrc = me.getMessage("out").getContent();
InMessage inMessage = new InMessage(getTransformer().toXMLStreamReader(outSrc), getUri());
getEndpoint().onReceive(context, inMessage);
me.setStatus(ExchangeStatus.DONE);
channel.send(me);
}
}
} catch (XFireException e) {
throw e;
} catch (Exception e) {
throw new XFireException("Error sending jbi exchange", e);
}
}
}