Search in sources :

Example 21 with OMOutputFormat

use of org.apache.axiom.om.OMOutputFormat in project wso2-axis2-transports by wso2.

the class PlainTextFormatterTest method testGetBytes.

private void testGetBytes(String encoding) throws Exception {
    MessageContext messageContext = createMessageContext(testString);
    OMOutputFormat format = new OMOutputFormat();
    format.setCharSetEncoding(encoding);
    byte[] bytes = new PlainTextFormatter().getBytes(messageContext, format);
    assertEquals(testString, new String(bytes, encoding));
}
Also used : MessageContext(org.apache.axis2.context.MessageContext) OMOutputFormat(org.apache.axiom.om.OMOutputFormat) PlainTextFormatter(org.apache.axis2.format.PlainTextFormatter)

Example 22 with OMOutputFormat

use of org.apache.axiom.om.OMOutputFormat in project wso2-axis2-transports by wso2.

the class TCPTransportSender method writeMessageOut.

/**
 * Writing the message to the output stream of the TCP socket after applying correct message formatter
 * This method is synchronized because there will be issue when formatter write to same output stream which causes
 * to mixed messages
 *
 * @param msgContext the message context
 * @param outputStream the socket output stream
 * @throws AxisFault if error occurred
 * @throws IOException if IO exception occurred
 */
private synchronized void writeMessageOut(MessageContext msgContext, OutputStream outputStream, String delimiter, String delimiterType) throws AxisFault, IOException {
    MessageFormatter messageFormatter = BaseUtils.getMessageFormatter(msgContext);
    OMOutputFormat format = BaseUtils.getOMOutputFormat(msgContext);
    messageFormatter.writeTo(msgContext, format, outputStream, true);
    if (delimiter != null && !delimiter.isEmpty()) {
        if (TCPConstants.BYTE_DELIMITER_TYPE.equalsIgnoreCase(delimiterType)) {
            outputStream.write((char) Integer.parseInt(delimiter.split("0x")[1], 16));
        } else {
            outputStream.write(delimiter.getBytes());
        }
    }
    outputStream.flush();
}
Also used : MessageFormatter(org.apache.axis2.transport.MessageFormatter) OMOutputFormat(org.apache.axiom.om.OMOutputFormat)

Example 23 with OMOutputFormat

use of org.apache.axiom.om.OMOutputFormat in project wso2-axis2-transports by wso2.

the class UDPSender method sendMessage.

@Override
public void sendMessage(MessageContext msgContext, String targetEPR, OutTransportInfo outTransportInfo) throws AxisFault {
    UDPOutTransportInfo udpOutInfo;
    if ((targetEPR == null) && (outTransportInfo != null)) {
        // this can happen only at the server side and send the message using back chanel
        udpOutInfo = (UDPOutTransportInfo) outTransportInfo;
    } else {
        udpOutInfo = new UDPOutTransportInfo(targetEPR);
    }
    MessageFormatter messageFormatter = MessageProcessorSelector.getMessageFormatter(msgContext);
    OMOutputFormat format = BaseUtils.getOMOutputFormat(msgContext);
    format.setContentType(udpOutInfo.getContentType());
    byte[] payload = messageFormatter.getBytes(msgContext, format);
    try {
        DatagramSocket socket = new DatagramSocket();
        if (log.isDebugEnabled()) {
            log.debug("Sending " + payload.length + " bytes to " + udpOutInfo.getAddress());
        }
        try {
            socket.send(new DatagramPacket(payload, payload.length, udpOutInfo.getAddress()));
            if (!msgContext.getOptions().isUseSeparateListener() && !msgContext.isServerSide()) {
                waitForReply(msgContext, socket, udpOutInfo.getContentType());
            }
        } finally {
            socket.close();
        }
    } catch (IOException ex) {
        throw new AxisFault("Unable to send packet", ex);
    }
}
Also used : AxisFault(org.apache.axis2.AxisFault) DatagramSocket(java.net.DatagramSocket) DatagramPacket(java.net.DatagramPacket) MessageFormatter(org.apache.axis2.transport.MessageFormatter) IOException(java.io.IOException) OMOutputFormat(org.apache.axiom.om.OMOutputFormat)

Example 24 with OMOutputFormat

use of org.apache.axiom.om.OMOutputFormat in project wso2-axis2-transports by wso2.

the class RabbitMQUtils method getMessageBody.

/**
 * Get the message body from the message context
 *
 * @param msgContext the message context
 * @return the message as the byte array
 * @throws IOException
 */
public static byte[] getMessageBody(MessageContext msgContext) throws IOException, AxisRabbitMQException {
    OMOutputFormat format = BaseUtils.getOMOutputFormat(msgContext);
    byte[] messageBody;
    try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        MessageFormatter messageFormatter = MessageProcessorSelector.getMessageFormatter(msgContext);
        messageFormatter.writeTo(msgContext, format, out, false);
        messageBody = out.toByteArray();
    } catch (AxisFault axisFault) {
        throw new AxisRabbitMQException("Unable to get the message formatter to use", axisFault);
    }
    return messageBody;
}
Also used : AxisFault(org.apache.axis2.AxisFault) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MessageFormatter(org.apache.axis2.transport.MessageFormatter) OMOutputFormat(org.apache.axiom.om.OMOutputFormat)

Example 25 with OMOutputFormat

use of org.apache.axiom.om.OMOutputFormat in project wso2-axis2-transports by wso2.

the class MSMQSender method createMSMQMessage.

/**
 * Generating MSMQ message wrapper in order to communicate with JNI
 * @param msgCtx
 * @param contentTypeProperty
 * @param queuName
 * @return
 * @throws AxisFault
 */
private Message createMSMQMessage(MessageContext msgCtx, String messageContentType, String queuName) throws AxisFault {
    String msgBody = null;
    byte[] msgByteBody = null;
    String msgType = getProperty(msgCtx, MSMQConstants.MSMQ_MESSAGE_TYPE);
    // String msgLable = "L:" + queuName+"["+contentType+"]"; // TODO
    // 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 MSMQ but just get the payload in its native format
    String msmqMessageType = guessMessageType(msgCtx);
    if (msmqMessageType == null) {
        OMOutputFormat format = BaseUtils.getOMOutputFormat(msgCtx);
        MessageFormatter messageFormatter = null;
        try {
            messageFormatter = MessageProcessorSelector.getMessageFormatter(msgCtx);
        } catch (AxisFault axisFault) {
            handleException("Unable to get the message formatter to use", axisFault);
        }
        String contentType = messageFormatter != null ? messageFormatter.getContentType(msgCtx, format, msgCtx.getSoapAction()) : "";
        boolean useBytesMessage = msgType != null && MSMQConstants.MSMQ_BYTE_MESSAGE.equals(msgType) || contentType.indexOf(HTTPConstants.HEADER_ACCEPT_MULTIPART_RELATED) > -1;
        OutputStream out = null;
        StringWriter sw = null;
        if (useBytesMessage) {
        // TODO: handle MSMQ byte message here
        } else {
            sw = new StringWriter();
            try {
                out = new WriterOutputStream(sw, format.getCharSetEncoding());
            } catch (UnsupportedCharsetException ex) {
                handleException("Unsupported encoding " + format.getCharSetEncoding(), ex);
            }
        }
        try {
            if (out != null) {
                messageFormatter.writeTo(msgCtx, format, out, true);
                out.close();
            }
        } catch (IOException e) {
            handleException("IO Error while creating BytesMessage", e);
        }
        if (!useBytesMessage) {
            msgBody = sw.toString();
        }
    } else if (MSMQConstants.MSMQ_BYTE_MESSAGE.equals(msmqMessageType)) {
    // TODO. handle .net byte messages here.
    } else if (MSMQConstants.MSMQ_TEXT_MESSAGE.equals(msmqMessageType)) {
        msgBody = msgCtx.getEnvelope().getBody().getFirstChildWithName(BaseConstants.DEFAULT_TEXT_WRAPPER).getText();
    }
    try {
        // Keep message correlation empty will be deciding later on the process
        Message message = new Message(msgBody != null ? msgBody : "", "", "");
        return message;
    } catch (UnsupportedEncodingException e) {
        log.error("Unsported message has been received: ", e);
        handleEexception("Unsported message has been received: ", e);
    }
    return null;
}
Also used : AxisFault(org.apache.axis2.AxisFault) Message(org.apache.axis2.transport.msmq.util.Message) OutputStream(java.io.OutputStream) WriterOutputStream(org.apache.commons.io.output.WriterOutputStream) UnsupportedEncodingException(java.io.UnsupportedEncodingException) MessageFormatter(org.apache.axis2.transport.MessageFormatter) IOException(java.io.IOException) WriterOutputStream(org.apache.commons.io.output.WriterOutputStream) StringWriter(java.io.StringWriter) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) OMOutputFormat(org.apache.axiom.om.OMOutputFormat)

Aggregations

OMOutputFormat (org.apache.axiom.om.OMOutputFormat)64 MessageFormatter (org.apache.axis2.transport.MessageFormatter)24 ByteArrayOutputStream (java.io.ByteArrayOutputStream)21 OutputStream (java.io.OutputStream)18 IOException (java.io.IOException)16 AxisFault (org.apache.axis2.AxisFault)13 MessageContext (org.apache.axis2.context.MessageContext)12 OMElement (org.apache.axiom.om.OMElement)11 DataHandler (javax.activation.DataHandler)7 ByteArrayInputStream (java.io.ByteArrayInputStream)6 InputStream (java.io.InputStream)6 StringWriter (java.io.StringWriter)6 XMLStreamException (javax.xml.stream.XMLStreamException)6 SOAPEnvelope (org.apache.axiom.soap.SOAPEnvelope)6 UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)5 OMText (org.apache.axiom.om.OMText)5 Map (java.util.Map)4 MultipartBody (org.apache.axiom.mime.MultipartBody)4 SOAPFactory (org.apache.axiom.soap.SOAPFactory)4 Collection (java.util.Collection)3