Search in sources :

Example 1 with SystemActionTemporaryResource

use of com.infiniteautomation.mango.rest.v2.util.SystemActionTemporaryResource in project ma-modules-public by infiniteautomation.

the class SystemActionRestV2Controller method cancel.

@ApiOperation(value = "Cancel Action", notes = "No Guarantees that the cancel will work, this is task dependent.")
@ApiResponses({ @ApiResponse(code = 401, message = "Unauthorized user access", response = ResponseEntity.class), @ApiResponse(code = 404, message = "No resource exists with given id", response = ResponseEntity.class), @ApiResponse(code = 500, message = "Error processing request", response = ResponseEntity.class) })
@RequestMapping(method = { RequestMethod.PUT }, value = { "/cancel/{resourceId}" })
public ResponseEntity<SystemActionTemporaryResource> cancel(HttpServletRequest request, @ApiParam(value = "Resource id", required = true, allowMultiple = false) @PathVariable String resourceId, @AuthenticationPrincipal User user) {
    SystemActionTemporaryResource resource = resources.get(resourceId);
    resource.cancel();
    return new ResponseEntity<>(resource, HttpStatus.OK);
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) SystemActionTemporaryResource(com.infiniteautomation.mango.rest.v2.util.SystemActionTemporaryResource) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) ApiResponses(com.wordnik.swagger.annotations.ApiResponses) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with SystemActionTemporaryResource

use of com.infiniteautomation.mango.rest.v2.util.SystemActionTemporaryResource in project ma-modules-public by infiniteautomation.

the class SystemActionRestV2Controller method performAction.

@ApiOperation(value = "Perform an Action", notes = "Kicks off action and returns temporary URL for status")
@ApiResponses({ @ApiResponse(code = 500, message = "Internal error", response = ResponseEntity.class), @ApiResponse(code = 404, message = "Not Found", response = ResponseEntity.class) })
@RequestMapping(method = RequestMethod.PUT, value = "/trigger/{action}")
public ResponseEntity<SystemActionTemporaryResource> performAction(@ApiParam(value = "Valid System Action", required = true, allowMultiple = false) @PathVariable String action, @ApiParam(value = "Input for task", required = false, allowMultiple = false) @RequestBody(required = false) JsonNode input, @AuthenticationPrincipal User user, UriComponentsBuilder builder) {
    // Kick off action
    SystemActionDefinition def = ModuleRegistry.getSystemActionDefinition(action);
    if (def == null)
        throw new NotFoundRestException();
    String resourceId = resources.generateResourceId();
    SystemActionTemporaryResource resource = new SystemActionTemporaryResource(resourceId, def.getTask(user, input), resources, new Date(System.currentTimeMillis() + 600000));
    // Resource can live for up to 10 minutes (TODO Configurable?)
    resources.put(resourceId, resource);
    URI location = builder.path("/v2/actions/status/{resourceId}").buildAndExpand(resourceId).toUri();
    HttpHeaders headers = new HttpHeaders();
    headers.setLocation(location);
    return new ResponseEntity<>(resource, headers, HttpStatus.OK);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) NotFoundRestException(com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException) ResponseEntity(org.springframework.http.ResponseEntity) SystemActionTemporaryResource(com.infiniteautomation.mango.rest.v2.util.SystemActionTemporaryResource) URI(java.net.URI) Date(java.util.Date) SystemActionDefinition(com.serotonin.m2m2.module.SystemActionDefinition) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) ApiResponses(com.wordnik.swagger.annotations.ApiResponses) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

SystemActionTemporaryResource (com.infiniteautomation.mango.rest.v2.util.SystemActionTemporaryResource)2 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)2 ApiResponses (com.wordnik.swagger.annotations.ApiResponses)2 ResponseEntity (org.springframework.http.ResponseEntity)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 NotFoundRestException (com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException)1 SystemActionDefinition (com.serotonin.m2m2.module.SystemActionDefinition)1 URI (java.net.URI)1 Date (java.util.Date)1 HttpHeaders (org.springframework.http.HttpHeaders)1