use of org.apache.axis2.builder.Builder in project wso2-axis2-transports by wso2.
the class RabbitMQMessageSender method publish.
/**
* Perform the creation of exchange/queue and the Outputstream
*
* @param message the RabbitMQ AMQP message
* @param msgContext the Axis2 MessageContext
*/
public void publish(RabbitMQMessage message, MessageContext msgContext) throws AxisRabbitMQException, IOException {
String exchangeName = null;
AMQP.BasicProperties basicProperties = null;
byte[] messageBody = null;
if (rmqChannel.isOpen()) {
String queueName = properties.get(RabbitMQConstants.QUEUE_NAME);
String routeKey = properties.get(RabbitMQConstants.QUEUE_ROUTING_KEY);
exchangeName = properties.get(RabbitMQConstants.EXCHANGE_NAME);
String exchangeType = properties.get(RabbitMQConstants.EXCHANGE_TYPE);
String replyTo = properties.get(RabbitMQConstants.REPLY_TO_NAME);
String correlationID = properties.get(RabbitMQConstants.CORRELATION_ID);
String queueAutoDeclareStr = properties.get(RabbitMQConstants.QUEUE_AUTODECLARE);
String exchangeAutoDeclareStr = properties.get(RabbitMQConstants.EXCHANGE_AUTODECLARE);
boolean queueAutoDeclare = true;
boolean exchangeAutoDeclare = true;
if (!StringUtils.isEmpty(queueAutoDeclareStr)) {
queueAutoDeclare = Boolean.parseBoolean(queueAutoDeclareStr);
}
if (!StringUtils.isEmpty(exchangeAutoDeclareStr)) {
exchangeAutoDeclare = Boolean.parseBoolean(exchangeAutoDeclareStr);
}
message.setReplyTo(replyTo);
if (StringUtils.isEmpty(correlationID)) {
// if reply-to is enabled a correlationID must be available. If not specified, use messageID
correlationID = message.getMessageId();
}
if (!StringUtils.isEmpty(correlationID)) {
message.setCorrelationId(correlationID);
}
if (queueName == null || queueName.equals("")) {
log.debug("No queue name is specified");
}
if (routeKey == null && !"x-consistent-hash".equals(exchangeType)) {
if (queueName == null || queueName.equals("")) {
log.debug("Routing key is not specified");
} else {
log.debug("Routing key is not specified. Using queue name as the routing key.");
routeKey = queueName;
}
}
// read publish properties corr id and route key from message context
Object prRouteKey = msgContext.getProperty(RabbitMQConstants.QUEUE_ROUTING_KEY);
Object prCorrId = msgContext.getProperty(RabbitMQConstants.CORRELATION_ID).toString();
if (prRouteKey != null) {
routeKey = prRouteKey.toString();
log.debug("Specifying routing key from axis2 properties");
}
if (prCorrId != null) {
message.setCorrelationId(prCorrId.toString());
log.debug("Specifying correlation id from axis2 properties");
}
// Declaring the queue
if (queueAutoDeclare && queueName != null && !queueName.equals("")) {
RabbitMQUtils.declareQueue(rmqChannel, queueName, properties);
}
// Declaring the exchange
if (exchangeAutoDeclare && exchangeName != null && !exchangeName.equals("")) {
RabbitMQUtils.declareExchange(rmqChannel, exchangeName, properties);
if (queueName != null && !"x-consistent-hash".equals(exchangeType)) {
// Create bind between the queue and exchange with the routeKey
try {
rmqChannel.getChannel().queueBind(queueName, exchangeName, routeKey);
} catch (IOException e) {
handleException("Error occurred while creating the bind between the queue: " + queueName + " & exchange: " + exchangeName + " with route-key " + routeKey, e);
}
}
}
AMQP.BasicProperties.Builder builder = buildBasicProperties(message);
String deliveryModeString = properties.get(RabbitMQConstants.QUEUE_DELIVERY_MODE);
int deliveryMode = RabbitMQConstants.DEFAULT_DELIVERY_MODE;
if (deliveryModeString != null) {
deliveryMode = Integer.parseInt(deliveryModeString);
}
builder.deliveryMode(deliveryMode);
if (!StringUtils.isEmpty(replyTo)) {
builder.replyTo(replyTo);
}
basicProperties = builder.build();
OMOutputFormat format = BaseUtils.getOMOutputFormat(msgContext);
MessageFormatter messageFormatter = null;
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
messageFormatter = MessageProcessorSelector.getMessageFormatter(msgContext);
} catch (AxisFault axisFault) {
throw new AxisRabbitMQException("Unable to get the message formatter to use", axisFault);
}
// given. Queue/exchange creation, bindings should be done at the broker
try {
// x-consistent-hash type
if (exchangeType != null && exchangeType.equals("x-consistent-hash")) {
routeKey = UUID.randomUUID().toString();
}
} catch (UnsupportedCharsetException ex) {
handleException("Unsupported encoding " + format.getCharSetEncoding(), ex);
}
try {
messageFormatter.writeTo(msgContext, format, out, false);
messageBody = out.toByteArray();
} catch (IOException e) {
handleException("IO Error while creating BytesMessage", e);
} finally {
if (out != null) {
out.close();
}
}
try {
/**
* Check for parameter on confirmed delivery in RabbitMq to ensure reliable delivery.
* Can be enabled by setting the parameter "rabbitmq.confirm.delivery" to "true" in broker URL.
*/
boolean isConfirmedDeliveryEnabled = Boolean.parseBoolean(properties.get(RabbitMQConstants.CONFIRMED_DELIVERY));
if (isConfirmedDeliveryEnabled) {
rmqChannel.getChannel().confirmSelect();
}
if (exchangeName != null && exchangeName != "") {
rmqChannel.getChannel().basicPublish(exchangeName, routeKey, basicProperties, messageBody);
} else {
rmqChannel.getChannel().basicPublish("", routeKey, basicProperties, messageBody);
}
if (isConfirmedDeliveryEnabled) {
rmqChannel.getChannel().waitForConfirmsOrDie();
}
} catch (IOException e) {
handleException("Error while publishing the message", e);
} catch (InterruptedException e) {
handleException("InterruptedException while waiting for AMQP message confirm :", e);
}
} else {
handleException("Channel cannot be created");
}
}
use of org.apache.axis2.builder.Builder in project wso2-axis2-transports by wso2.
the class RabbitMQRPCMessageSender method publish.
/**
* Perform the creation of exchange/queue and the Outputstream
*
* @param message the RabbitMQ AMQP message
* @param msgContext the Axis2 MessageContext
*/
private void publish(RabbitMQMessage message, MessageContext msgContext) throws AxisRabbitMQException, IOException {
String exchangeName = null;
AMQP.BasicProperties basicProperties = null;
byte[] messageBody = null;
if (dualChannel.isOpen()) {
String queueName = epProperties.get(RabbitMQConstants.QUEUE_NAME);
String routeKey = epProperties.get(RabbitMQConstants.QUEUE_ROUTING_KEY);
exchangeName = epProperties.get(RabbitMQConstants.EXCHANGE_NAME);
String exchangeType = epProperties.get(RabbitMQConstants.EXCHANGE_TYPE);
String correlationID = epProperties.get(RabbitMQConstants.CORRELATION_ID);
String replyTo = dualChannel.getReplyToQueue();
String queueAutoDeclareStr = epProperties.get(RabbitMQConstants.QUEUE_AUTODECLARE);
String exchangeAutoDeclareStr = epProperties.get(RabbitMQConstants.EXCHANGE_AUTODECLARE);
boolean queueAutoDeclare = true;
boolean exchangeAutoDeclare = true;
if (!StringUtils.isEmpty(queueAutoDeclareStr)) {
queueAutoDeclare = Boolean.parseBoolean(queueAutoDeclareStr);
}
if (!StringUtils.isEmpty(exchangeAutoDeclareStr)) {
exchangeAutoDeclare = Boolean.parseBoolean(exchangeAutoDeclareStr);
}
message.setReplyTo(replyTo);
if ((!StringUtils.isEmpty(replyTo)) && (StringUtils.isEmpty(correlationID))) {
// if reply-to is enabled a correlationID must be available. If not specified, use messageID
correlationID = message.getMessageId();
}
if (!StringUtils.isEmpty(correlationID)) {
message.setCorrelationId(correlationID);
}
if (queueName == null || queueName.equals("")) {
log.info("No queue name is specified");
}
if (routeKey == null && !"x-consistent-hash".equals(exchangeType)) {
if (queueName == null || queueName.equals("")) {
log.info("Routing key is not specified");
} else {
log.info("Routing key is not specified. Using queue name as the routing key.");
routeKey = queueName;
}
}
// Declaring the queue
if (queueAutoDeclare && queueName != null && !queueName.equals("")) {
// get channel with dualChannel.getChannel() since it will create a new channel if channel is closed
RabbitMQUtils.declareQueue(dualChannel, queueName, epProperties);
}
// Declaring the exchange
if (exchangeAutoDeclare && exchangeName != null && !exchangeName.equals("")) {
RabbitMQUtils.declareExchange(dualChannel, exchangeName, epProperties);
if (queueName != null && !"x-consistent-hash".equals(exchangeType)) {
// Create bind between the queue and exchange with the routeKey
try {
dualChannel.getChannel().queueBind(queueName, exchangeName, routeKey);
} catch (IOException e) {
handleException("Error occurred while creating the bind between the queue: " + queueName + " & exchange: " + exchangeName + " with route-key " + routeKey, e);
}
}
}
// build basic properties from message
AMQP.BasicProperties.Builder builder = buildBasicProperties(message);
String deliveryModeString = epProperties.get(RabbitMQConstants.QUEUE_DELIVERY_MODE);
int deliveryMode = RabbitMQConstants.DEFAULT_DELIVERY_MODE;
if (deliveryModeString != null) {
deliveryMode = Integer.parseInt(deliveryModeString);
}
// TODO : override properties from message with ones from transport properties
// set builder properties from transport properties (overrides current properties)
builder.deliveryMode(deliveryMode);
builder.replyTo(replyTo);
basicProperties = builder.build();
OMOutputFormat format = BaseUtils.getOMOutputFormat(msgContext);
MessageFormatter messageFormatter = null;
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
messageFormatter = MessageProcessorSelector.getMessageFormatter(msgContext);
} catch (AxisFault axisFault) {
throw new AxisRabbitMQException("Unable to get the message formatter to use", axisFault);
}
// given. Queue/exchange creation, bindings should be done at the broker
try {
// x-consistent-hash type
if (exchangeType != null && exchangeType.equals("x-consistent-hash")) {
routeKey = UUID.randomUUID().toString();
}
} catch (UnsupportedCharsetException ex) {
handleException("Unsupported encoding " + format.getCharSetEncoding(), ex);
}
try {
messageFormatter.writeTo(msgContext, format, out, false);
messageBody = out.toByteArray();
} catch (IOException e) {
handleException("IO Error while creating BytesMessage", e);
} finally {
if (out != null) {
out.close();
}
}
try {
if (exchangeName != null && exchangeName != "") {
if (log.isDebugEnabled()) {
log.debug("Publishing message to exchange " + exchangeName + " with route key " + routeKey);
}
dualChannel.getChannel().basicPublish(exchangeName, routeKey, basicProperties, messageBody);
} else {
if (log.isDebugEnabled()) {
log.debug("Publishing message with route key " + routeKey);
}
dualChannel.getChannel().basicPublish("", routeKey, basicProperties, messageBody);
}
} catch (IOException e) {
handleException("Error while publishing the message", e);
}
} else {
handleException("Channel cannot be created");
}
}
use of org.apache.axis2.builder.Builder in project pentaho-platform by pentaho.
the class AbstractAxisConfigurator method getAxisConfiguration.
/**
* Creates the AxisConfiguration object using an XML document. Subclasses of this class must provide the XML via an
* input stream. The concrete implementation can store the XML file wherever it wants as we only need an InputStream
*/
public AxisConfiguration getAxisConfiguration() throws AxisFault {
if (axisConfig != null) {
// we have already initialized
return axisConfig;
}
try {
// create a new AxisConfiguration
axisConfig = new AxisConfiguration();
// get the config XML input stream
InputStream in = getConfigXml();
// build the configuration
AxisConfigBuilder builder = new AxisConfigBuilder(in, axisConfig, null);
builder.populateConfig();
} catch (Exception e) {
e.printStackTrace();
throw AxisFault.makeFault(e);
}
// set this object as the Axis configurator. Axis will call loadServices().
axisConfig.setConfigurator(this);
return axisConfig;
}
use of org.apache.axis2.builder.Builder in project wso2-synapse by wso2.
the class VFSTransportListener method processFile.
/**
* Process a single file through Axis2
* @param entry the PollTableEntry for the file (or its parent directory or archive)
* @param file the file that contains the actual message pumped into Axis2
* @throws AxisFault on error
*/
private void processFile(PollTableEntry entry, FileObject file) throws AxisFault {
try {
FileContent content = file.getContent();
String fileName = file.getName().getBaseName();
String filePath = file.getName().getPath();
String fileURI = file.getName().getURI();
metrics.incrementBytesReceived(content.getSize());
Map<String, Object> transportHeaders = new HashMap<String, Object>();
transportHeaders.put(VFSConstants.FILE_PATH, filePath);
transportHeaders.put(VFSConstants.FILE_NAME, fileName);
transportHeaders.put(VFSConstants.FILE_URI, fileURI);
try {
transportHeaders.put(VFSConstants.FILE_LENGTH, content.getSize());
transportHeaders.put(VFSConstants.LAST_MODIFIED, content.getLastModifiedTime());
} catch (FileSystemException ignore) {
}
MessageContext msgContext = entry.createMessageContext();
String contentType = entry.getContentType();
if (BaseUtils.isBlank(contentType)) {
if (file.getName().getExtension().toLowerCase().endsWith(".xml")) {
contentType = "text/xml";
} else if (file.getName().getExtension().toLowerCase().endsWith(".txt")) {
contentType = "text/plain";
}
} else {
// Extract the charset encoding from the configured 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 the content type was not found, but the service defined it.. use it
if (contentType == null) {
if (entry.getContentType() != null) {
contentType = entry.getContentType();
} else if (VFSUtils.getProperty(content, BaseConstants.CONTENT_TYPE) != null) {
contentType = VFSUtils.getProperty(content, BaseConstants.CONTENT_TYPE);
}
}
// does the service specify a default reply file URI ?
String replyFileURI = entry.getReplyFileURI();
if (replyFileURI != null) {
msgContext.setProperty(Constants.OUT_TRANSPORT_INFO, new VFSOutTransportInfo(replyFileURI, entry.isFileLockingEnabled()));
}
// Determine the message builder to use
Builder builder;
if (contentType == null) {
if (log.isDebugEnabled()) {
log.debug("No content type specified. Using SOAP builder.");
}
builder = new SOAPBuilder();
} else {
int index = contentType.indexOf(';');
String type = index > 0 ? contentType.substring(0, index) : contentType;
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();
}
}
// set the message payload to the message context
InputStream in;
ManagedDataSource dataSource;
if (builder instanceof DataSourceMessageBuilder && entry.isStreaming()) {
in = null;
dataSource = ManagedDataSourceFactory.create(new FileObjectDataSource(file, contentType));
} else {
in = new AutoCloseInputStream(content.getInputStream());
dataSource = null;
}
try {
OMElement documentElement;
if (in != null) {
documentElement = builder.processDocument(in, contentType, msgContext);
} else {
documentElement = ((DataSourceMessageBuilder) builder).processDocument(dataSource, contentType, msgContext);
}
msgContext.setEnvelope(TransportUtils.createSOAPEnvelope(documentElement));
handleIncomingMessage(msgContext, transportHeaders, // * SOAP Action - not applicable *//
null, contentType);
} finally {
if (dataSource != null) {
dataSource.destroy();
}
}
if (log.isDebugEnabled()) {
log.debug("Processed file : " + VFSUtils.maskURLPassword(file.toString()) + " of Content-type : " + contentType);
}
} catch (FileSystemException e) {
handleException("Error reading file content or attributes : " + VFSUtils.maskURLPassword(file.toString()), e);
} finally {
try {
if (file != null) {
if (fsManager != null && file.getName() != null && file.getName().getScheme() != null && file.getName().getScheme().startsWith("file")) {
fsManager.closeFileSystem(file.getParent().getFileSystem());
}
file.close();
}
} catch (FileSystemException warn) {
// ignore the warning, since we handed over the stream close job to
// AutocloseInputstream..
}
}
}
use of org.apache.axis2.builder.Builder in project wso2-synapse by wso2.
the class BinaryRelayBuilderTest method testProcessDocument.
/**
* This method tests whether it is possible to create OMElement from the input stream
* @throws Exception
*/
@Test
public void testProcessDocument() throws Exception {
InputStream in = new ByteArrayInputStream("testString".getBytes());
String contentType = "application/xml";
MessageContext messageContext = new MessageContext();
BinaryRelayBuilder builder = new BinaryRelayBuilder();
OMElement response = builder.processDocument(in, contentType, messageContext);
String expected = "<?xml version='1.0' encoding='utf-8'?>" + "<soapenv:Envelope xmlns:soapenv=\"http://www.w3.org/2003/05/soap-envelope\">" + "<soapenv:Body><ns:binary xmlns:ns=\"http://ws.apache.org/commons/ns/payload\">dGVzdFN0cmluZw==</ns:binary>" + "</soapenv:Body></soapenv:Envelope>";
Assert.assertEquals("Couldn't create OMElement!", expected, response.toString());
}
Aggregations