use of org.apache.axis2.transport.jms.iowrappers.BytesMessageOutputStream in project wso2-axis2-transports by wso2.
the class MockEchoEndpoint method setUp.
@Setup
@SuppressWarnings("unused")
private void setUp(JMSTestEnvironment env, JMSRequestResponseChannel channel) throws Exception {
Destination destination = channel.getDestination();
Destination replyDestination = channel.getReplyDestination();
connection = env.getConnectionFactory().createConnection();
connection.setExceptionListener(this);
connection.start();
replyConnection = env.getConnectionFactory().createConnection();
replyConnection.setExceptionListener(this);
final Session replySession = replyConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
final MessageProducer producer = replySession.createProducer(replyDestination);
MessageConsumer consumer = connection.createSession(false, Session.AUTO_ACKNOWLEDGE).createConsumer(destination);
consumer.setMessageListener(new MessageListener() {
public void onMessage(Message message) {
try {
log.info("Message received: ID = " + message.getJMSMessageID());
Message reply;
if (message instanceof BytesMessage) {
reply = replySession.createBytesMessage();
IOUtils.copy(new BytesMessageInputStream((BytesMessage) message), new BytesMessageOutputStream((BytesMessage) reply));
} else if (message instanceof TextMessage) {
reply = replySession.createTextMessage();
((TextMessage) reply).setText(((TextMessage) message).getText());
} else {
// TODO
throw new UnsupportedOperationException("Unsupported message type");
}
reply.setJMSCorrelationID(message.getJMSMessageID());
reply.setStringProperty(BaseConstants.CONTENT_TYPE, message.getStringProperty(BaseConstants.CONTENT_TYPE));
producer.send(reply);
log.info("Message sent: ID = " + reply.getJMSMessageID());
} catch (Throwable ex) {
fireEndpointError(ex);
}
}
});
}
use of org.apache.axis2.transport.jms.iowrappers.BytesMessageOutputStream in project wso2-axis2-transports by wso2.
the class JMSSender method createJMSMessage.
/**
* Create a JMS Message from the given MessageContext and using the given
* session
*
* @param msgContext the MessageContext
* @param session the JMS session
* @param contentTypeProperty the message property to be used to store the
* content type
* @return a JMS message from the context and session
* @throws JMSException on exception
* @throws AxisFault on exception
*/
private Message createJMSMessage(MessageContext msgContext, Session session, String contentTypeProperty) throws JMSException, AxisFault {
Message message = null;
String msgType = getProperty(msgContext, JMSConstants.JMS_MESSAGE_TYPE);
// check the first element of the SOAP body, do we have content wrapped using the
// default wrapper elements for binary (BaseConstants.DEFAULT_BINARY_WRAPPER) or
// text (BaseConstants.DEFAULT_TEXT_WRAPPER) ? If so, do not create SOAP messages
// for JMS but just get the payload in its native format
String jmsPayloadType = guessMessageType(msgContext);
if (jmsPayloadType == null) {
OMOutputFormat format = BaseUtils.getOMOutputFormat(msgContext);
MessageFormatter messageFormatter = null;
try {
messageFormatter = MessageProcessorSelector.getMessageFormatter(msgContext);
} catch (AxisFault axisFault) {
throw new JMSException("Unable to get the message formatter to use");
}
String contentType = messageFormatter.getContentType(msgContext, format, msgContext.getSoapAction());
boolean useBytesMessage = msgType != null && JMSConstants.JMS_BYTE_MESSAGE.equals(msgType) || contentType.indexOf(HTTPConstants.HEADER_ACCEPT_MULTIPART_RELATED) > -1;
OutputStream out;
StringWriter sw;
if (useBytesMessage) {
BytesMessage bytesMsg = session.createBytesMessage();
sw = null;
out = new BytesMessageOutputStream(bytesMsg);
message = bytesMsg;
} else {
sw = new StringWriter();
try {
out = new WriterOutputStream(sw, format.getCharSetEncoding());
} catch (UnsupportedCharsetException ex) {
handleException("Unsupported encoding " + format.getCharSetEncoding(), ex);
return null;
}
}
try {
messageFormatter.writeTo(msgContext, format, out, true);
out.close();
} catch (IOException e) {
Transaction transaction = null;
try {
transaction = ((TransactionManager) msgContext.getProperty(JMSConstants.JMS_XA_TRANSACTION_MANAGER)).getTransaction();
rollbackXATransaction(transaction);
} catch (SystemException e1) {
handleException("Error occurred during obtaining transaction", e1);
}
handleException("IO Error while creating BytesMessage", e);
}
if (!useBytesMessage) {
TextMessage txtMsg = session.createTextMessage();
txtMsg.setText(sw.toString());
message = txtMsg;
}
if (contentTypeProperty != null) {
message.setStringProperty(contentTypeProperty, contentType);
}
} else if (JMSConstants.JMS_BYTE_MESSAGE.equals(jmsPayloadType)) {
message = session.createBytesMessage();
BytesMessage bytesMsg = (BytesMessage) message;
OMElement wrapper = msgContext.getEnvelope().getBody().getFirstChildWithName(BaseConstants.DEFAULT_BINARY_WRAPPER);
OMNode omNode = wrapper.getFirstOMChild();
if (omNode != null && omNode instanceof OMText) {
Object dh = ((OMText) omNode).getDataHandler();
if (dh != null && dh instanceof DataHandler) {
try {
((DataHandler) dh).writeTo(new BytesMessageOutputStream(bytesMsg));
} catch (IOException e) {
Transaction transaction = null;
try {
transaction = ((TransactionManager) msgContext.getProperty(JMSConstants.JMS_XA_TRANSACTION_MANAGER)).getTransaction();
rollbackXATransaction(transaction);
} catch (SystemException e1) {
handleException("Error occurred during obtaining transaction", e1);
}
handleException("Error serializing binary content of element : " + BaseConstants.DEFAULT_BINARY_WRAPPER, e);
}
}
}
} else if (JMSConstants.JMS_TEXT_MESSAGE.equals(jmsPayloadType)) {
message = session.createTextMessage();
TextMessage txtMsg = (TextMessage) message;
txtMsg.setText(msgContext.getEnvelope().getBody().getFirstChildWithName(BaseConstants.DEFAULT_TEXT_WRAPPER).getText());
} else if (JMSConstants.JMS_MAP_MESSAGE.equalsIgnoreCase(jmsPayloadType)) {
message = session.createMapMessage();
JMSUtils.convertXMLtoJMSMap(msgContext.getEnvelope().getBody().getFirstChildWithName(JMSConstants.JMS_MAP_QNAME), (MapMessage) message);
}
// set the JMS correlation ID if specified
String correlationId = getProperty(msgContext, JMSConstants.JMS_COORELATION_ID);
if (correlationId == null && msgContext.getRelatesTo() != null) {
correlationId = msgContext.getRelatesTo().getValue();
}
if (correlationId != null) {
message.setJMSCorrelationID(correlationId);
}
if (msgContext.isServerSide()) {
// set SOAP Action as a property on the JMS message
setProperty(message, msgContext, BaseConstants.SOAPACTION);
} else {
String action = msgContext.getOptions().getAction();
if (action != null) {
message.setStringProperty(BaseConstants.SOAPACTION, action);
}
}
JMSUtils.setTransportHeaders(msgContext, message);
return message;
}
Aggregations