use of com.netflix.spinnaker.front50.exception.BadRequestException in project ma-modules-public by infiniteautomation.
the class DataPointRestController method createDataPoint.
@ApiOperation(value = "Create a new data point")
@RequestMapping(method = RequestMethod.POST)
public ResponseEntity<DataPointModel> createDataPoint(@ApiParam(value = "Data point model", required = true) @RequestBody(required = true) DataPointModel model, @AuthenticationPrincipal User user, UriComponentsBuilder builder) {
DataSourceVO<?> dataSource = DataSourceDao.instance.getByXid(model.getDataSourceXid());
if (dataSource == null) {
throw new BadRequestException(new TranslatableMessage("rest.error.invalidDataSourceXid"));
}
Permissions.ensureDataSourcePermission(user, dataSource);
DataPointVO dataPoint = new DataPointVO(dataSource);
model.copyPropertiesTo(dataPoint);
if (model.getTemplateXid() != null) {
DataPointPropertiesTemplateVO template = (DataPointPropertiesTemplateVO) TemplateDao.instance.getByXid(model.getTemplateXid());
if (template == null) {
throw new BadRequestException(new TranslatableMessage("rest.error.invalidTemplateXid"));
}
dataPoint.withTemplate(template);
}
dataPoint.ensureValid();
Common.runtimeManager.saveDataPoint(dataPoint);
URI location = builder.path("/v2/data-points/{xid}").buildAndExpand(dataPoint.getXid()).toUri();
HttpHeaders headers = new HttpHeaders();
headers.setLocation(location);
return new ResponseEntity<>(new DataPointModel(dataPoint), headers, HttpStatus.CREATED);
}
use of com.netflix.spinnaker.front50.exception.BadRequestException in project ma-modules-public by infiniteautomation.
the class DataPointRestController method bulkDataPointOperation.
@ApiOperation(value = "Bulk get/create/update/delete data points", notes = "User must have read/edit permission for the data point")
@RequestMapping(method = RequestMethod.POST, value = "/bulk")
public ResponseEntity<TemporaryResource<DataPointBulkResponse, AbstractRestV2Exception>> bulkDataPointOperation(@RequestBody DataPointBulkRequest requestBody, @AuthenticationPrincipal User user, UriComponentsBuilder builder) {
VoAction defaultAction = requestBody.getAction();
DataPointModel defaultBody = requestBody.getBody();
List<DataPointIndividualRequest> requests = requestBody.getRequests();
if (requests == null) {
throw new BadRequestException(new TranslatableMessage("rest.error.mustNotBeNull", "requests"));
}
String resourceId = requestBody.getId();
Long expiration = requestBody.getExpiration();
Long timeout = requestBody.getTimeout();
TemporaryResource<DataPointBulkResponse, AbstractRestV2Exception> responseBody = bulkResourceManager.newTemporaryResource(RESOURCE_TYPE_BULK_DATA_POINT, resourceId, user.getId(), expiration, timeout, (resource) -> {
DataPointBulkResponse bulkResponse = new DataPointBulkResponse();
int i = 0;
resource.progress(bulkResponse, i++, requests.size());
for (DataPointIndividualRequest request : requests) {
UriComponentsBuilder reqBuilder = UriComponentsBuilder.newInstance();
DataPointIndividualResponse individualResponse = doIndividualRequest(request, defaultAction, defaultBody, user, reqBuilder);
bulkResponse.addResponse(individualResponse);
resource.progressOrSuccess(bulkResponse, i++, requests.size());
}
});
HttpHeaders headers = new HttpHeaders();
headers.setLocation(builder.path("/v2/data-points/bulk/{id}").buildAndExpand(responseBody.getId()).toUri());
return new ResponseEntity<TemporaryResource<DataPointBulkResponse, AbstractRestV2Exception>>(responseBody, headers, HttpStatus.CREATED);
}
use of com.netflix.spinnaker.front50.exception.BadRequestException in project ma-modules-public by infiniteautomation.
the class PasswordResetController method sendEmail.
@ApiOperation(value = "Sends the user an email containing a password reset link")
@RequestMapping(method = RequestMethod.POST, value = "/send-email")
public ResponseEntity<Void> sendEmail(@RequestBody SendEmailRequestBody body) throws AddressException, TemplateException, IOException {
User user = UserDao.instance.getUser(body.getUsername());
if (user == null) {
throw new NotFoundRestException();
}
String email = body.getEmail();
if (email == null) {
throw new BadRequestException(new TranslatableMessage("rest.error.emailRequired"));
}
String providedEmail = email.toLowerCase(Locale.ROOT);
String userEmail = user.getEmail().toLowerCase(Locale.ROOT);
if (!providedEmail.equals(userEmail)) {
throw new BadRequestException(new TranslatableMessage("rest.error.incorrectEmail"));
}
if (user.isDisabled()) {
throw new BadRequestException(new TranslatableMessage("rest.error.userIsDisabled"));
}
passwordResetService.sendEmail(user);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
use of com.netflix.spinnaker.front50.exception.BadRequestException in project ma-modules-public by infiniteautomation.
the class PasswordResetController method createTokenForUser.
@ApiOperation(value = "Creates a password reset token and link for the given user")
@RequestMapping(method = RequestMethod.POST, value = "/create")
@PreAuthorize("isAdmin() and isPasswordAuthenticated()")
public CreateTokenResponse createTokenForUser(@RequestBody CreateTokenRequest requestBody, @AuthenticationPrincipal User currentUser) throws AddressException, TemplateException, IOException {
String username = requestBody.getUsername();
boolean lockPassword = requestBody.isLockPassword();
boolean sendEmail = requestBody.isSendEmail();
Date expiry = requestBody.getExpiry();
User user = UserDao.instance.getUser(username);
if (user == null) {
throw new BadRequestException(new TranslatableMessage("rest.error.unknownUser", username));
}
if (user.getId() == currentUser.getId()) {
throw new AccessDeniedException(new TranslatableMessage("rest.error.cantResetOwnUser"));
}
if (lockPassword) {
UserDao.instance.lockPassword(user);
}
CreateTokenResponse response = new CreateTokenResponse();
String token = passwordResetService.generateToken(user, expiry);
response.setToken(token);
response.setFullUrl(passwordResetService.generateResetUrl(token));
response.setRelativeUrl(passwordResetService.generateRelativeResetUrl(token));
if (sendEmail) {
passwordResetService.sendEmail(user, token);
}
return response;
}
use of com.netflix.spinnaker.front50.exception.BadRequestException in project ma-modules-public by infiniteautomation.
the class JsonEmportV2Controller method uploadConfigurationFile.
@PreAuthorize("isAdmin()")
@ApiOperation(value = "Upload 1 configuration json file", notes = "Files should only contain the json object to be imported")
@RequestMapping(method = RequestMethod.POST, value = "/upload-file", consumes = { "multipart/form-data", "multipart/form-data;boundary=-----SWAG_BOUND" })
public ResponseEntity<ImportStatusProvider> uploadConfigurationFile(MultipartHttpServletRequest multipartRequest, UriComponentsBuilder builder, HttpServletRequest request, @ApiParam(value = "timeout for Status Resource to Expire, defaults to 5 minutes", required = false, allowMultiple = false) @RequestParam(value = "timeout", required = false) Long timeout, @AuthenticationPrincipal User user) throws RestValidationFailedException, IOException, JsonException {
Map<String, MultipartFile> map = multipartRequest.getFileMap();
if (map.size() != 1)
throw new BadRequestException(new TranslatableMessage("rest.error.oneFileOnly"));
Iterator<String> itr = multipartRequest.getFileNames();
MultipartFile file = multipartRequest.getFile(itr.next());
if (!file.isEmpty()) {
JsonReader jr = new JsonReader(Common.JSON_CONTEXT, new String(file.getBytes()));
JsonObject jo = jr.read(JsonObject.class);
String resourceId = importStatusResources.generateResourceId();
ImportStatusProvider statusProvider = new ImportStatusProvider(importStatusResources, resourceId, websocket, timeout, jo, user);
// Setup the Temporary Resource
this.importStatusResources.put(resourceId, statusProvider);
URI location = builder.path("/v2/json-emport/import/{id}").buildAndExpand(resourceId).toUri();
return getResourceCreated(statusProvider, location);
} else {
throw new BadRequestException(new TranslatableMessage("rest.error.noFileProvided"));
}
}
Aggregations