@GET @Path("/source/{id}") @ApiOperation(value = "Retrieve the event source with the passed id", responseClass = "EventSourceRest") @ApiError(code = 404, reason = "There is no event source with the passed id") public EventSourceRest getEventSource(@ApiParam("Id of the source to retrieve") @PathParam("id") int sourceId) { EventSource source = findEventSourceById(sourceId); EventSourceRest esr = convertEventSource(source); return esr; }
@DELETE @Path("definition/{id}") @ApiOperation(value = "Delete an alert definition", notes = "This operation is by default idempotent, returning 204." + "If you want to check if the definition existed at all, you need to pass the 'validate' query parameter.") @ApiErrors({ @ApiError(code = 204, reason = "Definition was deleted or did not exist with validation not set"), @ApiError(code = 404, reason = "Definition did not exist and validate was set") }) public Response deleteDefinition(@ApiParam("Id of the definition to delete") @PathParam("id") int definitionId, @ApiParam("Validate if the definition exists") @QueryParam("validate") @DefaultValue("false") boolean validate) { int count = alertDefinitionManager.removeAlertDefinitions(caller, new int[]{definitionId}); if (count == 0 && validate) { throw new StuffNotFoundException("Definition with id " + definitionId); } return Response.noContent().build(); }
@DELETE @Path("/{id}") @ApiOperation(value = "Remove the alert from the list of alerts", notes = "This operation is by default idempotent, returning 204." + "If you want to check if the alert existed at all, you need to pass the 'validate' query parameter.") @ApiErrors({ @ApiError(code = 204, reason = "Alert was deleted or did not exist with validation not set"), @ApiError(code = 404, reason = "Alert did not exist and validate was set") }) public Response purgeAlert(@ApiParam(value = "Id of the alert to remove") @PathParam("id") int id, @ApiParam("Validate if the alert exists") @QueryParam("validate") @DefaultValue("false") boolean validate) { int count = alertManager.deleteAlerts(caller, new int[]{id}); if (count == 0 && validate) { throw new StuffNotFoundException("Alert with id " + id); } return Response.noContent().build(); }
@DELETE @Path("/source/{id}") @ApiOperation(value = "Delete the event source with the passed id", notes = "This operation is by default idempotent, returning 204." + "If you want to check if the source existed at all, you need to pass the 'validate' query parameter.") @ApiErrors({ @ApiError(code = 204, reason = "Source was deleted or did not exist with validation not set"), @ApiError(code = 404, reason = "Source did not exist and validate was set") }) public Response deleteEventSource(@ApiParam("Id of the source to delete") @PathParam("id") int sourceId, @ApiParam("Validate if the content exists") @QueryParam("validate") @DefaultValue("false") boolean validate) { EventSource source = em.find(EventSource.class,sourceId); if (source!=null) { em.remove(source); // We have a cascade delete on the events TODO make operation async ? } else { if (validate) { throw new StuffNotFoundException("Event source with id " + sourceId); } } return Response.noContent().build(); }
@GET @Path("notification/{nid}") @ApiOperation("Return a notification definition by its id") @ApiError(code = 404, reason = "No notification with the passed id found") public Response getNotification( @ApiParam("The id of the notification definition to retrieve") @PathParam("nid") int notificationId) { AlertNotification notification = notificationMgr.getAlertNotification(caller,notificationId); if (notification==null) { throw new StuffNotFoundException("No notification with id " + notificationId); } AlertNotificationRest anr = notificationToNotificationRest(notification); return Response.ok(anr).build(); }
@GET @Path("condition/{cid}") @ApiOperation("Retrieve a condition of an alert definition by its condition id") @ApiError(code = 404, reason = "No condition with the passed id exists") public Response getCondition( @ApiParam("The id of the condition to retrieve") @PathParam("cid") int conditionId) { AlertCondition condition = conditionMgr.getAlertConditionById(conditionId); if (condition==null) { throw new StuffNotFoundException("No condition with id " + conditionId); } AlertConditionRest acr = conditionToConditionRest(condition); return Response.ok(acr).build(); }
@GET @GZIP @Path("/{id}/hierarchy") @Produces({"application/json","application/xml"}) @ApiOperation(value = "Retrieve the hierarchy of resources starting with the passed one", multiValueResponse = true, responseClass = "ResourceWithType") @ApiError(code = 404, reason = NO_RESOURCE_FOR_ID) public ResourceWithChildren getHierarchy(@ApiParam("Id of the resource to start with") @PathParam("id")int baseResourceId) { // TODO optimize to do less recursion Resource start = fetchResource(baseResourceId); return getHierarchy(start); }
@GET @Path("/{id}/availability/summary") @ApiError(code = 404, reason = NO_RESOURCE_FOR_ID) @ApiOperation(value = "Return the availability history for the passed resource", responseClass = "AvailabilitySummary", multiValueResponse = false) public Response getAvailabilitySummary( @ApiParam("Id of the resource to query") @PathParam("id") int resourceId, @Context HttpHeaders headers) { fetchResource(resourceId); ResourceAvailabilitySummary summary = resMgr.getAvailabilitySummary(caller,resourceId); AvailabilitySummary as = new AvailabilitySummary(resourceId,summary); Response.ResponseBuilder builder = Response.ok(as); MediaType type = headers.getAcceptableMediaTypes().get(0); builder.type(type); return builder.build(); }
@Cache(maxAge = 600) @GET @Path("{id}") @ApiOperation(value = "Return information about the resource type with the passed id",responseClass = "ResourceTypeRest") @ApiError(code = 404, reason = "There is no type with the passed id") public Response getTypeById( @PathParam("id") int resourceTypeId, @Context HttpHeaders headers, @Context UriInfo uriInfo) { ResourceType type; try { type = typeManager.getResourceTypeById(caller,resourceTypeId); } catch (ResourceTypeNotFoundException e) { throw new StuffNotFoundException("Resource type with id " + resourceTypeId); } ResourceTypeRest rtr = resourceTypeToResourceTypeRest(type); MediaType mediaType = headers.getAcceptableMediaTypes().get(0); Response.ResponseBuilder builder = Response.ok(); builder.type(mediaType); builder.entity(rtr); return builder.build(); }
@DELETE @Path("favorites/group/{id}") @ApiOperation(value = "Remove a group from favorites", notes = "This operation is by default idempotent, returning 204." + "If you want to check if the group was a favorite, you need to pass the 'validate' query parameter.") @ApiErrors({ @ApiError(code = 204, reason = "Group was removed or was no favorite with validation not set"), @ApiError(code = 404, reason = "Group was no favorite and validate was set") }) public void removeResourceGroupFromFavorites( @ApiParam(name = "id", value = "Id of the group") @PathParam("id") int id, @ApiParam("Validate if the group is a favorite") @QueryParam("validate") @DefaultValue("false") boolean validate) { Set<Integer> favIds = getGroupIdsForFavorites(); if (favIds.contains(id)) { favIds.remove(id); updateGroupFavorites(favIds); } else { if (validate) { throw new StuffNotFoundException("Group with id " + id + " in favorites "); } } }
@PUT @Path("data/{scheduleId}/trait/{timeStamp}") @Consumes({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML}) @ApiOperation(value = "Submit a new trait value for the passed schedule id") @ApiErrors({ @ApiError(code=404, reason = NO_SCHEDULE_FOR_ID), @ApiError(code=406, reason = "Timestamp is older than 7 days") }) public Response putTraitValue(@ApiParam("Id of the schedule") @PathParam("scheduleId") int scheduleId, @ApiParam("Timestamp of the metric") @PathParam("timeStamp") long timestamp, @ApiParam(value = "Data value", required = true) StringValue value) { obtainSchedule(scheduleId, false, DataType.TRAIT); long now = System.currentTimeMillis(); if (timestamp < now - SEVEN_DAYS) throw new IllegalArgumentException("Timestamp is older than 7 days"); Set<MeasurementDataTrait> traits = new HashSet<MeasurementDataTrait>(1); MeasurementDataPK pk = new MeasurementDataPK(timestamp,scheduleId); traits.add(new MeasurementDataTrait(pk,value.getValue())); dataManager.addTraitData(traits); return Response.ok().build(); }
@Cache(isPrivate = true,maxAge = 60) @GET @Path("{id}") @ApiOperation(value = "Get the group with the passed id") @ApiError(code = 404, reason = "Group with passed id not found") public Response getGroup(@ApiParam(value = "Id of the group") @PathParam("id") int id, @Context HttpHeaders headers, @Context UriInfo uriInfo) { ResourceGroup group = fetchGroup(id, false); GroupRest groupRest = fillGroup(group, uriInfo); MediaType mediaType = headers.getAcceptableMediaTypes().get(0); Response.ResponseBuilder builder = Response.ok(); builder.type(mediaType); if (mediaType.equals(MediaType.TEXT_HTML_TYPE)) { builder.entity(renderTemplate("group", groupRest)); } else { builder.entity(groupRest); } return builder.build(); }
@PUT @Path("/definition/{id}") @Consumes({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML}) @Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML}) @ApiOperation(value = "Update the definition (default enabled, default interval)", notes = "This operation may internally take a long time to complete and is thus only triggered by this call. " + "A return code of 200 only indicates that the operation was successfully submitted." , responseClass = "MetricSchedule") @ApiError(code = 404, reason = "No definition exists for the given id.") public Response updateDefinition(@ApiParam("Id of the definition to update") @PathParam("id") int definitionId, @ApiParam(value = "New definition data", required = true) MetricSchedule in, @ApiParam(value = "Update existing schedules for this definition as well?") @QueryParam("updateExisting") @DefaultValue("false") boolean updateExisting, @Context HttpHeaders headers) { MeasurementDefinition measurementDefinition = definitionManager.getMeasurementDefinition(caller, definitionId); if (measurementDefinition==null) throw new StuffNotFoundException("Definition with id " + definitionId); // Call an async method to do the work as this can take a looong time metricHandlerBean.submitDefinitionChange(definitionId, in, updateExisting); StringValue ret = new StringValue("Request submitted - this may take a while to complete."); return Response.ok(ret,headers.getAcceptableMediaTypes().get(0)).build(); }
@POST @Path("/") @ApiErrors({ @ApiError(code = 302, reason = "Creation is still happening. Check back with a GET on the Location.") }) @ApiOperation(value = "Create a new resource as a child of an existing resource. ", notes= "If a handle is given, a content based resource is created; the content identified by the handle is not removed from the content store." + "If no handle is given, a resource is created from the data of the passed 'resource' object.") public Response createResource( @ApiParam("The info about the resource. You need to supply resource name, resource type name, plugin name, id of the parent") CreateCBResourceRequest resource, @ApiParam("A handle that identifies content that has been uploaded to the server before.") @QueryParam("handle") String handle, @Context HttpHeaders headers, @Context UriInfo uriInfo) throws IOException { if (handle!=null) { return createContentBackedResource(resource,handle,headers,uriInfo); } else { return createResourceInternal(resource,headers,uriInfo); } }
@GET @Path("data/{scheduleId}/trait") @Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML}) @ApiOperation(value="Get the current value of the trait with the passed schedule id", responseClass = "StringValue") @ApiError(code = 404, reason = NO_SCHEDULE_FOR_ID) public Response getTraitValue(@ApiParam("Id of the schedule") @PathParam("scheduleId") int scheduleId) { MeasurementSchedule schedule = obtainSchedule(scheduleId, false, DataType.TRAIT); List<MeasurementDataTrait> traits = dataManager.findTraits(caller,schedule.getResource().getId(),schedule.getDefinition().getId()); Response.ResponseBuilder builder; if (traits!=null && traits.size()>0) { builder = Response.ok(); StringValue value = new StringValue(traits.get(0).getValue()); builder.entity(value); } else { builder = Response.status(Response.Status.NOT_FOUND); } return builder.build(); }
@GET @Path("/definition/{id}") @ApiOperation(value = "Get one AlertDefinition by id", responseClass = "AlertDefinitionRest") @ApiError(code = 404, reason = "No definition found with the passed id.") public Response getAlertDefinition(@ApiParam("Id of the alert definition to retrieve") @PathParam("id") int definitionId, @ApiParam("Should conditions and notifications be returned too?") @QueryParam("full") @DefaultValue("true") boolean full, @Context Request request, @Context UriInfo uriInfo) { AlertDefinition def = alertDefinitionManager.getAlertDefinition(caller, definitionId); if (def==null) { throw new StuffNotFoundException("AlertDefinition with id " + definitionId ); } EntityTag eTag = new EntityTag(Integer.toHexString(def.hashCode())); Response.ResponseBuilder builder = request.evaluatePreconditions(eTag); if (builder==null) { AlertDefinitionRest adr = definitionToDomain(def, full, uriInfo); builder = Response.ok(adr); } builder.tag(eTag); return builder.build(); }
@GET @Path("data/{scheduleId}/baseline") @Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML}) @ApiOperation(value = "Get the current baseline for the schedule") @ApiError(code = 404, reason = NO_SCHEDULE_FOR_ID) public Baseline getBaseline(@ApiParam("Id of the schedule") @PathParam("scheduleId") int scheduleId) { MeasurementSchedule schedule = obtainSchedule(scheduleId, true, DataType.MEASUREMENT); MeasurementBaseline mBase = schedule.getBaseline(); Baseline b; if (mBase==null) throw new StuffNotFoundException("Baseline for schedule [" + scheduleId +"]"); else b = new Baseline(mBase.getMin(),mBase.getMax(),mBase.getMean(),mBase.getComputeTime().getTime()); return b; }
@GET @Path("/definition/{id}") @Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML}) @ApiOperation(value = "Get the definition ", responseClass = "MetricSchedule") @ApiError(code = 404, reason = "No definition exists for the given id.") public Response getDefinition(@ApiParam("Id of the definition to obtain") @PathParam("id") int definitionId, @Context HttpHeaders headers) { MeasurementDefinition measurementDefinition = definitionManager.getMeasurementDefinition(caller, definitionId); if (measurementDefinition==null) throw new StuffNotFoundException("Definition with id " + definitionId); MetricSchedule schedule = new MetricSchedule(definitionId, measurementDefinition.getName(), measurementDefinition.getDisplayName(), measurementDefinition.isDefaultOn(), measurementDefinition.getDefaultInterval(), measurementDefinition.getUnits().getName(), measurementDefinition.getDataType().name()); return Response.ok(schedule,headers.getAcceptableMediaTypes().get(0)).build(); }
@GET @Path("/{id}/availability") @ApiError(code = 404, reason = NO_RESOURCE_FOR_ID) @ApiOperation(value = "Return the current availability for the passed resource", responseClass = "AvailabilityRest") public Response getAvailability(@ApiParam("Id of the resource to query") @PathParam("id") int resourceId, @Context HttpHeaders headers) { Availability avail = availMgr.getCurrentAvailabilityForResource(caller, resourceId); AvailabilityRest availabilityRest; if (avail.getAvailabilityType() != null) availabilityRest = new AvailabilityRest(avail.getAvailabilityType(), avail.getStartTime(), avail .getResource().getId()); else availabilityRest = new AvailabilityRest(avail.getStartTime(), resourceId); MediaType mediaType = headers.getAcceptableMediaTypes().get(0); Response.ResponseBuilder builder; if (mediaType.equals(MediaType.TEXT_HTML_TYPE)) { builder = Response.ok(renderTemplate("availability.ftl",availabilityRest), mediaType); } else { builder = Response.ok(availabilityRest); } return builder.build(); }
@GZIP @AddLinks @GET @Path(("/{id}/alerts")) @ApiError(code = 404, reason = NO_RESOURCE_FOR_ID) @ApiOperation("Get a list of links to the alerts for the passed resource") public List<Link> getAlertsForResource(@ApiParam("Id of the resource to query") @PathParam("id") int resourceId) { AlertCriteria criteria = new AlertCriteria(); // Check for resource existence fetchResource(resourceId); criteria.addFilterResourceIds(resourceId); PageList<Alert> alerts = alertManager.findAlertsByCriteria(caller, criteria); List<Link> links = new ArrayList<Link>(alerts.size()); for (Alert al : alerts) { Link link = new Link(); link.setRel("alert"); link.setHref("/alert/" + al.getId()); links.add(link); } return links; }