Search in sources :

Example 1 with OnmsEventCollection

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;
}
Also used : CriteriaBuilder(org.opennms.core.criteria.CriteriaBuilder) OnmsEventCollection(org.opennms.netmgt.model.OnmsEventCollection) Date(java.util.Date) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with OnmsEventCollection

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;
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpGet(org.apache.http.client.methods.HttpGet) ArrayList(java.util.ArrayList) IOException(java.io.IOException) IOException(java.io.IOException) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) StringReader(java.io.StringReader) OnmsEventCollection(org.opennms.netmgt.model.OnmsEventCollection) OnmsEvent(org.opennms.netmgt.model.OnmsEvent) Event(org.opennms.netmgt.xml.event.Event)

Example 3 with OnmsEventCollection

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;
}
Also used : CriteriaBuilder(org.opennms.core.criteria.CriteriaBuilder) OnmsEventCollection(org.opennms.netmgt.model.OnmsEventCollection) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

OnmsEventCollection (org.opennms.netmgt.model.OnmsEventCollection)3 GET (javax.ws.rs.GET)2 Produces (javax.ws.rs.Produces)2 CriteriaBuilder (org.opennms.core.criteria.CriteriaBuilder)2 Transactional (org.springframework.transaction.annotation.Transactional)2 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 Path (javax.ws.rs.Path)1 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)1 HttpGet (org.apache.http.client.methods.HttpGet)1 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)1 OnmsEvent (org.opennms.netmgt.model.OnmsEvent)1 Event (org.opennms.netmgt.xml.event.Event)1