Search in sources :

Example 1 with ESMTPErrorCode

use of com.helger.smtp.transport.ESMTPErrorCode 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)

Aggregations

CommonsArrayList (com.helger.commons.collection.impl.CommonsArrayList)1 IMutableEmailData (com.helger.smtp.data.IMutableEmailData)1 ISMTPSettings (com.helger.smtp.settings.ISMTPSettings)1 ESMTPErrorCode (com.helger.smtp.transport.ESMTPErrorCode)1 MailSendDetails (com.helger.smtp.transport.MailSendDetails)1 MailTransportError (com.helger.smtp.transport.MailTransportError)1 IMicroElement (com.helger.xml.microdom.IMicroElement)1 LocalDateTime (java.time.LocalDateTime)1 Nullable (javax.annotation.Nullable)1