Search in sources :

Example 6 with Request

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

the class AbstractXmlCollectionHandler 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";
    CollectionSetBuilder builder = new CollectionSetBuilder(agent);
    DateTime startTime = new DateTime();
    try {
        LOG.debug("collect: looping sources for collection {}", collection.getName());
        for (XmlSource source : collection.getXmlSources()) {
            final String urlStr = source.getUrl();
            final Request request = source.getRequest();
            LOG.debug("collect: starting source url '{}' collection with request: {}", urlStr, request);
            fillCollectionSet(urlStr, request, agent, builder, source);
            LOG.debug("collect: finished source url '{}' collection with {} resources", urlStr, builder.getNumResources());
        }
        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());
    }
}
Also used : CollectionSetBuilder(org.opennms.netmgt.collection.support.builder.CollectionSetBuilder) CollectionException(org.opennms.netmgt.collection.api.CollectionException) Request(org.opennms.protocols.xml.config.Request) DateTime(org.joda.time.DateTime) XmlSource(org.opennms.protocols.xml.config.XmlSource) XPathExpressionException(javax.xml.xpath.XPathExpressionException) CollectionException(org.opennms.netmgt.collection.api.CollectionException) ParseException(java.text.ParseException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException)

Example 7 with Request

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

the class HttpUrlConnectionIT method buildRequest.

/**
     * Builds the request.
     *
     * @param contentType the content type
     * @param contentData the content data
     * @return the request
     */
private Request buildRequest(String contentType, String contentData) {
    Request req = new Request();
    req.setMethod("POST");
    req.addParameter("timeout", "3000");
    req.addParameter("retries", "2");
    req.addHeader("User-Agent", "FireFox 22.0");
    req.setContent(new Content(contentType, contentData));
    return req;
}
Also used : Content(org.opennms.protocols.xml.config.Content) Request(org.opennms.protocols.xml.config.Request)

Example 8 with Request

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

the class HttpUrlConnectionIT method testXml.

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

Example 9 with Request

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

the class AbstractXmlCollectionHandler method parseRequest.

/**
     * Parses the request.
     *
     * @param unformattedRequest the unformatted request
     * @param agent the agent
     * @param collectionStep the collection step
     * @param parameters the service parameters
     * @return the request
     * @throws IllegalArgumentException the illegal argument exception
     */
@Override
public Request parseRequest(final NodeDao nodeDao, final Request unformattedRequest, final CollectionAgent agent, final Integer collectionStep, final Map<String, String> parameters) throws IllegalArgumentException {
    if (unformattedRequest == null)
        return null;
    final OnmsNode node = nodeDao.get(agent.getNodeId());
    final Request request = new Request();
    request.setMethod(unformattedRequest.getMethod());
    for (Header header : unformattedRequest.getHeaders()) {
        request.addHeader(header.getName(), parseString(header.getName(), header.getValue(), node, agent.getHostAddress(), collectionStep, parameters));
    }
    for (Parameter param : unformattedRequest.getParameters()) {
        request.addParameter(param.getName(), parseString(param.getName(), param.getValue(), node, agent.getHostAddress(), collectionStep, parameters));
    }
    final Content cnt = unformattedRequest.getContent();
    if (cnt != null)
        request.setContent(new Content(cnt.getType(), parseString("Content", cnt.getData(), node, agent.getHostAddress(), collectionStep, parameters)));
    return request;
}
Also used : OnmsNode(org.opennms.netmgt.model.OnmsNode) Header(org.opennms.protocols.xml.config.Header) Content(org.opennms.protocols.xml.config.Content) Request(org.opennms.protocols.xml.config.Request) Parameter(org.opennms.protocols.xml.config.Parameter)

Example 10 with Request

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

the class ContentTest method testJsonContent.

/**
     * Test JSON content.
     *
     * @throws Exception the exception
     */
@Test
public void testJsonContent() throws Exception {
    String xml = "<request method='POST'>\n" + "  <parameter name='retries' value='3'/>\n" + "  <parameter name='timeout' value='5000'/>\n" + "  <header name='User-Agent' value='Chrome'/>\n" + "  <header name='Host' value='{nodeLabel}'/>\n" + "  <content type='application/json'><![CDATA[\n" + "    { person: { firstName: 'Alejandro', lastName: 'Galue' } }" + "  ]]></content>\n" + "</request>";
    Request request = JaxbUtils.unmarshal(Request.class, xml);
    Assert.assertNotNull(request);
    Assert.assertEquals("Chrome", request.getHeaders().get(0).getValue());
    Assert.assertEquals("3", request.getParameters().get(0).getValue());
    Assert.assertEquals("application/json", request.getContent().getType());
    String json = request.getContent().getData();
    JSONObject o = JSONObject.fromObject(json);
    Person p = (Person) JSONObject.toBean(o.getJSONObject("person"), Person.class);
    Assert.assertNotNull(p);
    Assert.assertEquals("Alejandro", p.getFirstName());
}
Also used : JSONObject(net.sf.json.JSONObject) Request(org.opennms.protocols.xml.config.Request) Test(org.junit.Test)

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