use of org.apache.axis2.transport.jms.iowrappers.BytesMessageInputStream 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.BytesMessageInputStream 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";
} 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 {
handleException("Unsupported JMS message type " + message.getClass().getName());
// Make compiler happy
return;
}
msgContext.setEnvelope(TransportUtils.createSOAPEnvelope(documentElement));
}
use of org.apache.axis2.transport.jms.iowrappers.BytesMessageInputStream in project wso2-axis2-transports by wso2.
the class LogAspect method beforeSend.
@Before("(call(void javax.jms.MessageProducer.send(javax.jms.Message)) ||" + " call(void javax.jms.TopicPublisher.publish(javax.jms.Message))) && args(message)")
public void beforeSend(Message message) {
try {
OutputStream out = LogManager.INSTANCE.createLog("jms");
try {
PrintWriter pw = new PrintWriter(new OutputStreamWriter(out), false);
pw.println("Type: " + message.getClass().getName());
pw.println("JMS message ID: " + message.getJMSMessageID());
pw.println("JMS correlation ID: " + message.getJMSCorrelationID());
pw.println("JMS reply to: " + message.getJMSReplyTo());
for (Enumeration<?> e = message.getPropertyNames(); e.hasMoreElements(); ) {
String name = (String) e.nextElement();
pw.print(name);
pw.print(": ");
pw.println(message.getStringProperty(name));
}
pw.println();
pw.flush();
if (message instanceof BytesMessage) {
BytesMessage bytesMessage = (BytesMessage) message;
bytesMessage.reset();
IOUtils.copy(new BytesMessageInputStream(bytesMessage), out);
} else if (message instanceof TextMessage) {
pw.print(((TextMessage) message).getText());
pw.flush();
}
} finally {
out.close();
}
} catch (Throwable ex) {
log.error("Failed to dump JMS message", ex);
}
}
Aggregations