Search in sources :

Example 66 with MessagingException

use of javax.mail.MessagingException in project nhin-d by DirectProject.

the class TimelyAndReliableLocalDelivery method init.

/**
	 * {@inheritDoc}
	 */
public void init() throws MessagingException {
    super.init();
    try {
        final String sDispatchedDelay = GatewayConfiguration.getConfigurationParam(DISPATCHED_MDN_DELAY, this, "0");
        try {
            dispatchedMDNDelay = Integer.valueOf(sDispatchedDelay).intValue();
        } catch (NumberFormatException e) {
            // in case of parsing exceptions
            dispatchedMDNDelay = 0;
        }
        // create an instance of the local delivery if we can
        localDeliveryMailet = createLocalDeliveryClass();
        final Method initMethod = Mailet.class.getDeclaredMethod("init", MailetConfig.class);
        serviceMethod = Mailet.class.getDeclaredMethod("service", Mail.class);
        // set private objects if they exist
        final Class<?> localDeliveryMailetClass = localDeliveryMailet.getClass();
        Field field = getDeclaredFieldQuietly(localDeliveryMailetClass, "rrt");
        if (field != null) {
            field.setAccessible(true);
            field.set(localDeliveryMailet, rrt);
        }
        field = getDeclaredFieldQuietly(localDeliveryMailetClass, "usersRepository");
        if (field != null) {
            field.setAccessible(true);
            field.set(localDeliveryMailet, usersRepository);
        }
        field = getDeclaredFieldQuietly(localDeliveryMailetClass, "mailboxManager");
        if (field != null) {
            field.setAccessible(true);
            field.set(localDeliveryMailet, mailboxManager);
        }
        field = getDeclaredFieldQuietly(localDeliveryMailetClass, "domainList");
        if (field != null) {
            field.setAccessible(true);
            field.set(localDeliveryMailet, domainList);
        }
        field = getDeclaredFieldQuietly(localDeliveryMailetClass, "fileSystem");
        if (field != null) {
            field.setAccessible(true);
            field.set(localDeliveryMailet, fileSystem);
        }
        initMethod.invoke(localDeliveryMailet, this.getMailetConfig());
    } catch (Exception e) {
        throw new MessagingException("Failed to initialize TimelyAndReliableLocalDelivery.", e);
    }
    notificationProducer = new ReliableDispatchedNotificationProducer(new NotificationSettings(true, "Local Direct Delivery Agent", "Your message was successfully dispatched."));
}
Also used : Field(java.lang.reflect.Field) Mail(org.apache.mailet.Mail) MessagingException(javax.mail.MessagingException) NotificationSettings(org.nhindirect.gateway.smtp.NotificationSettings) Method(java.lang.reflect.Method) Mailet(org.apache.mailet.Mailet) MessagingException(javax.mail.MessagingException) ReliableDispatchedNotificationProducer(org.nhindirect.gateway.smtp.ReliableDispatchedNotificationProducer)

Example 67 with MessagingException

use of javax.mail.MessagingException in project nhin-d by DirectProject.

the class NHINDSecurityAndTrustMailet method getSender.

public static InternetAddress getSender(Mail mail) {
    InternetAddress retVal = null;
    if (mail.getSender() != null)
        retVal = mail.getSender().toInternetAddress();
    else {
        // try to get the sender from the message
        Address[] senderAddr = null;
        try {
            if (mail.getMessage() == null)
                return null;
            senderAddr = mail.getMessage().getFrom();
            if (senderAddr == null || senderAddr.length == 0)
                return null;
        } catch (MessagingException e) {
            return null;
        }
        // not the best way to do this
        retVal = (InternetAddress) senderAddr[0];
    }
    return retVal;
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) Address(javax.mail.Address) MailAddress(org.apache.mailet.MailAddress) InternetAddress(javax.mail.internet.InternetAddress) NHINDAddress(org.nhindirect.stagent.NHINDAddress) MessagingException(javax.mail.MessagingException)

Example 68 with MessagingException

use of javax.mail.MessagingException in project nhin-d by DirectProject.

the class AbstractNotificationAwareMailet method getSender.

/**
	 * Gets the sender attribute of a Mail message
	 * @param mail The message to retrive the sender from
	 * @return The message sender.
	 */
public static InternetAddress getSender(Mail mail) {
    InternetAddress retVal = null;
    if (mail.getSender() != null)
        retVal = mail.getSender().toInternetAddress();
    else {
        // try to get the sender from the message
        Address[] senderAddr = null;
        try {
            if (mail.getMessage() == null)
                return null;
            senderAddr = mail.getMessage().getFrom();
            if (senderAddr == null || senderAddr.length == 0)
                return null;
        } catch (MessagingException e) {
            return null;
        }
        // not the best way to do this
        retVal = (InternetAddress) senderAddr[0];
    }
    return retVal;
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) Address(javax.mail.Address) MailAddress(org.apache.mailet.MailAddress) InternetAddress(javax.mail.internet.InternetAddress) NHINDAddress(org.nhindirect.stagent.NHINDAddress) MessagingException(javax.mail.MessagingException)

Example 69 with MessagingException

use of javax.mail.MessagingException in project nhin-d by DirectProject.

the class NHINDSecurityAndTrustMailet_service_Test method testService_ProcessThrowsRuntimeException_AssertExceptionAndGhostState.

public void testService_ProcessThrowsRuntimeException_AssertExceptionAndGhostState() throws Exception {
    final MimeMessage mimeMsg = EntitySerializer.Default.deserialize(TestUtils.readMessageResource("PlainOutgoingMessage.txt"));
    final SmtpAgent mockAgent = mock(SmtpAgent.class);
    final Mail mockMail = mock(MockMail.class, CALLS_REAL_METHODS);
    when(mockMail.getRecipients()).thenReturn(null);
    when(mockMail.getSender()).thenReturn(new MailAddress("me@cerner.com"));
    doThrow(new RuntimeException("Just Passing Through")).when(mockAgent).processMessage((MimeMessage) any(), (NHINDAddressCollection) any(), (NHINDAddress) any());
    mockMail.setMessage(mimeMsg);
    NHINDSecurityAndTrustMailet mailet = new NHINDSecurityAndTrustMailet();
    mailet.agent = mockAgent;
    boolean exceptionOccured = false;
    try {
        mailet.service(mockMail);
    } catch (MessagingException e) {
        assertEquals("Failed to process message: Just Passing Through", e.getMessage());
        exceptionOccured = true;
    }
    assertFalse(exceptionOccured);
    assertEquals(Mail.GHOST, mockMail.getState());
}
Also used : Mail(org.apache.mailet.Mail) MailAddress(org.apache.mailet.MailAddress) MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) SmtpAgent(org.nhindirect.gateway.smtp.SmtpAgent)

Example 70 with MessagingException

use of javax.mail.MessagingException in project nhin-d by DirectProject.

the class DirectXdMailet method service.

/*
     * (non-Javadoc)
     * 
     * @see org.apache.mailet.base.GenericMailet#service(org.apache.mailet.Mail)
     */
@Override
public void service(Mail mail) throws MessagingException {
    LOGGER.info("Servicing DirectXdMailet");
    if (StringUtils.isBlank(endpointUrl)) {
        LOGGER.error("DirectXdMailet endpoint URL cannot be empty or null.");
        throw new MessagingException("DirectXdMailet endpoint URL cannot be empty or null.");
    }
    boolean successfulTransaction = false;
    final MimeMessage msg = mail.getMessage();
    final boolean isReliableAndTimely = TxUtil.isReliableAndTimelyRequested(msg);
    final NHINDAddressCollection initialRecipients = getMailRecipients(mail);
    final NHINDAddressCollection xdRecipients = new NHINDAddressCollection();
    final NHINDAddress sender = getMailSender(mail);
    Tx txToTrack = null;
    // Get recipients and create a collection of Strings
    final List<String> recipAddresses = new ArrayList<String>();
    for (NHINDAddress addr : initialRecipients) {
        recipAddresses.add(addr.getAddress());
    }
    // Service XD* addresses
    if (getResolver().hasXdEndpoints(recipAddresses)) {
        LOGGER.info("Recipients include XD endpoints");
        try {
            //List<Address> xdAddresses = new ArrayList<Address>();
            for (String s : getResolver().getXdEndpoints(recipAddresses)) {
                //xdAddresses.add((new MailAddress(s)).toInternetAddress());
                xdRecipients.add(new NHINDAddress(s));
            }
            txToTrack = this.getTxToTrack(msg, sender, xdRecipients);
            // Replace recipients with only XD* addresses
            //msg.setRecipients(RecipientType.TO, xdAddresses.toArray(new Address[0]));
            msg.setRecipients(RecipientType.TO, xdRecipients.toArray(new Address[0]));
            // Transform MimeMessage into ProvideAndRegisterDocumentSetRequestType object
            ProvideAndRegisterDocumentSetRequestType request = getMimeXDSTransformer().transform(msg);
            for (String directTo : recipAddresses) {
                String response = getDocumentRepository().forwardRequest(endpointUrl, request, directTo, sender.toString());
                if (!isSuccessful(response)) {
                    LOGGER.error("DirectXdMailet failed to deliver XD message.");
                    LOGGER.error(response);
                } else {
                    successfulTransaction = true;
                    if (isReliableAndTimely && txToTrack != null && txToTrack.getMsgType() == TxMessageType.IMF) {
                        // send MDN dispatch for messages the recipients that were successful
                        final Collection<NotificationMessage> notifications = notificationProducer.produce(new Message(msg), xdRecipients.toInternetAddressCollection());
                        if (notifications != null && notifications.size() > 0) {
                            LOGGER.debug("Sending MDN \"dispathed\" messages");
                            // create a message for each notification and put it on James "stack"
                            for (NotificationMessage message : notifications) {
                                try {
                                    getMailetContext().sendMail(message);
                                } catch (Throwable t) {
                                    // don't kill the process if this fails
                                    LOGGER.error("Error sending MDN dispatched message.", t);
                                }
                            }
                        }
                    }
                }
            }
        } catch (Throwable e) {
            LOGGER.error("DirectXdMailet delivery failure", e);
        }
    }
    // this basically sets the message back to it's original state with SMTP addresses only
    if (getResolver().hasSmtpEndpoints(recipAddresses)) {
        LOGGER.info("Recipients include SMTP endpoints");
        mail.setRecipients(getSmtpRecips(recipAddresses));
    } else {
        LOGGER.info("Recipients do not include SMTP endpoints");
        // No SMTP addresses, ghost it
        mail.setState(Mail.GHOST);
    }
    if (!successfulTransaction) {
        if (txToTrack != null && txToTrack.getMsgType() == TxMessageType.IMF) {
            // for good measure, send DSN messages back to the original sender on failure
            // create a DSN message
            this.sendDSN(txToTrack, xdRecipients, false);
        }
    }
}
Also used : Tx(org.nhindirect.common.tx.model.Tx) Address(javax.mail.Address) MailAddress(org.apache.mailet.MailAddress) NHINDAddress(org.nhindirect.stagent.NHINDAddress) NotificationMessage(org.nhindirect.stagent.mail.notifications.NotificationMessage) Message(org.nhindirect.stagent.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) NHINDAddressCollection(org.nhindirect.stagent.NHINDAddressCollection) ArrayList(java.util.ArrayList) NHINDAddress(org.nhindirect.stagent.NHINDAddress) NotificationMessage(org.nhindirect.stagent.mail.notifications.NotificationMessage) MimeMessage(javax.mail.internet.MimeMessage) ProvideAndRegisterDocumentSetRequestType(ihe.iti.xds_b._2007.ProvideAndRegisterDocumentSetRequestType)

Aggregations

MessagingException (javax.mail.MessagingException)343 MimeMessage (javax.mail.internet.MimeMessage)135 IOException (java.io.IOException)126 InternetAddress (javax.mail.internet.InternetAddress)70 MimeMultipart (javax.mail.internet.MimeMultipart)64 MimeBodyPart (javax.mail.internet.MimeBodyPart)63 Message (javax.mail.Message)49 Properties (java.util.Properties)45 Session (javax.mail.Session)45 InputStream (java.io.InputStream)43 Date (java.util.Date)34 Address (javax.mail.Address)33 PackageException (com.axway.ats.action.objects.model.PackageException)32 ArrayList (java.util.ArrayList)32 NoSuchMimePackageException (com.axway.ats.action.objects.model.NoSuchMimePackageException)31 PublicAtsApi (com.axway.ats.common.PublicAtsApi)31 ServiceException (com.zimbra.common.service.ServiceException)30 ByteArrayInputStream (java.io.ByteArrayInputStream)26 DataHandler (javax.activation.DataHandler)25 UnsupportedEncodingException (java.io.UnsupportedEncodingException)24