Search in sources :

Example 21 with ParameterValue

use of nl.nn.adapterframework.parameters.ParameterValue in project iaf by ibissource.

the class JmsSender method setProperties.

/**
 * sets the JMS message properties as descriped in the msgProperties arraylist
 * @param msg
 * @param msgProperties
 * @throws JMSException
 */
private void setProperties(Message msg, ParameterValueList msgProperties) throws JMSException {
    for (int i = 0; i < msgProperties.size(); i++) {
        ParameterValue property = msgProperties.getParameterValue(i);
        String type = property.getDefinition().getType();
        String name = property.getDefinition().getName();
        if (!isSoap() || !name.equals(getSoapHeaderParam())) {
            if (log.isDebugEnabled()) {
                log.debug(getLogPrefix() + "setting [" + type + "] property from param [" + name + "] to value [" + property.getValue() + "]");
            }
            if ("boolean".equalsIgnoreCase(type))
                msg.setBooleanProperty(name, property.asBooleanValue(false));
            else if ("byte".equalsIgnoreCase(type))
                msg.setByteProperty(name, property.asByteValue((byte) 0));
            else if ("double".equalsIgnoreCase(type))
                msg.setDoubleProperty(name, property.asDoubleValue(0));
            else if ("float".equalsIgnoreCase(type))
                msg.setFloatProperty(name, property.asFloatValue(0));
            else if ("int".equalsIgnoreCase(type))
                msg.setIntProperty(name, property.asIntegerValue(0));
            else if ("long".equalsIgnoreCase(type))
                msg.setLongProperty(name, property.asLongValue(0L));
            else if ("short".equalsIgnoreCase(type))
                msg.setShortProperty(name, property.asShortValue((short) 0));
            else if ("string".equalsIgnoreCase(type))
                msg.setStringProperty(name, property.asStringValue(""));
            else
                // if ("object".equalsIgnoreCase(type))
                msg.setObjectProperty(name, property.getValue());
        }
    }
}
Also used : ParameterValue(nl.nn.adapterframework.parameters.ParameterValue)

Example 22 with ParameterValue

use of nl.nn.adapterframework.parameters.ParameterValue in project iaf by ibissource.

the class JmsSender method sendMessage.

public String sendMessage(String correlationID, String message, ParameterResolutionContext prc, String soapHeader) throws SenderException, TimeOutException {
    Session s = null;
    MessageProducer mp = null;
    ParameterValueList pvl = null;
    if (prc != null && paramList != null) {
        try {
            pvl = prc.getValues(paramList);
        } catch (ParameterException e) {
            throw new SenderException(getLogPrefix() + "cannot extract parameters", e);
        }
    }
    if (isSoap()) {
        if (soapHeader == null) {
            if (pvl != null && StringUtils.isNotEmpty(getSoapHeaderParam())) {
                ParameterValue soapHeaderParamValue = pvl.getParameterValue(getSoapHeaderParam());
                if (soapHeaderParamValue == null) {
                    log.warn("no SoapHeader found using parameter [" + getSoapHeaderParam() + "]");
                } else {
                    soapHeader = soapHeaderParamValue.asStringValue("");
                }
            }
        }
        message = soapWrapper.putInEnvelope(message, getEncodingStyleURI(), getServiceNamespaceURI(), soapHeader);
        if (log.isDebugEnabled())
            log.debug(getLogPrefix() + "correlationId [" + correlationID + "] soap message [" + message + "]");
    }
    try {
        s = createSession();
        mp = getMessageProducer(s, getDestination(prc));
        Destination replyQueue = null;
        // create message
        Message msg = createTextMessage(s, correlationID, message);
        if (getMessageType() != null) {
            msg.setJMSType(getMessageType());
        }
        if (getDeliveryModeInt() > 0) {
            msg.setJMSDeliveryMode(getDeliveryModeInt());
            mp.setDeliveryMode(getDeliveryModeInt());
        }
        if (getPriority() >= 0) {
            msg.setJMSPriority(getPriority());
            mp.setPriority(getPriority());
        }
        // set properties
        if (pvl != null) {
            setProperties(msg, pvl);
        }
        if (replyToName != null) {
            replyQueue = getDestination(replyToName);
        } else {
            if (isSynchronous()) {
                replyQueue = getMessagingSource().getDynamicReplyQueue(s);
            }
        }
        if (replyQueue != null) {
            msg.setJMSReplyTo(replyQueue);
            if (log.isDebugEnabled())
                log.debug("replyTo set to queue [" + replyQueue.toString() + "]");
        }
        // send message
        send(mp, msg);
        if (log.isDebugEnabled()) {
            log.debug("[" + getName() + "] " + "sent message [" + message + "] " + "to [" + mp.getDestination() + "] " + "msgID [" + msg.getJMSMessageID() + "] " + "correlationID [" + msg.getJMSCorrelationID() + "] " + "using deliveryMode [" + getDeliveryMode() + "] " + ((replyToName != null) ? "replyTo [" + replyToName + "]" : ""));
        } else {
            if (log.isInfoEnabled()) {
                log.info("[" + getName() + "] " + "sent message to [" + mp.getDestination() + "] " + "msgID [" + msg.getJMSMessageID() + "] " + "correlationID [" + msg.getJMSCorrelationID() + "] " + "using deliveryMode [" + getDeliveryMode() + "] " + ((replyToName != null) ? "replyTo [" + replyToName + "]" : ""));
            }
        }
        if (isSynchronous()) {
            String replyCorrelationId = null;
            if (replyToName != null) {
                if ("CORRELATIONID".equalsIgnoreCase(getLinkMethod())) {
                    replyCorrelationId = correlationID;
                } else if ("CORRELATIONID_FROM_MESSAGE".equalsIgnoreCase(getLinkMethod())) {
                    replyCorrelationId = msg.getJMSCorrelationID();
                } else {
                    replyCorrelationId = msg.getJMSMessageID();
                }
            }
            if (log.isDebugEnabled())
                log.debug("[" + getName() + "] start waiting for reply on [" + replyQueue + "] requestMsgId [" + msg.getJMSMessageID() + "] replyCorrelationId [" + replyCorrelationId + "] for [" + getReplyTimeout() + "] ms");
            MessageConsumer mc = getMessageConsumerForCorrelationId(s, replyQueue, replyCorrelationId);
            try {
                Message rawReplyMsg = mc.receive(getReplyTimeout());
                if (rawReplyMsg == null) {
                    throw new TimeOutException("did not receive reply on [" + replyQueue + "] requestMsgId [" + msg.getJMSMessageID() + "] replyCorrelationId [" + replyCorrelationId + "] within [" + getReplyTimeout() + "] ms");
                }
                return getStringFromRawMessage(rawReplyMsg, prc != null ? prc.getSession() : null, isSoap(), getReplySoapHeaderSessionKey(), soapWrapper);
            } finally {
                if (mc != null) {
                    try {
                        mc.close();
                    } catch (JMSException e) {
                        log.warn("JmsSender [" + getName() + "] got exception closing message consumer for reply", e);
                    }
                }
            }
        }
        return msg.getJMSMessageID();
    } catch (JMSException e) {
        throw new SenderException(e);
    } catch (IOException e) {
        throw new SenderException(e);
    } catch (NamingException e) {
        throw new SenderException(e);
    } catch (DomBuilderException e) {
        throw new SenderException(e);
    } catch (TransformerException e) {
        throw new SenderException(e);
    } catch (JmsException e) {
        throw new SenderException(e);
    } finally {
        if (mp != null) {
            try {
                mp.close();
            } catch (JMSException e) {
                log.warn("JmsSender [" + getName() + "] got exception closing message producer", e);
            }
        }
        closeSession(s);
    }
}
Also used : Destination(javax.jms.Destination) ParameterValueList(nl.nn.adapterframework.parameters.ParameterValueList) MessageConsumer(javax.jms.MessageConsumer) ParameterValue(nl.nn.adapterframework.parameters.ParameterValue) Message(javax.jms.Message) JMSException(javax.jms.JMSException) IOException(java.io.IOException) TimeOutException(nl.nn.adapterframework.core.TimeOutException) ParameterException(nl.nn.adapterframework.core.ParameterException) NamingException(javax.naming.NamingException) MessageProducer(javax.jms.MessageProducer) DomBuilderException(nl.nn.adapterframework.util.DomBuilderException) SenderException(nl.nn.adapterframework.core.SenderException) TransformerException(javax.xml.transform.TransformerException) Session(javax.jms.Session)

Example 23 with ParameterValue

use of nl.nn.adapterframework.parameters.ParameterValue in project iaf by ibissource.

the class FixedResult method doPipe.

public PipeRunResult doPipe(Object input, IPipeLineSession session) throws PipeRunException {
    String result = returnString;
    if ((StringUtils.isNotEmpty(getFileName()) && isLookupAtRuntime()) || StringUtils.isNotEmpty(getFileNameSessionKey())) {
        String fileName = null;
        if (StringUtils.isNotEmpty(getFileNameSessionKey())) {
            fileName = (String) session.get(fileNameSessionKey);
        }
        if (fileName == null) {
            if (StringUtils.isNotEmpty(getFileName()) && isLookupAtRuntime()) {
                fileName = getFileName();
            }
        }
        URL resource = null;
        try {
            resource = ClassUtils.getResourceURL(classLoader, fileName);
        } catch (Throwable e) {
            throw new PipeRunException(this, getLogPrefix(session) + "got exception searching for [" + fileName + "]", e);
        }
        if (resource == null) {
            PipeForward fileNotFoundForward = findForward(FILE_NOT_FOUND_FORWARD);
            if (fileNotFoundForward != null) {
                return new PipeRunResult(fileNotFoundForward, input);
            } else {
                throw new PipeRunException(this, getLogPrefix(session) + "cannot find resource [" + fileName + "]");
            }
        }
        try {
            result = Misc.resourceToString(resource, SystemUtils.LINE_SEPARATOR);
        } catch (Throwable e) {
            throw new PipeRunException(this, getLogPrefix(session) + "got exception loading [" + fileName + "]", e);
        }
    }
    if (getParameterList() != null) {
        ParameterResolutionContext prc = new ParameterResolutionContext((String) input, session);
        ParameterValueList pvl;
        try {
            pvl = prc.getValues(getParameterList());
        } catch (ParameterException e) {
            throw new PipeRunException(this, getLogPrefix(session) + "exception extracting parameters", e);
        }
        for (int i = 0; i < pvl.size(); i++) {
            ParameterValue pv = pvl.getParameterValue(i);
            String replaceFrom;
            if (isReplaceFixedParams()) {
                replaceFrom = pv.getDefinition().getName();
            } else {
                replaceFrom = "${" + pv.getDefinition().getName() + "}";
            }
            result = replace(result, replaceFrom, pv.asStringValue(""));
        }
    }
    if (getSubstituteVars()) {
        result = StringResolver.substVars(returnString, session, appConstants);
    }
    if (StringUtils.isNotEmpty(styleSheetName)) {
        URL xsltSource = ClassUtils.getResourceURL(classLoader, styleSheetName);
        if (xsltSource != null) {
            try {
                String xsltResult = null;
                Transformer transformer = XmlUtils.createTransformer(xsltSource);
                xsltResult = XmlUtils.transformXml(transformer, result);
                result = xsltResult;
            } catch (IOException e) {
                throw new PipeRunException(this, getLogPrefix(session) + "cannot retrieve [" + styleSheetName + "], resource [" + xsltSource.toString() + "]", e);
            } catch (TransformerConfigurationException te) {
                throw new PipeRunException(this, getLogPrefix(session) + "got error creating transformer from file [" + styleSheetName + "]", te);
            } catch (TransformerException te) {
                throw new PipeRunException(this, getLogPrefix(session) + "got error transforming resource [" + xsltSource.toString() + "] from [" + styleSheetName + "]", te);
            } catch (DomBuilderException te) {
                throw new PipeRunException(this, getLogPrefix(session) + "caught DomBuilderException", te);
            }
        }
    }
    log.debug(getLogPrefix(session) + " returning fixed result [" + result + "]");
    return new PipeRunResult(getForward(), result);
}
Also used : ParameterValueList(nl.nn.adapterframework.parameters.ParameterValueList) Transformer(javax.xml.transform.Transformer) ParameterValue(nl.nn.adapterframework.parameters.ParameterValue) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) IOException(java.io.IOException) PipeForward(nl.nn.adapterframework.core.PipeForward) URL(java.net.URL) PipeRunResult(nl.nn.adapterframework.core.PipeRunResult) PipeRunException(nl.nn.adapterframework.core.PipeRunException) ParameterException(nl.nn.adapterframework.core.ParameterException) DomBuilderException(nl.nn.adapterframework.util.DomBuilderException) ParameterResolutionContext(nl.nn.adapterframework.parameters.ParameterResolutionContext) TransformerException(javax.xml.transform.TransformerException)

Example 24 with ParameterValue

use of nl.nn.adapterframework.parameters.ParameterValue in project iaf by ibissource.

the class HttpSenderBase method appendParameters.

protected boolean appendParameters(boolean parametersAppended, StringBuffer path, ParameterValueList parameters, Map<String, String> headersParamsMap) throws SenderException {
    if (parameters != null) {
        if (log.isDebugEnabled())
            log.debug(getLogPrefix() + "appending [" + parameters.size() + "] parameters");
    }
    for (int i = 0; i < parameters.size(); i++) {
        if (parametersToSkip.contains(paramList.get(i))) {
            if (log.isDebugEnabled())
                log.debug(getLogPrefix() + "skipping [" + paramList.get(i) + "]");
            continue;
        }
        ParameterValue pv = parameters.getParameterValue(i);
        if (headersParamsMap.keySet().contains(pv.getDefinition().getName())) {
            headersParamsMap.put(pv.getDefinition().getName(), pv.asStringValue(""));
        } else {
            try {
                if (parametersAppended) {
                    path.append("&");
                } else {
                    path.append("?");
                    parametersAppended = true;
                }
                String parameterToAppend = pv.getDefinition().getName() + "=" + URLEncoder.encode(pv.asStringValue(""), getCharSet());
                if (log.isDebugEnabled())
                    log.debug(getLogPrefix() + "appending parameter [" + parameterToAppend + "]");
                path.append(parameterToAppend);
            } catch (UnsupportedEncodingException e) {
                throw new SenderException(getLogPrefix() + "[" + getCharSet() + "] encoding error. Failed to add parameter [" + pv.getDefinition().getName() + "]");
            }
        }
    }
    return parametersAppended;
}
Also used : ParameterValue(nl.nn.adapterframework.parameters.ParameterValue) UnsupportedEncodingException(java.io.UnsupportedEncodingException) SenderException(nl.nn.adapterframework.core.SenderException)

Example 25 with ParameterValue

use of nl.nn.adapterframework.parameters.ParameterValue in project iaf by ibissource.

the class HttpSender method createMultiPartEntity.

protected HttpEntity createMultiPartEntity(String message, ParameterValueList parameters, IPipeLineSession session) throws SenderException {
    MultipartEntityBuilder entity = MultipartEntityBuilder.create();
    entity.setCharset(Charset.forName(getCharSet()));
    if (isMtomEnabled())
        entity.setMtomMultipart();
    if (StringUtils.isNotEmpty(getInputMessageParam())) {
        entity.addPart(createMultipartBodypart(getInputMessageParam(), message));
        log.debug(getLogPrefix() + "appended stringpart [" + getInputMessageParam() + "] with value [" + message + "]");
    }
    if (parameters != null) {
        for (int i = 0; i < parameters.size(); i++) {
            ParameterValue pv = parameters.getParameterValue(i);
            String paramType = pv.getDefinition().getType();
            String name = pv.getDefinition().getName();
            if (Parameter.TYPE_INPUTSTREAM.equals(paramType)) {
                Object value = pv.getValue();
                if (value instanceof InputStream) {
                    InputStream fis = (InputStream) value;
                    String fileName = null;
                    String sessionKey = pv.getDefinition().getSessionKey();
                    if (sessionKey != null) {
                        fileName = (String) session.get(sessionKey + "Name");
                    }
                    entity.addPart(createMultipartBodypart(name, fis, fileName));
                    log.debug(getLogPrefix() + "appended filepart [" + name + "] with value [" + value + "] and name [" + fileName + "]");
                } else {
                    throw new SenderException(getLogPrefix() + "unknown inputstream [" + value.getClass() + "] for parameter [" + name + "]");
                }
            } else {
                String value = pv.asStringValue("");
                entity.addPart(createMultipartBodypart(name, value));
                log.debug(getLogPrefix() + "appended stringpart [" + name + "] with value [" + value + "]");
            }
        }
    }
    if (StringUtils.isNotEmpty(getMultipartXmlSessionKey())) {
        String multipartXml = (String) session.get(getMultipartXmlSessionKey());
        if (StringUtils.isEmpty(multipartXml)) {
            log.warn(getLogPrefix() + "sessionKey [" + getMultipartXmlSessionKey() + "] is empty");
        } else {
            Element partsElement;
            try {
                partsElement = XmlUtils.buildElement(multipartXml);
            } catch (DomBuilderException e) {
                throw new SenderException(getLogPrefix() + "error building multipart xml", e);
            }
            Collection<Node> parts = XmlUtils.getChildTags(partsElement, "part");
            if (parts == null || parts.size() == 0) {
                log.warn(getLogPrefix() + "no part(s) in multipart xml [" + multipartXml + "]");
            } else {
                Iterator<Node> iter = parts.iterator();
                while (iter.hasNext()) {
                    Element partElement = (Element) iter.next();
                    // String partType = partElement.getAttribute("type");
                    String partName = partElement.getAttribute("name");
                    String partSessionKey = partElement.getAttribute("sessionKey");
                    String partMimeType = partElement.getAttribute("mimeType");
                    Object partObject = session.get(partSessionKey);
                    if (partObject instanceof InputStream) {
                        InputStream fis = (InputStream) partObject;
                        entity.addPart(createMultipartBodypart(partSessionKey, fis, partName, partMimeType));
                        log.debug(getLogPrefix() + "appended filepart [" + partSessionKey + "] with value [" + partObject + "] and name [" + partName + "]");
                    } else {
                        String partValue = (String) session.get(partSessionKey);
                        entity.addPart(createMultipartBodypart(partName, partValue, partMimeType));
                        log.debug(getLogPrefix() + "appended stringpart [" + partSessionKey + "]  with value [" + partValue + "]");
                    }
                }
            }
        }
    }
    // entity.seContentType(contentTypeMtom);
    return entity.build();
}
Also used : MultipartEntityBuilder(nl.nn.adapterframework.http.mime.MultipartEntityBuilder) ParameterValue(nl.nn.adapterframework.parameters.ParameterValue) InputStream(java.io.InputStream) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) DomBuilderException(nl.nn.adapterframework.util.DomBuilderException) SenderException(nl.nn.adapterframework.core.SenderException)

Aggregations

ParameterValue (nl.nn.adapterframework.parameters.ParameterValue)38 ParameterValueList (nl.nn.adapterframework.parameters.ParameterValueList)21 ParameterException (nl.nn.adapterframework.core.ParameterException)19 SenderException (nl.nn.adapterframework.core.SenderException)18 IOException (java.io.IOException)16 PipeRunException (nl.nn.adapterframework.core.PipeRunException)12 PipeRunResult (nl.nn.adapterframework.core.PipeRunResult)9 Message (nl.nn.adapterframework.stream.Message)7 URL (java.net.URL)6 TransformerException (javax.xml.transform.TransformerException)6 UnsupportedEncodingException (java.io.UnsupportedEncodingException)5 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)5 ParameterList (nl.nn.adapterframework.parameters.ParameterList)5 InputStream (java.io.InputStream)4 Transformer (javax.xml.transform.Transformer)4 DomBuilderException (nl.nn.adapterframework.util.DomBuilderException)4 JMSException (javax.jms.JMSException)3 ConfigurationException (nl.nn.adapterframework.configuration.ConfigurationException)3 TimeoutException (nl.nn.adapterframework.core.TimeoutException)3 ParameterResolutionContext (nl.nn.adapterframework.parameters.ParameterResolutionContext)3