Search in sources :

Example 11 with FileStorageException

use of io.jmix.core.FileStorageException in project jmix by jmix-framework.

the class ImapStoreBuilder method getMailSSLSocketFactory.

protected MailSSLSocketFactory getMailSSLSocketFactory(ImapMailBox box) throws MessagingException {
    MailSSLSocketFactory socketFactory;
    try {
        socketFactory = new MailSSLSocketFactory();
        if (config.isTrustAllCertificates()) {
            log.debug("Configure factory to trust all certificates");
            socketFactory.setTrustAllHosts(true);
        } else {
            socketFactory.setTrustAllHosts(false);
            if (box.getRootCertificate() != null) {
                log.debug("Configure factory to trust only known certificates and certificated from file#{}", box.getRootCertificate());
                if (fileStorage == null) {
                    fileStorage = fileStorageLocator.getDefault();
                }
                try (InputStream rootCert = fileStorage.openStream(box.getRootCertificate())) {
                    socketFactory.setTrustManagers(new TrustManager[] { new UnifiedTrustManager(rootCert) });
                } catch (FileStorageException | GeneralSecurityException | IOException e) {
                    throw new RuntimeException("SSL error", e);
                }
            }
        }
    } catch (GeneralSecurityException e) {
        throw new MessagingException("SSL Socket factory exception", e);
    }
    return socketFactory;
}
Also used : MailSSLSocketFactory(com.sun.mail.util.MailSSLSocketFactory) MessagingException(javax.mail.MessagingException) InputStream(java.io.InputStream) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException) FileStorageException(io.jmix.core.FileStorageException)

Example 12 with FileStorageException

use of io.jmix.core.FileStorageException in project jmix by jmix-framework.

the class LocalFileStorage method openStream.

@Override
public InputStream openStream(FileRef reference) {
    Path relativePath = getRelativePath(reference.getPath());
    Path[] roots = getStorageRoots();
    if (roots.length == 0) {
        log.error("No storage directories available");
        throw new FileStorageException(FileStorageException.Type.FILE_NOT_FOUND, reference.toString());
    }
    InputStream inputStream = null;
    for (Path root : roots) {
        Path path = root.resolve(relativePath);
        if (!path.toFile().exists()) {
            log.error("File " + path + " not found");
            continue;
        }
        try {
            inputStream = Files.newInputStream(path);
        } catch (IOException e) {
            log.error("Error opening input stream for " + path, e);
        }
    }
    if (inputStream != null) {
        return inputStream;
    } else {
        throw new FileStorageException(FileStorageException.Type.FILE_NOT_FOUND, reference.toString());
    }
}
Also used : Path(java.nio.file.Path) InputStream(java.io.InputStream) IOException(java.io.IOException) FileStorageException(io.jmix.core.FileStorageException)

Example 13 with FileStorageException

use of io.jmix.core.FileStorageException in project jmix by jmix-framework.

the class LocalFileStorage method getStorageRoots.

protected Path[] getStorageRoots() {
    if (storageRoots == null) {
        String storageDir = this.storageDir != null ? this.storageDir : properties.getStorageDir();
        if (StringUtils.isBlank(storageDir)) {
            String workDir = coreProperties.getWorkDir();
            Path dir = Paths.get(workDir, "filestorage");
            if (!dir.toFile().exists() && !dir.toFile().mkdirs()) {
                throw new FileStorageException(FileStorageException.Type.IO_EXCEPTION, "Cannot create filestorage directory: " + dir.toAbsolutePath().toString());
            }
            storageRoots = new Path[] { dir };
        } else {
            List<Path> list = new ArrayList<>();
            for (String str : storageDir.split(",")) {
                str = str.trim();
                if (!StringUtils.isEmpty(str)) {
                    Path path = Paths.get(str);
                    if (!list.contains(path))
                        list.add(path);
                }
            }
            storageRoots = list.toArray(new Path[0]);
        }
    }
    return storageRoots;
}
Also used : Path(java.nio.file.Path) ArrayList(java.util.ArrayList) FileStorageException(io.jmix.core.FileStorageException)

Aggregations

FileStorageException (io.jmix.core.FileStorageException)13 IOException (java.io.IOException)8 File (java.io.File)5 InputStream (java.io.InputStream)4 Path (java.nio.file.Path)4 UUID (java.util.UUID)3 SdkException (software.amazon.awssdk.core.exception.SdkException)3 S3Client (software.amazon.awssdk.services.s3.S3Client)3 FileRef (io.jmix.core.FileRef)2 BufferedInputStream (java.io.BufferedInputStream)2 FileNotFoundException (java.io.FileNotFoundException)2 FileOutputStream (java.io.FileOutputStream)2 ArrayList (java.util.ArrayList)2 MailSSLSocketFactory (com.sun.mail.util.MailSSLSocketFactory)1 FileInputStream (java.io.FileInputStream)1 OutputStream (java.io.OutputStream)1 GeneralSecurityException (java.security.GeneralSecurityException)1 MessagingException (javax.mail.MessagingException)1 CompleteMultipartUploadRequest (software.amazon.awssdk.services.s3.model.CompleteMultipartUploadRequest)1 CompletedMultipartUpload (software.amazon.awssdk.services.s3.model.CompletedMultipartUpload)1