Search in sources :

Example 1 with NotFoundRestException

use of com.infiniteautomation.mango.rest.latest.exception.NotFoundRestException in project ma-modules-public by infiniteautomation.

the class EventDetectorsRestController method getState.

@ApiOperation(value = "Get Event Detector's internal state", notes = "User must have read permission for the data point", response = AbstractEventDetectorRTModel.class)
@RequestMapping(method = RequestMethod.GET, value = "/runtime/{xid}")
public AbstractEventDetectorRTModel<?> getState(@ApiParam(value = "ID of Event detector", required = true, allowMultiple = false) @PathVariable String xid, @AuthenticationPrincipal PermissionHolder user, UriComponentsBuilder builder) {
    AbstractEventDetectorVO vo = service.get(xid);
    // For now all detectors are data point type
    DataPointRT rt = Common.runtimeManager.getDataPoint(vo.getSourceId());
    if (rt == null) {
        throw new TranslatableIllegalStateException(new TranslatableMessage("rest.error.pointNotEnabled", xid));
    }
    for (PointEventDetectorRT<?> edrt : rt.getEventDetectors()) {
        if (edrt.getVO().getId() == vo.getId()) {
            return modelMapper.map(edrt, AbstractEventDetectorRTModel.class, user);
        }
    }
    throw new NotFoundRestException();
}
Also used : NotFoundRestException(com.infiniteautomation.mango.rest.latest.exception.NotFoundRestException) AbstractEventDetectorVO(com.serotonin.m2m2.vo.event.detector.AbstractEventDetectorVO) DataPointRT(com.serotonin.m2m2.rt.dataImage.DataPointRT) TranslatableIllegalStateException(com.infiniteautomation.mango.util.exception.TranslatableIllegalStateException) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with NotFoundRestException

use of com.infiniteautomation.mango.rest.latest.exception.NotFoundRestException in project ma-modules-public by infiniteautomation.

the class LoggingRestController method getLogFile.

private ResponseEntity<FileSystemResource> getLogFile(String filename, boolean download) {
    Path file = Common.getLogsPath().resolve(filename);
    if (!filterFiles(file)) {
        throw new NotFoundRestException();
    }
    // TODO There is a known bug here where if the file rolls over during download/transmission the request will fail
    HttpHeaders responseHeaders = new HttpHeaders();
    String fileNameFromFile = file.getFileName().toString();
    if (fileNameFromFile.endsWith(".gz")) {
        responseHeaders.set(HttpHeaders.CONTENT_ENCODING, "gzip");
        fileNameFromFile = fileNameFromFile.substring(0, fileNameFromFile.length() - ".gz".length());
    }
    String contentDisposition = ContentDisposition.builder(download ? "attachment" : "inline").filename(fileNameFromFile).build().toString();
    responseHeaders.set(HttpHeaders.CONTENT_DISPOSITION, contentDisposition);
    return new ResponseEntity<>(new FileSystemResource(file), responseHeaders, HttpStatus.OK);
}
Also used : Path(java.nio.file.Path) HttpHeaders(org.springframework.http.HttpHeaders) NotFoundRestException(com.infiniteautomation.mango.rest.latest.exception.NotFoundRestException) ResponseEntity(org.springframework.http.ResponseEntity) FileSystemResource(org.springframework.core.io.FileSystemResource)

Example 3 with NotFoundRestException

use of com.infiniteautomation.mango.rest.latest.exception.NotFoundRestException in project ma-modules-public by infiniteautomation.

the class JsonDataRestController method getNode.

JsonNode getNode(final JsonNode existingData, final String[] dataPath) {
    JsonNode node = existingData;
    for (int i = 0; i < dataPath.length; i++) {
        String fieldName = dataPath[i];
        if (node.isObject()) {
            ObjectNode objectNode = (ObjectNode) node;
            node = objectNode.get(fieldName);
        } else if (node.isArray()) {
            ArrayNode arrayNode = (ArrayNode) node;
            int index = toArrayIndex(fieldName);
            node = arrayNode.get(index);
        } else {
            throw new BadRequestException(new TranslatableMessage("rest.error.cantGetFieldOfNodeType", node.getNodeType()));
        }
        if (node == null) {
            throw new NotFoundRestException();
        }
    }
    return node;
}
Also used : NotFoundRestException(com.infiniteautomation.mango.rest.latest.exception.NotFoundRestException) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) BadRequestException(com.infiniteautomation.mango.rest.latest.exception.BadRequestException) JsonNode(com.fasterxml.jackson.databind.JsonNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage)

Example 4 with NotFoundRestException

use of com.infiniteautomation.mango.rest.latest.exception.NotFoundRestException 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 5 with NotFoundRestException

use of com.infiniteautomation.mango.rest.latest.exception.NotFoundRestException in project ma-modules-public by infiniteautomation.

the class PartialUpdateArgumentResolver method resolveArgument.

@Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
    ServletServerHttpRequest req = createInputMessage(webRequest);
    PatchVORequestBody patch = parameter.getParameterAnnotation(PatchVORequestBody.class);
    Class<?> serviceClass = patch.service();
    AbstractVOService<?, ?> service = (AbstractVOService<?, ?>) context.getBean(serviceClass);
    PermissionHolder user = Common.getUser();
    // Set the source class into the request scope to use if validation fails
    webRequest.setAttribute(MangoRequestBodyAdvice.MODEL_CLASS, patch.modelClass(), RequestAttributes.SCOPE_REQUEST);
    Object vo;
    switch(patch.idType()) {
        case ID:
            Integer id = Integer.parseInt(getPathVariables(webRequest).get(ID));
            vo = service.get(id);
            if (vo == null)
                throw new NotFoundRestException();
            else {
                Object model = modelMapper.map(vo, patch.modelClass(), user);
                return readJavaType(model, req);
            }
        case XID:
            String xid = getPathVariables(webRequest).get(XID);
            vo = service.get(xid);
            if (vo == null)
                throw new NotFoundRestException();
            else {
                Object model = modelMapper.map(vo, patch.modelClass(), user);
                return readJavaType(model, req);
            }
        default:
        case OTHER:
            String other = getPathVariables(webRequest).get(patch.urlPathVariableName());
            vo = service.get(other);
            if (vo == null)
                throw new NotFoundRestException();
            else {
                Object model = modelMapper.map(vo, patch.modelClass(), user);
                return readJavaType(model, req);
            }
    }
}
Also used : ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) NotFoundRestException(com.infiniteautomation.mango.rest.latest.exception.NotFoundRestException) PatchVORequestBody(com.infiniteautomation.mango.rest.latest.patch.PatchVORequestBody) AbstractVOService(com.infiniteautomation.mango.spring.service.AbstractVOService) PermissionHolder(com.serotonin.m2m2.vo.permission.PermissionHolder)

Aggregations

NotFoundRestException (com.infiniteautomation.mango.rest.latest.exception.NotFoundRestException)11 ApiOperation (io.swagger.annotations.ApiOperation)6 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)6 ResponseEntity (org.springframework.http.ResponseEntity)4 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)3 HttpHeaders (org.springframework.http.HttpHeaders)3 VirtualSerialPortConfig (com.infiniteautomation.mango.io.serial.virtual.VirtualSerialPortConfig)2 BadRequestException (com.infiniteautomation.mango.rest.latest.exception.BadRequestException)2 DataPointRT (com.serotonin.m2m2.rt.dataImage.DataPointRT)2 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)2 URI (java.net.URI)2 Path (java.nio.file.Path)2 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 MangoPermission (com.infiniteautomation.mango.permission.MangoPermission)1 FileModel (com.infiniteautomation.mango.rest.latest.model.filestore.FileModel)1 PermissionDefinitionModel (com.infiniteautomation.mango.rest.latest.model.permissions.PermissionDefinitionModel)1 PatchVORequestBody (com.infiniteautomation.mango.rest.latest.patch.PatchVORequestBody)1