Search in sources :

Example 26 with ListenerException

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

the class FileListener method getMessage.

/**
 * Read the message from the specified file. If the file doesn't exist,
 * this methods waits a specified time before it attempts to read the file.
 *
 * @return
 * @throws TimeOutException
 * @throws ListenerException
 */
public String getMessage() throws TimeOutException, ListenerException {
    String result = null;
    if (waitBeforeRead != -1) {
        try {
            Thread.sleep(waitBeforeRead);
        } catch (InterruptedException e) {
            throw new ListenerException("Exception waiting before reading the file: " + e.getMessage(), e);
        }
    }
    File file = null;
    if (filename == null) {
        File[] files = FileUtils.getFiles(directory, wildcard, null, 0);
        if (files.length > 0) {
            file = files[0];
        }
    } else {
        file = new File(filename);
    }
    if (filename2 != null) {
        try {
            File file2 = new File(filename2);
            boolean equal = FileUtils.isFileBinaryEqual(file, file2);
            result = Boolean.toString(equal);
        } catch (IOException e) {
            throw new ListenerException("Exception comparing files '" + filename + "' and '" + filename2 + "': " + e.getMessage(), e);
        }
    } else {
        long startTime = System.currentTimeMillis();
        while ((file == null || !file.exists()) && System.currentTimeMillis() < startTime + timeOut) {
            try {
                Thread.sleep(interval);
            } catch (InterruptedException e) {
                throw new ListenerException("Exception waiting for file: " + e.getMessage(), e);
            }
            if (filename == null) {
                File[] files = FileUtils.getFiles(directory, wildcard, null, 0);
                if (files.length > 0) {
                    file = files[0];
                }
            }
        }
        if (file != null && file.exists()) {
            StringBuffer stringBuffer = new StringBuffer();
            FileInputStream fileInputStream = null;
            try {
                fileInputStream = new FileInputStream(file);
            } catch (IOException e) {
                throw new ListenerException("Exception opening file '" + file.getAbsolutePath() + "': " + e.getMessage(), e);
            }
            byte[] buffer = new byte[1024];
            try {
                int length = fileInputStream.read(buffer);
                while (length != -1) {
                    stringBuffer.append(new String(buffer, 0, length, "UTF-8"));
                    length = fileInputStream.read(buffer);
                }
            } catch (IOException e) {
                try {
                    fileInputStream.close();
                } catch (Exception e2) {
                }
                throw new ListenerException("Exception reading file '" + file.getAbsolutePath() + "': " + e.getMessage(), e);
            }
            try {
                fileInputStream.close();
            } catch (IOException e) {
                throw new ListenerException("Exception closing file '" + file.getAbsolutePath() + "': " + e.getMessage(), e);
            }
            result = stringBuffer.toString();
            if (!file.delete()) {
                throw new ListenerException("Could not delete file '" + file.getAbsolutePath() + "'.");
            }
        } else {
            throw new TimeOutException("Time out waiting for file.");
        }
    }
    return result;
}
Also used : IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) TimeOutException(nl.nn.adapterframework.core.TimeOutException) ListenerException(nl.nn.adapterframework.core.ListenerException) ListenerException(nl.nn.adapterframework.core.ListenerException) TimeOutException(nl.nn.adapterframework.core.TimeOutException) File(java.io.File)

Example 27 with ListenerException

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

the class HttpListenerServlet method invoke.

public void invoke(String message, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    ISecurityHandler securityHandler = new HttpSecurityHandler(request);
    IPipeLineSession messageContext = new PipeLineSessionBase();
    messageContext.put(IPipeLineSession.securityHandlerKey, securityHandler);
    messageContext.put("httpListenerServletRequest", request);
    messageContext.put("httpListenerServletResponse", response);
    String service = request.getParameter(SERVICE_ID_PARAM);
    Enumeration paramnames = request.getParameterNames();
    while (paramnames.hasMoreElements()) {
        String paramname = (String) paramnames.nextElement();
        String paramvalue = request.getParameter(paramname);
        if (log.isDebugEnabled()) {
            log.debug("HttpListenerServlet setting parameter [" + paramname + "] to [" + paramvalue + "]");
        }
        messageContext.put(paramname, paramvalue);
    }
    try {
        log.debug("HttpListenerServlet calling service [" + service + "]");
        String result = sd.dispatchRequest(service, null, message, messageContext);
        response.getWriter().print(result);
    } catch (ListenerException e) {
        log.warn("HttpListenerServlet caught exception, will rethrow as ServletException", e);
        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage());
    }
}
Also used : ISecurityHandler(nl.nn.adapterframework.core.ISecurityHandler) ListenerException(nl.nn.adapterframework.core.ListenerException) Enumeration(java.util.Enumeration) IPipeLineSession(nl.nn.adapterframework.core.IPipeLineSession) PipeLineSessionBase(nl.nn.adapterframework.core.PipeLineSessionBase)

Example 28 with ListenerException

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

the class FtpListener method waitAWhile.

private void waitAWhile() throws ListenerException {
    try {
        log.debug("FtpListener " + getName() + " starts waiting [" + responseTime + "] ms in chunks of [" + localResponseTime + "] ms");
        long timeWaited;
        for (timeWaited = 0; canGoOn() && timeWaited + localResponseTime < responseTime; timeWaited += localResponseTime) {
            Thread.sleep(localResponseTime);
        }
        if (canGoOn() && responseTime - timeWaited > 0) {
            Thread.sleep(responseTime - timeWaited);
        }
    } catch (InterruptedException e) {
        throw new ListenerException("Interrupted while listening", e);
    }
}
Also used : ListenerException(nl.nn.adapterframework.core.ListenerException)

Example 29 with ListenerException

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

the class MqttListener method open.

public void open() throws ListenerException {
    try {
        super.open();
        client.subscribe(getTopic(), getQos());
    } catch (Exception e) {
        e.printStackTrace();
        throw new ListenerException("Could not subscribe to topic", e);
    }
}
Also used : ListenerException(nl.nn.adapterframework.core.ListenerException) MqttException(org.eclipse.paho.client.mqttv3.MqttException) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ListenerException(nl.nn.adapterframework.core.ListenerException)

Example 30 with ListenerException

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

the class FxfListener method afterMessageProcessed.

public void afterMessageProcessed(PipeLineResult plr, Object rawMessage, Map threadContext) throws ListenerException {
    super.afterMessageProcessed(plr, rawMessage, threadContext);
    if (isMoveProcessedFile() && plr.getState().equalsIgnoreCase("success")) {
        File srcFile = null;
        File dstFile = null;
        try {
            String srcFileName = (String) threadContext.get(getFxfFileSessionKey());
            if (StringUtils.isEmpty(srcFileName)) {
                warn("No file to move");
            } else {
                srcFile = new File(srcFileName);
                if (!srcFile.exists()) {
                    warn("File [" + srcFileName + "] does not exist");
                } else {
                    File srcDir = srcFile.getParentFile();
                    String dstDirName = srcDir.getParent() + File.separator + getProcessedSiblingDirectory();
                    dstFile = new File(dstDirName, srcFile.getName());
                    dstFile = FileUtils.getFreeFile(dstFile);
                    if (!dstFile.getParentFile().exists()) {
                        if (isCreateProcessedDirectory()) {
                            if (dstFile.getParentFile().mkdirs()) {
                                log.debug("Created directory [" + dstFile.getParent() + "]");
                            } else {
                                log.warn("Directory [" + dstFile.getParent() + "] could not be created");
                            }
                        } else {
                            log.warn("Directory [" + dstFile.getParent() + "] does not exist");
                        }
                    }
                    if (FileUtils.moveFile(srcFile, dstFile, 1, 0) == null) {
                        warn("Could not move file [" + srcFile.getAbsolutePath() + "] to file [" + dstFile.getAbsolutePath() + "]");
                    } else {
                        log.info("Moved file [" + srcFile.getAbsolutePath() + "] to file [" + dstFile.getAbsolutePath() + "]");
                    }
                }
            }
        } catch (Exception e) {
            warn("Error while moving file [" + srcFile.getAbsolutePath() + "] to file [" + dstFile.getAbsolutePath() + "]: " + e.getMessage());
        }
    }
}
Also used : File(java.io.File) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) ListenerException(nl.nn.adapterframework.core.ListenerException)

Aggregations

ListenerException (nl.nn.adapterframework.core.ListenerException)84 ConfigurationException (nl.nn.adapterframework.configuration.ConfigurationException)37 IOException (java.io.IOException)19 HashMap (java.util.HashMap)18 Map (java.util.Map)18 JMSException (javax.jms.JMSException)16 SenderException (nl.nn.adapterframework.core.SenderException)14 SQLException (java.sql.SQLException)13 PreparedStatement (java.sql.PreparedStatement)11 Session (javax.jms.Session)10 LinkedHashMap (java.util.LinkedHashMap)9 IPipeLineSession (nl.nn.adapterframework.core.IPipeLineSession)9 TimeOutException (nl.nn.adapterframework.core.TimeOutException)9 IfsaException (nl.nn.adapterframework.extensions.ifsa.IfsaException)9 Connection (java.sql.Connection)8 ResultSet (java.sql.ResultSet)8 QueueSession (javax.jms.QueueSession)8 File (java.io.File)7 MessageConsumer (javax.jms.MessageConsumer)7 Message (javax.jms.Message)6