Search in sources :

Example 51 with PUT

use of javax.ws.rs.PUT in project opennms by OpenNMS.

the class CategoryRestService method updateCategory.

@PUT
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Path("/{categoryName}")
public Response updateCategory(@PathParam("categoryName") final String categoryName, final MultivaluedMapImpl params) {
    writeLock();
    try {
        OnmsCategory category = m_categoryDao.findByName(categoryName);
        if (category == null) {
            throw getException(Status.BAD_REQUEST, "Category with name '{}' was not found.", categoryName);
        }
        LOG.debug("updateCategory: updating category {}", category);
        BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(category);
        boolean modified = false;
        for (String key : params.keySet()) {
            if (wrapper.isWritableProperty(key)) {
                String stringValue = params.getFirst(key);
                Object value = wrapper.convertIfNecessary(stringValue, (Class<?>) wrapper.getPropertyType(key));
                wrapper.setPropertyValue(key, value);
                modified = true;
            }
        }
        LOG.debug("updateCategory: category {} updated", category);
        if (modified) {
            m_categoryDao.saveOrUpdate(category);
            return Response.noContent().build();
        }
        return Response.notModified().build();
    } finally {
        writeUnlock();
    }
}
Also used : BeanWrapper(org.springframework.beans.BeanWrapper) OnmsCategory(org.opennms.netmgt.model.OnmsCategory) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) PUT(javax.ws.rs.PUT)

Example 52 with PUT

use of javax.ws.rs.PUT in project opennms by OpenNMS.

the class KscRestService method addGraph.

@PUT
@Path("{kscReportId}")
@Transactional
public Response addGraph(@PathParam("kscReportId") final Integer kscReportId, @QueryParam("title") final String title, @QueryParam("reportName") final String reportName, @QueryParam("resourceId") final String resourceId, @QueryParam("timespan") String timespan) {
    writeLock();
    try {
        if (kscReportId == null || reportName == null || reportName == "" || resourceId == null || resourceId == "") {
            throw getException(Status.BAD_REQUEST, "Invalid request: reportName and resourceId cannot be empty!");
        }
        final Report report = m_kscReportFactory.getReportByIndex(kscReportId);
        if (report == null) {
            throw getException(Status.NOT_FOUND, "Invalid request: No KSC report found with ID: {}.", Integer.toString(kscReportId));
        }
        final Graph graph = new Graph();
        if (title != null) {
            graph.setTitle(title);
        }
        boolean found = false;
        for (final String valid : KSC_PerformanceReportFactory.TIMESPAN_OPTIONS) {
            if (valid.equals(timespan)) {
                found = true;
                break;
            }
        }
        if (!found) {
            LOG.debug("invalid timespan ('{}'), setting to '7_day' instead.", timespan);
            timespan = "7_day";
        }
        graph.setGraphtype(reportName);
        graph.setResourceId(resourceId);
        graph.setTimespan(timespan);
        report.addGraph(graph);
        m_kscReportFactory.setReport(kscReportId, report);
        try {
            m_kscReportFactory.saveCurrent();
        } catch (final Exception e) {
            throw getException(Status.INTERNAL_SERVER_ERROR, "Cannot save report with Id {} : {} ", kscReportId.toString(), e.getMessage());
        }
        return Response.noContent().build();
    } finally {
        writeUnlock();
    }
}
Also used : Graph(org.opennms.netmgt.config.kscReports.Graph) Report(org.opennms.netmgt.config.kscReports.Report) ParseException(java.text.ParseException) Path(javax.ws.rs.Path) PUT(javax.ws.rs.PUT) Transactional(org.springframework.transaction.annotation.Transactional)

Example 53 with PUT

use of javax.ws.rs.PUT in project opennms by OpenNMS.

the class IfServicesRestService method updateServices.

@PUT
public Response updateServices(@Context final UriInfo uriInfo, final MultivaluedMapImpl params) {
    final String status = params.getFirst("status");
    if (status == null || !status.matches("(A|R|S|F)")) {
        throw getException(Status.BAD_REQUEST, "Parameter status must be specified. Possible values: A (Managed), F (Forced Unmanaged), R (Rescan to Resume), S (Rescan to Suspend)");
    }
    final String services_csv = params.getFirst("services");
    final List<String> serviceList = new ArrayList<String>();
    if (services_csv != null) {
        for (String s : services_csv.split(",")) {
            serviceList.add(s);
        }
    }
    final Criteria c = getCriteria(uriInfo.getQueryParameters());
    c.setLimit(null);
    c.setOffset(null);
    final OnmsMonitoredServiceList services = new OnmsMonitoredServiceList(m_serviceDao.findMatching(c));
    if (services.isEmpty()) {
        throw getException(Status.BAD_REQUEST, "Can't find any service matching the provided criteria: {}.", uriInfo.getQueryParameters().toString());
    }
    boolean modified = false;
    for (OnmsMonitoredService svc : services) {
        boolean proceed = false;
        if (serviceList.isEmpty()) {
            proceed = true;
        } else {
            if (serviceList.contains(svc.getServiceName())) {
                proceed = true;
            }
        }
        if (proceed) {
            modified = true;
            final String currentStatus = svc.getStatus();
            svc.setStatus(status);
            m_serviceDao.update(svc);
            if ("S".equals(status) || ("A".equals(currentStatus) && "F".equals(status))) {
                LOG.debug("updateServices: suspending polling for service {} on node with IP {}", svc.getServiceName(), svc.getIpAddress().getHostAddress());
                // TODO ManageNodeServlet is sending this.
                sendEvent(EventConstants.SERVICE_UNMANAGED_EVENT_UEI, svc);
                sendEvent(EventConstants.SUSPEND_POLLING_SERVICE_EVENT_UEI, svc);
            }
            if ("R".equals(status) || ("F".equals(currentStatus) && "A".equals(status))) {
                LOG.debug("updateServices: resuming polling for service {} on node with IP {}", svc.getServiceName(), svc.getIpAddress().getHostAddress());
                sendEvent(EventConstants.RESUME_POLLING_SERVICE_EVENT_UEI, svc);
            }
        }
    }
    return modified ? Response.noContent().build() : Response.notModified().build();
}
Also used : ArrayList(java.util.ArrayList) OnmsMonitoredServiceList(org.opennms.netmgt.model.OnmsMonitoredServiceList) Criteria(org.opennms.core.criteria.Criteria) OnmsMonitoredService(org.opennms.netmgt.model.OnmsMonitoredService) PUT(javax.ws.rs.PUT)

Example 54 with PUT

use of javax.ws.rs.PUT in project opennms by OpenNMS.

the class NodeRestService method updateNode.

/**
     * <p>updateNode</p>
     *
     * @param nodeCriteria a {@link java.lang.String} object.
     * @param params a {@link org.opennms.web.rest.support.MultivaluedMapImpl} object.
     * @return a {@link javax.ws.rs.core.Response} object.
     */
@PUT
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Path("{nodeCriteria}")
public Response updateNode(@PathParam("nodeCriteria") final String nodeCriteria, final MultivaluedMapImpl params) {
    writeLock();
    try {
        final OnmsNode node = m_nodeDao.get(nodeCriteria);
        if (node == null)
            throw getException(Status.BAD_REQUEST, "Node {} was not found.", nodeCriteria);
        if (node.getAssetRecord().getGeolocation() == null) {
            node.getAssetRecord().setGeolocation(new OnmsGeolocation());
        }
        LOG.debug("updateNode: updating node {}", node);
        boolean modified = false;
        final BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(node);
        for (final String key : params.keySet()) {
            if (wrapper.isWritableProperty(key)) {
                final String stringValue = params.getFirst(key);
                final Object value = wrapper.convertIfNecessary(stringValue, (Class<?>) wrapper.getPropertyType(key));
                wrapper.setPropertyValue(key, value);
                modified = true;
            }
        }
        if (modified) {
            LOG.debug("updateNode: node {} updated", node);
            m_nodeDao.saveOrUpdate(node);
            return Response.noContent().build();
        }
        return Response.notModified().build();
    } finally {
        writeUnlock();
    }
}
Also used : BeanWrapper(org.springframework.beans.BeanWrapper) OnmsNode(org.opennms.netmgt.model.OnmsNode) OnmsGeolocation(org.opennms.netmgt.model.OnmsGeolocation) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) PUT(javax.ws.rs.PUT)

Example 55 with PUT

use of javax.ws.rs.PUT in project opennms by OpenNMS.

the class NotificationRestService method updateNotifications.

/**
     * <p>updateNotifications</p>
     *
     * @param params a {@link org.opennms.web.rest.support.MultivaluedMapImpl} object.
     */
@PUT
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Transactional
public Response updateNotifications(@Context final SecurityContext securityContext, final MultivaluedMapImpl params) {
    writeLock();
    try {
        Boolean ack = false;
        if (params.containsKey("ack")) {
            ack = "true".equals(params.getFirst("ack"));
            params.remove("ack");
        }
        final CriteriaBuilder builder = getCriteriaBuilder(params);
        for (final OnmsNotification notif : m_notifDao.findMatching(builder.toCriteria())) {
            processNotifAck(securityContext, notif, ack);
        }
        return Response.noContent().build();
    } finally {
        writeUnlock();
    }
}
Also used : CriteriaBuilder(org.opennms.core.criteria.CriteriaBuilder) OnmsNotification(org.opennms.netmgt.model.OnmsNotification) Consumes(javax.ws.rs.Consumes) PUT(javax.ws.rs.PUT) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

PUT (javax.ws.rs.PUT)203 Path (javax.ws.rs.Path)172 Consumes (javax.ws.rs.Consumes)108 Produces (javax.ws.rs.Produces)72 ApiOperation (io.swagger.annotations.ApiOperation)70 ApiResponses (io.swagger.annotations.ApiResponses)53 Timed (com.codahale.metrics.annotation.Timed)36 AuditEvent (org.graylog2.audit.jersey.AuditEvent)36 URI (java.net.URI)24 Response (javax.ws.rs.core.Response)23 AuditPolicy (co.cask.cdap.common.security.AuditPolicy)19 IOException (java.io.IOException)18 BeanWrapper (org.springframework.beans.BeanWrapper)16 BadRequestException (javax.ws.rs.BadRequestException)14 NotFoundException (javax.ws.rs.NotFoundException)14 WebApplicationException (javax.ws.rs.WebApplicationException)12 UriBuilder (javax.ws.rs.core.UriBuilder)12 UUID (java.util.UUID)11 GET (javax.ws.rs.GET)11 POST (javax.ws.rs.POST)11