Search in sources :

Example 11 with FileStorageException

use of com.haulmont.cuba.core.global.FileStorageException in project cuba by cuba-platform.

the class WebFileUploadField method getFileContent.

@Override
public InputStream getFileContent() {
    if (contentProvider != null) {
        return contentProvider.get();
    }
    FileDescriptor fileDescriptor = getValue();
    switch(mode) {
        case MANUAL:
            if (fileId == null) {
                return new FileDataProvider(fileDescriptor).provide();
            }
            File file = fileUploading.getFile(fileId);
            if (file != null) {
                try {
                    return new FileInputStream(file);
                } catch (FileNotFoundException e) {
                    log.error("Unable to get content of {}", file, e);
                }
                return null;
            }
            FileStorageService fileStorageService = beanLocator.get(FileStorageService.NAME);
            try {
                if (fileStorageService.fileExists(fileDescriptor)) {
                    return new FileDataProvider(fileDescriptor).provide();
                }
            } catch (FileStorageException e) {
                log.error("Unable to get content of {}", fileDescriptor, e);
                return null;
            }
            break;
        case IMMEDIATE:
            if (fileDescriptor != null) {
                return new FileDataProvider(fileDescriptor).provide();
            }
    }
    return null;
}
Also used : FileDataProvider(com.haulmont.cuba.gui.export.FileDataProvider) FileStorageService(com.haulmont.cuba.core.app.FileStorageService) FileStorageException(com.haulmont.cuba.core.global.FileStorageException) FileDescriptor(com.haulmont.cuba.core.entity.FileDescriptor)

Example 12 with FileStorageException

use of com.haulmont.cuba.core.global.FileStorageException in project cuba by cuba-platform.

the class WebFileUploadField method initUploadButton.

protected void initUploadButton(CubaFileUpload impl) {
    impl.setProgressWindowCaption(messages.getMainMessage("upload.uploadingProgressTitle"));
    impl.setUnableToUploadFileMessage(messages.getMainMessage("upload.unableToUploadFile"));
    impl.setCancelButtonCaption(messages.getMainMessage("upload.cancel"));
    impl.setCaption(messages.getMainMessage("upload.submit"));
    impl.setDropZonePrompt(messages.getMainMessage("upload.singleDropZonePrompt"));
    impl.setDescription(null);
    impl.setFileSizeLimit(getActualFileSizeLimit());
    impl.setReceiver(this::receiveUpload);
    impl.addStartedListener(event -> fireFileUploadStart(event.getFileName(), event.getContentLength()));
    impl.addFinishedListener(event -> fireFileUploadFinish(event.getFileName(), event.getContentLength()));
    impl.addSucceededListener(event -> {
        fileName = event.getFileName();
        fileId = tempFileId;
        saveFile(getFileDescriptor());
        component.setFileNameButtonCaption(fileName);
        fireFileUploadSucceed(event.getFileName(), event.getContentLength());
    });
    impl.addFailedListener(event -> {
        try {
            fileUploading.deleteFile(tempFileId);
            tempFileId = null;
        } catch (Exception e) {
            if (e instanceof FileStorageException) {
                FileStorageException fse = (FileStorageException) e;
                if (fse.getType() != FileStorageException.Type.FILE_NOT_FOUND) {
                    log.warn(String.format("Could not remove temp file %s after broken uploading", tempFileId));
                }
            }
            log.warn(String.format("Error while delete temp file %s", tempFileId));
        }
        fireFileUploadError(event.getFileName(), event.getContentLength(), event.getReason());
    });
    impl.addFileSizeLimitExceededListener(e -> {
        Notifications notifications = getScreenContext(this).getNotifications();
        notifications.create(NotificationType.WARNING).withCaption(messages.formatMainMessage("upload.fileTooBig.message", e.getFileName(), getFileSizeLimitString())).show();
    });
    impl.addFileExtensionNotAllowedListener(e -> {
        Notifications notifications = getScreenContext(this).getNotifications();
        notifications.create(NotificationType.WARNING).withCaption(messages.formatMainMessage("upload.fileIncorrectExtension.message", e.getFileName())).show();
    });
}
Also used : FileStorageException(com.haulmont.cuba.core.global.FileStorageException) Notifications(com.haulmont.cuba.gui.Notifications) FileStorageException(com.haulmont.cuba.core.global.FileStorageException)

Example 13 with FileStorageException

use of com.haulmont.cuba.core.global.FileStorageException in project cuba by cuba-platform.

the class WebFileDescriptorResource method createResource.

@Override
protected void createResource() {
    resource = new StreamResource(() -> {
        try {
            return AppBeans.get(FileLoader.class).openStream(fileDescriptor);
        } catch (FileStorageException e) {
            throw new RuntimeException(FILE_STORAGE_EXCEPTION_MESSAGE, e);
        }
    }, getResourceName());
    StreamResource streamResource = (StreamResource) this.resource;
    streamResource.setCacheTime(cacheTime);
    streamResource.setBufferSize(bufferSize);
}
Also used : StreamResource(com.vaadin.server.StreamResource) FileStorageException(com.haulmont.cuba.core.global.FileStorageException)

Example 14 with FileStorageException

use of com.haulmont.cuba.core.global.FileStorageException 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) {
        log.error("Unable to load file from middleware", e);
        error(response);
    }
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) InputStream(java.io.InputStream) FileStorageException(com.haulmont.cuba.core.global.FileStorageException)

Example 15 with FileStorageException

use of com.haulmont.cuba.core.global.FileStorageException in project cuba by cuba-platform.

the class AmazonS3FileStorage method openStream.

@Override
public InputStream openStream(FileDescriptor fileDescr) throws FileStorageException {
    URL amazonUrl = getAmazonUrl(fileDescr);
    // for a simple GET, we have no body so supply the precomputed 'empty' hash
    Map<String, String> headers = new HashMap<>();
    headers.put("x-amz-content-sha256", AWS4SignerBase.EMPTY_BODY_SHA256);
    String authorization = createAuthorizationHeader(amazonUrl, "GET", headers);
    headers.put("Authorization", authorization);
    HttpUtils.HttpResponse httpResponse = HttpUtils.invokeHttpRequest(amazonUrl, "GET", headers, null);
    if (httpResponse.isStatusOk()) {
        return httpResponse.getInputStream();
    } else if (httpResponse.isStatusNotFound()) {
        throw new FileStorageException(FileStorageException.Type.FILE_NOT_FOUND, "File not found" + getFileName(fileDescr));
    } else {
        String message = String.format("Could not get file %s. %s", getFileName(fileDescr), getInputStreamContent(httpResponse));
        throw new FileStorageException(FileStorageException.Type.IO_EXCEPTION, message);
    }
}
Also used : HttpUtils(com.haulmont.cuba.core.app.filestorage.amazon.util.HttpUtils) HashMap(java.util.HashMap) FileStorageException(com.haulmont.cuba.core.global.FileStorageException) URL(java.net.URL)

Aggregations

FileStorageException (com.haulmont.cuba.core.global.FileStorageException)23 FileDescriptor (com.haulmont.cuba.core.entity.FileDescriptor)9 IOException (java.io.IOException)5 InputStream (java.io.InputStream)5 File (java.io.File)4 HttpUtils (com.haulmont.cuba.core.app.filestorage.amazon.util.HttpUtils)3 SecurityContext (com.haulmont.cuba.core.sys.SecurityContext)3 FileUploadingAPI (com.haulmont.cuba.gui.upload.FileUploadingAPI)3 UserSession (com.haulmont.cuba.security.global.UserSession)3 URL (java.net.URL)3 HashMap (java.util.HashMap)3 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 ClientConfig (com.haulmont.cuba.client.ClientConfig)2 Transaction (com.haulmont.cuba.core.Transaction)2 Configuration (com.haulmont.cuba.core.global.Configuration)2 Messages (com.haulmont.cuba.core.global.Messages)2 Notifications (com.haulmont.cuba.gui.Notifications)2 CubaFileUpload (com.haulmont.cuba.web.toolkit.ui.CubaFileUpload)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 FileOutputStream (java.io.FileOutputStream)2