public static SOAPEnvelope getSOAPEnvelope(
Message message, MessageContext msgContext, InputStream in)
throws XMLStreamException {
SOAPEnvelope envelope = null;
StAXBuilder builder = null;
String contentType = JMSUtils.getProperty(message, JMSConstants.CONTENT_TYPE);
if (contentType != null) {
if (contentType.indexOf(
HTTPConstants.HEADER_ACCEPT_MULTIPART_RELATED) > -1) {
builder = BuilderUtil.getAttachmentsBuilder(
msgContext, in, contentType, true);
envelope = (SOAPEnvelope) builder.getDocumentElement();
} else {
String charSetEnc = BuilderUtil.getCharSetEncoding(contentType);
builder = BuilderUtil.getSOAPBuilder(in, charSetEnc);
// Set the encoding scheme in the message context
msgContext.setProperty(Constants.Configuration.CHARACTER_SET_ENCODING, charSetEnc);
envelope = (SOAPEnvelope) builder.getDocumentElement();
}
}
// handle pure plain vanilla POX and binary content (non SOAP)
if (builder == null) {
SOAPFactory soapFactory = new SOAP11Factory();
try {
XMLStreamReader xmlreader = StAXUtils.createXMLStreamReader
(in, MessageContext.DEFAULT_CHAR_SET_ENCODING);
// Set the encoding scheme in the message context
msgContext.setProperty(Constants.Configuration.CHARACTER_SET_ENCODING,
MessageContext.DEFAULT_CHAR_SET_ENCODING);
builder = new StAXOMBuilder(xmlreader);
builder.setOMBuilderFactory(soapFactory);
String ns = builder.getDocumentElement().getNamespace().getNamespaceURI();
if (SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI.equals(ns)) {
envelope = getEnvelope(in, SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI);
} else if (SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI.equals(ns)) {
envelope = getEnvelope(in, SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI);
} else {
// this is POX ... mark MC as REST
msgContext.setDoingREST(true);
envelope = soapFactory.getDefaultEnvelope();
envelope.getBody().addChild(builder.getDocumentElement());
}
} catch (Exception e) {
log.debug("Non SOAP/XML JMS message received");
Parameter operationParam = msgContext.getAxisService().
getParameter(JMSConstants.OPERATION_PARAM);
QName operationQName = (operationParam != null ?
getQName(operationParam.getValue()) : JMSConstants.DEFAULT_OPERATION);
AxisOperation operation = msgContext.getAxisService().getOperation(operationQName);
if (operation != null) {
msgContext.setAxisOperation(operation);
} else {
handleException("Cannot find operation : " + operationQName + " on the service "
+ msgContext.getAxisService());
}
Parameter wrapperParam = msgContext.getAxisService().
getParameter(JMSConstants.WRAPPER_PARAM);
QName wrapperQName = (wrapperParam != null ?
getQName(wrapperParam.getValue()) : JMSConstants.DEFAULT_WRAPPER);
OMElement wrapper = soapFactory.createOMElement(wrapperQName, null);
try {
if (message instanceof TextMessage) {
OMTextImpl textData = (OMTextImpl) soapFactory.createOMText(
((TextMessage) message).getText());
wrapper.addChild(textData);
} else if (message instanceof BytesMessage) {
BytesMessage bm = (BytesMessage) message;
byte[] msgBytes = new byte[(int) bm.getBodyLength()];
bm.reset();
bm.readBytes(msgBytes);
DataHandler dataHandler = new DataHandler(
new ByteArrayDataSource(msgBytes));
OMText textData = soapFactory.createOMText(dataHandler, true);
wrapper.addChild(textData);
msgContext.setDoingMTOM(true);
} else {
handleException("Unsupported JMS Message format : " + message.getJMSType());
}
envelope = soapFactory.getDefaultEnvelope();
envelope.getBody().addChild(wrapper);
} catch (JMSException j) {
handleException("Error wrapping JMS message into a SOAP envelope ", j);
}
}
}
String charEncOfMessage = builder == null ? null :
builder.getDocument() == null ? null : builder.getDocument().getCharsetEncoding();
String charEncOfTransport = ((String) msgContext.getProperty(
Constants.Configuration.CHARACTER_SET_ENCODING));
if (charEncOfMessage != null &&
!(charEncOfMessage.trim().length() == 0) &&