Search in sources :

Example 1 with Attrib

use of org.opennms.netmgt.config.httpdatacollection.Attrib in project opennms by OpenNMS.

the class HttpCollector method processResponse.

private static void processResponse(final Locale responseLocale, final String responseBodyAsString, final HttpCollectorAgent collectorAgent, final CollectionSetBuilder collectionSetBuilder) {
    LOG.debug("processResponse:");
    LOG.debug("responseBody = {}", responseBodyAsString);
    LOG.debug("getmatches = {}", collectorAgent.getUriDef().getUrl().getMatches());
    int numberOfButes = 0;
    int flags = 0;
    if (collectorAgent.getUriDef().getUrl().isCanonicalEquivalence()) {
        flags |= Pattern.CANON_EQ;
    }
    if (collectorAgent.getUriDef().getUrl().isCaseInsensitive()) {
        flags |= Pattern.CASE_INSENSITIVE;
    }
    if (collectorAgent.getUriDef().getUrl().isComments()) {
        flags |= Pattern.COMMENTS;
    }
    if (collectorAgent.getUriDef().getUrl().isDotall()) {
        flags |= Pattern.DOTALL;
    }
    if (collectorAgent.getUriDef().getUrl().isLiteral()) {
        flags |= Pattern.LITERAL;
    }
    if (collectorAgent.getUriDef().getUrl().isMultiline()) {
        flags |= Pattern.MULTILINE;
    }
    if (collectorAgent.getUriDef().getUrl().isUnicodeCase()) {
        flags |= Pattern.UNICODE_CASE;
    }
    if (collectorAgent.getUriDef().getUrl().isUnixLines()) {
        flags |= Pattern.UNIX_LINES;
    }
    LOG.debug("flags = {}", flags);
    Pattern p = Pattern.compile(collectorAgent.getUriDef().getUrl().getMatches(), flags);
    Matcher m = p.matcher(responseBodyAsString);
    final boolean matches = m.matches();
    if (matches) {
        LOG.debug("processResponse: found matching attributes: {}", matches);
        final List<Attrib> attribDefs = collectorAgent.getUriDef().getAttributes();
        final List<Locale> locales = new ArrayList<Locale>();
        if (responseLocale != null) {
            locales.add(responseLocale);
        }
        locales.add(Locale.getDefault());
        if (Locale.getDefault() != Locale.ENGLISH) {
            locales.add(Locale.ENGLISH);
        }
        // All node resources for HTTP; nothing of interface or "indexed resource" type
        final NodeLevelResource resource = new NodeLevelResource(collectorAgent.getAgent().getNodeId());
        for (final Attrib attribDef : attribDefs) {
            final AttributeType type = attribDef.getType();
            String value = null;
            try {
                value = m.group(attribDef.getMatchGroup());
            } catch (final IndexOutOfBoundsException e) {
                LOG.error("IndexOutOfBoundsException thrown while trying to find regex group, your regex does not contain the following group index: {}", attribDef.getMatchGroup());
                LOG.error("Regex statement: {}", collectorAgent.getUriDef().getUrl().getMatches());
                continue;
            }
            if (type.isNumeric()) {
                Number num = null;
                for (final Locale locale : locales) {
                    try {
                        num = NumberFormat.getNumberInstance(locale).parse(value);
                        LOG.debug("processResponse: found a parsable number with locale \"{}\".", locale);
                        break;
                    } catch (final ParseException e) {
                        LOG.warn("attribute {} failed to match a parsable number with locale \"{}\"! Matched \"{}\" instead.", attribDef.getAlias(), locale, value);
                    }
                }
                if (num == null) {
                    LOG.warn("processResponse: gave up attempting to parse numeric value, skipping group {}", attribDef.getMatchGroup());
                    continue;
                }
                LOG.debug("processResponse: adding numeric attribute {}", num);
                collectionSetBuilder.withNumericAttribute(resource, collectorAgent.getUriDef().getName(), attribDef.getAlias(), num, type);
                numberOfButes++;
            } else {
                LOG.debug("processResponse: adding string attribute {}", value);
                collectionSetBuilder.withStringAttribute(resource, collectorAgent.getUriDef().getName(), attribDef.getAlias(), value);
                numberOfButes++;
            }
        }
    } else {
        LOG.debug("processResponse: found matching attributes: {}", matches);
    }
    if (numberOfButes < 1) {
        LOG.warn("doCollection: no attributes defined by the response: {}", responseBodyAsString.trim());
        throw new HttpCollectorException("No attributes specified were found.");
    }
}
Also used : Locale(java.util.Locale) Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) NodeLevelResource(org.opennms.netmgt.collection.support.builder.NodeLevelResource) Attrib(org.opennms.netmgt.config.httpdatacollection.Attrib) AttributeType(org.opennms.netmgt.collection.api.AttributeType) ParseException(java.text.ParseException)

Aggregations

ParseException (java.text.ParseException)1 ArrayList (java.util.ArrayList)1 Locale (java.util.Locale)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 AttributeType (org.opennms.netmgt.collection.api.AttributeType)1 NodeLevelResource (org.opennms.netmgt.collection.support.builder.NodeLevelResource)1 Attrib (org.opennms.netmgt.config.httpdatacollection.Attrib)1