Search in sources :

Example 6 with OutTransportInfo

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

the class MailTransportSender method sendMail.

/**
 * Populate email with a SOAP formatted message
 * @param outInfo the out transport information holder
 * @param msgContext the message context that holds the message to be written
 * @throws AxisFault on error
 * @return id of the send mail message
 */
private String sendMail(MailOutTransportInfo outInfo, MessageContext msgContext) throws AxisFault, MessagingException, IOException {
    OMOutputFormat format = BaseUtils.getOMOutputFormat(msgContext);
    // Make sure that non textual attachements are sent with base64 transfer encoding
    // instead of binary.
    format.setProperty(OMOutputFormat.USE_CTE_BASE64_FOR_NON_TEXTUAL_ATTACHMENTS, true);
    MessageFormatter messageFormatter = BaseUtils.getMessageFormatter(msgContext);
    if (log.isDebugEnabled()) {
        log.debug("Creating MIME message using message formatter " + messageFormatter.getClass().getSimpleName());
    }
    WSMimeMessage message = null;
    if (outInfo.getFromAddress() != null) {
        message = new WSMimeMessage(session, outInfo.getFromAddress().getAddress());
    } else {
        message = new WSMimeMessage(session, "");
    }
    Map trpHeaders = (Map) msgContext.getProperty(MessageContext.TRANSPORT_HEADERS);
    if (log.isDebugEnabled() && trpHeaders != null) {
        log.debug("Using transport headers: " + trpHeaders);
    }
    // to the transport senders default From address
    if (outInfo.getTargetAddresses() != null && outInfo.getFromAddress() != null) {
        if (log.isDebugEnabled()) {
            log.debug("Setting From header to " + outInfo.getFromAddress().getAddress() + " from OutTransportInfo");
        }
        message.setFrom(outInfo.getFromAddress());
        message.setReplyTo((new Address[] { outInfo.getFromAddress() }));
    } else if (trpHeaders != null && trpHeaders.containsKey(MailConstants.MAIL_HEADER_FROM)) {
        InternetAddress from = new InternetAddress((String) trpHeaders.get(MailConstants.MAIL_HEADER_FROM));
        if (log.isDebugEnabled()) {
            log.debug("Setting From header to " + from.getAddress() + " from transport headers");
        }
        message.setFrom(from);
        message.setReplyTo(new Address[] { from });
    } else {
        if (smtpFromAddress != null) {
            if (log.isDebugEnabled()) {
                log.debug("Setting From header to " + smtpFromAddress.getAddress() + " from transport configuration");
            }
            message.setFrom(smtpFromAddress);
            message.setReplyTo(new Address[] { smtpFromAddress });
        } else {
            handleException("From address for outgoing message cannot be determined");
        }
    }
    // address from the out transport information
    if (trpHeaders != null && trpHeaders.containsKey(MailConstants.MAIL_HEADER_TO)) {
        Address[] to = InternetAddress.parse((String) trpHeaders.get(MailConstants.MAIL_HEADER_TO));
        if (log.isDebugEnabled()) {
            log.debug("Setting To header to " + InternetAddress.toString(to) + " from transport headers");
        }
        message.setRecipients(Message.RecipientType.TO, to);
    } else if (outInfo.getTargetAddresses() != null) {
        if (log.isDebugEnabled()) {
            log.debug("Setting To header to " + InternetAddress.toString(outInfo.getTargetAddresses()) + " from OutTransportInfo");
        }
        message.setRecipients(Message.RecipientType.TO, outInfo.getTargetAddresses());
    } else {
        handleException("To address for outgoing message cannot be determined");
    }
    // Cc list from original request message
    if (trpHeaders != null && trpHeaders.containsKey(MailConstants.MAIL_HEADER_CC)) {
        Address[] cc = InternetAddress.parse((String) trpHeaders.get(MailConstants.MAIL_HEADER_CC));
        if (log.isDebugEnabled()) {
            log.debug("Setting Cc header to " + InternetAddress.toString(cc) + " from transport headers");
        }
        message.setRecipients(Message.RecipientType.CC, cc);
    } else if (outInfo.getCcAddresses() != null) {
        if (log.isDebugEnabled()) {
            log.debug("Setting Cc header to " + InternetAddress.toString(outInfo.getCcAddresses()) + " from OutTransportInfo");
        }
        message.setRecipients(Message.RecipientType.CC, outInfo.getCcAddresses());
    }
    // custom transport header
    if (trpHeaders != null && trpHeaders.containsKey(MailConstants.MAIL_HEADER_BCC)) {
        InternetAddress[] bcc = InternetAddress.parse((String) trpHeaders.get(MailConstants.MAIL_HEADER_BCC));
        if (log.isDebugEnabled()) {
            log.debug("Adding Bcc header values " + InternetAddress.toString(bcc) + " from transport headers");
        }
        message.addRecipients(Message.RecipientType.BCC, bcc);
    }
    if (smtpBccAddresses != null) {
        if (log.isDebugEnabled()) {
            log.debug("Adding Bcc header values " + InternetAddress.toString(smtpBccAddresses) + " from transport configuration");
        }
        message.addRecipients(Message.RecipientType.BCC, smtpBccAddresses);
    }
    // set subject
    if (trpHeaders != null && trpHeaders.containsKey(MailConstants.MAIL_HEADER_SUBJECT)) {
        if (log.isDebugEnabled()) {
            log.debug("Setting Subject header to '" + trpHeaders.get(MailConstants.MAIL_HEADER_SUBJECT) + "' from transport headers");
        }
        message.setSubject((String) trpHeaders.get(MailConstants.MAIL_HEADER_SUBJECT));
    } else if (outInfo.getSubject() != null) {
        if (log.isDebugEnabled()) {
            log.debug("Setting Subject header to '" + outInfo.getSubject() + "' from transport headers");
        }
        message.setSubject(outInfo.getSubject());
    } else {
        if (log.isDebugEnabled()) {
            log.debug("Generating default Subject header from SOAP action");
        }
        message.setSubject(BaseConstants.SOAPACTION + ": " + msgContext.getSoapAction());
    }
    // if this is a reply, set reference to original message
    if (outInfo.getRequestMessageID() != null) {
        message.setHeader(MailConstants.MAIL_HEADER_IN_REPLY_TO, outInfo.getRequestMessageID());
        message.setHeader(MailConstants.MAIL_HEADER_REFERENCES, outInfo.getRequestMessageID());
    } else {
        if (trpHeaders != null && trpHeaders.containsKey(MailConstants.MAIL_HEADER_IN_REPLY_TO)) {
            message.setHeader(MailConstants.MAIL_HEADER_IN_REPLY_TO, (String) trpHeaders.get(MailConstants.MAIL_HEADER_IN_REPLY_TO));
        }
        if (trpHeaders != null && trpHeaders.containsKey(MailConstants.MAIL_HEADER_REFERENCES)) {
            message.setHeader(MailConstants.MAIL_HEADER_REFERENCES, (String) trpHeaders.get(MailConstants.MAIL_HEADER_REFERENCES));
        }
    }
    // set Date
    message.setSentDate(new Date());
    // set SOAPAction header
    message.setHeader(BaseConstants.SOAPACTION, msgContext.getSoapAction());
    // write body
    MessageFormatterEx messageFormatterEx;
    if (messageFormatter instanceof MessageFormatterEx) {
        messageFormatterEx = (MessageFormatterEx) messageFormatter;
    } else {
        messageFormatterEx = new MessageFormatterExAdapter(messageFormatter);
    }
    DataHandler dataHandler = new DataHandler(messageFormatterEx.getDataSource(msgContext, format, msgContext.getSoapAction()));
    MimeMultipart mimeMultiPart = null;
    String mFormat = (String) msgContext.getProperty(MailConstants.TRANSPORT_MAIL_FORMAT);
    if (mFormat == null) {
        mFormat = defaultMailFormat;
    }
    if (log.isDebugEnabled()) {
        log.debug("Using mail format '" + mFormat + "'");
    }
    MimePart mainPart;
    boolean isMultiPart = MailConstants.TRANSPORT_FORMAT_MP.equals(mFormat);
    boolean isAttachFile = MailConstants.TRANSPORT_FORMAT_ATTACHMENT.equals(mFormat);
    if (isMultiPart || isAttachFile) {
        mimeMultiPart = new MimeMultipart();
        MimeBodyPart mimeBodyPart1 = new MimeBodyPart();
        String body = (String) msgContext.getProperty(MailConstants.TRANSPORT_MAIL_BODY_WHEN_ATTACHED);
        if (body == null) {
            body = "Web Service Message Attached";
        }
        String bodyMime = (String) msgContext.getProperty(MailConstants.TRANSPORT_MAIL_BODY_MIME_WHEN_ATTACHED);
        if (bodyMime == null) {
            bodyMime = "text/plain";
        }
        mimeBodyPart1.setContent(body, bodyMime);
        MimeBodyPart mimeBodyPart2 = new MimeBodyPart();
        mimeMultiPart.addBodyPart(mimeBodyPart1);
        mimeMultiPart.addBodyPart(mimeBodyPart2);
        message.setContent(mimeMultiPart);
        if (isAttachFile) {
            String fileName = (String) msgContext.getProperty(MailConstants.TRANSPORT_FORMAT_ATTACHMENT_FILE);
            if (fileName != null) {
                mimeBodyPart2.setFileName(fileName);
            } else {
                mimeBodyPart2.setFileName("attachment");
            }
        }
        mainPart = mimeBodyPart2;
    } else {
        mainPart = message;
    }
    try {
        mainPart.setHeader(BaseConstants.SOAPACTION, msgContext.getSoapAction());
        mainPart.setDataHandler(dataHandler);
        // needs to scan the entire content to determine this.
        if (msgContext.getOptions().getProperty("Content-Transfer-Encoding") != null) {
            mainPart.setHeader("Content-Transfer-Encoding", (String) msgContext.getOptions().getProperty("Content-Transfer-Encoding"));
        } else {
            String contentType = dataHandler.getContentType().toLowerCase();
            if (!contentType.startsWith("multipart/") && CommonUtils.isTextualPart(contentType)) {
                mainPart.setHeader("Content-Transfer-Encoding", "quoted-printable");
            }
        }
        // setting any custom headers defined by the user
        if (msgContext.getOptions().getProperty(MailConstants.TRANSPORT_MAIL_CUSTOM_HEADERS) != null) {
            Map customTransportHeaders = (Map) msgContext.getOptions().getProperty(MailConstants.TRANSPORT_MAIL_CUSTOM_HEADERS);
            for (Object header : customTransportHeaders.keySet()) {
                mainPart.setHeader((String) header, (String) customTransportHeaders.get(header));
            }
        }
        log.debug("Sending message");
        Transport.send(message);
        // update metrics
        metrics.incrementMessagesSent(msgContext);
        long bytesSent = message.getBytesSent();
        if (bytesSent != -1) {
            metrics.incrementBytesSent(msgContext, bytesSent);
        }
    } catch (MessagingException e) {
        metrics.incrementFaultsSending();
        handleException("Error creating mail message or sending it to the configured server", e);
    }
    return message.getMessageID();
}
Also used : MessageFormatterExAdapter(org.apache.axis2.format.MessageFormatterExAdapter) MessageFormatter(org.apache.axis2.transport.MessageFormatter) DataHandler(javax.activation.DataHandler) MessageFormatterEx(org.apache.axis2.format.MessageFormatterEx) OMOutputFormat(org.apache.axiom.om.OMOutputFormat) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 7 with OutTransportInfo

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

the class FIXTransportSenderTest method testFIXTransportSenderSendMessage.

@Test()
public void testFIXTransportSenderSendMessage() throws Exception {
    String BEGIN_STRING = "FIX.4.1";
    String SENDER_ID = "BANZAI";
    String TARGET_ID = "SYNAPSE";
    int SEQ_NUM = 5;
    String SYMBOL = "APACHE";
    String CLORD_ID = "12345";
    String CHECKSUM = "67890";
    String TX_DATE = new Date().toString();
    String SESSION_ID = "FIX.4.1:BANZAI->SYNAPSE";
    Message message = new NewOrderSingle();
    message.getHeader().setField(new BeginString(BEGIN_STRING));
    message.getHeader().setField(new SenderCompID(SENDER_ID));
    message.getHeader().setField(new TargetCompID(TARGET_ID));
    message.getHeader().setField(new MsgSeqNum(SEQ_NUM));
    message.setField(new Symbol(SYMBOL));
    message.setField(new ClOrdID(CLORD_ID));
    message.setField(new TradeOriginationDate(TX_DATE));
    message.getTrailer().setField(new CheckSum(CHECKSUM));
    MessageContext msgCtx = new MessageContext();
    msgCtx.setProperty(FIXConstants.FIX_SERVICE_NAME, "sampleService");
    Map trpHeaders = new HashMap();
    trpHeaders.put(FIXConstants.FIX_MESSAGE_APPLICATION, "sampleApplication");
    msgCtx.setProperty(MessageContext.TRANSPORT_HEADERS, trpHeaders);
    FIXUtils.getInstance().setSOAPEnvelope(message, SEQ_NUM, SESSION_ID, msgCtx);
    OutTransportInfo info = new FIXOutTransportInfo("fix://dummyEPR");
    FIXTransportSender spy = PowerMockito.spy(new FIXTransportSender());
    PowerMockito.doReturn(true).when(spy, "sendUsingEPR", anyString(), anyString(), any(), anyString(), anyInt(), any());
    spy.sendMessage(msgCtx, "fix://dummyEPR", info);
    PowerMockito.verifyPrivate(spy, times(1)).invoke("sendUsingEPR", anyString(), anyString(), any(), anyString(), anyInt(), any());
}
Also used : NewOrderSingle(quickfix.fix41.NewOrderSingle) BeginString(quickfix.field.BeginString) SenderCompID(quickfix.field.SenderCompID) Message(quickfix.Message) HashMap(java.util.HashMap) Symbol(quickfix.field.Symbol) TargetCompID(quickfix.field.TargetCompID) ClOrdID(quickfix.field.ClOrdID) TradeOriginationDate(quickfix.field.TradeOriginationDate) BeginString(quickfix.field.BeginString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) MsgSeqNum(quickfix.field.MsgSeqNum) Date(java.util.Date) TradeOriginationDate(quickfix.field.TradeOriginationDate) OutTransportInfo(org.apache.axis2.transport.OutTransportInfo) CheckSum(quickfix.field.CheckSum) MessageContext(org.apache.axis2.context.MessageContext) HashMap(java.util.HashMap) Map(java.util.Map) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 8 with OutTransportInfo

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

the class HttpCoreNIOSender 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) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) OMOutputFormat(org.apache.axiom.om.OMOutputFormat)

Example 9 with OutTransportInfo

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

the class VFSTransportSenderTest method testReplyFileCreation.

/**
 * Test creation of reply file in target directory
 * @throws AxisFault
 */
public void testReplyFileCreation() throws AxisFault {
    String replyFileName = "testFile.txt";
    // Clear file holder
    MockFileHolder.getInstance().clear();
    VFSTransportSender vfsTransportSender = new VFSTransportSender();
    ConfigurationContext configurationContext = new ConfigurationContext(new AxisConfiguration());
    TransportOutDescription transportOutDescription = new TransportOutDescription("Test");
    vfsTransportSender.init(configurationContext, transportOutDescription);
    // Create message context
    org.apache.axis2.context.MessageContext mc = new org.apache.axis2.context.MessageContext();
    populateMessageContext(mc);
    // Add transport.vfs.ReplyFileName axis service parameter
    mc.getAxisService().addParameter(new Parameter(VFSConstants.REPLY_FILE_NAME, replyFileName));
    String filePath = "test1:///foo/bar/test-" + System.currentTimeMillis() + "/DIR/";
    String parameters = "?transport.vfs.CreateFolder=true";
    String fURI = filePath + parameters;
    OutTransportInfo outTransportInfo = new VFSOutTransportInfo(fURI, true);
    vfsTransportSender.sendMessage(mc, fURI, outTransportInfo);
    Assert.assertNotNull(MockFileHolder.getInstance().getFile(filePath + replyFileName));
}
Also used : ConfigurationContext(org.apache.axis2.context.ConfigurationContext) AxisConfiguration(org.apache.axis2.engine.AxisConfiguration) VFSOutTransportInfo(org.apache.synapse.commons.vfs.VFSOutTransportInfo) OutTransportInfo(org.apache.axis2.transport.OutTransportInfo) Parameter(org.apache.axis2.description.Parameter) MessageContext(org.apache.axis2.context.MessageContext) MessageContext(org.apache.axis2.context.MessageContext) VFSOutTransportInfo(org.apache.synapse.commons.vfs.VFSOutTransportInfo) TransportOutDescription(org.apache.axis2.description.TransportOutDescription)

Example 10 with OutTransportInfo

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

the class VFSTransportSenderTest method testMemoryLeakWhileLockCreation.

public void testMemoryLeakWhileLockCreation() throws AxisFault, NoSuchFieldException, IllegalAccessException {
    // Clear file holder
    MockFileHolder.getInstance().clear();
    VFSTransportSender vfsTransportSender = new VFSTransportSender();
    ConfigurationContext configurationContext = new ConfigurationContext(new AxisConfiguration());
    TransportOutDescription transportOutDescription = new TransportOutDescription("Test");
    vfsTransportSender.init(configurationContext, transportOutDescription);
    org.apache.axis2.context.MessageContext mc = new org.apache.axis2.context.MessageContext();
    populateMessageContext(mc);
    for (int i = 0; i < FILE_SEND_ITERATIONS; i++) {
        String fName = "test1://foo/bar/test" + i + "-" + System.currentTimeMillis() + ".ack?transport.vfs.MaxRetryCount=0&transport.vfs.ReconnectTimeout=1";
        OutTransportInfo outTransportInfo = new VFSOutTransportInfo(fName, true);
        try {
            vfsTransportSender.sendMessage(mc, fName, outTransportInfo);
        } catch (AxisFault fse) {
        // Ignore
        }
    }
    // Perform the GC
    System.gc();
    Map<?, ?> refReverseMap = getSoftReferenceMap(vfsTransportSender);
    assertEquals("If there is no memory leak, soft reference map size should be zero.", 0, refReverseMap.size());
}
Also used : AxisFault(org.apache.axis2.AxisFault) ConfigurationContext(org.apache.axis2.context.ConfigurationContext) AxisConfiguration(org.apache.axis2.engine.AxisConfiguration) VFSOutTransportInfo(org.apache.synapse.commons.vfs.VFSOutTransportInfo) OutTransportInfo(org.apache.axis2.transport.OutTransportInfo) MessageContext(org.apache.axis2.context.MessageContext) MessageContext(org.apache.axis2.context.MessageContext) VFSOutTransportInfo(org.apache.synapse.commons.vfs.VFSOutTransportInfo) TransportOutDescription(org.apache.axis2.description.TransportOutDescription)

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