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;
}
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());
}
}
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);
}
}
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);
}
}
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());
}
}
}
Aggregations