Search in sources :

Example 26 with BadRequestException

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

the class DataPointTagsRestController method doIndividualRequest.

private TagIndividualResponse doIndividualRequest(TagIndividualRequest request, BulkTagAction defaultAction, Map<String, String> defaultBody) {
    TagIndividualResponse result = new TagIndividualResponse();
    try {
        String xid = request.getXid();
        if (xid == null) {
            throw new BadRequestException(new TranslatableMessage("rest.error.mustNotBeNull", "xid"));
        }
        result.setXid(xid);
        BulkTagAction action = request.getAction() == null ? defaultAction : request.getAction();
        if (action == null) {
            throw new BadRequestException(new TranslatableMessage("rest.error.mustNotBeNull", "action"));
        }
        result.setAction(action);
        Map<String, String> tags = request.getBody() == null ? defaultBody : request.getBody();
        switch(action) {
            case GET:
                result.setBody(this.getTagsForDataPoint(xid));
                break;
            case SET:
                if (tags == null) {
                    throw new BadRequestException(new TranslatableMessage("rest.error.mustNotBeNull", "body"));
                }
                result.setBody(this.setTagsForDataPoint(xid, tags));
                break;
            case MERGE:
            case UPDATE:
                if (tags == null) {
                    throw new BadRequestException(new TranslatableMessage("rest.error.mustNotBeNull", "body"));
                }
                result.setBody(this.mergeTagsForDataPoint(xid, tags));
                break;
        }
    } catch (Exception e) {
        result.exceptionCaught(e);
    }
    return result;
}
Also used : BadRequestException(com.infiniteautomation.mango.rest.latest.exception.BadRequestException) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) BadRequestException(com.infiniteautomation.mango.rest.latest.exception.BadRequestException) AbstractRestException(com.infiniteautomation.mango.rest.latest.exception.AbstractRestException)

Example 27 with BadRequestException

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

the class UserRestController method bulkUserOperation.

@ApiOperation(value = "Bulk get/create/update/delete users", notes = "User must have read/edit permission for the user")
@RequestMapping(method = RequestMethod.POST, value = "/bulk")
public ResponseEntity<TemporaryResource<UserBulkResponse, AbstractRestException>> bulkUserOperation(@RequestBody UserBulkRequest requestBody, HttpServletRequest servletRequest, Authentication authentication, UriComponentsBuilder builder) {
    VoAction defaultAction = requestBody.getAction();
    UserModel defaultBody = requestBody.getBody();
    List<UserIndividualRequest> requests = requestBody.getRequests();
    if (requests == null) {
        throw new BadRequestException(new TranslatableMessage("rest.error.mustNotBeNull", "requests"));
    } else if (requests.isEmpty()) {
        throw new BadRequestException(new TranslatableMessage("rest.error.cantBeEmpty", "requests"));
    }
    String resourceId = requestBody.getId();
    Long expiration = requestBody.getExpiration();
    Long timeout = requestBody.getTimeout();
    TemporaryResource<UserBulkResponse, AbstractRestException> responseBody = bulkResourceManager.newTemporaryResource(RESOURCE_TYPE_BULK_USER, resourceId, expiration, timeout, (resource) -> {
        UserBulkResponse bulkResponse = new UserBulkResponse();
        int i = 0;
        resource.progressOrSuccess(bulkResponse, i++, requests.size());
        for (UserIndividualRequest request : requests) {
            UriComponentsBuilder reqBuilder = UriComponentsBuilder.newInstance();
            PermissionHolder resourceUser = Common.getUser();
            UserIndividualResponse individualResponse = doIndividualRequest(request, defaultAction, defaultBody, resourceUser, servletRequest, authentication, reqBuilder);
            bulkResponse.addResponse(individualResponse);
            resource.progressOrSuccess(bulkResponse, i++, requests.size());
        }
        return null;
    });
    HttpHeaders headers = new HttpHeaders();
    headers.setLocation(builder.path("/users/bulk/{id}").buildAndExpand(responseBody.getId()).toUri());
    return new ResponseEntity<>(responseBody, headers, HttpStatus.CREATED);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) VoAction(com.infiniteautomation.mango.rest.latest.bulk.VoAction) UserIndividualResponse(com.infiniteautomation.mango.rest.latest.model.user.UserIndividualResponse) PermissionHolder(com.serotonin.m2m2.vo.permission.PermissionHolder) UserModel(com.infiniteautomation.mango.rest.latest.model.user.UserModel) ResponseEntity(org.springframework.http.ResponseEntity) UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) UserIndividualRequest(com.infiniteautomation.mango.rest.latest.model.user.UserIndividualRequest) BadRequestException(com.infiniteautomation.mango.rest.latest.exception.BadRequestException) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) AbstractRestException(com.infiniteautomation.mango.rest.latest.exception.AbstractRestException) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 28 with BadRequestException

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

the class AuditEventInstanceModel method unwrapContext.

private JsonObject unwrapContext(String json) {
    try {
        JsonTypeReader reader = new JsonTypeReader(json);
        JsonValue value = reader.read();
        return value.toJsonObject();
    } catch (Exception e) {
        throw new BadRequestException(e);
    }
}
Also used : JsonValue(com.serotonin.json.type.JsonValue) BadRequestException(com.infiniteautomation.mango.rest.latest.exception.BadRequestException) JsonTypeReader(com.serotonin.json.type.JsonTypeReader) BadRequestException(com.infiniteautomation.mango.rest.latest.exception.BadRequestException) IOException(java.io.IOException) JsonException(com.serotonin.json.JsonException) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException)

Example 29 with BadRequestException

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

the class AsciiFileDataSourceRestController method validateASCIIString.

@ApiOperation(value = "Validate ASCII", notes = "")
@RequestMapping(method = RequestMethod.POST, value = "/validate-ascii/{xid}", consumes = { "text/plain;charset=UTF-8" })
public List<AsciiFileTestResultModel> validateASCIIString(@RequestBody String ascii, @ApiParam(value = "Valid ASCII data source XID", required = true, allowMultiple = false) @PathVariable String xid, @AuthenticationPrincipal PermissionHolder user) {
    DataSourceVO ds = service.get(xid);
    if (!(ds instanceof AsciiFileDataSourceVO))
        throw new BadRequestException(new TranslatableMessage("validate.incompatibleDataSourceType"));
    // Message we will work with
    String msg = StringEscapeUtils.unescapeJava(ascii);
    // Map to store the values vs the points they are for
    List<AsciiFileTestResultModel> results = new ArrayList<>();
    DataPointDao dpd = DataPointDao.getInstance();
    List<DataPointVO> points = dpd.getDataPoints(ds.getId());
    for (final DataPointVO vo : points) {
        MatchCallback callback = new MatchCallback() {

            @Override
            public void onMatch(String pointIdentifier, PointValueTime value) {
                AsciiFileTestResultModel result = new AsciiFileTestResultModel();
                results.add(result);
                result.setSuccess(true);
                result.setPointName(vo.getName());
                result.setPointXid(vo.getXid());
                result.setIdentifier(pointIdentifier);
                if (value != null) {
                    result.setValue(value.getValue().getObjectValue());
                    result.setTimestamp(new Date(value.getTime()));
                }
            }

            @Override
            public void pointPatternMismatch(String message, String pointValueRegex) {
                AsciiFileTestResultModel result = new AsciiFileTestResultModel();
                results.add(result);
                result.setSuccess(false);
                result.setPointName(vo.getName());
                result.setPointXid(vo.getXid());
                result.setError(new TranslatableMessage("dsEdit.file.test.noPointRegexMatch"));
            }

            @Override
            public void messagePatternMismatch(String message, String messageRegex) {
            }

            @Override
            public void pointNotIdentified(String message, String messageRegex, int pointIdentifierIndex) {
                AsciiFileTestResultModel result = new AsciiFileTestResultModel();
                results.add(result);
                result.setSuccess(false);
                result.setPointName(vo.getName());
                result.setPointXid(vo.getXid());
                result.setError(new TranslatableMessage("dsEdit.file.test.noIdentifierFound"));
            }

            @Override
            public void matchGeneralFailure(Exception e) {
                AsciiFileTestResultModel result = new AsciiFileTestResultModel();
                results.add(result);
                result.setSuccess(false);
                result.setPointName(vo.getName());
                result.setPointXid(vo.getXid());
                result.setError(new TranslatableMessage("common.default", e.getMessage()));
            }
        };
        AsciiFilePointLocatorVO locator = vo.getPointLocator();
        AsciiFileDataSourceRT.matchPointValueTime(msg, Pattern.compile(locator.getValueRegex()), locator.getPointIdentifier(), locator.getPointIdentifierIndex(), locator.getDataType(), locator.getValueIndex(), locator.getHasTimestamp(), locator.getTimestampIndex(), locator.getTimestampFormat(), callback);
    }
    return results;
}
Also used : DataSourceVO(com.serotonin.m2m2.vo.dataSource.DataSourceVO) AsciiFileDataSourceVO(com.infiniteautomation.asciifile.vo.AsciiFileDataSourceVO) DataPointVO(com.serotonin.m2m2.vo.DataPointVO) DataPointDao(com.serotonin.m2m2.db.dao.DataPointDao) AsciiFileTestResultModel(com.infiniteautomation.mango.rest.latest.model.AsciiFileTestResultModel) ArrayList(java.util.ArrayList) MatchCallback(com.infiniteautomation.mango.regex.MatchCallback) AsciiFilePointLocatorVO(com.infiniteautomation.asciifile.vo.AsciiFilePointLocatorVO) Date(java.util.Date) BadRequestException(com.infiniteautomation.mango.rest.latest.exception.BadRequestException) PermissionException(com.serotonin.m2m2.vo.permission.PermissionException) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) BadRequestException(com.infiniteautomation.mango.rest.latest.exception.BadRequestException) AsciiFileDataSourceVO(com.infiniteautomation.asciifile.vo.AsciiFileDataSourceVO) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

BadRequestException (com.infiniteautomation.mango.rest.latest.exception.BadRequestException)29 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)26 ApiOperation (io.swagger.annotations.ApiOperation)15 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)15 AbstractRestException (com.infiniteautomation.mango.rest.latest.exception.AbstractRestException)10 VoAction (com.infiniteautomation.mango.rest.latest.bulk.VoAction)8 ArrayList (java.util.ArrayList)7 ResponseEntity (org.springframework.http.ResponseEntity)7 HttpHeaders (org.springframework.http.HttpHeaders)6 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)5 PermissionHolder (com.serotonin.m2m2.vo.permission.PermissionHolder)5 UriComponentsBuilder (org.springframework.web.util.UriComponentsBuilder)5 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)4 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)4 DataPointModel (com.infiniteautomation.mango.rest.latest.model.dataPoint.DataPointModel)4 NotFoundException (com.infiniteautomation.mango.util.exception.NotFoundException)4 DataSourceVO (com.serotonin.m2m2.vo.dataSource.DataSourceVO)4 PermissionException (com.serotonin.m2m2.vo.permission.PermissionException)4 NotFoundRestException (com.infiniteautomation.mango.rest.latest.exception.NotFoundRestException)3