Search in sources :

Example 1 with Request

use of org.opennms.protocols.xml.config.Request in project opennms by OpenNMS.

the class Sftp3gppVTDXmlCollectionHandler method collect.

@Override
public CollectionSet collect(CollectionAgent agent, XmlDataCollection collection, Map<String, Object> parameters) throws CollectionException {
    String status = "finished";
    // Create a new collection set.
    CollectionSetBuilder builder = new CollectionSetBuilder(agent);
    // TODO We could be careful when handling exceptions because parsing exceptions will be treated different from connection or retrieval exceptions
    DateTime startTime = new DateTime();
    Sftp3gppUrlConnection connection = null;
    try {
        // FIXME: Does not support storeByFS
        ResourcePath resourcePath = ResourcePath.get(Integer.toString(agent.getNodeId()));
        for (XmlSource source : collection.getXmlSources()) {
            if (!source.getUrl().startsWith(Sftp3gppUrlHandler.PROTOCOL)) {
                throw new CollectionException("The 3GPP SFTP Collection Handler can only use the protocol " + Sftp3gppUrlHandler.PROTOCOL);
            }
            final String urlStr = source.getUrl();
            final Request request = source.getRequest();
            URL url = UrlFactory.getUrl(source.getUrl(), source.getRequest());
            String lastFile = Sftp3gppUtils.getLastFilename(getResourceStorageDao(), getServiceName(), resourcePath, url.getPath());
            connection = (Sftp3gppUrlConnection) url.openConnection();
            if (lastFile == null) {
                lastFile = connection.get3gppFileName();
                LOG.debug("collect(single): retrieving file from {}{}{} from {}", url.getPath(), File.separatorChar, lastFile, agent.getHostAddress());
                VTDNav doc = getVTDXmlDocument(urlStr, request);
                fillCollectionSet(agent, builder, source, doc);
                Sftp3gppUtils.setLastFilename(getResourceStorageDao(), getServiceName(), resourcePath, url.getPath(), lastFile);
                Sftp3gppUtils.deleteFile(connection, lastFile);
            } else {
                connection.connect();
                List<String> files = connection.getFileList();
                long lastTs = connection.getTimeStampFromFile(lastFile);
                boolean collected = false;
                for (String fileName : files) {
                    if (connection.getTimeStampFromFile(fileName) > lastTs) {
                        LOG.debug("collect(multiple): retrieving file {} from {}", fileName, agent.getHostAddress());
                        InputStream is = connection.getFile(fileName);
                        try {
                            VTDNav doc = getVTDXmlDocument(is, request);
                            fillCollectionSet(agent, builder, source, doc);
                        } finally {
                            IOUtils.closeQuietly(is);
                        }
                        Sftp3gppUtils.setLastFilename(getResourceStorageDao(), getServiceName(), resourcePath, url.getPath(), fileName);
                        Sftp3gppUtils.deleteFile(connection, fileName);
                        collected = true;
                    }
                }
                if (!collected) {
                    LOG.warn("collect: could not find any file after {} on {}", lastFile, agent);
                }
            }
        }
        return builder.build();
    } catch (Exception e) {
        status = "failed";
        throw new CollectionException(e.getMessage(), e);
    } finally {
        DateTime endTime = new DateTime();
        LOG.debug("collect: {} collection {}: duration: {} ms", status, collection.getName(), endTime.getMillis() - startTime.getMillis());
        UrlFactory.disconnect(connection);
    }
}
Also used : CollectionSetBuilder(org.opennms.netmgt.collection.support.builder.CollectionSetBuilder) CollectionException(org.opennms.netmgt.collection.api.CollectionException) InputStream(java.io.InputStream) Request(org.opennms.protocols.xml.config.Request) DateTime(org.joda.time.DateTime) XmlSource(org.opennms.protocols.xml.config.XmlSource) URL(java.net.URL) CollectionException(org.opennms.netmgt.collection.api.CollectionException) ResourcePath(org.opennms.netmgt.model.ResourcePath) Sftp3gppUrlConnection(org.opennms.protocols.sftp.Sftp3gppUrlConnection) VTDNav(com.ximpleware.VTDNav)

Example 2 with Request

use of org.opennms.protocols.xml.config.Request in project opennms by OpenNMS.

the class HttpUrlConnectionIT method testJson.

/**
     * Test POST Request based on JSON Data.
     *
     * @throws Exception the exception
     */
@Test
@JUnitHttpServer(port = 10342, https = false, webapps = { @Webapp(context = "/junit", path = "src/test/resources/test-webapp") })
public void testJson() throws Exception {
    String json = "{ person: { firstName: 'Alejandro', lastName: 'Galue' } }";
    Request req = buildRequest("application/json", json);
    executeRequest(req);
}
Also used : Request(org.opennms.protocols.xml.config.Request) Test(org.junit.Test) JUnitHttpServer(org.opennms.core.test.http.annotations.JUnitHttpServer)

Example 3 with Request

use of org.opennms.protocols.xml.config.Request in project opennms by OpenNMS.

the class HttpUrlConnectionIT method testForm.

/**
     * Test POST Request based on Form Data.
     *
     * @throws Exception the exception
     */
@Test
@JUnitHttpServer(port = 10342, https = false, webapps = { @Webapp(context = "/junit", path = "src/test/resources/test-webapp") })
public void testForm() throws Exception {
    String json = "<form-fields><form-field name='firstName'>Alejandro</form-field><form-field name='lastName'>Galue</form-field></form-fields>";
    Request req = buildRequest("application/x-www-form-urlencoded", json);
    executeRequest(req);
}
Also used : Request(org.opennms.protocols.xml.config.Request) Test(org.junit.Test) JUnitHttpServer(org.opennms.core.test.http.annotations.JUnitHttpServer)

Example 4 with Request

use of org.opennms.protocols.xml.config.Request in project opennms by OpenNMS.

the class Sftp3gppXmlCollectionHandler method collect.

/* (non-Javadoc)
     * @see org.opennms.protocols.xml.collector.XmlCollectionHandler#collect(org.opennms.netmgt.collectd.CollectionAgent, org.opennms.protocols.xml.config.XmlDataCollection, java.util.Map)
     */
@Override
public CollectionSet collect(CollectionAgent agent, XmlDataCollection collection, Map<String, Object> parameters) throws CollectionException {
    String status = "finished";
    // Create a new collection set.
    CollectionSetBuilder builder = new CollectionSetBuilder(agent);
    // TODO We could be careful when handling exceptions because parsing exceptions will be treated different from connection or retrieval exceptions
    DateTime startTime = new DateTime();
    Sftp3gppUrlConnection connection = null;
    try {
        ResourcePath resourcePath = ResourcePath.get(ResourceTypeUtils.SNMP_DIRECTORY, Integer.toString(agent.getNodeId()));
        for (XmlSource source : collection.getXmlSources()) {
            if (!source.getUrl().startsWith(Sftp3gppUrlHandler.PROTOCOL)) {
                throw new CollectionException("The 3GPP SFTP Collection Handler can only use the protocol " + Sftp3gppUrlHandler.PROTOCOL);
            }
            final String urlStr = source.getUrl();
            final Request request = source.getRequest();
            URL url = UrlFactory.getUrl(urlStr, request);
            String lastFile = Sftp3gppUtils.getLastFilename(getResourceStorageDao(), getServiceName(), resourcePath, url.getPath());
            connection = (Sftp3gppUrlConnection) url.openConnection();
            if (lastFile == null) {
                lastFile = connection.get3gppFileName();
                LOG.debug("collect(single): retrieving file from {}{}{} from {}", url.getPath(), File.separatorChar, lastFile, agent.getHostAddress());
                Document doc = getXmlDocument(urlStr, request);
                fillCollectionSet(agent, builder, source, doc);
                Sftp3gppUtils.setLastFilename(getResourceStorageDao(), getServiceName(), resourcePath, url.getPath(), lastFile);
                Sftp3gppUtils.deleteFile(connection, lastFile);
            } else {
                connection.connect();
                List<String> files = connection.getFileList();
                long lastTs = connection.getTimeStampFromFile(lastFile);
                boolean collected = false;
                for (String fileName : files) {
                    if (connection.getTimeStampFromFile(fileName) > lastTs) {
                        LOG.debug("collect(multiple): retrieving file {} from {}", fileName, agent.getHostAddress());
                        InputStream is = connection.getFile(fileName);
                        try {
                            Document doc = getXmlDocument(is, request);
                            IOUtils.closeQuietly(is);
                            fillCollectionSet(agent, builder, source, doc);
                        } finally {
                            IOUtils.closeQuietly(is);
                        }
                        Sftp3gppUtils.setLastFilename(getResourceStorageDao(), getServiceName(), resourcePath, url.getPath(), fileName);
                        Sftp3gppUtils.deleteFile(connection, fileName);
                        collected = true;
                    }
                }
                if (!collected) {
                    LOG.warn("collect: could not find any file after {} on {}", lastFile, agent);
                }
            }
        }
        return builder.build();
    } catch (Exception e) {
        status = "failed";
        throw new CollectionException(e.getMessage(), e);
    } finally {
        DateTime endTime = new DateTime();
        LOG.debug("collect: {} collection {}: duration: {} ms", status, collection.getName(), endTime.getMillis() - startTime.getMillis());
        UrlFactory.disconnect(connection);
    }
}
Also used : CollectionSetBuilder(org.opennms.netmgt.collection.support.builder.CollectionSetBuilder) CollectionException(org.opennms.netmgt.collection.api.CollectionException) InputStream(java.io.InputStream) Request(org.opennms.protocols.xml.config.Request) Document(org.w3c.dom.Document) DateTime(org.joda.time.DateTime) XmlSource(org.opennms.protocols.xml.config.XmlSource) URL(java.net.URL) CollectionException(org.opennms.netmgt.collection.api.CollectionException) ResourcePath(org.opennms.netmgt.model.ResourcePath) Sftp3gppUrlConnection(org.opennms.protocols.sftp.Sftp3gppUrlConnection)

Example 5 with Request

use of org.opennms.protocols.xml.config.Request in project opennms by OpenNMS.

the class XmlCollector method parseCollection.

public XmlDataCollection parseCollection(XmlDataCollection collection, XmlCollectionHandler handler, CollectionAgent agent, Map<String, Object> parameters) {
    // Clone the collection and perform token replacement in the source url and request using the handler
    XmlDataCollection preparsedCollection = collection.clone();
    // Remove blacklisted properties from the map
    Map<String, String> filteredParameters = filterParameters(parameters);
    for (XmlSource source : preparsedCollection.getXmlSources()) {
        final String originalUrlStr = source.getUrl();
        final String parsedUrlStr = handler.parseUrl(m_nodeDao, originalUrlStr, agent, collection.getXmlRrd().getStep(), filteredParameters);
        LOG.debug("parseCollection: original url: '{}', parsed url: '{}' ", originalUrlStr, parsedUrlStr);
        source.setUrl(parsedUrlStr);
        final Request originalRequest = source.getRequest();
        final Request parsedRequest = handler.parseRequest(m_nodeDao, originalRequest, agent, collection.getXmlRrd().getStep(), filteredParameters);
        LOG.debug("parseCollection: original request: '{}', parsed request: '{}' ", originalRequest, parsedRequest);
        source.setRequest(parsedRequest);
    }
    return preparsedCollection;
}
Also used : XmlDataCollection(org.opennms.protocols.xml.config.XmlDataCollection) Request(org.opennms.protocols.xml.config.Request) XmlSource(org.opennms.protocols.xml.config.XmlSource)

Aggregations

Request (org.opennms.protocols.xml.config.Request)12 Test (org.junit.Test)6 XmlSource (org.opennms.protocols.xml.config.XmlSource)4 DateTime (org.joda.time.DateTime)3 JUnitHttpServer (org.opennms.core.test.http.annotations.JUnitHttpServer)3 CollectionException (org.opennms.netmgt.collection.api.CollectionException)3 CollectionSetBuilder (org.opennms.netmgt.collection.support.builder.CollectionSetBuilder)3 InputStream (java.io.InputStream)2 URL (java.net.URL)2 ResourcePath (org.opennms.netmgt.model.ResourcePath)2 Sftp3gppUrlConnection (org.opennms.protocols.sftp.Sftp3gppUrlConnection)2 Content (org.opennms.protocols.xml.config.Content)2 VTDNav (com.ximpleware.VTDNav)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ParseException (java.text.ParseException)1 XPathExpressionException (javax.xml.xpath.XPathExpressionException)1 JSONObject (net.sf.json.JSONObject)1 OnmsNode (org.opennms.netmgt.model.OnmsNode)1 FormFields (org.opennms.protocols.http.FormFields)1