Search in sources :

Example 1 with OutTransportInfo

use of org.apache.axis2.transport.OutTransportInfo 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 2 with OutTransportInfo

use of org.apache.axis2.transport.OutTransportInfo in project wso2-synapse by wso2.

the class FIXTransportSender method sendUsingTrpOutInfo.

/**
 * Sends a FIX message using the SessionID in the OutTransportInfo
 *
 * @param trpOutInfo  the TransportOutInfo for the message
 * @param fixMessage  the FIX message to be sent
 * @param srcSession  String uniquely identifying the incoming session
 * @param counter     application level sequence number of the message
 * @param serviceName name of the AxisService for the message
 * @param msgCtx      Axis2 MessageContext
 * @return boolean value indicating the result
 * @throws AxisFault on error
 */
private boolean sendUsingTrpOutInfo(OutTransportInfo trpOutInfo, String serviceName, Message fixMessage, String srcSession, int counter, MessageContext msgCtx) throws AxisFault {
    FIXOutTransportInfo fixOut = (FIXOutTransportInfo) trpOutInfo;
    SessionID sessionID = fixOut.getSessionID();
    Map<String, String> fieldValues = FIXUtils.getMessageForwardingParameters(fixMessage);
    String beginString = fieldValues.get(FIXConstants.BEGIN_STRING);
    String deliverToCompID = fieldValues.get(FIXConstants.DELIVER_TO_COMP_ID);
    AxisService service = cfgCtx.getAxisConfiguration().getService(serviceName);
    // match BeginString values
    if (isValidationOn(service) && beginString != null && !beginString.equals(sessionID.getBeginString())) {
        handleException("BeginString validation is on. Cannot forward messages to a session" + " with a different BeginString");
    }
    if (deliverToCompID != null) {
        // message needs to be delivered to some other party
        if (!deliverToCompID.equals(sessionID.getTargetCompID())) {
            handleException("Cannot forward messages that do not have a valid DeliverToCompID field");
        } else {
            prepareToForwardMessage(fixMessage, fieldValues);
            setDeliverToXFields(fixMessage, service);
        }
    } else {
        setDeliverToXFields(fixMessage, fieldValues);
    }
    try {
        messageSender.sendMessage(fixMessage, sessionID, srcSession, counter, msgCtx, null);
        return true;
    } catch (SessionNotFound e) {
        log.error("Error while sending the FIX message. Session " + sessionID.toString() + " does" + " not exist", e);
        return false;
    }
}
Also used : AxisService(org.apache.axis2.description.AxisService)

Example 3 with OutTransportInfo

use of org.apache.axis2.transport.OutTransportInfo in project wso2-synapse by wso2.

the class VFSUtils method getFileName.

public static String getFileName(MessageContext msgCtx, VFSOutTransportInfo vfsOutInfo) {
    String fileName = null;
    // first preference to a custom filename set on the current message context
    Map transportHeaders = (Map) msgCtx.getProperty(MessageContext.TRANSPORT_HEADERS);
    if (transportHeaders != null) {
        fileName = (String) transportHeaders.get(VFSConstants.REPLY_FILE_NAME);
    }
    // if not, does the service (in its service.xml) specify one?
    if (fileName == null) {
        Parameter param = msgCtx.getAxisService().getParameter(VFSConstants.REPLY_FILE_NAME);
        if (param != null) {
            fileName = (String) param.getValue();
        }
    }
    // next check if the OutTransportInfo specifies one
    if (fileName == null) {
        fileName = vfsOutInfo.getOutFileName();
    }
    // if none works.. use default
    if (fileName == null) {
        fileName = VFSConstants.DEFAULT_RESPONSE_FILE;
    }
    return fileName;
}
Also used : Parameter(org.apache.axis2.description.Parameter) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with OutTransportInfo

use of org.apache.axis2.transport.OutTransportInfo in project wso2-synapse by wso2.

the class PassThroughHttpSender method sendUsingOutputStream.

private void sendUsingOutputStream(MessageContext msgContext) throws AxisFault {
    OMOutputFormat format = NhttpUtil.getOMOutputFormat(msgContext);
    MessageFormatter messageFormatter = MessageFormatterDecoratorFactory.createMessageFormatterDecorator(msgContext);
    OutputStream out = (OutputStream) msgContext.getProperty(MessageContext.TRANSPORT_OUT);
    if (msgContext.isServerSide()) {
        OutTransportInfo transportInfo = (OutTransportInfo) msgContext.getProperty(Constants.OUT_TRANSPORT_INFO);
        if (transportInfo != null) {
            transportInfo.setContentType(messageFormatter.getContentType(msgContext, format, msgContext.getSoapAction()));
        } else {
            throw new AxisFault(Constants.OUT_TRANSPORT_INFO + " has not been set");
        }
    }
    try {
        messageFormatter.writeTo(msgContext, format, out, false);
        out.close();
    } catch (IOException e) {
        handleException("IO Error sending response message", e);
    }
}
Also used : AxisFault(org.apache.axis2.AxisFault) OutTransportInfo(org.apache.axis2.transport.OutTransportInfo) OutputStream(java.io.OutputStream) MessageFormatter(org.apache.axis2.transport.MessageFormatter) IOException(java.io.IOException) OMOutputFormat(org.apache.axiom.om.OMOutputFormat)

Example 5 with OutTransportInfo

use of org.apache.axis2.transport.OutTransportInfo in project wso2-axis2-transports by wso2.

the class XMPPSender method sendMessage.

/**
 * Send the given message over XMPP transport
 *
 * @param msgCtx the axis2 message context
 * @throws AxisFault on error
 */
public void sendMessage(MessageContext msgCtx, String targetAddress, OutTransportInfo outTransportInfo) throws AxisFault {
    XMPPConnection xmppConnection = null;
    XMPPOutTransportInfo xmppOutTransportInfo = null;
    XMPPConnectionFactory connectionFactory;
    // if on client side,create connection to xmpp server
    if (msgCtx.isServerSide()) {
        xmppOutTransportInfo = (XMPPOutTransportInfo) msgCtx.getProperty(org.apache.axis2.Constants.OUT_TRANSPORT_INFO);
        connectionFactory = xmppOutTransportInfo.getConnectionFactory();
    } else {
        getConnectionDetailsFromClientOptions(msgCtx);
        connectionFactory = defaultConnectionFactory;
    }
    synchronized (this) {
        xmppConnection = connectionFactory.getXmppConnection();
        if (xmppConnection == null) {
            connectionFactory.connect(serverCredentials);
            xmppConnection = connectionFactory.getXmppConnection();
        }
    }
    Message message = new Message();
    Options options = msgCtx.getOptions();
    String serviceName = XMPPUtils.getServiceName(targetAddress);
    SOAPVersion version = msgCtx.getEnvelope().getVersion();
    if (version instanceof SOAP12Version) {
        message.setProperty(XMPPConstants.CONTENT_TYPE, HTTPConstants.MEDIA_TYPE_APPLICATION_SOAP_XML + "; action=" + msgCtx.getSoapAction());
    } else {
        message.setProperty(XMPPConstants.CONTENT_TYPE, HTTPConstants.MEDIA_TYPE_TEXT_XML);
    }
    if (targetAddress != null) {
        xmppOutTransportInfo = new XMPPOutTransportInfo(targetAddress);
        xmppOutTransportInfo.setConnectionFactory(defaultConnectionFactory);
    } else if (msgCtx.getTo() != null && !msgCtx.getTo().hasAnonymousAddress()) {
    // TODO
    } else if (msgCtx.isServerSide()) {
        xmppOutTransportInfo = (XMPPOutTransportInfo) msgCtx.getProperty(Constants.OUT_TRANSPORT_INFO);
    }
    try {
        if (msgCtx.isServerSide()) {
            message.setProperty(XMPPConstants.IS_SERVER_SIDE, new Boolean(false));
            message.setProperty(XMPPConstants.IN_REPLY_TO, xmppOutTransportInfo.getInReplyTo());
            message.setProperty(XMPPConstants.SEQUENCE_ID, xmppOutTransportInfo.getSequenceID());
        } else {
            // message is going to be processed on server side
            message.setProperty(XMPPConstants.IS_SERVER_SIDE, new Boolean(true));
            // we are sending a soap envelope as a message
            message.setProperty(XMPPConstants.CONTAINS_SOAP_ENVELOPE, new Boolean(true));
            message.setProperty(XMPPConstants.SERVICE_NAME, serviceName);
            String action = options.getAction();
            if (action == null) {
                AxisOperation axisOperation = msgCtx.getAxisOperation();
                if (axisOperation != null) {
                    action = axisOperation.getSoapAction();
                }
            }
            if (action != null) {
                message.setProperty(XMPPConstants.ACTION, action);
            }
        }
        if (xmppConnection == null) {
            handleException("Connection to XMPP Server is not established.");
        }
        // initialize the chat manager using connection
        ChatManager chatManager = xmppConnection.getChatManager();
        Chat chat = chatManager.createChat(xmppOutTransportInfo.getDestinationAccount(), null);
        boolean waitForResponse = msgCtx.getOperationContext() != null && WSDL2Constants.MEP_URI_OUT_IN.equals(msgCtx.getOperationContext().getAxisOperation().getMessageExchangePattern());
        OMElement msgElement;
        String messageToBeSent = "";
        if (XMPPConstants.XMPP_CONTENT_TYPE_STRING.equals(xmppOutTransportInfo.getContentType())) {
            // if request is received from a chat client, whole soap envelope
            // should not be sent.
            OMElement soapBodyEle = msgCtx.getEnvelope().getBody();
            OMElement responseEle = soapBodyEle.getFirstElement();
            if (responseEle != null) {
                msgElement = responseEle.getFirstElement();
            } else {
                msgElement = responseEle;
            }
        } else {
            // if request received from a ws client whole soap envelope
            // must be sent.
            msgElement = msgCtx.getEnvelope();
        }
        messageToBeSent = msgElement.toString();
        message.setBody(messageToBeSent);
        String key = null;
        if (waitForResponse && !msgCtx.isServerSide()) {
            PacketFilter filter = new PacketTypeFilter(message.getClass());
            xmppConnection.addPacketListener(xmppClientSidePacketListener, filter);
            key = UUID.randomUUID().toString();
            xmppClientSidePacketListener.listenForResponse(key, msgCtx);
            message.setProperty(XMPPConstants.SEQUENCE_ID, key);
        }
        chat.sendMessage(message);
        log.debug("Sent message :" + message.toXML());
        // Is this the best way to do this?
        if (waitForResponse && !msgCtx.isServerSide()) {
            xmppClientSidePacketListener.waitFor(key);
            // xmppConnection.disconnect();
            log.debug("Received response sucessfully");
        }
    } catch (XMPPException e) {
        log.error("Error occurred while sending the message : " + message.toXML(), e);
        handleException("Error occurred while sending the message : " + message.toXML(), e);
    } catch (InterruptedException e) {
        log.error("Error occurred while sending the message : " + message.toXML(), e);
        handleException("Error occurred while sending the message : " + message.toXML(), e);
    } finally {
    // if(xmppConnection != null && !msgCtx.isServerSide()){
    // xmppConnection.disconnect();
    // }
    }
}
Also used : Options(org.apache.axis2.client.Options) XMPPConnectionFactory(org.apache.axis2.transport.xmpp.util.XMPPConnectionFactory) PacketFilter(org.jivesoftware.smack.filter.PacketFilter) AxisMessage(org.apache.axis2.description.AxisMessage) Message(org.jivesoftware.smack.packet.Message) AxisOperation(org.apache.axis2.description.AxisOperation) OMElement(org.apache.axiom.om.OMElement) PacketTypeFilter(org.jivesoftware.smack.filter.PacketTypeFilter) XMPPConnection(org.jivesoftware.smack.XMPPConnection) XMPPOutTransportInfo(org.apache.axis2.transport.xmpp.util.XMPPOutTransportInfo) SOAPVersion(org.apache.axiom.soap.SOAPVersion) Chat(org.jivesoftware.smack.Chat) XMPPException(org.jivesoftware.smack.XMPPException) ChatManager(org.jivesoftware.smack.ChatManager) SOAP12Version(org.apache.axiom.soap.SOAP12Version)

Aggregations

OutTransportInfo (org.apache.axis2.transport.OutTransportInfo)6 AxisFault (org.apache.axis2.AxisFault)5 OMOutputFormat (org.apache.axiom.om.OMOutputFormat)4 MessageContext (org.apache.axis2.context.MessageContext)4 MessageFormatter (org.apache.axis2.transport.MessageFormatter)4 VFSOutTransportInfo (org.apache.synapse.commons.vfs.VFSOutTransportInfo)4 IOException (java.io.IOException)3 ConfigurationContext (org.apache.axis2.context.ConfigurationContext)3 Parameter (org.apache.axis2.description.Parameter)3 TransportOutDescription (org.apache.axis2.description.TransportOutDescription)3 AxisConfiguration (org.apache.axis2.engine.AxisConfiguration)3 OutputStream (java.io.OutputStream)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 InterruptedIOException (java.io.InterruptedIOException)1 DatagramPacket (java.net.DatagramPacket)1 DatagramSocket (java.net.DatagramSocket)1 Date (java.util.Date)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 DataHandler (javax.activation.DataHandler)1