use of com.haulmont.restapi.exception.RestAPIException in project cuba by cuba-platform.
the class AbstractEntityJsonTransformer method transformJson.
/**
* Method checks whether the passed JSON is an array of entities and if so it executes the {@link #transformEntityJson(ObjectNode, ObjectMapper)}
* method for each array element. Method executes the {@link #transformEntityJson(ObjectNode, ObjectMapper)} method otherwise.
* @param json JSON containing an entity or a list of entities
* @return transformed JSON
*/
@Override
public String transformJson(String json) {
ObjectMapper objectMapper = new ObjectMapper();
try {
JsonNode rootNode = objectMapper.readTree(json);
if (rootNode.isArray()) {
Iterator<JsonNode> iterator = rootNode.elements();
while (iterator.hasNext()) {
ObjectNode entityJsonNode = (ObjectNode) iterator.next();
transformEntityJson(entityJsonNode, objectMapper);
}
} else if (rootNode.isObject()) {
transformEntityJson((ObjectNode) rootNode, objectMapper);
}
return objectMapper.writeValueAsString(rootNode);
} catch (Exception e) {
throw new RestAPIException("JSON transformation failed", e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR, e);
}
}
use of com.haulmont.restapi.exception.RestAPIException in project cuba by cuba-platform.
the class DocumentationController method getProjectSwaggerJson.
@RequestMapping(value = "/swaggerDetailed.json", method = RequestMethod.GET, produces = APPLICATION_JSON_VALUE)
public String getProjectSwaggerJson() {
ObjectMapper jsonWriter = new ObjectMapper();
jsonWriter.setSerializationInclusion(JsonInclude.Include.NON_NULL);
try {
Swagger swagger = swaggerGenerator.generateSwagger();
return jsonWriter.writeValueAsString(swagger);
} catch (JsonProcessingException e) {
throw new RestAPIException("An error occurred while generating Swagger documentation", e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
}
}
use of com.haulmont.restapi.exception.RestAPIException in project cuba by cuba-platform.
the class FileDownloadController method downloadFile.
@GetMapping("/{fileDescriptorId}")
public void downloadFile(@PathVariable String fileDescriptorId, @RequestParam(required = false) Boolean attachment, HttpServletResponse response) {
UUID uuid;
try {
uuid = UUID.fromString(fileDescriptorId);
} catch (IllegalArgumentException e) {
throw new RestAPIException("Invalid entity ID", String.format("Cannot convert %s into valid entity ID", fileDescriptorId), HttpStatus.BAD_REQUEST);
}
LoadContext<FileDescriptor> ctx = LoadContext.create(FileDescriptor.class).setId(uuid);
FileDescriptor fd = dataService.load(ctx);
if (fd == null) {
throw new RestAPIException("File not found", "File not found. Id: " + fileDescriptorId, HttpStatus.NOT_FOUND);
}
try {
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Pragma", "no-cache");
response.setDateHeader("Expires", 0);
response.setHeader("Content-Type", getContentType(fd));
response.setHeader("Content-Disposition", (BooleanUtils.isTrue(attachment) ? "attachment" : "inline") + "; filename=\"" + fd.getName() + "\"");
downloadFromMiddlewareAndWriteResponse(fd, response);
} catch (Exception e) {
log.error("Error on downloading the file {}", fileDescriptorId, e);
throw new RestAPIException("Error on downloading the file", "", HttpStatus.INTERNAL_SERVER_ERROR);
}
}
use of com.haulmont.restapi.exception.RestAPIException in project cuba by cuba-platform.
the class FileUploadController method uploadFile.
/**
* Method for simple file upload. File contents are placed in the request body. Optional file name parameter is
* passed as a query param.
*/
@PostMapping(consumes = "!multipart/form-data")
public ResponseEntity<FileInfo> uploadFile(HttpServletRequest request, @RequestParam(required = false) String name) {
try {
String contentLength = request.getHeader("Content-Length");
long size = 0;
try {
size = Long.parseLong(contentLength);
} catch (NumberFormatException ignored) {
}
FileDescriptor fd = createFileDescriptor(name, size);
ServletInputStream is = request.getInputStream();
uploadToMiddleware(is, fd);
saveFileDescriptor(fd);
return createFileInfoResponseEntity(request, fd);
} catch (Exception e) {
log.error("File upload failed", e);
throw new RestAPIException("File upload failed", "File upload failed", HttpStatus.INTERNAL_SERVER_ERROR);
}
}
Aggregations