use of nl.nn.adapterframework.stream.MessageOutputStream in project iaf by ibissource.
the class IbisDebuggerAdvice method debugProvideOutputStream.
/**
* Provides advice for {@link IOutputStreamingSupport#provideOutputStream(PipeLineSession session, IForwardTarget next)}
*/
public MessageOutputStream debugProvideOutputStream(ProceedingJoinPoint proceedingJoinPoint, PipeLineSession session) throws Throwable {
if (!isEnabled()) {
return (MessageOutputStream) proceedingJoinPoint.proceed();
}
String correlationId = session == null ? null : session.getMessageId();
if (log.isDebugEnabled())
log.debug("debugProvideOutputStream thread id [" + Thread.currentThread().getId() + "] thread name [" + Thread.currentThread().getName() + "] correlationId [" + correlationId + "]");
if (proceedingJoinPoint.getTarget() instanceof ISender) {
ISender sender = (ISender) proceedingJoinPoint.getTarget();
// Use WriterPlaceHolder to make the contents that is later written to the MessageOutputStream appear as input of the Sender
WriterPlaceHolder writerPlaceHolder = ibisDebugger.senderInput(sender, correlationId, new WriterPlaceHolder());
MessageOutputStream resultStream = (MessageOutputStream) proceedingJoinPoint.proceed();
String resultMessage = handleMessageOutputStream(writerPlaceHolder, resultStream);
ibisDebugger.senderOutput(sender, correlationId, resultMessage);
return resultStream;
}
if (proceedingJoinPoint.getTarget() instanceof IPipe) {
IPipe pipe = (IPipe) proceedingJoinPoint.getTarget();
PipeLine pipeLine = pipe instanceof AbstractPipe ? ((AbstractPipe) pipe).getPipeLine() : new PipeLine();
// Use WriterPlaceHolder to make the contents that is later written to the MessageOutputStream appear as input of the Pipe
WriterPlaceHolder writerPlaceHolder = ibisDebugger.pipeInput(pipeLine, pipe, correlationId, new WriterPlaceHolder());
MessageOutputStream resultStream = (MessageOutputStream) proceedingJoinPoint.proceed();
String resultMessage = handleMessageOutputStream(writerPlaceHolder, resultStream);
ibisDebugger.pipeOutput(pipeLine, pipe, correlationId, resultMessage);
return resultStream;
}
log.warn("Could not identify outputstream provider [" + proceedingJoinPoint.getTarget().getClass().getName() + "] as pipe or sender");
return (MessageOutputStream) proceedingJoinPoint.proceed();
}
use of nl.nn.adapterframework.stream.MessageOutputStream in project iaf by ibissource.
the class Text2XmlPipe method doPipe.
@Override
public PipeRunResult doPipe(Message message, PipeLineSession session) throws PipeRunException {
if (message.asObject() == null) {
return new PipeRunResult(getSuccessForward(), new Message("<" + getXmlTag() + " nil=\"true\" />"));
} else if (message.isEmpty() && isUseCdataSection()) {
return new PipeRunResult(getSuccessForward(), new Message("<" + getXmlTag() + "><![CDATA[]]></" + getXmlTag() + ">"));
}
try (MessageOutputStream target = getTargetStream(session)) {
ContentHandler handler = target.asContentHandler();
try {
handler.startDocument();
handler.startElement("", getXmlTag(), getXmlTag(), new AttributesImpl());
try (BufferedReader reader = new BufferedReader(message.asReader())) {
String line;
boolean lineWritten = false;
while ((line = reader.readLine()) != null) {
if (lineWritten) {
handler.characters("\n".toCharArray(), 0, "\n".length());
}
if (isSplitLines()) {
handler.startElement("", SPLITTED_LINE_TAG, SPLITTED_LINE_TAG, new AttributesImpl());
}
if (isUseCdataSection()) {
((LexicalHandler) handler).startCDATA();
}
line = isReplaceNonXmlChars() ? XmlUtils.encodeCdataString(line) : line;
handler.characters(line.toCharArray(), 0, line.length());
lineWritten = true;
if (isUseCdataSection()) {
((LexicalHandler) handler).endCDATA();
}
if (isSplitLines()) {
handler.endElement("", SPLITTED_LINE_TAG, SPLITTED_LINE_TAG);
}
}
}
handler.endElement("", getXmlTag(), getXmlTag());
} finally {
handler.endDocument();
}
return target.getPipeRunResult();
} catch (Exception e) {
throw new PipeRunException(this, "Unexpected exception during splitting", e);
}
}
use of nl.nn.adapterframework.stream.MessageOutputStream in project iaf by ibissource.
the class TextSplitterPipe method doPipe.
@Override
public PipeRunResult doPipe(Message message, PipeLineSession session) throws PipeRunException {
try {
String[] result = new String[100];
int p, s, o = 0;
String inputString = message.asString();
if (softSplit) {
for (p = 0; p < inputString.length() - maxBlockLength; ) {
// find last space in msg part
for (s = p + maxBlockLength >= inputString.length() ? inputString.length() - 1 : p + maxBlockLength; s >= p && !Character.isWhitespace(inputString.charAt(s)) && inputString.charAt(s) != '-'; s--) ;
// now skip spaces
for (; s >= p && Character.isWhitespace(inputString.charAt(s)); s--) ;
// spaces found, soft break possible
if (s >= p) {
result[o++] = inputString.substring(p, s + 1);
for (p = s + 1; p < inputString.length() && Character.isWhitespace(inputString.charAt(p)); p++) ;
} else // no space found, soft-break not possible
{
result[o++] = inputString.substring(p, p + maxBlockLength < inputString.length() ? p + maxBlockLength : inputString.length());
p += maxBlockLength;
}
}
result[o++] = inputString.substring(p);
} else {
for (p = 0; p < inputString.length(); p += maxBlockLength) {
if (p + maxBlockLength <= inputString.length()) {
result[o++] = inputString.substring(p, p + maxBlockLength);
} else {
result[o++] = inputString.substring(p);
}
}
}
try (MessageOutputStream target = getTargetStream(session)) {
try (SaxDocumentBuilder saxBuilder = new SaxDocumentBuilder("text", target.asContentHandler())) {
for (int counter = 0; result[counter] != null; counter++) {
saxBuilder.addElement("block", result[counter]);
}
}
return target.getPipeRunResult();
}
} catch (Exception e) {
throw new PipeRunException(this, "Cannot create text blocks", e);
}
}
use of nl.nn.adapterframework.stream.MessageOutputStream in project iaf by ibissource.
the class MessageSendingPipe method provideOutputStream.
@Override
protected MessageOutputStream provideOutputStream(PipeLineSession session) throws StreamingException {
MessageOutputStream result = null;
if (sender instanceof IOutputStreamingSupport) {
// TODO insert output validator
// TODO insert output wrapper
IOutputStreamingSupport streamingSender = (IOutputStreamingSupport) sender;
result = streamingSender.provideOutputStream(session, getNextPipe());
// TODO insert input wrapper
// TODO insert input validator
}
return result;
}
use of nl.nn.adapterframework.stream.MessageOutputStream in project iaf by ibissource.
the class IteratingPipe method sendMessage.
@Override
protected PipeRunResult sendMessage(Message input, PipeLineSession session, ISender sender, Map<String, Object> threadContext) throws SenderException, TimeoutException, IOException {
// sendResult has a messageID for async senders, the result for sync senders
StopReason stopReason = null;
try (MessageOutputStream target = getTargetStream(session)) {
try (Writer resultWriter = target.asWriter()) {
ItemCallback callback = createItemCallBack(session, sender, resultWriter);
stopReason = iterateOverInput(input, session, threadContext, callback);
}
PipeRunResult prr = target.getPipeRunResult();
if (stopReason != null) {
PipeForward forward = getForwards().get(stopReason.getForwardName());
if (forward != null) {
prr.setPipeForward(forward);
}
}
return prr;
} catch (SenderException | TimeoutException | IOException e) {
throw e;
} catch (Exception e) {
throw new SenderException(getLogPrefix(session) + "Exception on transforming input", e);
}
}
Aggregations