Search in sources :

Example 1 with FileNameCreator

use of com.adaptris.core.FileNameCreator in project interlok by adaptris.

the class FtpProducer method doProduce.

@Override
public void doProduce(AdaptrisMessage msg, String endpoint) throws ProduceException {
    FileTransferConnection conn = retrieveConnection(FileTransferConnection.class);
    FileTransferClient client = null;
    FileNameCreator creator = filenameCreatorToUse();
    try {
        client = conn.connect(endpoint);
        String dirRoot = conn.getDirectoryRoot(endpoint);
        String fileName = creator.createName(msg);
        String destFilename = dirRoot + destDirectory + SLASH + fileName;
        String buildFilename = dirRoot + buildDirectory + SLASH + fileName;
        if (conn.additionalDebug()) {
            log.trace("buildFilename=[{}], destFilename=[{}]", buildFilename, destFilename);
        } else {
            log.debug("destFilename=[{}]", destFilename);
        }
        msg.addMetadata(CoreConstants.PRODUCED_NAME_KEY, fileName);
        if (getEncoder() != null) {
            byte[] bytesToWrite = encode(msg);
            client.put(bytesToWrite, buildFilename);
        } else {
            try (InputStream in = msg.getInputStream()) {
                client.put(in, buildFilename);
            }
        }
        client.rename(buildFilename, destFilename);
    } catch (Exception e) {
        throw new ProduceException(e);
    } finally {
        conn.disconnect(client);
    }
}
Also used : FileNameCreator(com.adaptris.core.FileNameCreator) FileTransferClient(com.adaptris.filetransfer.FileTransferClient) InputStream(java.io.InputStream) ProduceException(com.adaptris.core.ProduceException) CoreException(com.adaptris.core.CoreException) ProduceException(com.adaptris.core.ProduceException)

Example 2 with FileNameCreator

use of com.adaptris.core.FileNameCreator in project interlok by adaptris.

the class FsProducer method doProduce.

@Override
@SuppressWarnings({ "lgtm[java/path-injection]" })
protected void doProduce(AdaptrisMessage msg, String baseUrl) throws ProduceException {
    FileNameCreator creator = filenameCreatorToUse();
    try {
        URL url = FsHelper.createUrlFromString(baseUrl, true);
        validateDir(url);
        File filetoWrite = new File(FsHelper.createFileReference(url), creator.createName(msg));
        addProducerMetadata(msg, filetoWrite);
        write(msg, filetoWrite);
        log.debug("msg produced to destination [{}]", url);
    } catch (Exception e) {
        throw new ProduceException(e);
    }
}
Also used : FileNameCreator(com.adaptris.core.FileNameCreator) File(java.io.File) URL(java.net.URL) ProduceException(com.adaptris.core.ProduceException) IOException(java.io.IOException) CoreException(com.adaptris.core.CoreException) ProduceException(com.adaptris.core.ProduceException)

Aggregations

CoreException (com.adaptris.core.CoreException)2 FileNameCreator (com.adaptris.core.FileNameCreator)2 ProduceException (com.adaptris.core.ProduceException)2 FileTransferClient (com.adaptris.filetransfer.FileTransferClient)1 File (java.io.File)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 URL (java.net.URL)1