use of org.apache.axis2.format.TextMessageBuilderAdapter in project wso2-axis2-transports by wso2.
the class JMSUtils method setSOAPEnvelope.
/**
* Set the SOAPEnvelope to the Axis2 MessageContext, from the JMS Message passed in
* @param message the JMS message read
* @param msgContext the Axis2 MessageContext to be populated
* @param contentType content type for the message
* @throws AxisFault
* @throws JMSException
*/
public static void setSOAPEnvelope(Message message, MessageContext msgContext, String contentType) throws AxisFault, JMSException {
if (contentType == null) {
if (message instanceof TextMessage) {
contentType = "text/plain";
msgContext.setProperty(org.apache.axis2.Constants.Configuration.CONTENT_TYPE, "text/plain");
} else {
contentType = "application/octet-stream";
}
if (log.isDebugEnabled()) {
log.debug("No content type specified; assuming " + contentType);
}
}
int index = contentType.indexOf(';');
String type = index > 0 ? contentType.substring(0, index) : contentType;
Builder builder = BuilderUtil.getBuilderFromSelector(type, msgContext);
if (builder == null) {
if (log.isDebugEnabled()) {
log.debug("No message builder found for type '" + type + "'. Falling back to SOAP.");
}
builder = new SOAPBuilder();
}
OMElement documentElement;
if (message instanceof BytesMessage) {
// Extract the charset encoding from the content type and
// set the CHARACTER_SET_ENCODING property as e.g. SOAPBuilder relies on this.
String charSetEnc = null;
try {
if (contentType != null) {
charSetEnc = new ContentType(contentType).getParameter("charset");
}
} catch (ParseException ex) {
// ignore
}
msgContext.setProperty(Constants.Configuration.CHARACTER_SET_ENCODING, charSetEnc);
if (builder instanceof DataSourceMessageBuilder) {
documentElement = ((DataSourceMessageBuilder) builder).processDocument(new BytesMessageDataSource((BytesMessage) message), contentType, msgContext);
} else {
documentElement = builder.processDocument(new BytesMessageInputStream((BytesMessage) message), contentType, msgContext);
}
} else if (message instanceof TextMessage) {
TextMessageBuilder textMessageBuilder;
if (builder instanceof TextMessageBuilder) {
textMessageBuilder = (TextMessageBuilder) builder;
} else {
textMessageBuilder = new TextMessageBuilderAdapter(builder);
}
String content = ((TextMessage) message).getText();
documentElement = textMessageBuilder.processDocument(content, contentType, msgContext);
} else if (message instanceof MapMessage) {
documentElement = convertJMSMapToXML((MapMessage) message);
} else {
Class msgClass = message.getClass();
String content = "Unsupported JMS message type : " + (msgClass != null ? msgClass.getName() : "undefined.");
log.error(content);
msgContext.setProperty(JMSConstants.SENDING_FAULT, true);
msgContext.setProperty(JMSConstants.ERROR_MESSAGE, content);
// ERROR_CODE is not set for msg context since it will suspend the ep if not defined in the configs and
// under this case we don't need the default behavior to get the endpoint suspended.
TextMessageBuilder textMessageBuilder = new TextMessageBuilderAdapter(builder);
documentElement = textMessageBuilder.processDocument(content, contentType, msgContext);
}
msgContext.setEnvelope(TransportUtils.createSOAPEnvelope(documentElement));
}
use of org.apache.axis2.format.TextMessageBuilderAdapter in project wso2-axis2-transports by wso2.
the class MSMQUtil method setSOAPEnvelope.
public static void setSOAPEnvelope(Message message, MessageContext msgContext, String contentType) throws AxisFault {
if (contentType == null) {
// TODO;we only support text/plain
contentType = "text/plain";
if (log.isDebugEnabled()) {
log.debug("No content type specified; assuming " + contentType);
}
}
int index = contentType.indexOf(';');
String type = index > 0 ? contentType.substring(0, index) : contentType;
Builder builder = BuilderUtil.getBuilderFromSelector(type, msgContext);
String messageBody = null;
try {
messageBody = message.getBodyAsString();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (builder == null) {
if (log.isDebugEnabled()) {
log.debug("No message builder found for type' " + type + ".Using SOAP builder");
}
builder = new SOAPBuilder();
}
OMElement documentElement;
// TODO: we need to handle the message types separately. Assume text message builder format
TextMessageBuilder textMessageBuilder;
if (builder instanceof TextMessageBuilder) {
textMessageBuilder = (TextMessageBuilder) builder;
} else {
textMessageBuilder = new TextMessageBuilderAdapter(builder);
}
documentElement = textMessageBuilder.processDocument(messageBody, contentType, msgContext);
msgContext.setEnvelope(TransportUtils.createSOAPEnvelope(documentElement));
}
Aggregations