use of com.haulmont.restapi.exception.RestAPIException in project cuba by cuba-platform.
the class DocumentationController method getProjectSwaggerYaml.
@RequestMapping(value = "/swaggerDetailed.yaml", method = RequestMethod.GET, produces = "application/yaml")
public String getProjectSwaggerYaml() {
ObjectMapper jsonWriter = new ObjectMapper().setSerializationInclusion(JsonInclude.Include.NON_NULL);
YAMLMapper yamlMapper = new YAMLMapper().disable(WRITE_DOC_START_MARKER);
try {
Swagger swagger = swaggerGenerator.generateSwagger();
JsonNode jsonNode = jsonWriter.readTree(jsonWriter.writeValueAsBytes(swagger));
return yamlMapper.writeValueAsString(jsonNode);
} catch (IOException 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 DocumentationController method getSwaggerJson.
@RequestMapping(value = "/swagger.json", method = RequestMethod.GET, produces = "application/json")
public String getSwaggerJson() {
String yaml = getSwaggerYaml();
ObjectMapper yamlReader = new ObjectMapper(new YAMLFactory());
Object obj;
try {
obj = yamlReader.readValue(yaml, Object.class);
ObjectMapper jsonWriter = new ObjectMapper();
return jsonWriter.writeValueAsString(obj);
} catch (IOException e) {
throw new RestAPIException("Internal server error", e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
}
}
use of com.haulmont.restapi.exception.RestAPIException in project cuba by cuba-platform.
the class FileDownloadController method downloadFromMiddlewareAndWriteResponse.
protected void downloadFromMiddlewareAndWriteResponse(FileDescriptor fd, HttpServletResponse response) throws IOException {
ServletOutputStream os = response.getOutputStream();
try (InputStream is = fileLoader.openStream(fd)) {
IOUtils.copy(is, os);
os.flush();
} catch (FileStorageException e) {
throw new RestAPIException("Unable to download file from FileStorage", "Unable to download file from FileStorage: " + fd.getId(), HttpStatus.INTERNAL_SERVER_ERROR);
}
}
use of com.haulmont.restapi.exception.RestAPIException in project cuba by cuba-platform.
the class FileUploadController method uploadFile.
/**
* Method for multipart file upload. It expects the file contents to be passed in the part called 'file'
*/
@PostMapping(consumes = "multipart/form-data")
public ResponseEntity<FileInfo> uploadFile(@RequestParam("file") MultipartFile file, @RequestParam(required = false) String name, HttpServletRequest request) {
try {
if (Strings.isNullOrEmpty(name)) {
name = file.getOriginalFilename();
}
long size = file.getSize();
FileDescriptor fd = createFileDescriptor(name, size);
InputStream is = file.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);
}
}
use of com.haulmont.restapi.exception.RestAPIException in project cuba by cuba-platform.
the class EntitiesControllerManager method createEntity.
public CreatedEntityInfo createEntity(String entityJson, String entityName, String modelVersion) {
String transformedEntityName = restControllerUtils.transformEntityNameIfRequired(entityName, modelVersion, JsonTransformationDirection.FROM_VERSION);
MetaClass metaClass = restControllerUtils.getMetaClass(transformedEntityName);
checkCanCreateEntity(metaClass);
entityJson = restControllerUtils.transformJsonIfRequired(entityName, modelVersion, JsonTransformationDirection.FROM_VERSION, entityJson);
Entity entity;
try {
entity = entitySerializationAPI.entityFromJson(entityJson, metaClass);
} catch (Exception e) {
throw new RestAPIException("Cannot deserialize an entity from JSON", "", HttpStatus.BAD_REQUEST, e);
}
EntityImportView entityImportView = entityImportViewBuilderAPI.buildFromJson(entityJson, metaClass);
Collection<Entity> importedEntities;
try {
importedEntities = entityImportExportService.importEntities(Collections.singletonList(entity), entityImportView, true);
} catch (EntityImportException e) {
throw new RestAPIException("Entity creation failed", e.getMessage(), HttpStatus.BAD_REQUEST, e);
}
// if many entities were created (because of @Composition references) we must find the main entity
return getMainEntityInfo(importedEntities, metaClass, modelVersion);
}
Aggregations