Search in sources :

Example 11 with BadRequestException

use of com.netflix.spinnaker.front50.exception.BadRequestException in project ma-modules-public by infiniteautomation.

the class AuthenticationTokenRestController method createToken.

@ApiOperation(value = "Create auth token", notes = "Creates an authentication token for the current user or for the username specified (admin only)")
@RequestMapping(path = "/create", method = RequestMethod.POST)
@PreAuthorize("isAuthenticated() and isPasswordAuthenticated()")
public ResponseEntity<TokenModel> createToken(@RequestBody CreateTokenRequest requestBody, @AuthenticationPrincipal User currentUser) {
    Date expiry = requestBody.getExpiry();
    String username = requestBody.getUsername();
    User user = currentUser;
    if (username != null && !username.equals(currentUser.getUsername())) {
        if (!currentUser.isAdmin()) {
            throw new AccessDeniedException(new TranslatableMessage("rest.error.onlyAdminsCanCreateTokens"));
        }
        user = UserDao.instance.getUser(username);
        if (user == null) {
            throw new BadRequestException(new TranslatableMessage("rest.error.unknownUser", username));
        }
    }
    String token = tokenAuthService.generateToken(user, expiry);
    return new ResponseEntity<>(new TokenModel(token), HttpStatus.CREATED);
}
Also used : AccessDeniedException(com.infiniteautomation.mango.rest.v2.exception.AccessDeniedException) ResponseEntity(org.springframework.http.ResponseEntity) User(com.serotonin.m2m2.vo.User) BadRequestException(com.infiniteautomation.mango.rest.v2.exception.BadRequestException) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) Date(java.util.Date) TokenModel(com.infiniteautomation.mango.rest.v2.model.jwt.TokenModel) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 12 with BadRequestException

use of com.netflix.spinnaker.front50.exception.BadRequestException in project ma-modules-public by infiniteautomation.

the class DataPointRestController method doIndividualRequest.

private DataPointIndividualResponse doIndividualRequest(DataPointIndividualRequest request, VoAction defaultAction, DataPointModel defaultBody, User user, UriComponentsBuilder builder) {
    DataPointIndividualResponse result = new DataPointIndividualResponse();
    try {
        String xid = request.getXid();
        VoAction action = request.getAction() == null ? defaultAction : request.getAction();
        if (action == null) {
            throw new BadRequestException(new TranslatableMessage("rest.error.mustNotBeNull", "action"));
        }
        result.setAction(action);
        DataPointModel body = request.getBody() == null ? defaultBody : request.getBody();
        switch(action) {
            case GET:
                if (xid == null) {
                    throw new BadRequestException(new TranslatableMessage("rest.error.mustNotBeNull", "xid"));
                }
                result.setBody(this.getDataPoint(xid, user));
                break;
            case CREATE:
                if (body == null) {
                    throw new BadRequestException(new TranslatableMessage("rest.error.mustNotBeNull", "body"));
                }
                result.setBody(this.createDataPoint(body, user, builder).getBody());
                break;
            case UPDATE:
                if (xid == null) {
                    throw new BadRequestException(new TranslatableMessage("rest.error.mustNotBeNull", "xid"));
                }
                if (body == null) {
                    throw new BadRequestException(new TranslatableMessage("rest.error.mustNotBeNull", "body"));
                }
                result.setBody(this.updateDataPoint(xid, body, user, builder).getBody());
                break;
            case DELETE:
                if (xid == null) {
                    throw new BadRequestException(new TranslatableMessage("rest.error.mustNotBeNull", "xid"));
                }
                result.setBody(this.deleteDataPoint(xid, user));
                break;
        }
    } catch (Exception e) {
        result.exceptionCaught(e);
    }
    return result;
}
Also used : VoAction(com.infiniteautomation.mango.rest.v2.bulk.VoAction) DataPointModel(com.infiniteautomation.mango.rest.v2.model.dataPoint.DataPointModel) BadRequestException(com.infiniteautomation.mango.rest.v2.exception.BadRequestException) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) BadRequestException(com.infiniteautomation.mango.rest.v2.exception.BadRequestException) AbstractRestV2Exception(com.infiniteautomation.mango.rest.v2.exception.AbstractRestV2Exception) AccessDeniedException(com.infiniteautomation.mango.rest.v2.exception.AccessDeniedException) NotFoundRestException(com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException)

Example 13 with BadRequestException

use of com.netflix.spinnaker.front50.exception.BadRequestException in project ma-modules-public by infiniteautomation.

the class PointValueRestController method buildMap.

/**
 * Build and validate the map of Requested Data Points
 * @param user
 * @param xids
 * @return
 */
protected Map<Integer, DataPointVO> buildMap(User user, String[] xids, RollupEnum rollup) {
    // Build the map, check permissions
    Map<Integer, DataPointVO> voMap = new HashMap<Integer, DataPointVO>();
    for (String xid : xids) {
        DataPointVO vo = DataPointDao.instance.getByXid(xid);
        if (vo == null) {
            throw new NotFoundRestException();
        } else {
            if (!Permissions.hasDataPointReadPermission(user, vo))
                throw new AccessDeniedException();
        }
        // TODO Add support for NONE Default Rollup
        if (rollup == RollupEnum.POINT_DEFAULT && vo.getRollup() == RollupEnum.NONE.getId())
            throw new BadRequestException(new TranslatableMessage("common.default", "Default point rollup of NONE is not yet supported for point with xid: " + xid));
        ;
        // Validate the rollup
        switch(vo.getPointLocator().getDataTypeId()) {
            case DataTypes.ALPHANUMERIC:
            case DataTypes.BINARY:
            case DataTypes.IMAGE:
            case DataTypes.MULTISTATE:
                if (rollup.nonNumericSupport() == false)
                    throw new BadRequestException(new TranslatableMessage("rest.validate.rollup.incompatible", rollup.toString(), xid));
                break;
            case DataTypes.NUMERIC:
                break;
        }
        voMap.put(vo.getId(), vo);
    }
    // Do we have any points
    if (voMap.isEmpty())
        throw new NotFoundRestException();
    return voMap;
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) NotFoundRestException(com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException) AccessDeniedException(com.infiniteautomation.mango.rest.v2.exception.AccessDeniedException) HashMap(java.util.HashMap) BadRequestException(com.infiniteautomation.mango.rest.v2.exception.BadRequestException) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage)

Example 14 with BadRequestException

use of com.netflix.spinnaker.front50.exception.BadRequestException in project ma-modules-public by infiniteautomation.

the class DataPointTagsRestController method bulkDataPointTagOperationSync.

@ApiOperation(value = "Synchronously bulk get/set/add data point tags for a list of XIDs", notes = "User must have read/edit permission for the data point")
@RequestMapping(method = RequestMethod.POST, value = "/bulk-sync")
public TagBulkResponse bulkDataPointTagOperationSync(@RequestBody TagBulkRequest requestBody, @AuthenticationPrincipal User user) {
    BulkTagAction defaultAction = requestBody.getAction();
    Map<String, String> defaultBody = requestBody.getBody();
    List<TagIndividualRequest> requests = requestBody.getRequests();
    if (requests == null) {
        throw new BadRequestException(new TranslatableMessage("rest.error.mustNotBeNull", "requests"));
    }
    TagBulkResponse response = new TagBulkResponse();
    for (TagIndividualRequest request : requests) {
        TagIndividualResponse individualResponse = doIndividualRequest(request, defaultAction, defaultBody, user);
        response.addResponse(individualResponse);
    }
    return response;
}
Also used : BadRequestException(com.infiniteautomation.mango.rest.v2.exception.BadRequestException) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 15 with BadRequestException

use of com.netflix.spinnaker.front50.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, User user) {
    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, user));
                break;
            case SET:
                if (tags == null) {
                    throw new BadRequestException(new TranslatableMessage("rest.error.mustNotBeNull", "body"));
                }
                result.setBody(this.setTagsForDataPoint(xid, tags, user));
                break;
            case MERGE:
                if (tags == null) {
                    throw new BadRequestException(new TranslatableMessage("rest.error.mustNotBeNull", "body"));
                }
                result.setBody(this.addTagsForDataPoint(xid, tags, user));
                break;
        }
    } catch (Exception e) {
        result.exceptionCaught(e);
    }
    return result;
}
Also used : BadRequestException(com.infiniteautomation.mango.rest.v2.exception.BadRequestException) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) BadRequestException(com.infiniteautomation.mango.rest.v2.exception.BadRequestException) AbstractRestV2Exception(com.infiniteautomation.mango.rest.v2.exception.AbstractRestV2Exception) AccessDeniedException(com.infiniteautomation.mango.rest.v2.exception.AccessDeniedException) NotFoundRestException(com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException)

Aggregations

BadRequestException (com.infiniteautomation.mango.rest.v2.exception.BadRequestException)17 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)17 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)10 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)10 NotFoundRestException (com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException)6 ResponseEntity (org.springframework.http.ResponseEntity)6 AccessDeniedException (com.infiniteautomation.mango.rest.v2.exception.AccessDeniedException)5 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)4 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)4 AbstractRestV2Exception (com.infiniteautomation.mango.rest.v2.exception.AbstractRestV2Exception)4 DataPointModel (com.infiniteautomation.mango.rest.v2.model.dataPoint.DataPointModel)4 HttpHeaders (org.springframework.http.HttpHeaders)4 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)4 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)3 User (com.serotonin.m2m2.vo.User)3 URI (java.net.URI)3 VoAction (com.infiniteautomation.mango.rest.v2.bulk.VoAction)2 BadRequestException (com.netflix.spinnaker.front50.exception.BadRequestException)2 JsonObject (com.serotonin.json.type.JsonObject)2