Search in sources :

Example 1 with SystemActionTemporaryResource

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

the class SystemActionRestController 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, @RequestParam(required = false, defaultValue = "12000000") Long timeout, @AuthenticationPrincipal PermissionHolder 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() + timeout));
    // Resource can live for up to 10 minutes (TODO Configurable?)
    resources.put(resourceId, resource);
    URI location = builder.path("/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.latest.exception.NotFoundRestException) ResponseEntity(org.springframework.http.ResponseEntity) SystemActionTemporaryResource(com.infiniteautomation.mango.rest.latest.util.SystemActionTemporaryResource) URI(java.net.URI) Date(java.util.Date) SystemActionDefinition(com.serotonin.m2m2.module.SystemActionDefinition) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with SystemActionTemporaryResource

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

the class SystemActionRestController 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 PermissionHolder 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.latest.util.SystemActionTemporaryResource) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

SystemActionTemporaryResource (com.infiniteautomation.mango.rest.latest.util.SystemActionTemporaryResource)2 ApiOperation (io.swagger.annotations.ApiOperation)2 ApiResponses (io.swagger.annotations.ApiResponses)2 ResponseEntity (org.springframework.http.ResponseEntity)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 NotFoundRestException (com.infiniteautomation.mango.rest.latest.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