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());
}
}
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;
}
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);
}
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;
}
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());
}
Aggregations