Search in sources :

Example 31 with PipeRunException

use of nl.nn.adapterframework.core.PipeRunException 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 32 with PipeRunException

use of nl.nn.adapterframework.core.PipeRunException 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 33 with PipeRunException

use of nl.nn.adapterframework.core.PipeRunException 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 34 with PipeRunException

use of nl.nn.adapterframework.core.PipeRunException 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 35 with PipeRunException

use of nl.nn.adapterframework.core.PipeRunException 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

PipeRunException (nl.nn.adapterframework.core.PipeRunException)101 PipeRunResult (nl.nn.adapterframework.core.PipeRunResult)65 ConfigurationException (nl.nn.adapterframework.configuration.ConfigurationException)40 IOException (java.io.IOException)34 ParameterResolutionContext (nl.nn.adapterframework.parameters.ParameterResolutionContext)32 PipeForward (nl.nn.adapterframework.core.PipeForward)20 ParameterException (nl.nn.adapterframework.core.ParameterException)16 ParameterValueList (nl.nn.adapterframework.parameters.ParameterValueList)13 Parameter (nl.nn.adapterframework.parameters.Parameter)12 InputStream (java.io.InputStream)10 File (java.io.File)9 Map (java.util.Map)9 ParameterList (nl.nn.adapterframework.parameters.ParameterList)8 FixedQuerySender (nl.nn.adapterframework.jdbc.FixedQuerySender)7 DomBuilderException (nl.nn.adapterframework.util.DomBuilderException)7 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)6 PipeStartException (nl.nn.adapterframework.core.PipeStartException)6 ArrayList (java.util.ArrayList)5 ZipInputStream (java.util.zip.ZipInputStream)5 IAdapter (nl.nn.adapterframework.core.IAdapter)5