Search in sources :

Example 1 with HttpPostInfo

use of org.opennms.netmgt.rtc.datablock.HttpPostInfo in project opennms by OpenNMS.

the class DataSender method sendData.

/**
     * Loop through the categories and send out data for all categories that
     * have changed
     */
public synchronized void sendData() {
    LOG.debug("In DataSender sendData()");
    // loop through and send info
    for (final RTCCategory cat : m_dataMgr.getCategories().values()) {
        // get label
        final String catlabel = cat.getLabel();
        LOG.debug("DataSender:sendData(): Category '{}'", catlabel);
        // get the post info for this category
        final Set<HttpPostInfo> urlList = m_catUrlMap.get(catlabel);
        if (urlList == null || urlList.size() <= 0) {
            // a category that no one is listening for?
            LOG.debug("DataSender: category '{}' has no listeners", catlabel);
            continue;
        }
        LOG.debug("DataSender: category '{}' has listeners - converting to xml...", catlabel);
        final EuiLevel euidata;
        try {
            euidata = m_dataMgr.getEuiLevel(cat);
        } catch (final Throwable t) {
            LOG.warn("DataSender: unable to convert data to xml for category: '{}'", catlabel, t);
            continue;
        }
        // do a HTTP POST if subscribed
        if (urlList != null && urlList.size() > 0) {
            final Iterator<HttpPostInfo> urlIter = urlList.iterator();
            while (urlIter.hasNext()) {
                final HttpPostInfo postInfo = urlIter.next();
                InputStream inp = null;
                try {
                    LOG.debug("DataSender: posting data to: {}", postInfo.getURLString());
                    final String marshaledUeiData = JaxbUtils.marshal(euidata);
                    try (final StringReader inr = new StringReader(marshaledUeiData)) {
                        inp = HttpUtils.post(postInfo.getURL(), inr, postInfo.getUser(), postInfo.getPassword(), 8 * HttpUtils.DEFAULT_POST_BUFFER_SIZE, HttpUtils.DEFAULT_CONNECT_TIMEOUT);
                    }
                    LOG.debug("DataSender: posted data for category: {}", catlabel);
                    final byte[] tmp = new byte[1024];
                    int bytesRead;
                    while ((bytesRead = inp.read(tmp)) != -1) {
                        if (LOG.isDebugEnabled()) {
                            if (bytesRead > 0) {
                                LOG.debug("DataSender: post response: {}", new String(tmp, 0, bytesRead));
                            }
                        }
                    }
                    postInfo.clearErrors();
                } catch (final Throwable t) {
                    LOG.warn("DataSender: unable to send data for category: {} due to {}: {}", catlabel, t.getClass().getName(), t.getMessage(), t);
                    postInfo.incrementErrors();
                } finally {
                    IOUtils.closeQuietly(inp);
                }
                // check to see if URL had too many errors
                if (POST_ERROR_LIMIT > 0 && postInfo.getErrors() >= POST_ERROR_LIMIT) {
                    // unsubscribe the URL
                    urlIter.remove();
                    LOG.warn("URL {} UNSUBSCRIBED due to reaching error limit {}", postInfo.getURLString(), postInfo.getErrors());
                }
            }
        }
    }
}
Also used : HttpPostInfo(org.opennms.netmgt.rtc.datablock.HttpPostInfo) InputStream(java.io.InputStream) StringReader(java.io.StringReader) EuiLevel(org.opennms.netmgt.xml.rtc.EuiLevel) RTCCategory(org.opennms.netmgt.rtc.datablock.RTCCategory)

Example 2 with HttpPostInfo

use of org.opennms.netmgt.rtc.datablock.HttpPostInfo in project opennms by OpenNMS.

the class DataSender method subscribe.

/**
     * Subscribe - Add the received URL and related info to the category->URLs map
     * so the sendData() can send out to appropriate URLs for each category.
     * Also send the latest info for the category
     *
     * @param url a {@link java.lang.String} object.
     * @param catlabel a {@link java.lang.String} object.
     * @param user a {@link java.lang.String} object.
     * @param passwd a {@link java.lang.String} object.
     */
public synchronized void subscribe(final String url, final String catlabel, final String user, final String passwd) {
    // send category data to the newly subscribed URL
    // look up info for this category
    final RTCCategory cat = m_dataMgr.getCategories().get(catlabel);
    if (cat == null) {
        // oops! category for which we have no info!
        LOG.warn("RTC: No information available for category: {}", catlabel);
        return;
    }
    // create new HttpPostInfo
    final HttpPostInfo postInfo;
    try {
        postInfo = new HttpPostInfo(url, catlabel, user, passwd);
    } catch (final MalformedURLException mue) {
        LOG.warn("ERROR subscribing: Invalid URL '{}' - Data WILL NOT be SENT to the specified url", url);
        return;
    }
    // Add the URL to the list for the specified category
    Set<HttpPostInfo> urlList = m_catUrlMap.get(catlabel);
    if (urlList == null) {
        urlList = new HashSet<HttpPostInfo>();
        m_catUrlMap.put(catlabel, urlList);
    }
    if (!urlList.add(postInfo)) {
        LOG.debug("Already subscribed to URL: {}\tcatlabel: {}\tuser: {} - IGNORING LATEST subscribe event", url, catlabel, user);
    } else {
        LOG.debug("Subscribed to URL: {}\tcatlabel: {}\tuser:{}", url, catlabel, user);
    }
    try {
        m_dsrPool.execute(new Runnable() {

            @Override
            public void run() {
                // send data
                InputStream inp = null;
                try {
                    LOG.debug("DataSender: posting data to: {}", url);
                    final EuiLevel euidata = m_dataMgr.getEuiLevel(cat);
                    // Connect with a fairly long timeout to allow the web UI time to register the
                    // {@link RTCPostServlet}. Actually, this doesn't seem to work because the POST
                    // will immediately throw a {@link ConnectException} if the web UI isn't ready
                    // yet. Oh well.
                    final String marshaledUeiData = JaxbUtils.marshal(euidata);
                    try (final StringReader inr = new StringReader(marshaledUeiData)) {
                        inp = HttpUtils.post(postInfo.getURL(), inr, user, passwd, 8 * HttpUtils.DEFAULT_POST_BUFFER_SIZE, 60000);
                    }
                    final byte[] tmp = new byte[1024];
                    int bytesRead;
                    while ((bytesRead = inp.read(tmp)) != -1) {
                        if (LOG.isDebugEnabled()) {
                            if (bytesRead > 0) {
                                LOG.debug("DataSender: post response: {}", new String(tmp, 0, bytesRead));
                            }
                        }
                    }
                    LOG.debug("DataSender: posted data for category: {}", catlabel);
                } catch (final ConnectException e) {
                    // These exceptions will be thrown if we try to POST RTC data before the web UI is available.
                    // Don't log a large stack trace for this because it will happen during startup before the
                    // RTCPostServlet is ready to handle requests.
                    LOG.warn("DataSender:  Unable to send category '{}' to URL '{}': {}", catlabel, url, e.getMessage());
                } catch (final Throwable t) {
                    LOG.warn("DataSender:  Unable to send category '{}' to URL '{}'", catlabel, url, t);
                } finally {
                    IOUtils.closeQuietly(inp);
                }
            }
        });
    } catch (RejectedExecutionException e) {
        LOG.warn("Unable to queue datasender. The task was rejected by the pool. Current queue size: {}.", m_queue.size(), e);
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) InputStream(java.io.InputStream) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) HttpPostInfo(org.opennms.netmgt.rtc.datablock.HttpPostInfo) StringReader(java.io.StringReader) EuiLevel(org.opennms.netmgt.xml.rtc.EuiLevel) RTCCategory(org.opennms.netmgt.rtc.datablock.RTCCategory) ConnectException(java.net.ConnectException)

Example 3 with HttpPostInfo

use of org.opennms.netmgt.rtc.datablock.HttpPostInfo in project opennms by OpenNMS.

the class DataSender method unsubscribe.

/**
     * Unsubscribe - remove the received URL and related info from the
     * category->URLs map so the sendData() will know when it sends data out
     *
     * @param urlStr a {@link java.lang.String} object.
     */
public synchronized void unsubscribe(final String urlStr) {
    final URL url;
    try {
        url = new URL(urlStr);
    } catch (final MalformedURLException mue) {
        LOG.warn("ERROR unsubscribing: Invalid URL: {}", urlStr);
        return;
    }
    // the specified URL
    for (final String key : m_catUrlMap.keySet()) {
        final Set<HttpPostInfo> value = m_catUrlMap.get(key);
        if (value == null)
            continue;
        final Iterator<HttpPostInfo> postSet = value.iterator();
        while (postSet.hasNext()) {
            final HttpPostInfo postInfo = postSet.next();
            if (url.toExternalForm().equals(postInfo.getURL().toExternalForm())) {
                postSet.remove();
            }
        }
    }
    LOG.debug("Unsubscribed URL: {}", url);
}
Also used : MalformedURLException(java.net.MalformedURLException) HttpPostInfo(org.opennms.netmgt.rtc.datablock.HttpPostInfo) URL(java.net.URL)

Aggregations

HttpPostInfo (org.opennms.netmgt.rtc.datablock.HttpPostInfo)3 InputStream (java.io.InputStream)2 StringReader (java.io.StringReader)2 MalformedURLException (java.net.MalformedURLException)2 RTCCategory (org.opennms.netmgt.rtc.datablock.RTCCategory)2 EuiLevel (org.opennms.netmgt.xml.rtc.EuiLevel)2 ConnectException (java.net.ConnectException)1 URL (java.net.URL)1 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)1