Search in sources :

Example 6 with RemoteFile

use of org.eclipse.scout.rt.shared.services.common.file.RemoteFile in project scout.rt by eclipse.

the class FileService method getRemoteFile.

@Override
public File getRemoteFile(String dir, String simpleName, Locale locale, boolean checkCache) {
    RemoteFile spec = null;
    File f = null;
    if (locale != null && simpleName != null && simpleName.lastIndexOf('.') != -1) {
        String filename = simpleName;
        String language = locale.toString().replaceAll("__", "_");
        String prefix = filename.substring(0, filename.lastIndexOf('.')) + "_";
        String suffix = filename.substring(filename.lastIndexOf('.'));
        filename = prefix + language + suffix;
        File test = getFileLocation(dir, filename, false);
        while (!test.exists()) {
            if (language.indexOf('_') == -1) {
                filename = simpleName;
                break;
            }
            language = language.substring(0, language.lastIndexOf('_'));
            filename = prefix + language + suffix;
            test = getFileLocation(dir, filename, false);
        }
        f = getFileLocation(dir, filename, false);
        spec = new RemoteFile(dir, filename, locale, 0L);
    } else {
        f = getFileLocation(dir, simpleName, false);
        spec = new RemoteFile(dir, simpleName, locale, 0L);
    }
    if (f.exists()) {
        spec.setLastModified(f.lastModified());
    }
    // 
    if (checkCache) {
        IRemoteFileService svc = BEANS.get(IRemoteFileService.class);
        spec = svc.getRemoteFile(spec);
        try {
            if (spec.getName() != null && !spec.getName().equalsIgnoreCase(f.getName())) {
                if (locale != null && f.getName().length() > spec.getName().length()) {
                    // if local file has longer name (including locale), this means that
                    // this file was deleted on the server
                    f.delete();
                }
                f = getFileLocation(spec.getDirectory(), spec.getName(), false);
            }
            if (spec.exists() && spec.hasContent()) {
                try (OutputStream out = new FileOutputStream(f)) {
                    spec.writeData(out);
                }
                f.setLastModified(spec.getLastModified());
            } else if (!spec.exists()) {
                f.delete();
            }
        } catch (IOException e) {
            throw new ProcessingException("error writing remote file in local store", e);
        }
    }
    return f;
}
Also used : OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File) RemoteFile(org.eclipse.scout.rt.shared.services.common.file.RemoteFile) RemoteFile(org.eclipse.scout.rt.shared.services.common.file.RemoteFile) IRemoteFileService(org.eclipse.scout.rt.shared.services.common.file.IRemoteFileService) ProcessingException(org.eclipse.scout.rt.platform.exception.ProcessingException)

Example 7 with RemoteFile

use of org.eclipse.scout.rt.shared.services.common.file.RemoteFile in project scout.rt by eclipse.

the class FileService method syncRemoteFilesInternal.

private void syncRemoteFilesInternal(String serverFolderPath, FilenameFilter filter, boolean useServerFolderStructureOnClient) {
    IRemoteFileService svc = BEANS.get(IRemoteFileService.class);
    String[][] realFiles = getFiles(serverFolderPath, filter, useServerFolderStructureOnClient);
    RemoteFile[] existingFileInfoOnClient = new RemoteFile[realFiles.length];
    for (int i = 0; i < realFiles.length; i++) {
        RemoteFile rf = new RemoteFile(realFiles[i][0], realFiles[i][1], 0);
        String dir = m_rootPath == null ? realFiles[i][0] : "";
        File f = getFileLocation(dir, realFiles[i][1], false);
        if (f.exists()) {
            rf.setLastModified(f.lastModified());
        }
        existingFileInfoOnClient[i] = rf;
    }
    existingFileInfoOnClient = svc.getRemoteFiles(serverFolderPath, filter, existingFileInfoOnClient);
    for (RemoteFile spec : existingFileInfoOnClient) {
        String fileDirectory = useServerFolderStructureOnClient ? spec.getDirectory() : null;
        File f = getFileLocation(fileDirectory, spec.getName(), false);
        if (spec.exists() && spec.hasContent()) {
            try {
                if (spec.hasMoreParts()) {
                    // file is splitted - get all parts
                    int counter = 0;
                    long fileDate = spec.getLastModified();
                    File part = getFileLocation(fileDirectory, spec.getName() + "." + counter, false);
                    try (OutputStream out = new FileOutputStream(part)) {
                        spec.writeData(out);
                    }
                    part.setLastModified(fileDate);
                    RemoteFile specPart = spec;
                    while (specPart.hasMoreParts()) {
                        counter++;
                        part = getFileLocation(fileDirectory, spec.getName() + "." + counter, false);
                        if (!part.exists() || fileDate != part.lastModified()) {
                            specPart = svc.getRemoteFilePart(spec, counter);
                            try (OutputStream out = new FileOutputStream(part)) {
                                specPart.writeData(out);
                            }
                            part.setLastModified(fileDate);
                        } else {
                        // resuming canceled part: nothing to do
                        }
                    }
                    // put together
                    counter = 0;
                    f = getFileLocation(fileDirectory, spec.getName(), false);
                    try (OutputStream out = new FileOutputStream(f)) {
                        part = getFileLocation(fileDirectory, spec.getName() + "." + counter, false);
                        while (part.exists()) {
                            try (InputStream in = new FileInputStream(part)) {
                                byte[] buf = new byte[102400];
                                int len;
                                while ((len = in.read(buf)) > 0) {
                                    out.write(buf, 0, len);
                                }
                                out.flush();
                            }
                            part.delete();
                            counter++;
                            part = getFileLocation(fileDirectory, spec.getName() + "." + counter, false);
                        }
                    }
                    f.setLastModified(fileDate);
                } else {
                    // normal files
                    try (OutputStream out = new FileOutputStream(f)) {
                        spec.writeData(out);
                    }
                    f.setLastModified(spec.getLastModified());
                }
            } catch (IOException e) {
                throw new ProcessingException("error writing remote file in local store", e);
            }
        } else if (!spec.exists()) {
            f.delete();
        }
    }
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) RemoteFile(org.eclipse.scout.rt.shared.services.common.file.RemoteFile) RemoteFile(org.eclipse.scout.rt.shared.services.common.file.RemoteFile) IRemoteFileService(org.eclipse.scout.rt.shared.services.common.file.IRemoteFileService) ProcessingException(org.eclipse.scout.rt.platform.exception.ProcessingException)

Example 8 with RemoteFile

use of org.eclipse.scout.rt.shared.services.common.file.RemoteFile in project scout.rt by eclipse.

the class RemoteFileServlet method writeResource.

private boolean writeResource(final HttpServletRequest req, final HttpServletResponse resp, final String resourcePath) throws IOException {
    IRemoteFileService rfs = BEANS.get(getConfiguredRemoteFileServiceClass());
    RemoteFile spec = new RemoteFile((resourcePath == null) ? null : StringUtility.join("", m_folder, resourcePath), -1);
    RemoteFile remoteFile = rfs.getRemoteFile(spec);
    if (!remoteFile.exists()) {
        return false;
    }
    HttpCacheObject obj = new HttpCacheObject(new HttpCacheKey(resourcePath), remoteFile.toBinaryResource());
    if (BEANS.get(HttpCacheControl.class).checkAndSetCacheHeaders(req, resp, obj)) {
        return true;
    }
    rfs.streamRemoteFile(remoteFile, resp.getOutputStream());
    return true;
}
Also used : HttpCacheControl(org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheControl) HttpCacheKey(org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheKey) RemoteFile(org.eclipse.scout.rt.shared.services.common.file.RemoteFile) HttpCacheObject(org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheObject) IRemoteFileService(org.eclipse.scout.rt.shared.services.common.file.IRemoteFileService)

Example 9 with RemoteFile

use of org.eclipse.scout.rt.shared.services.common.file.RemoteFile in project scout.rt by eclipse.

the class RemoteFileService method getFileInternal.

private File getFileInternal(RemoteFile spec) {
    File root = new File(getRootPath());
    File folder = null;
    if (spec.getDirectory() == null || spec.getDirectory().length() == 0) {
        folder = new File(getRootPath());
    } else {
        String tmp = spec.getDirectory();
        tmp = tmp.replaceAll("\\\\", "/");
        tmp = tmp.replaceAll("//", "/");
        folder = new File(getRootPath(), tmp);
    }
    String canonicalRoot;
    String canonicalFolder;
    String canonicalSimpleName;
    try {
        canonicalRoot = root.getCanonicalPath();
        canonicalFolder = folder.getCanonicalPath();
        canonicalSimpleName = new File(canonicalFolder, spec.getName()).getName();
    } catch (IOException e) {
        throw new ProcessingException("invalid or unaccessible path", e);
    }
    if (canonicalFolder == null || !canonicalFolder.startsWith(canonicalRoot)) {
        throw new SecurityException("invalid or unaccessible path");
    }
    // if the remote file is requested from the RemoteFileServlet, spec.getName() will start with an "/"
    if (canonicalSimpleName == null || !canonicalSimpleName.equals(spec.getName().startsWith("/") ? spec.getName().substring(1) : spec.getName())) {
        throw new SecurityException("invalid or unaccessible path");
    }
    // 
    String filename = canonicalSimpleName;
    if (spec.getLocale() != null && filename.lastIndexOf('.') != -1) {
        // check locale string for hacking patterns (only allow
        String localeText = spec.getLocale().toString().replaceAll("__", "_");
        if (!LOCALE_SECURITY_PATTERN.matcher(localeText).matches()) {
            throw new SecurityException("invalid or unaccessible path");
        }
        String[] checkedLocaleParts = localeText.split("_", 3);
        String prefix = filename.substring(0, filename.lastIndexOf('.'));
        String suffix = filename.substring(filename.lastIndexOf('.'));
        for (int i = checkedLocaleParts.length - 1; i >= 0; i--) {
            if (prefix.toLowerCase().endsWith(checkedLocaleParts[i].toLowerCase())) {
                prefix = prefix.substring(0, prefix.length() - checkedLocaleParts[i].length());
                if (prefix.endsWith("_")) {
                    prefix = prefix.substring(0, prefix.length() - 1);
                }
            }
        }
        if (!prefix.endsWith("_")) {
            prefix = prefix + "_";
        }
        filename = prefix + localeText + suffix;
        File test = new File(canonicalFolder, filename);
        while (!test.exists()) {
            if (localeText.indexOf('_') == -1) {
                filename = canonicalSimpleName;
                break;
            }
            localeText = localeText.substring(0, localeText.lastIndexOf('_'));
            filename = prefix + localeText + suffix;
            test = new File(canonicalFolder, filename);
        }
    }
    File file = new File(canonicalFolder, filename);
    return file;
}
Also used : IOException(java.io.IOException) File(java.io.File) RemoteFile(org.eclipse.scout.rt.shared.services.common.file.RemoteFile) ProcessingException(org.eclipse.scout.rt.platform.exception.ProcessingException)

Example 10 with RemoteFile

use of org.eclipse.scout.rt.shared.services.common.file.RemoteFile in project scout.rt by eclipse.

the class RemoteFileService method streamRemoteFile.

@Override
public void streamRemoteFile(RemoteFile spec, OutputStream out) {
    File file = getFileInternal(spec);
    if (!file.exists()) {
        throw new ProcessingException("remote file does not exist: " + spec.getPath());
    }
    int len = (int) file.length();
    byte[] buf = new byte[Math.min(102400, len)];
    int written = 0;
    int delta = 0;
    try (BufferedInputStream in = new BufferedInputStream(new FileInputStream(file))) {
        while (written < len) {
            delta = in.read(buf);
            out.write(buf, 0, delta);
            written += delta;
        }
    } catch (IOException e) {
        throw new ProcessingException("error streaming file: " + file.getAbsolutePath(), e);
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) IOException(java.io.IOException) File(java.io.File) RemoteFile(org.eclipse.scout.rt.shared.services.common.file.RemoteFile) FileInputStream(java.io.FileInputStream) ProcessingException(org.eclipse.scout.rt.platform.exception.ProcessingException)

Aggregations

RemoteFile (org.eclipse.scout.rt.shared.services.common.file.RemoteFile)14 IOException (java.io.IOException)9 ProcessingException (org.eclipse.scout.rt.platform.exception.ProcessingException)9 File (java.io.File)7 IRemoteFileService (org.eclipse.scout.rt.shared.services.common.file.IRemoteFileService)7 FileInputStream (java.io.FileInputStream)3 FileOutputStream (java.io.FileOutputStream)3 BufferedInputStream (java.io.BufferedInputStream)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 OutputStream (java.io.OutputStream)2 X509Certificate (java.security.cert.X509Certificate)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 FilenameFilter (java.io.FilenameFilter)1 GeneralSecurityException (java.security.GeneralSecurityException)1 KeyStoreException (java.security.KeyStoreException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 CertificateException (java.security.cert.CertificateException)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1