Search in sources :

Example 1 with ParameterException

use of nl.nn.adapterframework.core.ParameterException in project iaf by ibissource.

the class Parameter method getValue.

/**
 * determines the raw value
 * @param alreadyResolvedParameters
 * @return the raw value as object
 * @throws IbisException
 */
public Object getValue(ParameterValueList alreadyResolvedParameters, ParameterResolutionContext prc) throws ParameterException {
    Object result = null;
    log.debug("Calculating value for Parameter [" + getName() + "]");
    if (!configured) {
        throw new ParameterException("Parameter [" + getName() + "] not configured");
    }
    String retrievedSessionKey;
    if (transformerPoolSessionKey != null) {
        try {
            retrievedSessionKey = transformerPoolSessionKey.transform(prc.getInput(), null);
        } catch (Exception e) {
            throw new ParameterException("SessionKey for parameter [" + getName() + "] exception on transformation to get name", e);
        }
    } else {
        retrievedSessionKey = getSessionKey();
    }
    TransformerPool pool = getTransformerPool();
    if (pool != null) {
        try {
            Object transformResult = null;
            Source source = null;
            if (StringUtils.isNotEmpty(getValue())) {
                source = XmlUtils.stringToSourceForSingleUse(getValue(), prc.isNamespaceAware());
            } else if (StringUtils.isNotEmpty(retrievedSessionKey)) {
                String sourceString;
                Object sourceObject = prc.getSession().get(retrievedSessionKey);
                if (TYPE_LIST.equals(getType()) && sourceObject instanceof List) {
                    List<String> items = (List<String>) sourceObject;
                    XmlBuilder itemsXml = new XmlBuilder("items");
                    for (Iterator<String> it = items.iterator(); it.hasNext(); ) {
                        String item = it.next();
                        XmlBuilder itemXml = new XmlBuilder("item");
                        itemXml.setValue(item);
                        itemsXml.addSubElement(itemXml);
                    }
                    sourceString = itemsXml.toXML();
                } else if (TYPE_MAP.equals(getType()) && sourceObject instanceof Map) {
                    Map<String, String> items = (Map<String, String>) sourceObject;
                    XmlBuilder itemsXml = new XmlBuilder("items");
                    for (Iterator<String> it = items.keySet().iterator(); it.hasNext(); ) {
                        String item = it.next();
                        XmlBuilder itemXml = new XmlBuilder("item");
                        itemXml.addAttribute("name", item);
                        itemXml.setValue(items.get(item));
                        itemsXml.addSubElement(itemXml);
                    }
                    sourceString = itemsXml.toXML();
                } else {
                    sourceString = (String) sourceObject;
                }
                if (StringUtils.isNotEmpty(sourceString)) {
                    log.debug("Parameter [" + getName() + "] using sessionvariable [" + retrievedSessionKey + "] as source for transformation");
                    source = XmlUtils.stringToSourceForSingleUse(sourceString, prc.isNamespaceAware());
                } else {
                    log.debug("Parameter [" + getName() + "] sessionvariable [" + retrievedSessionKey + "] empty, no transformation will be performed");
                }
            } else if (StringUtils.isNotEmpty(getPattern())) {
                String sourceString = format(alreadyResolvedParameters, prc);
                if (StringUtils.isNotEmpty(sourceString)) {
                    log.debug("Parameter [" + getName() + "] using pattern [" + getPattern() + "] as source for transformation");
                    source = XmlUtils.stringToSourceForSingleUse(sourceString, prc.isNamespaceAware());
                } else {
                    log.debug("Parameter [" + getName() + "] pattern [" + getPattern() + "] empty, no transformation will be performed");
                }
            } else {
                source = prc.getInputSource();
            }
            if (source != null) {
                if (transformerPoolRemoveNamespaces != null) {
                    String rnResult = transformerPoolRemoveNamespaces.transform(source, null);
                    source = XmlUtils.stringToSource(rnResult);
                }
                transformResult = transform(source, prc);
            }
            if (!(transformResult instanceof String) || StringUtils.isNotEmpty((String) transformResult)) {
                result = transformResult;
            }
        } catch (Exception e) {
            throw new ParameterException("Parameter [" + getName() + "] exception on transformation to get parametervalue", e);
        }
    } else {
        if (StringUtils.isNotEmpty(retrievedSessionKey)) {
            result = prc.getSession().get(retrievedSessionKey);
        } else if (StringUtils.isNotEmpty(getPattern())) {
            result = format(alreadyResolvedParameters, prc);
        } else if (StringUtils.isNotEmpty(getValue())) {
            result = getValue();
        } else {
            result = prc.getInput();
        }
    }
    if (result != null) {
        if (log.isDebugEnabled()) {
            log.debug("Parameter [" + getName() + "] resolved to [" + (isHidden() ? hide(result.toString()) : result) + "]");
        }
    } else {
        // if value is null then return specified default value
        StringTokenizer stringTokenizer = new StringTokenizer(getDefaultValueMethods(), ",");
        while (result == null && stringTokenizer.hasMoreElements()) {
            String token = stringTokenizer.nextToken();
            if ("defaultValue".equals(token)) {
                result = getDefaultValue();
            } else if ("sessionKey".equals(token)) {
                result = prc.getSession().get(retrievedSessionKey);
            } else if ("pattern".equals(token)) {
                result = format(alreadyResolvedParameters, prc);
            } else if ("value".equals(token)) {
                result = getValue();
            } else if ("input".equals(token)) {
                result = prc.getInput();
            }
        }
        log.debug("Parameter [" + getName() + "] resolved to defaultvalue [" + (isHidden() ? hide(result.toString()) : result) + "]");
    }
    if (result != null && result instanceof String) {
        if (getMinLength() >= 0 && !TYPE_NUMBER.equals(getType())) {
            if (result.toString().length() < getMinLength()) {
                log.debug("Padding parameter [" + getName() + "] because length [" + result.toString().length() + "] deceeds minLength [" + getMinLength() + "]");
                result = StringUtils.rightPad(result.toString(), getMinLength());
            }
        }
        if (getMaxLength() >= 0) {
            if (result.toString().length() > getMaxLength()) {
                log.debug("Trimming parameter [" + getName() + "] because length [" + result.toString().length() + "] exceeds maxLength [" + getMaxLength() + "]");
                result = result.toString().substring(0, getMaxLength());
            }
        }
        if (TYPE_NODE.equals(getType())) {
            try {
                result = XmlUtils.buildNode((String) result, prc.isNamespaceAware());
                if (log.isDebugEnabled())
                    log.debug("final result [" + result.getClass().getName() + "][" + result + "]");
            } catch (DomBuilderException e) {
                throw new ParameterException("Parameter [" + getName() + "] could not parse result [" + result + "] to XML nodeset", e);
            }
        }
        if (TYPE_DOMDOC.equals(getType())) {
            try {
                result = XmlUtils.buildDomDocument((String) result, prc.isNamespaceAware(), prc.isXslt2());
                if (log.isDebugEnabled())
                    log.debug("final result [" + result.getClass().getName() + "][" + result + "]");
            } catch (DomBuilderException e) {
                throw new ParameterException("Parameter [" + getName() + "] could not parse result [" + result + "] to XML document", e);
            }
        }
        if (TYPE_DATE.equals(getType()) || TYPE_DATETIME.equals(getType()) || TYPE_TIMESTAMP.equals(getType()) || TYPE_TIME.equals(getType())) {
            log.debug("Parameter [" + getName() + "] converting result [" + result + "] to date using formatString [" + getFormatString() + "]");
            DateFormat df = new SimpleDateFormat(getFormatString());
            try {
                result = df.parseObject((String) result);
            } catch (ParseException e) {
                throw new ParameterException("Parameter [" + getName() + "] could not parse result [" + result + "] to Date using formatString [" + getFormatString() + "]", e);
            }
        }
        if (TYPE_XMLDATETIME.equals(getType())) {
            log.debug("Parameter [" + getName() + "] converting result [" + result + "] from xml dateTime to date");
            result = DateUtils.parseXmlDateTime((String) result);
        }
        if (TYPE_NUMBER.equals(getType())) {
            log.debug("Parameter [" + getName() + "] converting result [" + result + "] to number decimalSeparator [" + decimalFormatSymbols.getDecimalSeparator() + "] groupingSeparator [" + decimalFormatSymbols.getGroupingSeparator() + "]");
            DecimalFormat df = new DecimalFormat();
            df.setDecimalFormatSymbols(decimalFormatSymbols);
            try {
                Number n = df.parse((String) result);
                result = n;
            } catch (ParseException e) {
                throw new ParameterException("Parameter [" + getName() + "] could not parse result [" + result + "] to number decimalSeparator [" + decimalFormatSymbols.getDecimalSeparator() + "] groupingSeparator [" + decimalFormatSymbols.getGroupingSeparator() + "]", e);
            }
            if (getMinLength() >= 0 && result.toString().length() < getMinLength()) {
                log.debug("Adding leading zeros to parameter [" + getName() + "]");
                result = StringUtils.leftPad(result.toString(), getMinLength(), '0');
            }
        }
        if (TYPE_INTEGER.equals(getType())) {
            log.debug("Parameter [" + getName() + "] converting result [" + result + "] to integer");
            try {
                Integer i = Integer.parseInt((String) result);
                result = i;
            } catch (NumberFormatException e) {
                throw new ParameterException("Parameter [" + getName() + "] could not parse result [" + result + "] to integer", e);
            }
        }
    }
    if (result != null) {
        if (getMinInclusive() != null || getMaxInclusive() != null) {
            if (getMinInclusive() != null) {
                if (((Number) result).floatValue() < minInclusive.floatValue()) {
                    log.debug("Replacing parameter [" + getName() + "] because value [" + result + "] exceeds minInclusive [" + getMinInclusive() + "]");
                    result = minInclusive;
                }
            }
            if (getMaxInclusive() != null) {
                if (((Number) result).floatValue() > maxInclusive.floatValue()) {
                    log.debug("Replacing parameter [" + getName() + "] because value [" + result + "] exceeds maxInclusive [" + getMaxInclusive() + "]");
                    result = maxInclusive;
                }
            }
        }
    }
    return result;
}
Also used : DecimalFormat(java.text.DecimalFormat) TransformerException(javax.xml.transform.TransformerException) IbisException(nl.nn.adapterframework.core.IbisException) ParseException(java.text.ParseException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) DomBuilderException(nl.nn.adapterframework.util.DomBuilderException) IOException(java.io.IOException) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) ParameterException(nl.nn.adapterframework.core.ParameterException) TransformerPool(nl.nn.adapterframework.util.TransformerPool) Source(javax.xml.transform.Source) StringTokenizer(java.util.StringTokenizer) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) Iterator(java.util.Iterator) XmlBuilder(nl.nn.adapterframework.util.XmlBuilder) INamedObject(nl.nn.adapterframework.core.INamedObject) ParameterException(nl.nn.adapterframework.core.ParameterException) ArrayList(java.util.ArrayList) List(java.util.List) DomBuilderException(nl.nn.adapterframework.util.DomBuilderException) ParseException(java.text.ParseException) Map(java.util.Map) SimpleDateFormat(java.text.SimpleDateFormat)

Example 2 with ParameterException

use of nl.nn.adapterframework.core.ParameterException in project iaf by ibissource.

the class Parameter method format.

private String format(ParameterValueList alreadyResolvedParameters, ParameterResolutionContext prc) throws ParameterException {
    int startNdx = -1;
    int endNdx = 0;
    // replace the named parameter with numbered parameters
    StringBuffer formatPattern = new StringBuffer();
    List<Object> params = new ArrayList<Object>();
    int paramPosition = 0;
    while (endNdx != -1) {
        // get name of parameter in pattern to be substituted
        startNdx = pattern.indexOf("{", endNdx);
        if (startNdx == -1) {
            formatPattern.append(pattern.substring(endNdx));
            break;
        } else if (endNdx != -1) {
            formatPattern.append(pattern.substring(endNdx, startNdx));
        }
        int tmpEndNdx = pattern.indexOf("}", startNdx);
        endNdx = pattern.indexOf(",", startNdx);
        if (endNdx == -1 || endNdx > tmpEndNdx) {
            endNdx = tmpEndNdx;
        }
        if (endNdx == -1) {
            throw new ParameterException(new ParseException("Bracket is not closed", startNdx));
        }
        String substitutionName = pattern.substring(startNdx + 1, endNdx);
        // get value
        Object substitutionValue = getValueForFormatting(alreadyResolvedParameters, prc, substitutionName);
        params.add(substitutionValue);
        formatPattern.append('{').append(paramPosition++);
    }
    return MessageFormat.format(formatPattern.toString(), params.toArray());
}
Also used : ArrayList(java.util.ArrayList) INamedObject(nl.nn.adapterframework.core.INamedObject) ParameterException(nl.nn.adapterframework.core.ParameterException) ParseException(java.text.ParseException)

Example 3 with ParameterException

use of nl.nn.adapterframework.core.ParameterException in project iaf by ibissource.

the class JdbcQuerySenderBase method executeOtherQuery.

protected String executeOtherQuery(Connection connection, String correlationID, PreparedStatement statement, String message, ParameterResolutionContext prc, ParameterList newParamList) throws SenderException {
    ResultSet resultset = null;
    try {
        int numRowsAffected = 0;
        if (StringUtils.isNotEmpty(getRowIdSessionKey())) {
            CallableStatement cstmt = getCallWithRowIdReturned(connection, correlationID, message);
            int ri = 1;
            if (prc != null && paramList != null) {
                ParameterValueList parameters = prc.getValues(newParamList);
                applyParameters(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 (prc != null)
                prc.getSession().put(getRowIdSessionKey(), rowId);
        } else {
            log.debug(getLogPrefix() + "executing a SQL command");
            numRowsAffected = statement.executeUpdate();
        }
        if (StringUtils.isNotEmpty(getResultQuery())) {
            Statement resStmt = null;
            try {
                resStmt = connection.createStatement();
                log.debug("obtaining result from [" + getResultQuery() + "]");
                ResultSet rs = resStmt.executeQuery(getResultQuery());
                return getResult(rs);
            } finally {
                if (resStmt != null) {
                    resStmt.close();
                }
            }
        }
        if (getColumnsReturnedList() != null) {
            return getResult(getReturnedColumns(getColumnsReturnedList(), statement));
        }
        if (isScalar()) {
            return numRowsAffected + "";
        }
        return "<result><rowsupdated>" + numRowsAffected + "</rowsupdated></result>";
    } catch (SQLException sqle) {
        throw new SenderException(getLogPrefix() + "got exception executing a SQL command", sqle);
    } catch (JdbcException e) {
        throw new SenderException(getLogPrefix() + "got exception executing a SQL command", e);
    } catch (IOException e) {
        throw new SenderException(getLogPrefix() + "got exception executing a SQL command", e);
    } catch (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);
    } finally {
        try {
            if (resultset != null) {
                resultset.close();
            }
        } catch (SQLException e) {
            log.warn(new SenderException(getLogPrefix() + "got exception closing resultset", e));
        }
    }
}
Also used : ParameterValueList(nl.nn.adapterframework.parameters.ParameterValueList) SQLException(java.sql.SQLException) CallableStatement(java.sql.CallableStatement) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) CallableStatement(java.sql.CallableStatement) ResultSet(java.sql.ResultSet) JMSException(javax.jms.JMSException) ParameterException(nl.nn.adapterframework.core.ParameterException) IOException(java.io.IOException) SenderException(nl.nn.adapterframework.core.SenderException)

Example 4 with ParameterException

use of nl.nn.adapterframework.core.ParameterException in project iaf by ibissource.

the class LdapFindMemberPipe method doPipeWithException.

public PipeRunResult doPipeWithException(Object input, IPipeLineSession session) throws PipeRunException {
    String dnSearchIn_work;
    String dnFind_work;
    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);
        }
    }
    dnSearchIn_work = getParameterValue(pvl, "dnSearchIn");
    if (dnSearchIn_work == null) {
        dnSearchIn_work = getDnSearchIn();
    }
    dnFind_work = getParameterValue(pvl, "dnFind");
    if (dnFind_work == null) {
        dnFind_work = getDnFind();
    }
    boolean found = false;
    if (StringUtils.isNotEmpty(dnSearchIn_work) && StringUtils.isNotEmpty(dnFind_work)) {
        try {
            found = findMember(getHost(), getPort(), dnSearchIn_work, isUseSsl(), dnFind_work, isRecursiveSearch());
        } catch (NamingException e) {
            throw new PipeRunException(this, getLogPrefix(session) + "exception on ldap lookup", e);
        }
    }
    if (!found) {
        String msg = getLogPrefix(session) + "dn [" + dnFind_work + "] not found as member in url [" + retrieveUrl(getHost(), getPort(), dnSearchIn_work, isUseSsl()) + "]";
        if (notFoundForward == null) {
            throw new PipeRunException(this, msg);
        } else {
            log.info(msg);
            return new PipeRunResult(notFoundForward, input);
        }
    }
    return new PipeRunResult(getForward(), input);
}
Also used : PipeRunResult(nl.nn.adapterframework.core.PipeRunResult) ParameterValueList(nl.nn.adapterframework.parameters.ParameterValueList) PipeRunException(nl.nn.adapterframework.core.PipeRunException) ParameterException(nl.nn.adapterframework.core.ParameterException) NamingException(javax.naming.NamingException) ParameterResolutionContext(nl.nn.adapterframework.parameters.ParameterResolutionContext)

Example 5 with ParameterException

use of nl.nn.adapterframework.core.ParameterException in project iaf by ibissource.

the class CmisSender method getSession.

public Session getSession(ParameterResolutionContext prc) throws SenderException {
    if (session == null || !isKeepSession()) {
        String authAlias_work = null;
        String userName_work = null;
        String password_work = null;
        ParameterValueList pvl = null;
        try {
            if (prc != null && paramList != null) {
                pvl = prc.getValues(paramList);
                if (pvl != null) {
                    ParameterValue pv = pvl.getParameterValue("authAlias");
                    if (pv != null) {
                        authAlias_work = (String) pv.getValue();
                    }
                    pv = pvl.getParameterValue("userName");
                    if (pv != null) {
                        userName_work = (String) pv.getValue();
                    }
                    pv = pvl.getParameterValue("password");
                    if (pv != null) {
                        password_work = (String) pv.getValue();
                    }
                }
            }
        } catch (ParameterException e) {
            throw new SenderException(getLogPrefix() + "Sender [" + getName() + "] caught exception evaluating parameters", e);
        }
        if (authAlias_work == null) {
            authAlias_work = getAuthAlias();
        }
        if (userName_work == null) {
            userName_work = getUserName();
        }
        if (password_work == null) {
            password_work = getPassword();
        }
        CredentialFactory cf = new CredentialFactory(authAlias_work, userName_work, password_work);
        session = connect(cf.getUsername(), cf.getPassword());
    }
    return session;
}
Also used : ParameterValueList(nl.nn.adapterframework.parameters.ParameterValueList) ParameterValue(nl.nn.adapterframework.parameters.ParameterValue) CredentialFactory(nl.nn.adapterframework.util.CredentialFactory) ParameterException(nl.nn.adapterframework.core.ParameterException) SenderException(nl.nn.adapterframework.core.SenderException)

Aggregations

ParameterException (nl.nn.adapterframework.core.ParameterException)35 ParameterValueList (nl.nn.adapterframework.parameters.ParameterValueList)23 ParameterResolutionContext (nl.nn.adapterframework.parameters.ParameterResolutionContext)18 PipeRunException (nl.nn.adapterframework.core.PipeRunException)15 SenderException (nl.nn.adapterframework.core.SenderException)14 PipeRunResult (nl.nn.adapterframework.core.PipeRunResult)13 IOException (java.io.IOException)11 ConfigurationException (nl.nn.adapterframework.configuration.ConfigurationException)10 TimeOutException (nl.nn.adapterframework.core.TimeOutException)7 HashMap (java.util.HashMap)6 Map (java.util.Map)6 ParameterList (nl.nn.adapterframework.parameters.ParameterList)6 JMSException (javax.jms.JMSException)5 PipeForward (nl.nn.adapterframework.core.PipeForward)5 Parameter (nl.nn.adapterframework.parameters.Parameter)5 ParameterValue (nl.nn.adapterframework.parameters.ParameterValue)5 DomBuilderException (nl.nn.adapterframework.util.DomBuilderException)5 URL (java.net.URL)4 ArrayList (java.util.ArrayList)4 INamedObject (nl.nn.adapterframework.core.INamedObject)4