Search in sources :

Example 21 with Message

use of nl.nn.adapterframework.stream.Message in project iaf by ibissource.

the class CmisDeleteAction method objectNotFound.

@Test
public void objectNotFound() throws Exception {
    sender.setBindingType("browser");
    sender.setAction("delete");
    sender.setResultOnNotFound("document-not-found");
    sender.configure();
    Message result = sendMessage(NOT_FOUND_SELECTOR);
    assertThat(result.asString(), Matchers.startsWith("document-not-found"));
}
Also used : Message(nl.nn.adapterframework.stream.Message) Test(org.junit.Test)

Example 22 with Message

use of nl.nn.adapterframework.stream.Message in project iaf by ibissource.

the class Adapter method processMessageWithExceptions.

@Override
public PipeLineResult processMessageWithExceptions(String messageId, Message message, PipeLineSession pipeLineSession) throws ListenerException {
    PipeLineResult result = new PipeLineResult();
    long startTime = System.currentTimeMillis();
    boolean processingSuccess = true;
    // prevent executing a stopped adapter
    // the receivers should implement this, but you never now....
    RunState currentRunState = getRunState();
    if (currentRunState != RunState.STARTED && currentRunState != RunState.STOPPING) {
        String msgAdapterNotOpen = "Adapter [" + getName() + "] in state [" + currentRunState + "], cannot process message";
        throw new ListenerException(new ManagedStateException(msgAdapterNotOpen));
    }
    incNumOfMessagesInProcess(startTime);
    String lastNDC = ThreadContext.peek();
    String newNDC = "mid [" + messageId + "]";
    boolean ndcChanged = !newNDC.equals(lastNDC);
    try {
        if (ndcChanged) {
            ThreadContext.push(newNDC);
        }
        if (StringUtils.isNotEmpty(composedHideRegex)) {
            IbisMaskingLayout.addToThreadLocalReplace(composedHideRegex);
        }
        StringBuilder additionalLogging = new StringBuilder();
        String xPathLogKeys = (String) pipeLineSession.get("xPathLogKeys");
        if (StringUtils.isNotEmpty(xPathLogKeys)) {
            StringTokenizer tokenizer = new StringTokenizer(xPathLogKeys, ",");
            while (tokenizer.hasMoreTokens()) {
                String logName = tokenizer.nextToken();
                String xPathResult = (String) pipeLineSession.get(logName);
                additionalLogging.append(" and ");
                additionalLogging.append(logName);
                additionalLogging.append(" [" + xPathResult + "]");
            }
        }
        String format = "Adapter [%s] received message [%s] with messageId [%s]";
        if (msgLog.isEnabled(MSGLOG_LEVEL_TERSE)) {
            String messageOrSize = (isMsgLogHidden()) ? "SIZE=" + getFileSizeAsBytes(message) : message.toString();
            msgLog.log(MSGLOG_LEVEL_TERSE, String.format(format, getName(), messageOrSize, messageId) + additionalLogging);
        }
        if (log.isDebugEnabled()) {
            log.debug(String.format(format, getName(), message, messageId) + additionalLogging);
        } else if (log.isInfoEnabled()) {
            log.info(String.format("Adapter [%s] received message with messageId [%s]" + additionalLogging, getName(), messageId));
        }
        if (Message.isEmpty(message) && isReplaceNullMessage()) {
            log.debug("Adapter [" + getName() + "] replaces null message with messageId [" + messageId + "] by empty message");
            message = new Message("");
        }
        result = pipeline.process(messageId, message, pipeLineSession);
        String duration;
        if (msgLogHumanReadable) {
            duration = Misc.getAge(startTime);
        } else {
            duration = Misc.getDurationInMs(startTime);
        }
        String exitCode = ", exit-code [" + result.getExitCode() + "]";
        String format2 = "Adapter [%s] messageId [%s] duration [%s] got exit-state [%s]" + (result.getExitCode() != 0 ? exitCode : "") + " and result [%s] from PipeLine";
        if (msgLog.isEnabled(MSGLOG_LEVEL_TERSE)) {
            String resultOrSize = (isMsgLogHidden()) ? "SIZE=" + getFileSizeAsBytes(result.getResult()) : result.toString();
            msgLog.log(MSGLOG_LEVEL_TERSE, String.format(format2, getName(), messageId, duration, result.getState(), resultOrSize));
        }
        if (log.isDebugEnabled()) {
            log.debug(String.format(format2, getName(), messageId, duration, result.getState(), result.getResult()));
        }
        return result;
    } catch (Throwable t) {
        ListenerException e;
        if (t instanceof ListenerException) {
            e = (ListenerException) t;
        } else {
            e = new ListenerException(t);
        }
        processingSuccess = false;
        incNumOfMessagesInError();
        addErrorMessageToMessageKeeper("error processing message with messageId [" + messageId + "]: ", e);
        throw e;
    } finally {
        long endTime = System.currentTimeMillis();
        long duration = endTime - startTime;
        // reset the InProcess fields, and increase processedMessagesCount
        decNumOfMessagesInProcess(duration, processingSuccess);
        if (log.isDebugEnabled()) {
            // for performance reasons
            log.debug("Adapter: [" + getName() + "] STAT: Finished processing message with messageId [" + messageId + "] exit-state [" + result.getState() + "] started " + DateUtils.format(new Date(startTime), DateUtils.FORMAT_FULL_GENERIC) + " finished " + DateUtils.format(new Date(endTime), DateUtils.FORMAT_FULL_GENERIC) + " total duration: " + duration + " msecs");
        } else {
            log.info("Adapter [" + getName() + "] completed message with messageId [" + messageId + "] with exit-state [" + result.getState() + "]");
        }
        IbisMaskingLayout.removeThreadLocalReplace();
        if (ndcChanged) {
            ThreadContext.pop();
        }
        if (ThreadContext.getDepth() == 0) {
            ThreadContext.removeStack();
        }
    }
}
Also used : RunState(nl.nn.adapterframework.util.RunState) StringTokenizer(java.util.StringTokenizer) Message(nl.nn.adapterframework.stream.Message) Date(java.util.Date)

Example 23 with Message

use of nl.nn.adapterframework.stream.Message in project iaf by ibissource.

the class RecordXmlTransformer method handleRecord.

@Override
public String handleRecord(PipeLineSession session, List<String> parsedRecord) throws Exception {
    String xml = getXml(parsedRecord);
    if (transformerPool != null) {
        if (log.isDebugEnabled()) {
            log.debug("Transformer [" + getName() + "] record before XSL transformation [" + xml + "]");
        }
        Message message = new Message(xml);
        ParameterValueList pvl = paramList == null ? null : paramList.getValues(message, session);
        return transformerPool.transform(message.asSource(), pvl);
    }
    return xml;
}
Also used : ParameterValueList(nl.nn.adapterframework.parameters.ParameterValueList) Message(nl.nn.adapterframework.stream.Message)

Example 24 with Message

use of nl.nn.adapterframework.stream.Message in project iaf by ibissource.

the class EsbJmsTransactionalStorage method storeMessage.

@Override
public String storeMessage(String messageId, String correlationId, Date receivedDate, String comments, String label, S message) throws SenderException {
    Session session = null;
    try {
        Map<String, Object> parameterValues = createParameterValues(messageId, correlationId, receivedDate, comments, message);
        String logRequest;
        if (getType().equalsIgnoreCase("E")) {
            log.debug(getLogPrefix() + "creating exceptionLog request");
            logRequest = exceptionLogTp.transform("<dummy/>", parameterValues, true);
        } else {
            log.debug(getLogPrefix() + "creating auditLog request");
            logRequest = auditLogTp.transform("<dummy/>", parameterValues, true);
        }
        session = createSession();
        javax.jms.Message msg = createMessage(session, null, new Message(logRequest));
        String returnMessage = send(session, getDestination(), msg);
        log.debug(getLogPrefix() + "sent message [" + logRequest + "] " + "to [" + getDestination() + "] " + "msgID [" + msg.getJMSMessageID() + "] " + "correlationID [" + msg.getJMSCorrelationID() + "]");
        return returnMessage;
    } catch (Exception e) {
        throw new SenderException(e);
    } finally {
        closeSession(session);
    }
}
Also used : Message(nl.nn.adapterframework.stream.Message) TextMessage(javax.jms.TextMessage) SenderException(nl.nn.adapterframework.core.SenderException) TransformerException(javax.xml.transform.TransformerException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) DomBuilderException(nl.nn.adapterframework.util.DomBuilderException) IOException(java.io.IOException) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) JMSException(javax.jms.JMSException) SenderException(nl.nn.adapterframework.core.SenderException) ListenerException(nl.nn.adapterframework.core.ListenerException) Session(javax.jms.Session)

Example 25 with Message

use of nl.nn.adapterframework.stream.Message in project iaf by ibissource.

the class WsdlGeneratorPipe method doPipe.

@Override
public PipeRunResult doPipe(Message message, PipeLineSession session) throws PipeRunException {
    Message fileInSession = session.getMessage(getSessionKey());
    if (fileInSession == null) {
        throw new PipeRunException(this, getLogPrefix(session) + "got null value from session under key [" + getSessionKey() + "]");
    }
    File tempDir;
    String fileName;
    try (InputStream inputStream = fileInSession.asInputStream()) {
        tempDir = FileUtils.createTempDir(null, "WEB-INF" + File.separator + "classes");
        fileName = session.getMessage(getFilenameSessionKey()).asString();
        if (FileUtils.extensionEqualsIgnoreCase(fileName, "zip")) {
            FileUtils.unzipStream(inputStream, tempDir);
        } else {
            File file = new File(tempDir, fileName);
            Misc.streamToFile(inputStream, file);
            file.deleteOnExit();
        }
    } catch (IOException e) {
        throw new PipeRunException(this, getLogPrefix(session) + " Exception on uploading and unzipping/writing file", e);
    }
    File propertiesFile = new File(tempDir, getPropertiesFileName());
    PipeLine pipeLine;
    ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader();
    try {
        // A DirectoryClassloader is used to create a new 'dummy' pipeline, see createPipeLineFromPropertiesFile(String)
        // This method reads a properties file and xsd's (when present) to programmatically 'create' a pipeline.
        // The pipeline will then be used to generate a new WSDL file.
        DirectoryClassLoader directoryClassLoader = new DirectoryClassLoader(originalClassLoader);
        directoryClassLoader.setDirectory(tempDir.getPath());
        directoryClassLoader.setBasePath(".");
        directoryClassLoader.configure(getAdapter().getConfiguration().getIbisManager().getIbisContext(), "dummy");
        Thread.currentThread().setContextClassLoader(directoryClassLoader);
        if (propertiesFile.exists()) {
            pipeLine = createPipeLineFromPropertiesFile(propertiesFile);
        } else {
            File xsdFile = FileUtils.getFirstFile(tempDir);
            pipeLine = createPipeLineFromXsdFile(xsdFile);
        }
    } catch (Exception e) {
        throw new PipeRunException(this, getLogPrefix(session) + " Exception on generating wsdl", e);
    } finally {
        if (originalClassLoader != null) {
            Thread.currentThread().setContextClassLoader(originalClassLoader);
        }
    }
    Object result = null;
    OutputStream zipOut = null;
    OutputStream fullWsdlOut = null;
    try {
        Adapter adapter = new Adapter();
        Configuration configuration = new Configuration();
        configuration.setClassLoader(getConfigurationClassLoader());
        adapter.setConfiguration(configuration);
        String fileBaseName = FileUtils.getBaseName(fileName).replaceAll(" ", "_");
        adapter.setName(fileBaseName);
        Receiver receiver = new Receiver();
        EsbJmsListener esbJmsListener = new EsbJmsListener();
        esbJmsListener.setQueueConnectionFactoryName("jms/qcf_" + fileBaseName);
        esbJmsListener.setDestinationName("jms/dest_" + fileBaseName);
        receiver.setListener(esbJmsListener);
        adapter.registerReceiver(receiver);
        adapter.setPipeLine(pipeLine);
        WsdlGenerator wsdl = null;
        String generationInfo = "at " + RestListenerUtils.retrieveRequestURL(session);
        wsdl = new WsdlGenerator(pipeLine, generationInfo);
        wsdl.setIndent(true);
        wsdl.init();
        File wsdlDir = FileUtils.createTempDir(tempDir);
        // zip (with includes)
        File zipOutFile = new File(wsdlDir, wsdl.getFilename() + ".zip");
        zipOutFile.deleteOnExit();
        zipOut = new FileOutputStream(zipOutFile);
        wsdl.setUseIncludes(true);
        wsdl.zip(zipOut, null);
        // full wsdl (without includes)
        File fullWsdlOutFile = new File(wsdlDir, wsdl.getFilename() + ".wsdl");
        fullWsdlOutFile.deleteOnExit();
        fullWsdlOut = new FileOutputStream(fullWsdlOutFile);
        wsdl.setUseIncludes(false);
        wsdl.wsdl(fullWsdlOut, null);
        Dir2Xml dx = new Dir2Xml();
        dx.setPath(wsdlDir.getPath());
        result = dx.getDirList();
    } catch (Exception e) {
        throw new PipeRunException(this, getLogPrefix(session) + " Exception on generating wsdl", e);
    } finally {
        try {
            if (zipOut != null) {
                zipOut.close();
            }
            if (fullWsdlOut != null) {
                fullWsdlOut.close();
            }
        } catch (IOException e1) {
            log.warn("exception closing outputstream", e1);
        }
    }
    return new PipeRunResult(getSuccessForward(), result);
}
Also used : Dir2Xml(nl.nn.adapterframework.util.Dir2Xml) DirectoryClassLoader(nl.nn.adapterframework.configuration.classloaders.DirectoryClassLoader) Message(nl.nn.adapterframework.stream.Message) Configuration(nl.nn.adapterframework.configuration.Configuration) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) Receiver(nl.nn.adapterframework.receivers.Receiver) Adapter(nl.nn.adapterframework.core.Adapter) IOException(java.io.IOException) PipeRunException(nl.nn.adapterframework.core.PipeRunException) IOException(java.io.IOException) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) PipeRunResult(nl.nn.adapterframework.core.PipeRunResult) FileOutputStream(java.io.FileOutputStream) PipeRunException(nl.nn.adapterframework.core.PipeRunException) DirectoryClassLoader(nl.nn.adapterframework.configuration.classloaders.DirectoryClassLoader) PipeLine(nl.nn.adapterframework.core.PipeLine) File(java.io.File) WsdlGenerator(nl.nn.adapterframework.soap.WsdlGenerator)

Aggregations

Message (nl.nn.adapterframework.stream.Message)598 Test (org.junit.Test)385 PipeLineSession (nl.nn.adapterframework.core.PipeLineSession)220 PipeRunResult (nl.nn.adapterframework.core.PipeRunResult)114 IOException (java.io.IOException)112 SenderException (nl.nn.adapterframework.core.SenderException)97 ConfigurationException (nl.nn.adapterframework.configuration.ConfigurationException)54 ParameterValueList (nl.nn.adapterframework.parameters.ParameterValueList)54 Parameter (nl.nn.adapterframework.parameters.Parameter)52 PipeForward (nl.nn.adapterframework.core.PipeForward)41 Date (java.util.Date)37 TimeoutException (nl.nn.adapterframework.core.TimeoutException)31 UrlMessage (nl.nn.adapterframework.stream.UrlMessage)31 PipeRunException (nl.nn.adapterframework.core.PipeRunException)30 ByteArrayInputStream (java.io.ByteArrayInputStream)29 InputStream (java.io.InputStream)29 ParameterList (nl.nn.adapterframework.parameters.ParameterList)28 ListenerException (nl.nn.adapterframework.core.ListenerException)27 ParameterException (nl.nn.adapterframework.core.ParameterException)25 SAXException (org.xml.sax.SAXException)19