use of org.apache.axiom.om.OMOutputFormat 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.axiom.om.OMOutputFormat in project wso2-synapse by wso2.
the class PassThroughHttpSender method sendRequestContent.
private void sendRequestContent(final MessageContext msgContext) throws AxisFault {
if (Boolean.TRUE.equals(msgContext.getProperty(PassThroughConstants.MESSAGE_BUILDER_INVOKED))) {
synchronized (msgContext) {
while (!Boolean.TRUE.equals(msgContext.getProperty(PassThroughConstants.WAIT_BUILDER_IN_STREAM_COMPLETE)) && !Boolean.TRUE.equals(msgContext.getProperty("PASSTHRU_CONNECT_ERROR"))) {
try {
msgContext.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
if (Boolean.TRUE.equals(msgContext.getProperty("PASSTHRU_CONNECT_ERROR"))) {
return;
}
OutputStream out = (OutputStream) msgContext.getProperty(PassThroughConstants.BUILDER_OUTPUT_STREAM);
if (out != null) {
String disableChunking = (String) msgContext.getProperty(PassThroughConstants.DISABLE_CHUNKING);
String forceHttp10 = (String) msgContext.getProperty(PassThroughConstants.FORCE_HTTP_1_0);
Pipe pipe = (Pipe) msgContext.getProperty(PassThroughConstants.PASS_THROUGH_PIPE);
if ("true".equals(disableChunking) || "true".equals(forceHttp10)) {
MessageFormatter formatter = MessageFormatterDecoratorFactory.createMessageFormatterDecorator(msgContext);
OMOutputFormat format = PassThroughTransportUtils.getOMOutputFormat(msgContext);
long messageSize = 0;
try {
OverflowBlob overflowBlob = setStreamAsTempData(formatter, msgContext, format);
messageSize = overflowBlob.getLength();
msgContext.setProperty(PassThroughConstants.PASSTROUGH_MESSAGE_LENGTH, messageSize);
overflowBlob.writeTo(out);
} catch (IOException e) {
// TODO Auto-generated catch block
handleException("IO while building message", e);
}
// to ignore any entity enclosed methods available.
if (HTTPConstants.HTTP_METHOD_GET.equals(msgContext.getProperty(Constants.Configuration.HTTP_METHOD)) || RelayUtils.isDeleteRequestWithoutPayload(msgContext)) {
pipe.setSerializationCompleteWithoutData(true);
} else if (messageSize == 0 && (msgContext.getProperty(PassThroughConstants.FORCE_POST_PUT_NOBODY) != null && (Boolean) msgContext.getProperty(PassThroughConstants.FORCE_POST_PUT_NOBODY))) {
pipe.setSerializationCompleteWithoutData(true);
} else {
pipe.setSerializationComplete(true);
}
} else {
// to ignore any entity enclosed methods available.
if (HTTPConstants.HTTP_METHOD_GET.equals(msgContext.getProperty(Constants.Configuration.HTTP_METHOD)) || RelayUtils.isDeleteRequestWithoutPayload(msgContext)) {
pipe.setSerializationCompleteWithoutData(true);
return;
}
if ((msgContext.getProperty(PassThroughConstants.FORCE_POST_PUT_NOBODY) != null && (Boolean) msgContext.getProperty(PassThroughConstants.FORCE_POST_PUT_NOBODY))) {
pipe.setSerializationCompleteWithoutData(true);
return;
}
if ((disableChunking == null || !"true".equals(disableChunking)) || (forceHttp10 == null || !"true".equals(forceHttp10))) {
MessageFormatter formatter = MessageFormatterDecoratorFactory.createMessageFormatterDecorator(msgContext);
OMOutputFormat format = PassThroughTransportUtils.getOMOutputFormat(msgContext);
formatter.writeTo(msgContext, format, out, false);
}
if ((msgContext.getProperty(PassThroughConstants.REST_GET_DELETE_INVOKE) != null && (Boolean) msgContext.getProperty(PassThroughConstants.REST_GET_DELETE_INVOKE))) {
pipe.setSerializationCompleteWithoutData(true);
} else {
pipe.setSerializationComplete(true);
}
}
}
}
}
use of org.apache.axiom.om.OMOutputFormat in project webservices-axiom by apache.
the class ParserInputStreamDataSource method getXMLBytes.
@Override
public byte[] getXMLBytes(String encoding) {
if (log.isDebugEnabled()) {
log.debug("Entry ParserInputStreamDataSource.getXMLBytes(encoding)");
}
try {
InputStream is = data.readParserInputStream();
if (is != null) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
OMOutputFormat format = new OMOutputFormat();
format.setCharSetEncoding(encoding);
try {
BufferUtils.inputStream2OutputStream(is, baos);
if (log.isDebugEnabled()) {
log.debug("Exit ParserInputStreamDataSource.getXMLBytes(encoding)");
}
return baos.toByteArray();
} catch (IOException e) {
throw new OMException(e);
}
} else {
// via SerializeAndConsume call
if (log.isDebugEnabled()) {
log.warn("Parser was already read, recovering by just returning new byte[0]");
log.debug("Exit ParserInputStreamDataSource.getXMLBytes(encoding)");
}
return new byte[0];
}
} catch (XMLStreamException e) {
throw new OMException(e);
}
}
use of org.apache.axiom.om.OMOutputFormat in project webservices-axiom by apache.
the class MIMEOutputUtils method writeMM7Message.
/**
* @deprecated Axiom only supports standard SwA messages. However, {@link OMMultipartWriter}
* provides a flexible way to build MIME packages for non standard formats such as
* MM7.
*/
public static void writeMM7Message(StringWriter writer, OutputStream outputStream, Attachments attachments, OMOutputFormat format, String innerPartCID, String innerBoundary) {
try {
OMMultipartWriter mpw = new OMMultipartWriter(outputStream, format);
Writer rootPartWriter = new OutputStreamWriter(mpw.writeRootPart(), format.getCharSetEncoding());
rootPartWriter.write(writer.toString());
rootPartWriter.close();
if (attachments.getContentIDSet().size() != 0) {
OMOutputFormat innerFormat = new OMOutputFormat(format);
innerFormat.setMimeBoundary(innerBoundary);
OutputStream innerOutputStream = mpw.writePart("multipart/related; boundary=\"" + innerBoundary + "\"", innerPartCID);
OMMultipartWriter innerMpw = new OMMultipartWriter(innerOutputStream, innerFormat);
Collection ids = Arrays.asList(attachments.getAllContentIDs());
for (Iterator it = ids.iterator(); it.hasNext(); ) {
String id = (String) it.next();
innerMpw.writePart(attachments.getDataHandler(id), id);
}
innerMpw.complete();
innerOutputStream.close();
}
mpw.complete();
} catch (IOException e) {
throw new OMException("Error while writing to the OutputStream.", e);
}
}
use of org.apache.axiom.om.OMOutputFormat in project webservices-axiom by apache.
the class CommonUtils method logDebug.
/**
* Writes the om to a log.debug.
* This method assumes optimized mtom attachments
* Also calculates the length of the message.
* @param om OMElement
* @param log Log
* @param limit limit of message to write
* @return length of entire message
*/
public static long logDebug(OMElement om, Log log, int limit) {
OMOutputFormat format = new OMOutputFormat();
format.setDoOptimize(true);
format.setIgnoreXMLDeclaration(true);
return logDebug(om, log, limit, format);
}
Aggregations