Search in sources :

Example 16 with PipeRunResult

use of nl.nn.adapterframework.core.PipeRunResult in project iaf by ibissource.

the class CompressPipe method doPipe.

public PipeRunResult doPipe(Object input, IPipeLineSession session) throws PipeRunException {
    try {
        Object result;
        InputStream in;
        OutputStream out;
        boolean zipMultipleFiles = false;
        if (messageIsContent) {
            if (input instanceof byte[]) {
                in = new ByteArrayInputStream((byte[]) input);
            } else {
                in = new ByteArrayInputStream(input.toString().getBytes());
            }
        } else {
            if (compress && StringUtils.contains((String) input, ";")) {
                zipMultipleFiles = true;
                in = null;
            } else {
                in = new FileInputStream((String) input);
            }
        }
        if (resultIsContent) {
            out = new ByteArrayOutputStream();
            result = out;
        } else {
            String outFilename = null;
            if (messageIsContent) {
                outFilename = FileUtils.getFilename(getParameterList(), session, (File) null, filenamePattern);
            } else {
                outFilename = FileUtils.getFilename(getParameterList(), session, new File((String) input), filenamePattern);
            }
            File outFile = new File(outputDirectory, outFilename);
            result = outFile.getAbsolutePath();
            out = new FileOutputStream(outFile);
        }
        if (zipMultipleFiles) {
            ZipOutputStream zipper = new ZipOutputStream(out);
            StringTokenizer st = new StringTokenizer((String) input, ";");
            while (st.hasMoreElements()) {
                String fn = st.nextToken();
                String zipEntryName = getZipEntryName(fn, session);
                zipper.putNextEntry(new ZipEntry(zipEntryName));
                in = new FileInputStream(fn);
                try {
                    int readLength = 0;
                    byte[] block = new byte[4096];
                    while ((readLength = in.read(block)) > 0) {
                        zipper.write(block, 0, readLength);
                    }
                } finally {
                    in.close();
                    zipper.closeEntry();
                }
            }
            zipper.close();
            out = zipper;
        } else {
            if (compress) {
                if ("gz".equals(fileFormat) || fileFormat == null && resultIsContent) {
                    out = new GZIPOutputStream(out);
                } else {
                    ZipOutputStream zipper = new ZipOutputStream(out);
                    String zipEntryName = getZipEntryName(input, session);
                    zipper.putNextEntry(new ZipEntry(zipEntryName));
                    out = zipper;
                }
            } else {
                if ("gz".equals(fileFormat) || fileFormat == null && messageIsContent) {
                    in = new GZIPInputStream(in);
                } else {
                    ZipInputStream zipper = new ZipInputStream(in);
                    String zipEntryName = getZipEntryName(input, session);
                    if (zipEntryName.equals("")) {
                        // Use first entry found
                        zipper.getNextEntry();
                    } else {
                        // Position the stream at the specified entry
                        ZipEntry zipEntry = zipper.getNextEntry();
                        while (zipEntry != null && !zipEntry.getName().equals(zipEntryName)) {
                            zipEntry = zipper.getNextEntry();
                        }
                    }
                    in = zipper;
                }
            }
            try {
                int readLength = 0;
                byte[] block = new byte[4096];
                while ((readLength = in.read(block)) > 0) {
                    out.write(block, 0, readLength);
                }
            } finally {
                out.close();
                in.close();
            }
        }
        return new PipeRunResult(getForward(), getResultMsg(result));
    } catch (Exception e) {
        PipeForward exceptionForward = findForward(EXCEPTIONFORWARD);
        if (exceptionForward != null) {
            log.warn(getLogPrefix(session) + "exception occured, forwarded to [" + exceptionForward.getPath() + "]", e);
            String originalMessage;
            if (input instanceof String) {
                originalMessage = (String) input;
            } else {
                originalMessage = "Object of type " + input.getClass().getName();
            }
            String resultmsg = new ErrorMessageFormatter().format(getLogPrefix(session), e, this, originalMessage, session.getMessageId(), 0);
            return new PipeRunResult(exceptionForward, resultmsg);
        }
        throw new PipeRunException(this, "Unexpected exception during compression", e);
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) ZipInputStream(java.util.zip.ZipInputStream) FileInputStream(java.io.FileInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ZipOutputStream(java.util.zip.ZipOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FileOutputStream(java.io.FileOutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) ZipEntry(java.util.zip.ZipEntry) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PipeForward(nl.nn.adapterframework.core.PipeForward) FileInputStream(java.io.FileInputStream) PipeRunException(nl.nn.adapterframework.core.PipeRunException) ParameterException(nl.nn.adapterframework.core.ParameterException) GZIPInputStream(java.util.zip.GZIPInputStream) PipeRunResult(nl.nn.adapterframework.core.PipeRunResult) StringTokenizer(java.util.StringTokenizer) ZipInputStream(java.util.zip.ZipInputStream) ErrorMessageFormatter(nl.nn.adapterframework.errormessageformatters.ErrorMessageFormatter) ByteArrayInputStream(java.io.ByteArrayInputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) PipeRunException(nl.nn.adapterframework.core.PipeRunException) File(java.io.File)

Example 17 with PipeRunResult

use of nl.nn.adapterframework.core.PipeRunResult in project iaf by ibissource.

the class DelayPipe method doPipe.

public PipeRunResult doPipe(Object input, IPipeLineSession session) throws PipeRunException {
    try {
        log.info(getLogPrefix(session) + "starts waiting for " + getDelayTime() + " ms.");
        Thread.sleep(getDelayTime());
    } catch (InterruptedException e) {
        throw new PipeRunException(this, getLogPrefix(session) + "delay interrupted", e);
    }
    log.info(getLogPrefix(session) + "ends waiting for " + getDelayTime() + " ms.");
    return new PipeRunResult(getForward(), input);
}
Also used : PipeRunResult(nl.nn.adapterframework.core.PipeRunResult) PipeRunException(nl.nn.adapterframework.core.PipeRunException)

Example 18 with PipeRunResult

use of nl.nn.adapterframework.core.PipeRunResult in project iaf by ibissource.

the class BytesOutputPipe method doPipe.

public PipeRunResult doPipe(Object input, IPipeLineSession session) throws PipeRunException {
    Object result = null;
    Variant in = new Variant(input);
    try {
        XMLReader parser = XMLReaderFactory.createXMLReader();
        FieldsContentHandler fieldsContentHandler = new FieldsContentHandler();
        parser.setContentHandler(fieldsContentHandler);
        parser.parse(in.asXmlInputSource());
        result = fieldsContentHandler.getResult();
    } catch (SAXException e) {
        throw new PipeRunException(this, "SAXException", e);
    } catch (IOException e) {
        throw new PipeRunException(this, "IOException", e);
    }
    return new PipeRunResult(getForward(), result);
}
Also used : Variant(nl.nn.adapterframework.util.Variant) PipeRunResult(nl.nn.adapterframework.core.PipeRunResult) PipeRunException(nl.nn.adapterframework.core.PipeRunException) IOException(java.io.IOException) XMLReader(org.xml.sax.XMLReader) SAXException(org.xml.sax.SAXException)

Example 19 with PipeRunResult

use of nl.nn.adapterframework.core.PipeRunResult in project iaf by ibissource.

the class ChecksumPipe method doPipe.

public PipeRunResult doPipe(Object input, IPipeLineSession session) throws PipeRunException {
    String message = (String) input;
    String result;
    try {
        ChecksumGenerator cg = createChecksumGenerator();
        if (isInputIsFile()) {
            byte[] barr = new byte[1000];
            FileInputStream fis = new FileInputStream((String) input);
            int c;
            while ((c = fis.read(barr)) >= 0) {
                cg.update(barr, c);
            }
        } else {
            byte[] barr;
            if (StringUtils.isEmpty(getCharset())) {
                barr = message.getBytes();
            } else {
                barr = message.getBytes(getCharset());
            }
            cg.update(barr, barr.length);
        }
        result = cg.getResult();
        return new PipeRunResult(getForward(), result);
    } catch (Exception e) {
        throw new PipeRunException(this, "cannot calculate [" + getType() + "]" + (isInputIsFile() ? " on file [" + input + "]" : " using charset [" + getCharset() + "]"), e);
    }
}
Also used : PipeRunResult(nl.nn.adapterframework.core.PipeRunResult) PipeRunException(nl.nn.adapterframework.core.PipeRunException) FileInputStream(java.io.FileInputStream) PipeRunException(nl.nn.adapterframework.core.PipeRunException) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 20 with PipeRunResult

use of nl.nn.adapterframework.core.PipeRunResult in project iaf by ibissource.

the class CleanupOldFilesPipe method doPipe.

public PipeRunResult doPipe(Object input, IPipeLineSession session) throws PipeRunException {
    try {
        String filename;
        if (StringUtils.isNotEmpty(getFilePattern())) {
            filename = FileUtils.getFilename(getParameterList(), session, "", getFilePattern());
        } else {
            if (StringUtils.isNotEmpty(getFilePatternSessionKey())) {
                filename = FileUtils.getFilename(getParameterList(), session, "", (String) session.get(getFilePatternSessionKey()));
            } else {
                if (StringUtils.isEmpty((String) input)) {
                    throw new PipeRunException(this, "input empty, but should contain filename to delete");
                } else {
                    File in = new File(input.toString());
                    filename = in.getName();
                }
            }
        }
        List delFiles = getFilesForDeletion(filename);
        if (delFiles != null && delFiles.size() > 0) {
            for (Iterator fileIt = delFiles.iterator(); fileIt.hasNext(); ) {
                File file = (File) fileIt.next();
                String curfilename = file.getName();
                if (file.delete()) {
                    log.info(getLogPrefix(session) + "deleted file [" + file.getAbsolutePath() + "]");
                } else {
                    log.warn(getLogPrefix(session) + "could not delete file [" + file.getAbsolutePath() + "]");
                }
            }
        } else {
            log.info(getLogPrefix(session) + "no files match pattern [" + filename + "]");
        }
        if (isDeleteEmptySubdirectories()) {
            File file = new File(filename);
            if (file.exists()) {
                deleteEmptySubdirectories(getLogPrefix(session), file, 0);
            }
        }
        return new PipeRunResult(getForward(), input);
    } catch (Exception e) {
        throw new PipeRunException(this, "Error while deleting file(s)", e);
    }
}
Also used : PipeRunResult(nl.nn.adapterframework.core.PipeRunResult) PipeRunException(nl.nn.adapterframework.core.PipeRunException) Iterator(java.util.Iterator) List(java.util.List) ArrayList(java.util.ArrayList) File(java.io.File) PipeRunException(nl.nn.adapterframework.core.PipeRunException)

Aggregations

PipeRunResult (nl.nn.adapterframework.core.PipeRunResult)89 PipeRunException (nl.nn.adapterframework.core.PipeRunException)65 PipeForward (nl.nn.adapterframework.core.PipeForward)23 ConfigurationException (nl.nn.adapterframework.configuration.ConfigurationException)22 ParameterResolutionContext (nl.nn.adapterframework.parameters.ParameterResolutionContext)21 IOException (java.io.IOException)17 ParameterException (nl.nn.adapterframework.core.ParameterException)14 ParameterValueList (nl.nn.adapterframework.parameters.ParameterValueList)11 Test (org.junit.Test)11 File (java.io.File)10 Map (java.util.Map)10 ParameterList (nl.nn.adapterframework.parameters.ParameterList)8 InputStream (java.io.InputStream)7 DomBuilderException (nl.nn.adapterframework.util.DomBuilderException)7 Parameter (nl.nn.adapterframework.parameters.Parameter)6 FileInputStream (java.io.FileInputStream)4 StringReader (java.io.StringReader)4 Iterator (java.util.Iterator)4 StringTokenizer (java.util.StringTokenizer)4 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)4