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));
}
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();
}
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);
}
}
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;
}
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;
}
Aggregations