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