Search in sources :

Example 16 with RestAPIException

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);
    }
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) RestAPIException(com.haulmont.restapi.exception.RestAPIException) JsonNode(com.fasterxml.jackson.databind.JsonNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) RestAPIException(com.haulmont.restapi.exception.RestAPIException) IOException(java.io.IOException)

Example 17 with RestAPIException

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

Example 18 with RestAPIException

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);
    }
}
Also used : RestAPIException(com.haulmont.restapi.exception.RestAPIException) UUID(java.util.UUID) FileDescriptor(com.haulmont.cuba.core.entity.FileDescriptor) RestAPIException(com.haulmont.restapi.exception.RestAPIException) IOException(java.io.IOException)

Example 19 with RestAPIException

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);
    }
}
Also used : ServletInputStream(javax.servlet.ServletInputStream) 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)

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