use of nl.nn.adapterframework.core.PipeRunResult in project iaf by ibissource.
the class CompressPipe method doPipe.
public PipeRunResult doPipe(Object input, IPipeLineSession session) throws PipeRunException {
try {
Object result;
InputStream in;
OutputStream out;
boolean zipMultipleFiles = false;
if (messageIsContent) {
if (input instanceof byte[]) {
in = new ByteArrayInputStream((byte[]) input);
} else {
in = new ByteArrayInputStream(input.toString().getBytes());
}
} else {
if (compress && StringUtils.contains((String) input, ";")) {
zipMultipleFiles = true;
in = null;
} else {
in = new FileInputStream((String) input);
}
}
if (resultIsContent) {
out = new ByteArrayOutputStream();
result = out;
} else {
String outFilename = null;
if (messageIsContent) {
outFilename = FileUtils.getFilename(getParameterList(), session, (File) null, filenamePattern);
} else {
outFilename = FileUtils.getFilename(getParameterList(), session, new File((String) input), filenamePattern);
}
File outFile = new File(outputDirectory, outFilename);
result = outFile.getAbsolutePath();
out = new FileOutputStream(outFile);
}
if (zipMultipleFiles) {
ZipOutputStream zipper = new ZipOutputStream(out);
StringTokenizer st = new StringTokenizer((String) input, ";");
while (st.hasMoreElements()) {
String fn = st.nextToken();
String zipEntryName = getZipEntryName(fn, session);
zipper.putNextEntry(new ZipEntry(zipEntryName));
in = new FileInputStream(fn);
try {
int readLength = 0;
byte[] block = new byte[4096];
while ((readLength = in.read(block)) > 0) {
zipper.write(block, 0, readLength);
}
} finally {
in.close();
zipper.closeEntry();
}
}
zipper.close();
out = zipper;
} else {
if (compress) {
if ("gz".equals(fileFormat) || fileFormat == null && resultIsContent) {
out = new GZIPOutputStream(out);
} else {
ZipOutputStream zipper = new ZipOutputStream(out);
String zipEntryName = getZipEntryName(input, session);
zipper.putNextEntry(new ZipEntry(zipEntryName));
out = zipper;
}
} else {
if ("gz".equals(fileFormat) || fileFormat == null && messageIsContent) {
in = new GZIPInputStream(in);
} else {
ZipInputStream zipper = new ZipInputStream(in);
String zipEntryName = getZipEntryName(input, session);
if (zipEntryName.equals("")) {
// Use first entry found
zipper.getNextEntry();
} else {
// Position the stream at the specified entry
ZipEntry zipEntry = zipper.getNextEntry();
while (zipEntry != null && !zipEntry.getName().equals(zipEntryName)) {
zipEntry = zipper.getNextEntry();
}
}
in = zipper;
}
}
try {
int readLength = 0;
byte[] block = new byte[4096];
while ((readLength = in.read(block)) > 0) {
out.write(block, 0, readLength);
}
} finally {
out.close();
in.close();
}
}
return new PipeRunResult(getForward(), getResultMsg(result));
} catch (Exception e) {
PipeForward exceptionForward = findForward(EXCEPTIONFORWARD);
if (exceptionForward != null) {
log.warn(getLogPrefix(session) + "exception occured, forwarded to [" + exceptionForward.getPath() + "]", e);
String originalMessage;
if (input instanceof String) {
originalMessage = (String) input;
} else {
originalMessage = "Object of type " + input.getClass().getName();
}
String resultmsg = new ErrorMessageFormatter().format(getLogPrefix(session), e, this, originalMessage, session.getMessageId(), 0);
return new PipeRunResult(exceptionForward, resultmsg);
}
throw new PipeRunException(this, "Unexpected exception during compression", e);
}
}
use of nl.nn.adapterframework.core.PipeRunResult in project iaf by ibissource.
the class DelayPipe method doPipe.
public PipeRunResult doPipe(Object input, IPipeLineSession session) throws PipeRunException {
try {
log.info(getLogPrefix(session) + "starts waiting for " + getDelayTime() + " ms.");
Thread.sleep(getDelayTime());
} catch (InterruptedException e) {
throw new PipeRunException(this, getLogPrefix(session) + "delay interrupted", e);
}
log.info(getLogPrefix(session) + "ends waiting for " + getDelayTime() + " ms.");
return new PipeRunResult(getForward(), input);
}
use of nl.nn.adapterframework.core.PipeRunResult in project iaf by ibissource.
the class BytesOutputPipe method doPipe.
public PipeRunResult doPipe(Object input, IPipeLineSession session) throws PipeRunException {
Object result = null;
Variant in = new Variant(input);
try {
XMLReader parser = XMLReaderFactory.createXMLReader();
FieldsContentHandler fieldsContentHandler = new FieldsContentHandler();
parser.setContentHandler(fieldsContentHandler);
parser.parse(in.asXmlInputSource());
result = fieldsContentHandler.getResult();
} catch (SAXException e) {
throw new PipeRunException(this, "SAXException", e);
} catch (IOException e) {
throw new PipeRunException(this, "IOException", e);
}
return new PipeRunResult(getForward(), result);
}
use of nl.nn.adapterframework.core.PipeRunResult in project iaf by ibissource.
the class ChecksumPipe method doPipe.
public PipeRunResult doPipe(Object input, IPipeLineSession session) throws PipeRunException {
String message = (String) input;
String result;
try {
ChecksumGenerator cg = createChecksumGenerator();
if (isInputIsFile()) {
byte[] barr = new byte[1000];
FileInputStream fis = new FileInputStream((String) input);
int c;
while ((c = fis.read(barr)) >= 0) {
cg.update(barr, c);
}
} else {
byte[] barr;
if (StringUtils.isEmpty(getCharset())) {
barr = message.getBytes();
} else {
barr = message.getBytes(getCharset());
}
cg.update(barr, barr.length);
}
result = cg.getResult();
return new PipeRunResult(getForward(), result);
} catch (Exception e) {
throw new PipeRunException(this, "cannot calculate [" + getType() + "]" + (isInputIsFile() ? " on file [" + input + "]" : " using charset [" + getCharset() + "]"), e);
}
}
use of nl.nn.adapterframework.core.PipeRunResult in project iaf by ibissource.
the class CleanupOldFilesPipe method doPipe.
public PipeRunResult doPipe(Object input, IPipeLineSession session) throws PipeRunException {
try {
String filename;
if (StringUtils.isNotEmpty(getFilePattern())) {
filename = FileUtils.getFilename(getParameterList(), session, "", getFilePattern());
} else {
if (StringUtils.isNotEmpty(getFilePatternSessionKey())) {
filename = FileUtils.getFilename(getParameterList(), session, "", (String) session.get(getFilePatternSessionKey()));
} else {
if (StringUtils.isEmpty((String) input)) {
throw new PipeRunException(this, "input empty, but should contain filename to delete");
} else {
File in = new File(input.toString());
filename = in.getName();
}
}
}
List delFiles = getFilesForDeletion(filename);
if (delFiles != null && delFiles.size() > 0) {
for (Iterator fileIt = delFiles.iterator(); fileIt.hasNext(); ) {
File file = (File) fileIt.next();
String curfilename = file.getName();
if (file.delete()) {
log.info(getLogPrefix(session) + "deleted file [" + file.getAbsolutePath() + "]");
} else {
log.warn(getLogPrefix(session) + "could not delete file [" + file.getAbsolutePath() + "]");
}
}
} else {
log.info(getLogPrefix(session) + "no files match pattern [" + filename + "]");
}
if (isDeleteEmptySubdirectories()) {
File file = new File(filename);
if (file.exists()) {
deleteEmptySubdirectories(getLogPrefix(session), file, 0);
}
}
return new PipeRunResult(getForward(), input);
} catch (Exception e) {
throw new PipeRunException(this, "Error while deleting file(s)", e);
}
}
Aggregations