Search in sources :

Example 1 with DateParameters

use of com.helger.as2lib.params.DateParameters in project as2-lib by phax.

the class AS2Message method generateMessageID.

@Override
@Nonnull
@Nonempty
public String generateMessageID() {
    final CompositeParameters aParams = new CompositeParameters(false).add("date", new DateParameters()).add("msg", new MessageParameters(this)).add("rand", new RandomParameters());
    final String sIDFormat = partnership().getMessageIDFormat(DEFAULT_ID_FORMAT);
    final StringBuilder aSB = new StringBuilder();
    aSB.append('<');
    try {
        aSB.append(aParams.format(sIDFormat));
    } catch (final AS2InvalidParameterException ex) {
        // useless, but what to do?
        aSB.append(sIDFormat);
    }
    aSB.append('>');
    return aSB.toString();
}
Also used : CompositeParameters(com.helger.as2lib.params.CompositeParameters) AS2InvalidParameterException(com.helger.as2lib.params.AS2InvalidParameterException) MessageParameters(com.helger.as2lib.params.MessageParameters) DateParameters(com.helger.as2lib.params.DateParameters) RandomParameters(com.helger.as2lib.params.RandomParameters) Nonnull(javax.annotation.Nonnull) Nonempty(com.helger.commons.annotation.Nonempty)

Example 2 with DateParameters

use of com.helger.as2lib.params.DateParameters in project as2-lib by phax.

the class MDNFileModule method getFilename.

@Override
protected String getFilename(@Nonnull final IMessage aMsg, @Nullable final String sFileParam) throws AS2InvalidParameterException {
    final IMessageMDN aMDN = aMsg.getMDN();
    final CompositeParameters aCompParams = new CompositeParameters(false).add("date", new DateParameters()).add("mdn", new MessageMDNParameters(aMDN));
    return aCompParams.format(sFileParam);
}
Also used : MessageMDNParameters(com.helger.as2lib.params.MessageMDNParameters) CompositeParameters(com.helger.as2lib.params.CompositeParameters) DateParameters(com.helger.as2lib.params.DateParameters) IMessageMDN(com.helger.as2lib.message.IMessageMDN)

Example 3 with DateParameters

use of com.helger.as2lib.params.DateParameters in project as2-lib by phax.

the class DirectoryResenderModule method resendFile.

protected void resendFile(@Nonnull final File aFile) throws AS2Exception {
    if (LOGGER.isDebugEnabled())
        LOGGER.debug("Processing " + aFile.getAbsolutePath());
    final CompositeParameters aParams = new CompositeParameters(false).add("date", new DateParameters());
    IMessage aMsg = null;
    try {
        try {
            final String sResendAction;
            String sRetries;
            try (final FileInputStream aFIS = new FileInputStream(aFile);
                final ObjectInputStream aOIS = new ObjectInputStream(aFIS)) {
                sResendAction = (String) aOIS.readObject();
                sRetries = (String) aOIS.readObject();
                aMsg = (IMessage) aOIS.readObject();
            }
            // Decrement retries
            sRetries = Integer.toString(Integer.parseInt(sRetries) - 1);
            // Transmit the message
            if (LOGGER.isInfoEnabled())
                LOGGER.info("loaded message for resend." + aMsg.getLoggingText());
            final ICommonsMap<String, Object> aOptions = new CommonsHashMap<>();
            aOptions.put(IProcessorResenderModule.OPTION_RETRIES, sRetries);
            getSession().getMessageProcessor().handle(sResendAction, aMsg, aOptions);
            if (AS2IOHelper.getFileOperationManager().deleteFile(aFile).isFailure()) {
                // again
                throw new AS2Exception("File was successfully sent but not deleted: " + aFile.getAbsolutePath());
            }
            if (LOGGER.isInfoEnabled())
                LOGGER.info("deleted " + aFile.getAbsolutePath() + aMsg.getLoggingText());
        } catch (final IOException | ClassNotFoundException ex) {
            // caught 3 lines below
            throw WrappedAS2Exception.wrap(ex);
        }
    } catch (final AS2Exception ex) {
        ex.terminate(aFile, aMsg);
        final String sErrorDirectory = aParams.format(getAttributeAsStringRequired(ATTR_ERROR_DIRECTORY));
        // Use the source name as the default
        final String sErrorFilename = StringHelper.getNotEmpty(aParams.format(attrs().getAsString(ATTR_STORED_ERROR_FILENAME)), aFile.getName());
        AS2IOHelper.handleError(aFile, sErrorDirectory, sErrorFilename);
    }
}
Also used : IMessage(com.helger.as2lib.message.IMessage) DateParameters(com.helger.as2lib.params.DateParameters) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) CompositeParameters(com.helger.as2lib.params.CompositeParameters) AS2Exception(com.helger.as2lib.exception.AS2Exception) WrappedAS2Exception(com.helger.as2lib.exception.WrappedAS2Exception) CommonsHashMap(com.helger.commons.collection.impl.CommonsHashMap) ObjectInputStream(java.io.ObjectInputStream)

Example 4 with DateParameters

use of com.helger.as2lib.params.DateParameters in project as2-lib by phax.

the class AbstractDirectoryPollingModule method processFile.

protected void processFile(@Nonnull final File aFile) throws AS2Exception {
    LOGGER.info("processing " + aFile.getAbsolutePath());
    final IMessage aMsg = createMessage();
    aMsg.attrs().putIn(CFileAttribute.MA_FILEPATH, aFile.getAbsolutePath());
    aMsg.attrs().putIn(CFileAttribute.MA_FILENAME, aFile.getName());
    /*
     * asynch mdn logic 2007-03-12 save the file name into message object, it
     * will be stored into pending information file
     */
    aMsg.attrs().putIn(CFileAttribute.MA_PENDING_FILENAME, aFile.getName());
    if (LOGGER.isDebugEnabled())
        LOGGER.debug("AS2Message was created");
    final CompositeParameters aParams = new CompositeParameters(false).add("date", new DateParameters()).add("msg", new MessageParameters(aMsg));
    try {
        updateMessage(aMsg, aFile);
        LOGGER.info("file assigned to message " + aFile.getAbsolutePath() + aMsg.getLoggingText());
        if (aMsg.getData() == null)
            throw new AS2InvalidMessageException("No Data");
        // Transmit the message - requires a module installed that implements the
        // "send" action (like com.helger.as2lib.processor.sender.AS2SenderModule)
        getSession().getMessageProcessor().handle(IProcessorSenderModule.DO_SEND, aMsg, null);
        if (LOGGER.isDebugEnabled())
            LOGGER.debug("AS2Message was successfully handled my the MessageProcessor");
        /*
       * asynch mdn logic 2007-03-12 If the return status is pending in msg's
       * attribute "status" then copy the transmitted file to pending folder and
       * wait for the receiver to make another HTTP call to post AsyncMDN
       */
        if (CFileAttribute.MA_STATUS_PENDING.equals(aMsg.attrs().getAsString(CFileAttribute.MA_STATUS))) {
            // Copy the file to the pending folder
            final String sFolderName = AS2IOHelper.getSafeFileAndFolderName(aMsg.partnership().getAttribute(CFileAttribute.MA_STATUS_PENDING));
            final String sFilename = FilenameHelper.getAsSecureValidASCIIFilename(aMsg.attrs().getAsString(CFileAttribute.MA_PENDING_FILENAME));
            final File aPendingFile = new File(sFolderName, sFilename);
            final FileIOError aIOErr = AS2IOHelper.getFileOperationManager().copyFile(aFile, aPendingFile);
            if (aIOErr.isFailure())
                throw new AS2Exception("File was successfully sent but not copied to pending folder: " + aPendingFile + " - " + aIOErr.toString());
            LOGGER.info("Copied '" + aFile.getAbsolutePath() + "' to pending folder '" + aPendingFile.getAbsolutePath() + "'" + aMsg.getLoggingText());
        }
        if (attrs().containsKey(ATTR_SENT_DIRECTORY)) {
            // If the Sent Directory option is set, move the transmitted file to
            // the sent directory
            File aSentFile = null;
            try {
                final String sSentDirectory = AS2IOHelper.getSafeFileAndFolderName(aParams.format(attrs().getAsString(ATTR_SENT_DIRECTORY)));
                // Default to the original filename
                final String sSentFilename = StringHelper.getNotEmpty(FilenameHelper.getAsSecureValidASCIIFilename(aParams.format(attrs().getAsString(ATTR_STORED_SENT_FILENAME))), aFile.getName());
                aSentFile = new File(AS2IOHelper.getDirectoryFile(sSentDirectory), sSentFilename);
                aSentFile = AS2IOHelper.moveFile(aFile, aSentFile, false, true);
                if (LOGGER.isInfoEnabled())
                    LOGGER.info("Moved '" + aFile.getAbsolutePath() + "' to '" + aSentFile.getAbsolutePath() + "'" + aMsg.getLoggingText());
            } catch (final IOException ex) {
                new AS2Exception("File was successfully sent but not moved to sent folder: '" + aSentFile.getAbsolutePath() + "'", ex).terminate();
            }
        } else {
            // The "Sent Directory" option was not set - so delete the file
            if (LOGGER.isDebugEnabled())
                LOGGER.debug("Trying to delete file " + aFile.getAbsolutePath());
            if (AS2IOHelper.getFileOperationManager().deleteFileIfExisting(aFile).isFailure()) {
                // Delete the file if a sent directory isn't set
                throw new AS2Exception("File was successfully sent but not deleted: '" + aFile.getAbsolutePath() + "'");
            }
            if (LOGGER.isInfoEnabled())
                LOGGER.info("Deleted file '" + aFile.getAbsolutePath() + "'" + aMsg.getLoggingText());
        }
    } catch (final AS2Exception ex) {
        ex.terminate(aFile, aMsg);
        final String sErrorDirectory = AS2IOHelper.getSafeFileAndFolderName(aParams.format(getAttributeAsStringRequired(ATTR_ERROR_DIRECTORY)));
        // Use the source name as the default
        final String sErrorFilename = StringHelper.getNotEmpty(FilenameHelper.getAsSecureValidASCIIFilename(aParams.format(attrs().getAsString(ATTR_STORED_ERROR_FILENAME))), aFile.getName());
        AS2IOHelper.handleError(aFile, sErrorDirectory, sErrorFilename);
    }
}
Also used : CompositeParameters(com.helger.as2lib.params.CompositeParameters) FileIOError(com.helger.commons.io.file.FileIOError) AS2Exception(com.helger.as2lib.exception.AS2Exception) WrappedAS2Exception(com.helger.as2lib.exception.WrappedAS2Exception) IMessage(com.helger.as2lib.message.IMessage) MessageParameters(com.helger.as2lib.params.MessageParameters) DateParameters(com.helger.as2lib.params.DateParameters) IOException(java.io.IOException) File(java.io.File)

Example 5 with DateParameters

use of com.helger.as2lib.params.DateParameters in project as2-lib by phax.

the class AbstractActiveNetModule method handleError.

public void handleError(@Nonnull final IMessage aMsg, @Nonnull final AS2Exception aSrcEx) {
    if (LOGGER.isTraceEnabled())
        LOGGER.trace("Handling error in " + ClassHelper.getClassLocalName(this.getClass()) + " for message with ID " + aMsg.getMessageID() + " and exception " + ClassHelper.getClassLocalName(aSrcEx.getClass()) + " with error " + aSrcEx.getMessage());
    aSrcEx.terminate(aMsg);
    try {
        final CompositeParameters aParams = new CompositeParameters(false).add("date", new DateParameters()).add("msg", new MessageParameters(aMsg));
        final String sErrorFilename = aParams.format(getErrorFormat());
        final String sErrorDirectory = aParams.format(getErrorDirectory());
        if (StringHelper.hasText(sErrorDirectory)) {
            final File aMsgErrorFile = AS2IOHelper.getUniqueFile(AS2IOHelper.getDirectoryFile(sErrorDirectory), FilenameHelper.getAsSecureValidFilename(sErrorFilename));
            // Default false for backwards compatibility reason
            final boolean bStoreBody = isErrorStoreBody();
            try (final OutputStream aFOS = FileHelper.getOutputStream(aMsgErrorFile)) {
                final String sMsgText = aMsg.getAsString();
                aFOS.write(sMsgText.getBytes());
                // Also store the body of the source message?
                if (bStoreBody)
                    StreamHelper.copyInputStreamToOutputStream(aMsg.getData().getInputStream(), aFOS);
            }
            // make sure an error of this event is logged
            new AS2InvalidMessageException("Stored invalid message to " + aMsgErrorFile.getAbsolutePath()).terminate();
        } else {
            LOGGER.warn("No error directory present, so ignoring the error");
        }
    } catch (final Exception ex) {
        WrappedAS2Exception.wrap(ex).terminate(aMsg);
    }
}
Also used : CompositeParameters(com.helger.as2lib.params.CompositeParameters) OutputStream(java.io.OutputStream) MessageParameters(com.helger.as2lib.params.MessageParameters) DateParameters(com.helger.as2lib.params.DateParameters) File(java.io.File) AS2Exception(com.helger.as2lib.exception.AS2Exception) WrappedAS2Exception(com.helger.as2lib.exception.WrappedAS2Exception) IOException(java.io.IOException)

Aggregations

CompositeParameters (com.helger.as2lib.params.CompositeParameters)5 DateParameters (com.helger.as2lib.params.DateParameters)5 AS2Exception (com.helger.as2lib.exception.AS2Exception)3 WrappedAS2Exception (com.helger.as2lib.exception.WrappedAS2Exception)3 MessageParameters (com.helger.as2lib.params.MessageParameters)3 IOException (java.io.IOException)3 IMessage (com.helger.as2lib.message.IMessage)2 File (java.io.File)2 IMessageMDN (com.helger.as2lib.message.IMessageMDN)1 AS2InvalidParameterException (com.helger.as2lib.params.AS2InvalidParameterException)1 MessageMDNParameters (com.helger.as2lib.params.MessageMDNParameters)1 RandomParameters (com.helger.as2lib.params.RandomParameters)1 Nonempty (com.helger.commons.annotation.Nonempty)1 CommonsHashMap (com.helger.commons.collection.impl.CommonsHashMap)1 FileIOError (com.helger.commons.io.file.FileIOError)1 FileInputStream (java.io.FileInputStream)1 ObjectInputStream (java.io.ObjectInputStream)1 OutputStream (java.io.OutputStream)1 Nonnull (javax.annotation.Nonnull)1