use of org.opennms.netmgt.model.OnmsEventCollection in project opennms by OpenNMS.
the class EventRestService method getEventsBetween.
/**
* Returns all the events which match the filter/query in the query
* parameters
*
* @return Collection of OnmsEventCollection (ready to be XML-ified)
* @throws java.text.ParseException
* if any.
*/
@GET
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON, MediaType.APPLICATION_ATOM_XML })
@Path("between")
@Transactional
public OnmsEventCollection getEventsBetween(@Context final UriInfo uriInfo) throws ParseException {
final MultivaluedMap<String, String> params = uriInfo.getQueryParameters();
final String column;
if (params.containsKey("column")) {
column = params.getFirst("column");
params.remove("column");
} else {
column = "eventTime";
}
Date begin;
if (params.containsKey("begin")) {
try {
begin = ISO8601_FORMATTER.parseLocalDateTime(params.getFirst("begin")).toDate();
} catch (final Throwable t1) {
try {
begin = ISO8601_FORMATTER_MILLIS.parseDateTime(params.getFirst("begin")).toDate();
} catch (final Throwable t2) {
throw getException(Status.BAD_REQUEST, "Can't parse start date");
}
}
params.remove("begin");
} else {
begin = new Date(0);
}
Date end;
if (params.containsKey("end")) {
try {
end = ISO8601_FORMATTER.parseLocalDateTime(params.getFirst("end")).toDate();
} catch (final Throwable t1) {
try {
end = ISO8601_FORMATTER_MILLIS.parseLocalDateTime(params.getFirst("end")).toDate();
} catch (final Throwable t2) {
throw getException(Status.BAD_REQUEST, "Can't parse end date");
}
}
params.remove("end");
} else {
end = new Date();
}
final CriteriaBuilder builder = getCriteriaBuilder(params);
builder.match("all");
try {
builder.between(column, begin, end);
} catch (final Throwable t) {
throw getException(Status.BAD_REQUEST, "Unable to parse " + begin + " and " + end + " as dates!");
}
final OnmsEventCollection coll = new OnmsEventCollection(m_eventDao.findMatching(builder.toCriteria()));
coll.setTotalCount(m_eventDao.countMatching(builder.count().toCriteria()));
return coll;
}
use of org.opennms.netmgt.model.OnmsEventCollection in project opennms by OpenNMS.
the class OnmsRestEventsClient method getEvents.
public List<Event> getEvents(Integer limit, Integer offset) {
List<Event> retrievedEvents = new ArrayList<>();
CloseableHttpClient httpclient = getNewClient();
String query = "";
String limitStr = (limit == null) ? null : Integer.toString(limit);
String offsetStr = (offset == null) ? null : Integer.toString(offset);
if (limitStr != null) {
query = "?limit=" + limitStr;
if (offset != null) {
query = query + "&offset=" + offsetStr;
}
} else {
if (offset != null) {
query = "?offset=" + offsetStr;
}
}
try {
// importing events generated from opennms-webapp-rest/src/main/java/org/opennms/web/rest/v1/EventRestService.java
HttpGet getRequest = new HttpGet(onmsUrl + EVENTS_URI + query);
getRequest.addHeader("accept", "application/XML");
LOG.debug("Executing request " + getRequest.getRequestLine());
CloseableHttpResponse response = httpclient.execute(getRequest);
if (response.getStatusLine().getStatusCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : " + response.getStatusLine().getStatusCode());
}
String responseStr = null;
try {
LOG.debug("----------------------------------------");
LOG.debug(response.getStatusLine().toString());
responseStr = EntityUtils.toString(response.getEntity());
LOG.debug("response String: " + responseStr);
LOG.debug("----------------------------------------");
} finally {
response.close();
}
StringReader reader = new StringReader(responseStr);
OnmsEventCollection eventCollection = JaxbUtils.unmarshal(OnmsEventCollection.class, reader);
LOG.debug("received xml OnmsEvent's ----------------------------------------");
LOG.debug("eventCollection offset:" + eventCollection.getOffset() + " totalCount:" + eventCollection.getTotalCount() + " size" + eventCollection.size());
for (int i = 0; i < eventCollection.size(); i++) {
LOG.debug("event:" + eventCollection.get(i));
}
LOG.debug("converting to events ----------------------------------------");
for (int i = 0; i < eventCollection.size(); i++) {
Event event = toEvent(eventCollection.get(i));
LOG.debug(event.toString());
retrievedEvents.add(event);
}
} catch (Exception e) {
throw new RuntimeException("exception when getting event list", e);
} finally {
try {
httpclient.close();
} catch (IOException e) {
}
}
return retrievedEvents;
}
use of org.opennms.netmgt.model.OnmsEventCollection in project opennms by OpenNMS.
the class EventRestService method getEvents.
/**
* Returns all the events which match the filter/query in the query
* parameters
*
* @return Collection of OnmsEventCollection (ready to be XML-ified)
* @throws java.text.ParseException
* if any.
*/
@GET
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON, MediaType.APPLICATION_ATOM_XML })
@Transactional
public OnmsEventCollection getEvents(@Context final UriInfo uriInfo) throws ParseException {
final CriteriaBuilder builder = getCriteriaBuilder(uriInfo.getQueryParameters());
builder.orderBy("eventTime").asc();
final OnmsEventCollection coll = new OnmsEventCollection(m_eventDao.findMatching(builder.toCriteria()));
coll.setTotalCount(m_eventDao.countMatching(builder.count().toCriteria()));
return coll;
}
Aggregations