Search in sources :

Example 6 with Resource

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

the class ClassLoaderURIResolver method resolveToResource.

public Resource resolveToResource(String href, String base) throws TransformerException {
    String absoluteOrRelativeRef;
    String globalClasspathRef = null;
    String protocol = null;
    if (href.startsWith("/") || href.contains(":")) {
        // href is absolute, search on the full classpath
        absoluteOrRelativeRef = href;
        if (href.contains(":")) {
            protocol = href.substring(0, href.indexOf(":"));
        }
        if (StringUtils.isNotEmpty(protocol)) {
            // if href contains a protocol, verify that it's allowed to look it up
            if (allowedProtocols.isEmpty()) {
                throw new TransformerException("Cannot lookup resource [" + href + "] with protocol [" + protocol + "], no allowedProtocols");
            } else if (!allowedProtocols.contains(protocol)) {
                throw new TransformerException("Cannot lookup resource [" + href + "] not allowed with protocol [" + protocol + "] allowedProtocols " + allowedProtocols.toString());
            }
        }
    } else {
        // It must be relative to the base, or if that not exists, on the root of the classpath
        if (base != null && base.contains("/")) {
            absoluteOrRelativeRef = base.substring(0, base.lastIndexOf("/") + 1) + href;
            // if ref1 fails, try href on the global classpath
            globalClasspathRef = href;
            if (base.contains(":")) {
                protocol = base.substring(0, base.indexOf(":"));
            }
        } else {
            // cannot use base to prefix href
            absoluteOrRelativeRef = href;
        }
    }
    String ref = absoluteOrRelativeRef;
    Resource resource = Resource.getResource(scopeProvider, ref, protocol);
    if (resource == null && globalClasspathRef != null) {
        if (log.isDebugEnabled())
            log.debug("Could not resolve href [" + href + "] base [" + base + "] as [" + ref + "], now trying ref2 [" + globalClasspathRef + "] protocol [" + protocol + "]");
        ref = globalClasspathRef;
        resource = Resource.getResource(scopeProvider, ref, null);
    }
    if (resource == null) {
        String message = "Cannot get resource for href [" + href + "] with base [" + base + "] as ref [" + ref + "]" + (globalClasspathRef == null ? "" : " nor as ref [" + absoluteOrRelativeRef + "]") + " protocol [" + protocol + "] in scope [" + scopeProvider + "]";
        // log.warn(message); // TODO could log this message here, because Saxon does not log the details of the exception thrown. This will cause some duplicate messages, however. See for instance XsltSenderTest for example.
        throw new TransformerException(message);
    }
    if (log.isDebugEnabled())
        log.debug("resolved href [" + href + "] base [" + base + "] to resource [" + resource + "]");
    return resource;
}
Also used : Resource(nl.nn.adapterframework.core.Resource) TransformerException(javax.xml.transform.TransformerException)

Example 7 with Resource

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

the class ConfigurationDigesterTest method simpleXsdWithDefaultAndFixedAttributed.

@Test
public void simpleXsdWithDefaultAndFixedAttributed() throws Exception {
    URL schemaURL = TestFileUtils.getTestFileURL("/Digester/resolveDefaultAttribute.xsd");
    ValidatorHandler validatorHandler = XmlUtils.getValidatorHandler(schemaURL);
    XmlWriter writer = new XmlWriter();
    validatorHandler.setContentHandler(writer);
    validatorHandler.setErrorHandler(new XmlErrorHandler());
    Resource resource = Resource.getResource("/Digester/resolveAttributes.xml");
    XmlUtils.parseXml(resource, validatorHandler);
    assertEquals("<note one=\"1\" two=\"2\"/>", writer.toString().trim());
}
Also used : ValidatorHandler(javax.xml.validation.ValidatorHandler) Resource(nl.nn.adapterframework.core.Resource) URL(java.net.URL) XmlWriter(nl.nn.adapterframework.xml.XmlWriter) Test(org.junit.Test)

Example 8 with Resource

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

the class MigratorTest method testSQLWriter.

@Test
public void testSQLWriter() throws Exception {
    assumeTrue(getDataSourceName().equals("H2"));
    Resource resource = Resource.getResource("/Migrator/DatabaseChangelog_plus_changes.xml");
    assertNotNull(resource);
    StringWriter writer = new StringWriter();
    migrator.update(writer, resource);
    String sqlChanges = TestFileUtils.getTestFile("/Migrator/sql_changes.sql");
    String result = applyIgnores(writer.toString());
    result = removeComments(result);
    TestAssertions.assertEqualsIgnoreCRLF(sqlChanges, result);
}
Also used : StringWriter(java.io.StringWriter) Resource(nl.nn.adapterframework.core.Resource) Test(org.junit.Test)

Example 9 with Resource

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

the class TransformerPoolTest method plainViaUrl.

@Test
public void plainViaUrl() throws Exception {
    Resource resource = Resource.getResource(stylesheetURL);
    TransformerPool transformerPool = TransformerPool.getInstance(resource);
    String result = transformerPool.transform(xml, null);
    result = result.replaceAll("[\n\r]", "");
    assertEquals(expectedStylesheetURL, result);
}
Also used : Resource(nl.nn.adapterframework.core.Resource) Test(org.junit.Test)

Example 10 with Resource

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

the class SendTibcoMessage method doPipeWithTimeoutGuarded.

@Override
public PipeRunResult doPipeWithTimeoutGuarded(Message input, PipeLineSession session) throws PipeRunException {
    Connection connection = null;
    Session jSession = null;
    MessageProducer msgProducer = null;
    Destination destination = null;
    String url_work;
    String authAlias_work;
    String userName_work;
    String password_work;
    String queueName_work;
    MessageProtocol protocol = getMessageProtocol();
    int replyTimeout_work;
    String soapAction_work;
    String result = null;
    try {
        input.preserve();
    } catch (IOException e) {
        throw new PipeRunException(this, "cannot preserve input", e);
    }
    ParameterValueList pvl = null;
    if (getParameterList() != null) {
        try {
            pvl = getParameterList().getValues(input, session);
        } catch (ParameterException e) {
            throw new PipeRunException(this, getLogPrefix(session) + "exception on extracting parameters", e);
        }
    }
    url_work = getParameterValue(pvl, "url");
    if (url_work == null) {
        url_work = getUrl();
    }
    authAlias_work = getParameterValue(pvl, "authAlias");
    if (authAlias_work == null) {
        authAlias_work = getAuthAlias();
    }
    userName_work = (pvl.contains("username")) ? getParameterValue(pvl, "username") : getParameterValue(pvl, "userName");
    if (userName_work == null) {
        userName_work = getUsername();
    }
    password_work = getParameterValue(pvl, "password");
    if (password_work == null) {
        password_work = getPassword();
    }
    queueName_work = getParameterValue(pvl, "queueName");
    if (queueName_work == null) {
        queueName_work = getQueueName();
    }
    String protocolParam = getParameterValue(pvl, "messageProtocol");
    if (protocolParam != null) {
        protocol = EnumUtils.parse(MessageProtocol.class, protocolParam);
    }
    String replyTimeout_work_str = getParameterValue(pvl, "replyTimeout");
    if (replyTimeout_work_str == null) {
        replyTimeout_work = getReplyTimeout();
    } else {
        replyTimeout_work = Integer.parseInt(replyTimeout_work_str);
    }
    soapAction_work = getParameterValue(pvl, "soapAction");
    if (soapAction_work == null)
        soapAction_work = getSoapAction();
    if (StringUtils.isEmpty(soapAction_work) && !StringUtils.isEmpty(queueName_work)) {
        String[] q = queueName_work.split("\\.");
        if (q.length > 0) {
            if (q[0].equalsIgnoreCase("P2P") && q.length >= 4) {
                soapAction_work = q[3];
            } else if (q[0].equalsIgnoreCase("ESB") && q.length == 8) {
                soapAction_work = q[5] + "_" + q[6];
            } else if (q[0].equalsIgnoreCase("ESB") && q.length > 8) {
                soapAction_work = q[6] + "_" + q[7];
            }
        }
    }
    if (StringUtils.isEmpty(soapAction_work)) {
        log.debug(getLogPrefix(session) + "deriving default soapAction");
        try {
            Resource resource = Resource.getResource(this, "/xml/xsl/esb/soapAction.xsl");
            TransformerPool tp = TransformerPool.getInstance(resource, 2);
            soapAction_work = tp.transform(input.asString(), null);
        } catch (Exception e) {
            log.error(getLogPrefix(session) + "failed to execute soapAction.xsl");
        }
    }
    if (protocol == null) {
        throw new PipeRunException(this, getLogPrefix(session) + "messageProtocol must be set");
    }
    CredentialFactory cf = new CredentialFactory(authAlias_work, userName_work, password_work);
    try {
        TibjmsAdmin admin;
        try {
            admin = TibcoUtils.getActiveServerAdmin(url_work, cf);
        } catch (TibjmsAdminException e) {
            log.debug(getLogPrefix(session) + "caught exception", e);
            admin = null;
        }
        if (admin != null) {
            QueueInfo queueInfo;
            try {
                queueInfo = admin.getQueue(queueName_work);
            } catch (Exception e) {
                throw new PipeRunException(this, getLogPrefix(session) + " exception on getting queue info", e);
            }
            if (queueInfo == null) {
                throw new PipeRunException(this, getLogPrefix(session) + " queue [" + queueName_work + "] does not exist");
            }
            try {
                admin.close();
            } catch (TibjmsAdminException e) {
                log.warn(getLogPrefix(session) + "exception on closing Tibjms Admin", e);
            }
        }
        ConnectionFactory factory = new com.tibco.tibjms.TibjmsConnectionFactory(url_work);
        connection = factory.createConnection(cf.getUsername(), cf.getPassword());
        jSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        destination = jSession.createQueue(queueName_work);
        msgProducer = jSession.createProducer(destination);
        TextMessage msg = jSession.createTextMessage();
        msg.setText(input.asString());
        Destination replyQueue = null;
        if (protocol == MessageProtocol.REQUEST_REPLY) {
            replyQueue = jSession.createTemporaryQueue();
            msg.setJMSReplyTo(replyQueue);
            msg.setJMSDeliveryMode(DeliveryMode.NON_PERSISTENT);
            msgProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
            msgProducer.setTimeToLive(replyTimeout_work);
        } else {
            msg.setJMSDeliveryMode(DeliveryMode.PERSISTENT);
            msgProducer.setDeliveryMode(DeliveryMode.PERSISTENT);
        }
        if (StringUtils.isNotEmpty(soapAction_work)) {
            log.debug(getLogPrefix(session) + "setting [SoapAction] property to value [" + soapAction_work + "]");
            msg.setStringProperty("SoapAction", soapAction_work);
        }
        msgProducer.send(msg);
        if (log.isDebugEnabled()) {
            log.debug(getLogPrefix(session) + "sent message [" + msg.getText() + "] " + "to [" + msgProducer.getDestination() + "] " + "msgID [" + msg.getJMSMessageID() + "] " + "correlationID [" + msg.getJMSCorrelationID() + "] " + "replyTo [" + msg.getJMSReplyTo() + "]");
        } else {
            if (log.isInfoEnabled()) {
                log.info(getLogPrefix(session) + "sent message to [" + msgProducer.getDestination() + "] " + "msgID [" + msg.getJMSMessageID() + "] " + "correlationID [" + msg.getJMSCorrelationID() + "] " + "replyTo [" + msg.getJMSReplyTo() + "]");
            }
        }
        if (protocol == MessageProtocol.REQUEST_REPLY) {
            String replyCorrelationId = msg.getJMSMessageID();
            MessageConsumer msgConsumer = jSession.createConsumer(replyQueue, "JMSCorrelationID='" + replyCorrelationId + "'");
            log.debug(getLogPrefix(session) + "] start waiting for reply on [" + replyQueue + "] selector [" + replyCorrelationId + "] for [" + replyTimeout_work + "] ms");
            connection.start();
            javax.jms.Message rawReplyMsg = msgConsumer.receive(replyTimeout_work);
            if (rawReplyMsg == null) {
                throw new PipeRunException(this, getLogPrefix(session) + "did not receive reply on [" + replyQueue + "] replyCorrelationId [" + replyCorrelationId + "] within [" + replyTimeout_work + "] ms");
            }
            TextMessage replyMsg = (TextMessage) rawReplyMsg;
            result = replyMsg.getText();
        } else {
            result = msg.getJMSMessageID();
        }
    } catch (IOException | JMSException e) {
        throw new PipeRunException(this, getLogPrefix(session) + " exception on sending message to Tibco queue", e);
    } finally {
        if (connection != null) {
            try {
                connection.close();
            } catch (JMSException e) {
                log.warn(getLogPrefix(session) + "exception on closing connection", e);
            }
        }
    }
    return new PipeRunResult(getSuccessForward(), result);
}
Also used : QueueInfo(com.tibco.tibjms.admin.QueueInfo) Destination(javax.jms.Destination) ParameterValueList(nl.nn.adapterframework.parameters.ParameterValueList) JMSException(javax.jms.JMSException) TransformerPool(nl.nn.adapterframework.util.TransformerPool) PipeRunResult(nl.nn.adapterframework.core.PipeRunResult) ConnectionFactory(javax.jms.ConnectionFactory) ParameterException(nl.nn.adapterframework.core.ParameterException) MessageConsumer(javax.jms.MessageConsumer) CredentialFactory(nl.nn.adapterframework.util.CredentialFactory) Connection(javax.jms.Connection) Resource(nl.nn.adapterframework.core.Resource) IOException(java.io.IOException) TibjmsAdmin(com.tibco.tibjms.admin.TibjmsAdmin) PipeRunException(nl.nn.adapterframework.core.PipeRunException) IOException(java.io.IOException) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) JMSException(javax.jms.JMSException) TibjmsAdminException(com.tibco.tibjms.admin.TibjmsAdminException) ParameterException(nl.nn.adapterframework.core.ParameterException) TibjmsAdminException(com.tibco.tibjms.admin.TibjmsAdminException) PipeRunException(nl.nn.adapterframework.core.PipeRunException) MessageProducer(javax.jms.MessageProducer) TextMessage(javax.jms.TextMessage) PipeLineSession(nl.nn.adapterframework.core.PipeLineSession) Session(javax.jms.Session)

Aggregations

Resource (nl.nn.adapterframework.core.Resource)27 Test (org.junit.Test)12 IOException (java.io.IOException)10 ConfigurationException (nl.nn.adapterframework.configuration.ConfigurationException)7 XmlWriter (nl.nn.adapterframework.xml.XmlWriter)7 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)6 SAXException (org.xml.sax.SAXException)6 TransformerException (javax.xml.transform.TransformerException)4 TransformerPool (nl.nn.adapterframework.util.TransformerPool)4 URL (java.net.URL)3 PipeRunException (nl.nn.adapterframework.core.PipeRunException)3 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 ValidatorHandler (javax.xml.validation.ValidatorHandler)2 ConfigurationDigester (nl.nn.adapterframework.configuration.ConfigurationDigester)2 CredentialFactory (nl.nn.adapterframework.util.CredentialFactory)2 Digester (org.apache.commons.digester3.Digester)2 QueueInfo (com.tibco.tibjms.admin.QueueInfo)1 TibjmsAdmin (com.tibco.tibjms.admin.TibjmsAdmin)1 TibjmsAdminException (com.tibco.tibjms.admin.TibjmsAdminException)1 File (java.io.File)1