use of com.serotonin.m2m2.i18n.TranslatableMessage in project ma-modules-public by infiniteautomation.
the class JsonEmportV2Controller method importConfiguration.
@PreAuthorize("isAdmin()")
@ApiOperation(value = "Import Configuration", notes = "Submit the request and get a URL for the results")
@RequestMapping(method = { RequestMethod.POST })
public ResponseEntity<ImportStatusProvider> importConfiguration(HttpServletRequest request, UriComponentsBuilder builder, @ApiParam(value = "Optional timeout for resource to expire, defaults to 5 minutes", required = false, allowMultiple = false) @RequestParam(value = "timeout", required = false) Long timeout, @RequestBody(required = true) JsonValue config, @AuthenticationPrincipal User user) {
if (config instanceof JsonObject) {
// Setup the Temporary Resource
String resourceId = importStatusResources.generateResourceId();
ImportStatusProvider statusProvider = new ImportStatusProvider(importStatusResources, resourceId, websocket, timeout, config.toJsonObject(), user);
this.importStatusResources.put(resourceId, statusProvider);
URI location = builder.path("/v2/json-emport/import/{id}").buildAndExpand(resourceId).toUri();
return getResourceCreated(statusProvider, location);
} else {
throw new GenericRestException(HttpStatus.NOT_ACCEPTABLE, new TranslatableMessage("emport.invalidImportData"));
}
}
use of com.serotonin.m2m2.i18n.TranslatableMessage 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"));
}
}
use of com.serotonin.m2m2.i18n.TranslatableMessage in project ma-modules-public by infiniteautomation.
the class SessionExceptionRestV2Controller method clearLatest.
@ApiOperation(value = "Clear Last Exception for your session", notes = "")
@ApiResponses({ @ApiResponse(code = 401, message = "Unauthorized user access", response = ResponseEntity.class), @ApiResponse(code = 404, message = "No Exception exists", response = ResponseEntity.class), @ApiResponse(code = 500, message = "Error processing request", response = ResponseEntity.class) })
@RequestMapping(method = { RequestMethod.PUT }, value = { "/latest" }, produces = { "application/json" })
public ResponseEntity<Map<String, Exception>> clearLatest(HttpServletRequest request) {
RestProcessResult<Map<String, Exception>> result = new RestProcessResult<>(HttpStatus.OK);
// Get latest Session Exception
HttpSession session = request.getSession(false);
if (session == null)
throw new ServerErrorException(new TranslatableMessage("rest.error.noSession"));
Map<String, Exception> exceptionMap = new HashMap<String, Exception>();
for (String key : exceptionKeys) {
exceptionMap.put(key, (Exception) session.getAttribute(key));
session.removeAttribute(key);
}
return result.createResponseEntity(exceptionMap);
}
use of com.serotonin.m2m2.i18n.TranslatableMessage in project ma-modules-public by infiniteautomation.
the class SessionExceptionRestV2Controller method getLatest.
@ApiOperation(value = "Get Last Exception for your session", notes = "")
@ApiResponses({ @ApiResponse(code = 401, message = "Unauthorized user access", response = ResponseEntity.class), @ApiResponse(code = 404, message = "No Exception exists", response = ResponseEntity.class), @ApiResponse(code = 500, message = "Error processing request", response = ResponseEntity.class) })
@RequestMapping(method = { RequestMethod.GET }, value = { "/latest" }, produces = { "application/json" })
public ResponseEntity<Map<String, Exception>> getLatest(HttpServletRequest request) {
RestProcessResult<Map<String, Exception>> result = new RestProcessResult<>(HttpStatus.OK);
// Get latest Session Exception
HttpSession session = request.getSession(false);
if (session == null)
throw new ServerErrorException(new TranslatableMessage("rest.error.noSession"));
Map<String, Exception> exceptionMap = new HashMap<String, Exception>();
for (String key : exceptionKeys) {
exceptionMap.put(key, (Exception) session.getAttribute(key));
}
return result.createResponseEntity(exceptionMap);
}
use of com.serotonin.m2m2.i18n.TranslatableMessage 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);
}
Aggregations