Search in sources :

Example 1 with MailTransportError

use of com.helger.smtp.transport.MailTransportError in project ph-web by phax.

the class FailedMailDataTest method testWithExceptionAndData.

@Test
public void testWithExceptionAndData() {
    final ISMTPSettings aSettings = _createSMTPSettings();
    final EmailData aData = _createEmailData();
    final Throwable aError = new MockException("Test error");
    final FailedMailData aFMD = new FailedMailData(aSettings, aData, new MailTransportError(aError));
    assertNotNull(aFMD.getID());
    assertNotNull(aFMD.getErrorDateTime());
    assertEquals(aSettings, aFMD.getSMTPSettings());
    assertEquals(aData.getSentDateTime(), aFMD.getOriginalSentDateTime());
    assertEquals(aData, aFMD.getEmailData());
    assertTrue(aFMD.hasTransportError());
    assertNotNull(aFMD.getTransportThrowableMessage());
    assertNotNull(aFMD.getTransportError());
    XMLTestHelper.testMicroTypeConversion(aFMD);
}
Also used : MockException(com.helger.commons.exception.mock.MockException) MailTransportError(com.helger.smtp.transport.MailTransportError) ISMTPSettings(com.helger.smtp.settings.ISMTPSettings) EmailData(com.helger.smtp.data.EmailData) Test(org.junit.Test)

Example 2 with MailTransportError

use of com.helger.smtp.transport.MailTransportError in project ph-web by phax.

the class FailedMailDataTest method testWithException.

@Test
public void testWithException() {
    final ISMTPSettings aSettings = _createSMTPSettings();
    final Throwable aError = new MockException("Test error");
    final FailedMailData aFMD = new FailedMailData(aSettings, new MailTransportError(aError));
    assertNotNull(aFMD.getID());
    assertNotNull(aFMD.getErrorDateTime());
    assertEquals(aSettings, aFMD.getSMTPSettings());
    assertNull(aFMD.getOriginalSentDateTime());
    assertNull(aFMD.getEmailData());
    assertTrue(aFMD.hasTransportError());
    assertNotNull(aFMD.getTransportThrowableMessage());
    assertNotNull(aFMD.getTransportError());
    XMLTestHelper.testMicroTypeConversion(aFMD);
}
Also used : MockException(com.helger.commons.exception.mock.MockException) MailTransportError(com.helger.smtp.transport.MailTransportError) ISMTPSettings(com.helger.smtp.settings.ISMTPSettings) Test(org.junit.Test)

Example 3 with MailTransportError

use of com.helger.smtp.transport.MailTransportError in project ph-web by phax.

the class FailedMailDataMicroTypeConverter method convertToNative.

@Nullable
public FailedMailData convertToNative(@Nonnull final IMicroElement eFailedMail) {
    final String sID = eFailedMail.getAttributeValue(ATTR_ID);
    if (sID == null) {
        LOGGER.error("Failed to read ID");
        return null;
    }
    // Read error date/time
    final String sErrorDT = eFailedMail.getAttributeValue(ATTR_ERRORDT);
    if (sErrorDT == null) {
        LOGGER.error("Failed to read error date/time");
        return null;
    }
    LocalDateTime aErrorDT = PDTWebDateHelper.getLocalDateTimeFromXSD(sErrorDT);
    if (aErrorDT == null)
        aErrorDT = TypeConverter.convert(sErrorDT, LocalDateTime.class);
    if (aErrorDT == null) {
        if (LOGGER.isErrorEnabled())
            LOGGER.error("Failed to parse error date '" + sErrorDT + "'");
        return null;
    }
    // read original sent date/time
    final String sOriginalSentDT = eFailedMail.getAttributeValue(ATTR_ORIGINALSENT_DT);
    LocalDateTime aOriginalSentDT = null;
    if (sOriginalSentDT != null) {
        aOriginalSentDT = PDTWebDateHelper.getLocalDateTimeFromXSD(sOriginalSentDT);
        if (aOriginalSentDT == null)
            aOriginalSentDT = TypeConverter.convert(sOriginalSentDT, LocalDateTime.class);
    }
    // SMTP settings
    final IMicroElement eSMTPSettings = eFailedMail.getFirstChildElement(ELEMENT_SMTP_SETTINGS);
    if (eSMTPSettings == null) {
        LOGGER.error("Failed to get child element of SMTP settings!");
        return null;
    }
    final ISMTPSettings aSMTPSettings = MicroTypeConverter.convertToNative(eSMTPSettings, SMTPSettings.class);
    // email data (may be null)
    final IMicroElement eEmailData = eFailedMail.getFirstChildElement(ELEMENT_EMAIL_DATA);
    final IMutableEmailData aEmailData = MicroTypeConverter.convertToNative(eEmailData, EmailData.class);
    // error message
    final String sErrorMessage = MicroHelper.getChildTextContent(eFailedMail, ELEMENT_ERROR_MSG);
    final Exception aException = StringHelper.hasNoText(sErrorMessage) ? null : new Exception(sErrorMessage);
    MailTransportError aError = null;
    if (aException != null) {
        final ICommonsList<MailSendDetails> aDetails = new CommonsArrayList<>();
        for (final IMicroElement eDetails : eFailedMail.getAllChildElements(ELEMENT_DETAILS)) {
            final boolean bAddressValid = StringParser.parseBool(eDetails.getAttributeValue(ATTR_ADDRESS_VALID));
            final String sAddress = eDetails.getAttributeValue(ATTR_ADDRESS);
            final String sCommand = eDetails.getAttributeValue(ATTR_COMMAND);
            final String sDetailsErrorMessage = eDetails.getAttributeValue(ATTR_ERROR_MESSAGE);
            final int nErrorCode = StringParser.parseInt(eDetails.getAttributeValue(ATTR_ERROR_CODE), -1);
            final ESMTPErrorCode eErrorCode = ESMTPErrorCode.getFromIDOrDefault(nErrorCode, ESMTPErrorCode.FALLBACK);
            aDetails.add(new MailSendDetails(bAddressValid, sAddress, sCommand, sDetailsErrorMessage, eErrorCode));
        }
        aError = new MailTransportError(aException, aDetails);
    }
    return new FailedMailData(sID, aErrorDT, aSMTPSettings, aOriginalSentDT, aEmailData, aError);
}
Also used : LocalDateTime(java.time.LocalDateTime) ESMTPErrorCode(com.helger.smtp.transport.ESMTPErrorCode) MailSendDetails(com.helger.smtp.transport.MailSendDetails) IMutableEmailData(com.helger.smtp.data.IMutableEmailData) IMicroElement(com.helger.xml.microdom.IMicroElement) MailTransportError(com.helger.smtp.transport.MailTransportError) ISMTPSettings(com.helger.smtp.settings.ISMTPSettings) CommonsArrayList(com.helger.commons.collection.impl.CommonsArrayList) Nullable(javax.annotation.Nullable)

Example 4 with MailTransportError

use of com.helger.smtp.transport.MailTransportError in project ph-web by phax.

the class FailedMailDataMicroTypeConverter method convertToMicroElement.

@Nonnull
public IMicroElement convertToMicroElement(@Nonnull final FailedMailData aFailedMail, @Nullable final String sNamespaceURI, @Nonnull final String sTagName) {
    final IMicroElement eFailedMail = new MicroElement(sNamespaceURI, sTagName);
    eFailedMail.setAttribute(ATTR_ID, aFailedMail.getID());
    eFailedMail.setAttributeWithConversion(ATTR_ERRORDT, aFailedMail.getErrorDateTime());
    eFailedMail.setAttributeWithConversion(ATTR_ORIGINALSENT_DT, aFailedMail.getOriginalSentDateTime());
    // SMTP settings
    eFailedMail.appendChild(MicroTypeConverter.convertToMicroElement(aFailedMail.getSMTPSettings(), sNamespaceURI, ELEMENT_SMTP_SETTINGS));
    // email data
    eFailedMail.appendChild(MicroTypeConverter.convertToMicroElement(aFailedMail.getEmailData(), sNamespaceURI, ELEMENT_EMAIL_DATA));
    final MailTransportError aTransportError = aFailedMail.getTransportError();
    if (aTransportError != null) {
        eFailedMail.appendElement(sNamespaceURI, ELEMENT_ERROR_MSG).appendText(aTransportError.getThrowable().getMessage());
        for (final MailSendDetails aDetails : aTransportError.getAllDetails()) {
            eFailedMail.appendElement(sNamespaceURI, ELEMENT_DETAILS).setAttribute(ATTR_ADDRESS_VALID, aDetails.isAddressValid()).setAttribute(ATTR_ADDRESS, aDetails.getAddress()).setAttribute(ATTR_COMMAND, aDetails.getCommand()).setAttribute(ATTR_ERROR_MESSAGE, aDetails.getErrorMessage()).setAttribute(ATTR_ERROR_CODE, aDetails.getErrorCode().getECode());
        }
    }
    return eFailedMail;
}
Also used : MicroElement(com.helger.xml.microdom.MicroElement) IMicroElement(com.helger.xml.microdom.IMicroElement) MailSendDetails(com.helger.smtp.transport.MailSendDetails) IMicroElement(com.helger.xml.microdom.IMicroElement) MailTransportError(com.helger.smtp.transport.MailTransportError) Nonnull(javax.annotation.Nonnull)

Aggregations

MailTransportError (com.helger.smtp.transport.MailTransportError)4 ISMTPSettings (com.helger.smtp.settings.ISMTPSettings)3 MockException (com.helger.commons.exception.mock.MockException)2 MailSendDetails (com.helger.smtp.transport.MailSendDetails)2 IMicroElement (com.helger.xml.microdom.IMicroElement)2 Test (org.junit.Test)2 CommonsArrayList (com.helger.commons.collection.impl.CommonsArrayList)1 EmailData (com.helger.smtp.data.EmailData)1 IMutableEmailData (com.helger.smtp.data.IMutableEmailData)1 ESMTPErrorCode (com.helger.smtp.transport.ESMTPErrorCode)1 MicroElement (com.helger.xml.microdom.MicroElement)1 LocalDateTime (java.time.LocalDateTime)1 Nonnull (javax.annotation.Nonnull)1 Nullable (javax.annotation.Nullable)1