Search in sources :

Example 1 with RestAPIException

use of io.jmix.rest.exception.RestAPIException in project jmix by jmix-framework.

the class FileDownloadController method downloadFile.

@GetMapping
public void downloadFile(@RequestParam String fileRef, @RequestParam(required = false) Boolean attachment, HttpServletResponse response) {
    checkFileDownloadPermission();
    try {
        FileRef fileReference;
        fileReference = FileRef.fromString(fileRef);
        fileTransferService.downloadAndWriteResponse(fileReference, fileReference.getStorageName(), attachment, response);
    } catch (IllegalArgumentException e) {
        throw new RestAPIException("Invalid file reference", String.format("Cannot convert '%s' into valid file reference", fileRef), HttpStatus.BAD_REQUEST, e);
    }
}
Also used : FileRef(io.jmix.core.FileRef) RestAPIException(io.jmix.rest.exception.RestAPIException) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 2 with RestAPIException

use of io.jmix.rest.exception.RestAPIException in project jmix by jmix-framework.

the class CubaFileDownloadController method checkFileDownloadPermission.

protected void checkFileDownloadPermission() {
    RestFileDownloadContext downloadContext = new RestFileDownloadContext();
    accessManager.applyRegisteredConstraints(downloadContext);
    if (!downloadContext.isPermitted()) {
        throw new RestAPIException("File download failed", "File download is not permitted", HttpStatus.FORBIDDEN);
    }
    if (!security.isEntityOpPermitted(FileDescriptor.class, EntityOp.READ)) {
        throw new RestAPIException("Reading forbidden", "Reading of the sys$FileDescriptor is forbidden", HttpStatus.FORBIDDEN);
    }
}
Also used : RestAPIException(io.jmix.rest.exception.RestAPIException) RestFileDownloadContext(io.jmix.rest.accesscontext.RestFileDownloadContext) FileDescriptor(com.haulmont.cuba.core.entity.FileDescriptor)

Example 3 with RestAPIException

use of io.jmix.rest.exception.RestAPIException in project jmix by jmix-framework.

the class CubaFileUploadController method checkFileUploadPermission.

protected void checkFileUploadPermission() {
    RestFileUploadContext uploadContext = new RestFileUploadContext();
    accessManager.applyRegisteredConstraints(uploadContext);
    if (!uploadContext.isPermitted()) {
        throw new RestAPIException("File upload failed", "File upload is not permitted", HttpStatus.FORBIDDEN);
    }
    if (!security.isEntityOpPermitted(FileDescriptor.class, EntityOp.CREATE)) {
        throw new RestAPIException("Creating forbidden", "Creating of the sys$FileDescriptor is forbidden", HttpStatus.FORBIDDEN);
    }
}
Also used : RestAPIException(io.jmix.rest.exception.RestAPIException) RestFileUploadContext(io.jmix.rest.accesscontext.RestFileUploadContext) FileDescriptor(com.haulmont.cuba.core.entity.FileDescriptor)

Example 4 with RestAPIException

use of io.jmix.rest.exception.RestAPIException in project jmix by jmix-framework.

the class CubaFileUploadController method checkFileExists.

protected void checkFileExists(@Nullable String id) {
    if (Strings.isNullOrEmpty(id)) {
        return;
    }
    LoadContext<FileDescriptor> ctx = new LoadContext<>(FileDescriptor.class).setId(UUID.fromString(id));
    FileDescriptor fileDescriptor = dataManager.load(ctx);
    if (fileDescriptor != null) {
        log.error("File with id = {} already exists", id);
        throw new RestAPIException("File already exists", String.format("File with id = %s already exists", id), HttpStatus.INTERNAL_SERVER_ERROR);
    }
}
Also used : RestAPIException(io.jmix.rest.exception.RestAPIException) FileDescriptor(com.haulmont.cuba.core.entity.FileDescriptor)

Example 5 with RestAPIException

use of io.jmix.rest.exception.RestAPIException in project jmix by jmix-framework.

the class DatatypesControllerManager method getDatatypesJson.

public String getDatatypesJson() {
    JsonArray jsonArray = new JsonArray();
    try {
        for (String id : datatypes.getIds()) {
            JsonObject jsonObject = new JsonObject();
            jsonObject.addProperty("id", id);
            // for backward compatibility
            jsonObject.addProperty("name", id);
            Datatype datatype = datatypes.get(id);
            if (datatype instanceof ParameterizedDatatype) {
                Map<String, Object> parameters = ((ParameterizedDatatype) datatype).getParameters();
                for (Map.Entry<String, Object> entry : parameters.entrySet()) {
                    jsonObject.addProperty(entry.getKey(), entry.getValue().toString());
                }
            }
            jsonArray.add(jsonObject);
        }
    } catch (Exception e) {
        log.error("Fail to get datatype settings", e);
        throw new RestAPIException("Fail to get datatype settings", e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR, e);
    }
    return jsonArray.toString();
}
Also used : JsonArray(com.google.gson.JsonArray) ParameterizedDatatype(io.jmix.core.metamodel.datatype.ParameterizedDatatype) JsonObject(com.google.gson.JsonObject) RestAPIException(io.jmix.rest.exception.RestAPIException) JsonObject(com.google.gson.JsonObject) Map(java.util.Map) RestAPIException(io.jmix.rest.exception.RestAPIException) Datatype(io.jmix.core.metamodel.datatype.Datatype) ParameterizedDatatype(io.jmix.core.metamodel.datatype.ParameterizedDatatype)

Aggregations

RestAPIException (io.jmix.rest.exception.RestAPIException)32 MetaClass (io.jmix.core.metamodel.model.MetaClass)8 RestFilterParseException (io.jmix.rest.impl.service.filter.RestFilterParseException)7 FileDescriptor (com.haulmont.cuba.core.entity.FileDescriptor)6 EntityImportException (io.jmix.core.impl.importexport.EntityImportException)5 EntitySerializationException (io.jmix.core.impl.serialization.EntitySerializationException)5 EntityValidationException (io.jmix.core.validation.EntityValidationException)5 IOException (java.io.IOException)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 Datatype (io.jmix.core.metamodel.datatype.Datatype)2 RestFileDownloadContext (io.jmix.rest.accesscontext.RestFileDownloadContext)2 RestFileUploadContext (io.jmix.rest.accesscontext.RestFileUploadContext)2 RestFilterParseResult (io.jmix.rest.impl.service.filter.RestFilterParseResult)2 ResponseInfo (io.jmix.rest.impl.service.filter.data.ResponseInfo)2 InputStream (java.io.InputStream)2 Method (java.lang.reflect.Method)2 ParseException (java.text.ParseException)2 ArrayList (java.util.ArrayList)2 ServletInputStream (javax.servlet.ServletInputStream)2 PostMapping (org.springframework.web.bind.annotation.PostMapping)2