Search in sources :

Example 26 with PipeRunResult

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

the class GetPrincipalPipe method doPipe.

public PipeRunResult doPipe(Object input, IPipeLineSession session) throws PipeRunException {
    Principal principal = session.getPrincipal();
    String principalName = "";
    if (principal == null) {
        log.warn(getLogPrefix(session) + "no principal found");
    } else {
        try {
            principalName = principal.getName();
        } catch (Throwable e) {
            throw new PipeRunException(this, getLogPrefix(session) + "got exception getting name from principal", e);
        }
    }
    return new PipeRunResult(getForward(), principalName);
}
Also used : PipeRunResult(nl.nn.adapterframework.core.PipeRunResult) PipeRunException(nl.nn.adapterframework.core.PipeRunException) Principal(java.security.Principal)

Example 27 with PipeRunResult

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

the class Text2XmlPipe method doPipe.

/**
 * @see nl.nn.adapterframework.core.IPipe#doPipe(Object, IPipeLineSession)
 */
public PipeRunResult doPipe(Object input, IPipeLineSession session) throws PipeRunException {
    if (isSplitLines() && input != null) {
        try {
            Reader reader = new StringReader(input.toString());
            if (replaceNonXmlChars) {
                reader = new EncapsulatingReader(reader, "", "", true);
            }
            BufferedReader br = new BufferedReader(reader);
            String l;
            StringBuffer result = new StringBuffer();
            while ((l = br.readLine()) != null) {
                result.append("<line>" + addCdataSection(l) + "</line>");
            }
            input = result.toString();
            br.close();
        } catch (IOException e) {
            throw new PipeRunException(this, "Unexpected exception during splitting", e);
        }
    } else if (replaceNonXmlChars && input != null) {
        input = addCdataSection(XmlUtils.encodeCdataString(input.toString()));
    } else {
        input = addCdataSection((input == null ? null : input.toString()));
    }
    String resultString = (isIncludeXmlDeclaration() ? "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" : "") + "<" + getXmlTag() + ">" + input + "</" + xmlTag + ">";
    return new PipeRunResult(getForward(), resultString);
}
Also used : PipeRunResult(nl.nn.adapterframework.core.PipeRunResult) EncapsulatingReader(nl.nn.adapterframework.util.EncapsulatingReader) StringReader(java.io.StringReader) BufferedReader(java.io.BufferedReader) PipeRunException(nl.nn.adapterframework.core.PipeRunException) StringReader(java.io.StringReader) EncapsulatingReader(nl.nn.adapterframework.util.EncapsulatingReader) BufferedReader(java.io.BufferedReader) Reader(java.io.Reader) IOException(java.io.IOException)

Example 28 with PipeRunResult

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

the class TimeoutGuardPipe method doPipe.

public PipeRunResult doPipe(Object input, IPipeLineSession session) throws PipeRunException {
    ParameterValueList pvl = null;
    if (getParameterList() != null) {
        ParameterResolutionContext prc = new ParameterResolutionContext((String) input, session);
        try {
            pvl = prc.getValues(getParameterList());
        } catch (ParameterException e) {
            throw new PipeRunException(this, getLogPrefix(session) + "exception on extracting parameters", e);
        }
    }
    int timeout_work;
    String timeout_work_str = getParameterValue(pvl, "timeout");
    if (timeout_work_str == null) {
        timeout_work = getTimeout();
    } else {
        timeout_work = Integer.valueOf(timeout_work_str);
    }
    DoPipe doPipe = new DoPipe(input, session, Thread.currentThread().getName(), NDC.peek());
    ExecutorService service = Executors.newSingleThreadExecutor();
    Future future = service.submit(doPipe);
    String result = null;
    try {
        log.debug(getLogPrefix(session) + "setting timeout of [" + timeout_work + "] s");
        result = (String) future.get(timeout_work, TimeUnit.SECONDS);
    } catch (Exception e) {
        String msg;
        if (e instanceof TimeoutException) {
            String errorMsg = getLogPrefix(session) + "exceeds timeout of [" + timeout_work + "] s, interupting";
            future.cancel(true);
            msg = e.getClass().getName() + ": " + errorMsg;
        } else {
            msg = e.getClass().getName();
        }
        if (isThrowException()) {
            throw new PipeRunException(this, msg, e);
        } else {
            String msgString = msg + ": " + e.getMessage();
            log.error(msgString, e);
            String msgCdataString = "<![CDATA[" + msgString + "]]>";
            result = "<error>" + msgCdataString + "</error>";
        }
    } finally {
        service.shutdown();
    }
    return new PipeRunResult(getForward(), result);
}
Also used : PipeRunResult(nl.nn.adapterframework.core.PipeRunResult) ParameterValueList(nl.nn.adapterframework.parameters.ParameterValueList) PipeRunException(nl.nn.adapterframework.core.PipeRunException) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) ParameterException(nl.nn.adapterframework.core.ParameterException) ParameterResolutionContext(nl.nn.adapterframework.parameters.ParameterResolutionContext) PipeRunException(nl.nn.adapterframework.core.PipeRunException) TimeoutException(java.util.concurrent.TimeoutException) ParameterException(nl.nn.adapterframework.core.ParameterException) TimeoutException(java.util.concurrent.TimeoutException)

Example 29 with PipeRunResult

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

the class UnzipPipe method doPipe.

public PipeRunResult doPipe(Object input, IPipeLineSession session) throws PipeRunException {
    InputStream in;
    if (input instanceof InputStream) {
        in = (InputStream) input;
    } else {
        String filename = (String) input;
        try {
            in = new FileInputStream(filename);
        } catch (FileNotFoundException e) {
            throw new PipeRunException(this, "could not find file [" + filename + "]", e);
        }
    }
    if (StringUtils.isEmpty(getDirectory())) {
        String directory = (String) session.get(getDirectorySessionKey());
        if (StringUtils.isEmpty(directory))
            throw new PipeRunException(this, "directorySessionKey is empty");
        dir = new File(directory);
        if (!dir.exists()) {
            throw new PipeRunException(this, "directorySessionKey [" + directory + "] does not exist");
        }
        if (!dir.isDirectory()) {
            throw new PipeRunException(this, "directorySessionKey [" + directory + "] is not a directory");
        }
    }
    String entryResults = "";
    int count = 0;
    ZipInputStream zis = new ZipInputStream(new BufferedInputStream(in));
    try {
        ZipEntry ze;
        while ((ze = zis.getNextEntry()) != null) {
            if (ze.isDirectory()) {
                if (isCreateSubdirectories()) {
                    File tmpFile = new File(dir, ze.getName());
                    if (!tmpFile.exists()) {
                        if (tmpFile.mkdirs()) {
                            log.debug(getLogPrefix(session) + "created directory [" + tmpFile.getPath() + "]");
                        } else {
                            log.warn(getLogPrefix(session) + "directory [" + tmpFile.getPath() + "] could not be created");
                        }
                    } else {
                        log.debug(getLogPrefix(session) + "directory entry [" + tmpFile.getPath() + "] already exists");
                    }
                } else {
                    log.warn(getLogPrefix(session) + "skipping directory entry [" + ze.getName() + "]");
                }
            } else {
                String filename = ze.getName();
                String basename = null;
                String extension = null;
                int dotPos = filename.indexOf('.');
                if (dotPos >= 0) {
                    extension = filename.substring(dotPos);
                    basename = filename.substring(0, dotPos);
                    log.debug(getLogPrefix(session) + "parsed filename [" + basename + "] extension [" + extension + "]");
                } else {
                    basename = filename;
                }
                File tmpFile;
                if (isKeepOriginalFileName()) {
                    tmpFile = new File(dir, filename);
                    if (tmpFile.exists()) {
                        throw new PipeRunException(this, "file [" + tmpFile.getAbsolutePath() + "] already exists");
                    }
                } else {
                    tmpFile = File.createTempFile(basename, extension, dir);
                }
                if (isDeleteOnExit()) {
                    tmpFile.deleteOnExit();
                }
                if (isCreateSubdirectories()) {
                    // extra check
                    File tmpDir = tmpFile.getParentFile();
                    if (!tmpDir.exists()) {
                        if (tmpDir.mkdirs()) {
                            log.debug(getLogPrefix(session) + "created directory [" + tmpDir.getPath() + "]");
                        } else {
                            log.warn(getLogPrefix(session) + "directory [" + tmpDir.getPath() + "] could not be created");
                        }
                    }
                }
                FileOutputStream fos = new FileOutputStream(tmpFile);
                log.debug(getLogPrefix(session) + "writing ZipEntry [" + ze.getName() + "] to file [" + tmpFile.getPath() + "]");
                count++;
                Misc.streamToStream(zis, fos, false);
                fos.close();
                if (isCollectResults()) {
                    entryResults += "<result item=\"" + count + "\"><zipEntry>" + XmlUtils.encodeCdataString(ze.getName()) + "</zipEntry><fileName>" + XmlUtils.encodeCdataString(tmpFile.getPath()) + "</fileName></result>";
                }
            }
        }
    } catch (IOException e) {
        throw new PipeRunException(this, "cannot unzip", e);
    } finally {
        try {
            zis.close();
        } catch (IOException e1) {
            log.warn(getLogPrefix(session) + "exception closing zip", e1);
        }
    }
    String result = "<results count=\"" + count + "\">" + entryResults + "</results>";
    return new PipeRunResult(getForward(), result);
}
Also used : BufferedInputStream(java.io.BufferedInputStream) ZipInputStream(java.util.zip.ZipInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) PipeRunResult(nl.nn.adapterframework.core.PipeRunResult) ZipInputStream(java.util.zip.ZipInputStream) BufferedInputStream(java.io.BufferedInputStream) FileOutputStream(java.io.FileOutputStream) PipeRunException(nl.nn.adapterframework.core.PipeRunException) File(java.io.File)

Example 30 with PipeRunResult

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

the class UploadFilePipe method doPipe.

public PipeRunResult doPipe(Object input, IPipeLineSession session) throws PipeRunException {
    InputStream inputStream = (InputStream) session.get(getSessionKey());
    if (inputStream == null) {
        throw new PipeRunException(this, getLogPrefix(session) + "got null value from session under key [" + getSessionKey() + "]");
    }
    File dir;
    if (StringUtils.isNotEmpty(getDirectory())) {
        dir = new File(getDirectory());
    } else {
        if (StringUtils.isNotEmpty(getDirectorySessionKey())) {
            dir = new File((String) session.get(getDirectorySessionKey()));
        } else {
            dir = new File(input.toString());
        }
    }
    if (!dir.exists()) {
        if (dir.mkdirs()) {
            log.debug(getLogPrefix(session) + "created directory [" + dir.getPath() + "]");
        } else {
            log.warn(getLogPrefix(session) + "directory [" + dir.getPath() + "] could not be created");
        }
    }
    String fileName;
    try {
        fileName = (String) session.get("fileName");
        if (FileUtils.extensionEqualsIgnoreCase(fileName, "zip")) {
            FileUtils.unzipStream(inputStream, dir);
        } else {
            throw new PipeRunException(this, getLogPrefix(session) + "file extension [" + FileUtils.getFileNameExtension(fileName) + "] should be 'zip'");
        }
    } catch (IOException e) {
        throw new PipeRunException(this, getLogPrefix(session) + " Exception on uploading and unzipping/writing file", e);
    }
    return new PipeRunResult(getForward(), dir.getPath());
}
Also used : PipeRunResult(nl.nn.adapterframework.core.PipeRunResult) InputStream(java.io.InputStream) PipeRunException(nl.nn.adapterframework.core.PipeRunException) IOException(java.io.IOException) File(java.io.File)

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