Search in sources :

Example 46 with Message

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

the class JdbcQuerySenderBase method executeUpdateBlobQuery.

protected Message executeUpdateBlobQuery(PreparedStatement statement, Message contents) throws SenderException {
    BlobOutputStream blobOutputStream = null;
    try {
        try {
            blobOutputStream = getBlobOutputStream(statement, blobColumn, isBlobsCompressed());
            if (contents != null) {
                if (StringUtils.isNotEmpty(getStreamCharset())) {
                    contents = new Message(contents.asReader(getStreamCharset()));
                }
                InputStream inputStream = contents.asInputStream(getBlobCharset());
                if (!isCloseInputstreamOnExit()) {
                    inputStream = StreamUtil.dontClose(inputStream);
                }
                Misc.streamToStream(inputStream, blobOutputStream);
            }
        } finally {
            if (blobOutputStream != null) {
                blobOutputStream.close();
            }
        }
    } catch (SQLException | JdbcException | IOException e) {
        throw new SenderException(getLogPrefix() + "got exception executing an update BLOB command", e);
    }
    return blobOutputStream == null ? null : new Message(blobOutputStream.getWarnings().toXML());
}
Also used : Message(nl.nn.adapterframework.stream.Message) SQLException(java.sql.SQLException) InputStream(java.io.InputStream) IOException(java.io.IOException) SenderException(nl.nn.adapterframework.core.SenderException)

Example 47 with Message

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

the class JdbcQuerySenderBase method executeStatementSet.

protected PipeRunResult executeStatementSet(QueryExecutionContext queryExecutionContext, Message message, PipeLineSession session, IForwardTarget next) throws SenderException, TimeoutException {
    try {
        PreparedStatement statement = queryExecutionContext.getStatement();
        JdbcUtil.applyParameters(getDbmsSupport(), statement, queryExecutionContext.getParameterList(), message, session);
        switch(queryExecutionContext.getQueryType()) {
            case SELECT:
                Object blobSessionVar = null;
                Object clobSessionVar = null;
                if (session != null && StringUtils.isNotEmpty(getBlobSessionKey())) {
                    blobSessionVar = session.getMessage(getBlobSessionKey()).asObject();
                }
                if (session != null && StringUtils.isNotEmpty(getClobSessionKey())) {
                    clobSessionVar = session.getMessage(getClobSessionKey()).asObject();
                }
                if (isStreamResultToServlet()) {
                    HttpServletResponse response = (HttpServletResponse) session.get(PipeLineSession.HTTP_RESPONSE_KEY);
                    String contentType = session.getMessage("contentType").asString();
                    String contentDisposition = session.getMessage("contentDisposition").asString();
                    return executeSelectQuery(statement, blobSessionVar, clobSessionVar, response, contentType, contentDisposition, session, next);
                } else {
                    return executeSelectQuery(statement, blobSessionVar, clobSessionVar, session, next);
                }
            case UPDATEBLOB:
                if (StringUtils.isNotEmpty(getBlobSessionKey())) {
                    return new PipeRunResult(null, executeUpdateBlobQuery(statement, session == null ? null : session.getMessage(getBlobSessionKey())));
                }
                return new PipeRunResult(null, executeUpdateBlobQuery(statement, message));
            case UPDATECLOB:
                if (StringUtils.isNotEmpty(getClobSessionKey())) {
                    return new PipeRunResult(null, executeUpdateClobQuery(statement, session == null ? null : session.getMessage(getClobSessionKey())));
                }
                return new PipeRunResult(null, executeUpdateClobQuery(statement, message));
            case PACKAGE:
                return new PipeRunResult(null, executePackageQuery(queryExecutionContext));
            case OTHER:
                Message result = executeOtherQuery(queryExecutionContext, message, session);
                if (getBatchSize() > 0 && ++queryExecutionContext.iteration >= getBatchSize()) {
                    int[] results = statement.executeBatch();
                    int numRowsAffected = 0;
                    for (int i : results) {
                        numRowsAffected += i;
                    }
                    result = new Message("<result><rowsupdated>" + numRowsAffected + "</rowsupdated></result>");
                    statement.clearBatch();
                    queryExecutionContext.iteration = 0;
                }
                return new PipeRunResult(null, result);
            default:
                throw new IllegalStateException("Unsupported queryType: [" + queryExecutionContext.getQueryType() + "]");
        }
    } catch (SenderException e) {
        if (e.getCause() instanceof SQLException) {
            SQLException sqle = (SQLException) e.getCause();
            if (sqle.getErrorCode() == 1013) {
                throw new TimeoutException("Timeout of [" + getTimeout() + "] sec expired");
            }
        }
        throw new SenderException(e);
    } catch (Throwable t) {
        throw new SenderException(getLogPrefix() + "got exception sending message", t);
    } finally {
        closeStatementSet(queryExecutionContext, session);
        ParameterList newParameterList = queryExecutionContext.getParameterList();
        if (isCloseInputstreamOnExit() && newParameterList != null) {
            for (int i = 0; i < newParameterList.size(); i++) {
                Parameter param = newParameterList.getParameter(i);
                if (param.getType() == ParameterType.INPUTSTREAM) {
                    log.debug(getLogPrefix() + "Closing inputstream for parameter [" + param.getName() + "]");
                    try {
                        Object object = newParameterList.getParameter(i).getValue(null, message, session, true);
                        if (object instanceof AutoCloseable) {
                            ((AutoCloseable) object).close();
                        } else {
                            log.error("unable to auto-close parameter [" + param.getName() + "]");
                        }
                    } catch (Exception e) {
                        log.warn(new SenderException(getLogPrefix() + "got exception closing inputstream", e));
                    }
                }
            }
        }
    }
}
Also used : Message(nl.nn.adapterframework.stream.Message) SQLException(java.sql.SQLException) HttpServletResponse(javax.servlet.http.HttpServletResponse) PreparedStatement(java.sql.PreparedStatement) TimeoutException(nl.nn.adapterframework.core.TimeoutException) ParseException(java.text.ParseException) StreamingException(nl.nn.adapterframework.stream.StreamingException) JMSException(javax.jms.JMSException) SenderException(nl.nn.adapterframework.core.SenderException) SQLException(java.sql.SQLException) IOException(java.io.IOException) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) ParameterException(nl.nn.adapterframework.core.ParameterException) PipeRunResult(nl.nn.adapterframework.core.PipeRunResult) ParameterList(nl.nn.adapterframework.parameters.ParameterList) Parameter(nl.nn.adapterframework.parameters.Parameter) SenderException(nl.nn.adapterframework.core.SenderException) TimeoutException(nl.nn.adapterframework.core.TimeoutException)

Example 48 with Message

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

the class JdbcQuerySenderBase method executeOtherQuery.

protected Message executeOtherQuery(Connection connection, PreparedStatement statement, String query, PreparedStatement resStmt, Message message, PipeLineSession session, ParameterList parameterList) throws SenderException {
    try {
        int numRowsAffected = 0;
        if (StringUtils.isNotEmpty(getRowIdSessionKey())) {
            try (CallableStatement cstmt = getCallWithRowIdReturned(connection, query)) {
                int ri = 1;
                if (parameterList != null) {
                    ParameterValueList parameters = parameterList.getValues(message, session);
                    JdbcUtil.applyParameters(getDbmsSupport(), cstmt, parameters);
                    ri = parameters.size() + 1;
                }
                cstmt.registerOutParameter(ri, Types.VARCHAR);
                log.debug(getLogPrefix() + "executing a SQL command");
                numRowsAffected = cstmt.executeUpdate();
                String rowId = cstmt.getString(ri);
                if (session != null)
                    session.put(getRowIdSessionKey(), rowId);
            }
        } else {
            log.debug(getLogPrefix() + "executing a SQL command");
            if (getBatchSize() > 0) {
                statement.addBatch();
            } else {
                numRowsAffected = statement.executeUpdate();
            }
        }
        if (resStmt != null) {
            if (log.isDebugEnabled())
                log.debug("obtaining result from [" + convertedResultQuery + "]");
            try (ResultSet rs = resStmt.executeQuery()) {
                return getResult(rs);
            }
        }
        if (getColumnsReturnedList() != null) {
            return getResult(getReturnedColumns(getColumnsReturnedList(), statement));
        }
        if (isScalar()) {
            return new Message(Integer.toString(numRowsAffected));
        }
        return new Message("<result>" + (getBatchSize() > 0 ? "addedToBatch" : "<rowsupdated>" + numRowsAffected + "</rowsupdated>") + "</result>");
    } catch (SQLException e) {
        throw new SenderException(getLogPrefix() + "got exception executing query [" + query + "]", e);
    } catch (JdbcException | IOException | JMSException e) {
        throw new SenderException(getLogPrefix() + "got exception executing a SQL command", e);
    } catch (ParameterException e) {
        throw new SenderException(getLogPrefix() + "got exception evaluating parameters", e);
    }
}
Also used : ParameterValueList(nl.nn.adapterframework.parameters.ParameterValueList) Message(nl.nn.adapterframework.stream.Message) SQLException(java.sql.SQLException) JMSException(javax.jms.JMSException) IOException(java.io.IOException) CallableStatement(java.sql.CallableStatement) ResultSet(java.sql.ResultSet) ParameterException(nl.nn.adapterframework.core.ParameterException) SenderException(nl.nn.adapterframework.core.SenderException)

Example 49 with Message

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

the class XmlJmsBrowserSender method sendMessage.

@Override
public Message sendMessage(Message message, PipeLineSession session) throws SenderException, TimeoutException {
    Element queueBrowserElement;
    String root = null;
    String jmsRealm = null;
    String queueConnectionFactoryName = null;
    String destinationName = null;
    DestinationType destinationType = null;
    try {
        queueBrowserElement = XmlUtils.buildElement(message.asString());
        root = queueBrowserElement.getTagName();
        jmsRealm = XmlUtils.getChildTagAsString(queueBrowserElement, "jmsRealm");
        queueConnectionFactoryName = XmlUtils.getChildTagAsString(queueBrowserElement, "queueConnectionFactoryName");
        destinationName = XmlUtils.getChildTagAsString(queueBrowserElement, "destinationName");
        destinationType = EnumUtils.parse(DestinationType.class, XmlUtils.getChildTagAsString(queueBrowserElement, "destinationType"));
    } catch (Exception e) {
        throw new SenderException(getLogPrefix() + "got exception parsing [" + message + "]", e);
    }
    JmsBrowser<javax.jms.Message> jmsBrowser = createJmsBrowser();
    jmsBrowser.setName("XmlQueueBrowserSender");
    if (jmsRealm != null) {
        jmsBrowser.setJmsRealm(jmsRealm);
    }
    if (queueConnectionFactoryName != null) {
        jmsBrowser.setQueueConnectionFactoryName(queueConnectionFactoryName);
    }
    jmsBrowser.setDestinationName(destinationName);
    jmsBrowser.setDestinationType(destinationType);
    IMessageBrowsingIterator it = null;
    boolean remove = false;
    if (root.equalsIgnoreCase("browse")) {
    // OK
    } else {
        if (root.equalsIgnoreCase("remove")) {
            remove = true;
        } else {
            throw new SenderException(getLogPrefix() + "unknown root element [" + root + "]");
        }
    }
    XmlBuilder result = new XmlBuilder("result");
    XmlBuilder items;
    if (remove) {
        items = new XmlBuilder("itemsRemoved");
    } else {
        items = new XmlBuilder("items");
    }
    try {
        int count = 0;
        it = jmsBrowser.getIterator();
        while (it.hasNext()) {
            count++;
            JmsMessageBrowserIteratorItem jmsMessageBrowserIteratorItem = (JmsMessageBrowserIteratorItem) it.next();
            if (remove) {
                jmsBrowser.deleteMessage(jmsMessageBrowserIteratorItem.getJMSMessageID());
            } else {
                // browse
                XmlBuilder item = new XmlBuilder("item");
                XmlBuilder timestamp = new XmlBuilder("timestamp");
                timestamp.setValue(new java.util.Date(jmsMessageBrowserIteratorItem.getJMSTimestamp()).toString());
                item.addSubElement(timestamp);
                XmlBuilder messageId = new XmlBuilder("messageId");
                messageId.setValue(jmsMessageBrowserIteratorItem.getJMSMessageID());
                item.addSubElement(messageId);
                XmlBuilder correlationId = new XmlBuilder("correlationId");
                correlationId.setValue(jmsMessageBrowserIteratorItem.getCorrelationId());
                item.addSubElement(correlationId);
                XmlBuilder msg = new XmlBuilder("message");
                msg.setCdataValue(jmsMessageBrowserIteratorItem.getText());
                item.addSubElement(msg);
                items.addSubElement(item);
            }
        }
        if (remove) {
            items.setValue(Integer.toString(count));
        } else {
            items.addAttribute("count", count);
        }
        result.addSubElement(items);
    } catch (ListenerException e) {
        throw new SenderException(getLogPrefix() + "got exception browsing messages", e);
    } finally {
        try {
            if (it != null) {
                it.close();
            }
        } catch (ListenerException e) {
            log.warn(getLogPrefix() + "exception on closing message browser iterator", e);
        }
    }
    return new Message(result.toXML());
}
Also used : Message(nl.nn.adapterframework.stream.Message) Element(org.w3c.dom.Element) IMessageBrowsingIterator(nl.nn.adapterframework.core.IMessageBrowsingIterator) DestinationType(nl.nn.adapterframework.jms.JMSFacade.DestinationType) SenderException(nl.nn.adapterframework.core.SenderException) TimeoutException(nl.nn.adapterframework.core.TimeoutException) ListenerException(nl.nn.adapterframework.core.ListenerException) ListenerException(nl.nn.adapterframework.core.ListenerException) XmlBuilder(nl.nn.adapterframework.util.XmlBuilder) SenderException(nl.nn.adapterframework.core.SenderException)

Example 50 with Message

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

the class JMSFacade method extractMessage.

/**
 * Extracts string from message obtained from getRawMessage(Map). May also extract
 * other parameters from the message and put those in the threadContext.
 */
public Message extractMessage(Object rawMessage, Map<String, Object> context, boolean soap, String soapHeaderSessionKey, SoapWrapper soapWrapper) throws JMSException, SAXException, TransformerException, IOException {
    // TextMessage message = null;
    Message message;
    /*
		try {
			message = (TextMessage) rawMessage;
		} catch (ClassCastException e) {
			log.error("message received by listener on ["+ getDestinationName()+ "] was not of type TextMessage, but ["+rawMessage.getClass().getName()+"]", e);
			return null;
		}
		rawMessageText= message.getText();
*/
    if (rawMessage instanceof IMessageWrapper) {
        message = ((IMessageWrapper) rawMessage).getMessage();
    } else if (rawMessage instanceof TextMessage) {
        message = new Message(((TextMessage) rawMessage).getText(), getContext((TextMessage) rawMessage));
    } else if (rawMessage instanceof BytesMessage) {
        BytesMessage bytesMsg = (BytesMessage) rawMessage;
        InputStream input = new InputStream() {

            @Override
            public int read() throws IOException {
                try {
                    return bytesMsg.readByte();
                } catch (JMSException e) {
                    throw new IOException("Cannot read JMS message", e);
                }
            }

            @Override
            public int read(byte[] b) throws IOException {
                try {
                    return bytesMsg.readBytes(b);
                } catch (JMSException e) {
                    throw new IOException("Cannot read JMS message", e);
                }
            }

            @Override
            public int read(byte[] b, int off, int len) throws IOException {
                try {
                    byte[] readbuf = new byte[len];
                    int result = bytesMsg.readBytes(readbuf);
                    if (result > 0) {
                        System.arraycopy(readbuf, 0, b, off, result);
                    }
                    return result;
                } catch (JMSException e) {
                    throw new IOException("Cannot read JMS message", e);
                }
            }
        };
        message = new Message(new BufferedInputStream(input), getContext((BytesMessage) rawMessage));
    } else if (rawMessage == null) {
        message = Message.nullMessage();
    } else {
        message = Message.asMessage(rawMessage);
    }
    if (!soap) {
        return message;
    }
    message.preserve();
    Message messageText = extractMessageBody(message, context, soapWrapper);
    if (StringUtils.isNotEmpty(soapHeaderSessionKey)) {
        String soapHeader = soapWrapper.getHeader(message);
        context.put(soapHeaderSessionKey, soapHeader);
    }
    return messageText;
}
Also used : IMessageWrapper(nl.nn.adapterframework.core.IMessageWrapper) Message(nl.nn.adapterframework.stream.Message) TextMessage(javax.jms.TextMessage) BytesMessage(javax.jms.BytesMessage) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) BytesMessage(javax.jms.BytesMessage) JMSException(javax.jms.JMSException) IOException(java.io.IOException) TextMessage(javax.jms.TextMessage)

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