Search in sources :

Example 1 with MultivaluedMapImpl

use of org.opennms.web.rest.support.MultivaluedMapImpl in project opennms by OpenNMS.

the class FormPropertiesReader method readFrom.

/**
 * {@inheritDoc}
 */
@Override
public MultivaluedMapImpl readFrom(final Class<MultivaluedMapImpl> type, final Type genericType, final Annotation[] annotations, final MediaType mediaType, final MultivaluedMap<String, String> httpHeaders, final InputStream entityStream) throws IOException, WebApplicationException {
    final MultivaluedMapImpl result = new MultivaluedMapImpl();
    final Enumeration<String> en = m_httpServletRequest.getParameterNames();
    while (en.hasMoreElements()) {
        final String parmName = en.nextElement();
        final String[] parmValue = m_httpServletRequest.getParameterValues(parmName);
        result.put(parmName, Arrays.asList(parmValue));
    }
    return result;
}
Also used : MultivaluedMapImpl(org.opennms.web.rest.support.MultivaluedMapImpl)

Example 2 with MultivaluedMapImpl

use of org.opennms.web.rest.support.MultivaluedMapImpl in project opennms by OpenNMS.

the class OnmsRestService method applyQueryFilters.

protected static void applyQueryFilters(final MultivaluedMap<String, String> p, final CriteriaBuilder builder, final Integer defaultLimit) {
    final MultivaluedMap<String, String> params = new MultivaluedMapImpl();
    params.putAll(p);
    builder.distinct();
    builder.limit(defaultLimit);
    if (params.containsKey("limit")) {
        builder.limit(Integer.valueOf(params.getFirst("limit")));
        params.remove("limit");
    }
    if (params.containsKey("offset")) {
        builder.offset(Integer.valueOf(params.getFirst("offset")));
        params.remove("offset");
    }
    if (params.containsKey("orderBy")) {
        builder.clearOrder();
        builder.orderBy(params.getFirst("orderBy"));
        params.remove("orderBy");
        if (params.containsKey("order")) {
            if ("desc".equalsIgnoreCase(params.getFirst("order"))) {
                builder.desc();
            } else {
                builder.asc();
            }
            params.remove("order");
        }
    }
    if (Boolean.getBoolean("org.opennms.web.rest.enableQuery")) {
        final String query = removeParameter(params, "query");
        if (query != null)
            builder.sql(query);
    }
    final String matchType;
    final String match = removeParameter(params, "match");
    if (match == null) {
        matchType = "all";
    } else {
        matchType = match;
    }
    builder.match(matchType);
    final Class<?> criteriaClass = builder.toCriteria().getCriteriaClass();
    final BeanWrapper wrapper = getBeanWrapperForClass(criteriaClass);
    final String comparatorParam = removeParameter(params, "comparator", "eq").toLowerCase();
    final Criteria currentCriteria = builder.toCriteria();
    for (final String key : params.keySet()) {
        for (final String paramValue : params.get(key)) {
            // the actual implementation com.sun.jersey.core.util.MultivaluedMapImpl returns a String, so this is fine in some way ...
            if ("null".equalsIgnoreCase(paramValue)) {
                builder.isNull(key);
            } else if ("notnull".equalsIgnoreCase(paramValue)) {
                builder.isNotNull(key);
            } else {
                Object value;
                Class<?> type = Object.class;
                try {
                    type = currentCriteria.getType(key);
                } catch (final IntrospectionException e) {
                    LOG.debug("Unable to determine type for key {}", key);
                }
                if (type == null) {
                    type = Object.class;
                }
                LOG.debug("comparator = {}, key = {}, propertyType = {}", comparatorParam, key, type);
                if (comparatorParam.equals("contains") || comparatorParam.equals("iplike") || comparatorParam.equals("ilike") || comparatorParam.equals("like")) {
                    value = paramValue;
                } else {
                    LOG.debug("convertIfNecessary({}, {})", key, paramValue);
                    try {
                        value = wrapper.convertIfNecessary(paramValue, type);
                    } catch (final Throwable t) {
                        LOG.debug("failed to introspect (key = {}, value = {})", key, paramValue, t);
                        value = paramValue;
                    }
                }
                try {
                    final Method m = builder.getClass().getMethod(comparatorParam, String.class, Object.class);
                    m.invoke(builder, new Object[] { key, value });
                } catch (final Throwable t) {
                    LOG.warn("Unable to find method for comparator: {}, key: {}, value: {}", comparatorParam, key, value, t);
                }
            }
        }
    }
}
Also used : BeanWrapper(org.springframework.beans.BeanWrapper) IntrospectionException(java.beans.IntrospectionException) MultivaluedMapImpl(org.opennms.web.rest.support.MultivaluedMapImpl) Criteria(org.opennms.core.criteria.Criteria) Method(java.lang.reflect.Method)

Example 3 with MultivaluedMapImpl

use of org.opennms.web.rest.support.MultivaluedMapImpl in project opennms by OpenNMS.

the class OutageRestService method forNodeId.

/**
 * <p>forNodeId</p>
 *
 * @param nodeId a int.
 * @param dateRange a long.
 * @param startTs a java.lang.Long.
 * @param endTs a java.lang.Long.
 * @return a {@link org.opennms.netmgt.model.OnmsOutageCollection} object.
 */
@GET
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON, MediaType.APPLICATION_ATOM_XML })
@Transactional
@Path("forNode/{nodeId}")
public OnmsOutageCollection forNodeId(@Context final UriInfo uriInfo, @PathParam("nodeId") final int nodeId, @DefaultValue("604800000") @QueryParam("dateRange") final long dateRange, @QueryParam("start") final Long startTs, @QueryParam("end") final Long endTs) {
    final CriteriaBuilder builder = new CriteriaBuilder(OnmsOutage.class);
    builder.eq("node.id", nodeId);
    builder.alias("monitoredService", "monitoredService");
    builder.alias("monitoredService.ipInterface", "ipInterface");
    builder.alias("monitoredService.ipInterface.node", "node");
    builder.alias("monitoredService.serviceType", "serviceType");
    final MultivaluedMap<String, String> params = new MultivaluedMapImpl();
    params.putAll(uriInfo.getQueryParameters());
    LOG.debug("Processing outages for node {} using {}", nodeId, params);
    if (startTs != null && endTs != null) {
        params.remove("start");
        params.remove("end");
        final Date start = new Date(startTs);
        final Date end = new Date(endTs);
        LOG.debug("Getting all outages from {} to {} for node {}", start, end, nodeId);
        builder.or(Restrictions.isNull("ifRegainedService"), Restrictions.and(Restrictions.gt("ifLostService", start), Restrictions.lt("ifLostService", end)));
    } else {
        params.remove("dateRange");
        final Date start = new Date(System.currentTimeMillis() - dateRange);
        LOG.debug("Getting all outgae from {} to current date for node {}", start, nodeId);
        builder.or(Restrictions.isNull("ifRegainedService"), Restrictions.gt("ifLostService", start));
    }
    applyQueryFilters(params, builder);
    builder.orderBy("id").desc();
    return new OnmsOutageCollection(m_outageDao.findMatching(builder.toCriteria()));
}
Also used : CriteriaBuilder(org.opennms.core.criteria.CriteriaBuilder) MultivaluedMapImpl(org.opennms.web.rest.support.MultivaluedMapImpl) OnmsOutageCollection(org.opennms.netmgt.model.OnmsOutageCollection) 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)

Aggregations

MultivaluedMapImpl (org.opennms.web.rest.support.MultivaluedMapImpl)3 IntrospectionException (java.beans.IntrospectionException)1 Method (java.lang.reflect.Method)1 Date (java.util.Date)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 Criteria (org.opennms.core.criteria.Criteria)1 CriteriaBuilder (org.opennms.core.criteria.CriteriaBuilder)1 OnmsOutageCollection (org.opennms.netmgt.model.OnmsOutageCollection)1 BeanWrapper (org.springframework.beans.BeanWrapper)1 Transactional (org.springframework.transaction.annotation.Transactional)1