Search in sources :

Example 1 with RestAPIException

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);
    }
}
Also used : YAMLMapper(com.fasterxml.jackson.dataformat.yaml.YAMLMapper) Swagger(io.swagger.models.Swagger) RestAPIException(com.haulmont.restapi.exception.RestAPIException) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with RestAPIException

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);
    }
}
Also used : YAMLFactory(com.fasterxml.jackson.dataformat.yaml.YAMLFactory) RestAPIException(com.haulmont.restapi.exception.RestAPIException) IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with RestAPIException

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);
    }
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) InputStream(java.io.InputStream) RestAPIException(com.haulmont.restapi.exception.RestAPIException)

Example 4 with RestAPIException

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);
    }
}
Also used : ServletInputStream(javax.servlet.ServletInputStream) InputStream(java.io.InputStream) RestAPIException(com.haulmont.restapi.exception.RestAPIException) FileDescriptor(com.haulmont.cuba.core.entity.FileDescriptor) RestAPIException(com.haulmont.restapi.exception.RestAPIException) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 5 with RestAPIException

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);
}
Also used : EntityImportView(com.haulmont.cuba.core.app.importexport.EntityImportView) MetaClass(com.haulmont.chile.core.model.MetaClass) EntityImportException(com.haulmont.cuba.core.app.importexport.EntityImportException) RestAPIException(com.haulmont.restapi.exception.RestAPIException) RestFilterParseException(com.haulmont.restapi.service.filter.RestFilterParseException) EntityImportException(com.haulmont.cuba.core.app.importexport.EntityImportException) RestAPIException(com.haulmont.restapi.exception.RestAPIException)

Aggregations

RestAPIException (com.haulmont.restapi.exception.RestAPIException)19 MetaClass (com.haulmont.chile.core.model.MetaClass)8 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)4 JsonObject (com.google.gson.JsonObject)4 Entity (com.haulmont.cuba.core.entity.Entity)4 RestFilterParseException (com.haulmont.restapi.service.filter.RestFilterParseException)4 IOException (java.io.IOException)4 EntityImportException (com.haulmont.cuba.core.app.importexport.EntityImportException)3 FileDescriptor (com.haulmont.cuba.core.entity.FileDescriptor)3 ParseException (java.text.ParseException)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 Datatype (com.haulmont.chile.core.datatypes.Datatype)2 EntityImportView (com.haulmont.cuba.core.app.importexport.EntityImportView)2 EntitySerializationOption (com.haulmont.cuba.core.app.serialization.EntitySerializationOption)2 Swagger (io.swagger.models.Swagger)2 InputStream (java.io.InputStream)2 Method (java.lang.reflect.Method)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1