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);
}
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;
}
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;
}
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;
}
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;
}
Aggregations