Example usage for org.apache.commons.vfs2 FileContent getContentInfo

List of usage examples for org.apache.commons.vfs2 FileContent getContentInfo

Introduction

In this page you can find the example usage for org.apache.commons.vfs2 FileContent getContentInfo.

Prototype

FileContentInfo getContentInfo() throws FileSystemException;

Source Link

Document

get the content info.

Usage

From source file:net.sourceforge.fullsync.ui.FileObjectChooser.java

private void populateList() throws FileSystemException {
    FileObject[] children = activeFileObject.getChildren();
    Arrays.sort(children, (o1, o2) -> {
        try {/* ww  w . ja  v  a 2  s .c  o  m*/
            if ((o1.getType() == FileType.FOLDER) && (o2.getType() == FileType.FILE)) {
                return -1;
            } else if ((o1.getType() == FileType.FILE) && (o2.getType() == FileType.FOLDER)) {
                return 1;
            }
            return o1.getName().getBaseName().compareTo(o2.getName().getBaseName());
        } catch (FileSystemException fse) {
            fse.printStackTrace();
            return 0;
        }
    });

    DateFormat df = DateFormat.getDateTimeInstance();

    for (int i = 0; i < children.length; i++) {
        FileObject data = children[i];

        TableItem item;
        if (tableItems.getItemCount() <= i) {
            item = new TableItem(tableItems, SWT.NULL);
        } else {
            item = tableItems.getItem(i);
        }

        item.setText(0, data.getName().getBaseName());
        String type = data.getType().getName(); //FIXME: translate type name {file,folder}

        if (data.getType().hasContent()) {
            FileContent content = data.getContent();
            String contentType = content.getContentInfo().getContentType();
            if (null != contentType) {
                type += " (" + contentType + ")"; //$NON-NLS-1$ //$NON-NLS-2$
            }
            item.setText(1, String.valueOf(content.getSize()));
            item.setText(3, df.format(new Date(content.getLastModifiedTime())));
        } else {
            item.setText(1, ""); //$NON-NLS-1$
            item.setText(3, ""); //$NON-NLS-1$
        }
        item.setText(2, type);

        if (data.getType() == FileType.FOLDER) {
            item.setImage(imageRepository.getImage("FS_Folder_Collapsed.gif")); //$NON-NLS-1$
        } else {
            item.setImage(imageRepository.getImage("FS_File_text_plain.gif")); //$NON-NLS-1$
        }

        item.setData(data);
    }
    tableItems.setItemCount(children.length);
}

From source file:dslab.crawler.pack.CrawlerPack.java

public String getFromRemote(String uri) {

    // clear cache
    fileSystem.getFilesCache().close();//  w ww. j a v a 2  s  . co  m

    String remoteContent;
    String remoteEncoding = "utf-8";

    log.debug("Loading remote URI:" + uri);
    FileContent fileContent;

    try {
        // set cookie if cookies set
        if (0 < this.cookies.size()) {
            FileSystemOptions fsOptions = new FileSystemOptions();
            HttpFileSystemConfigBuilder.getInstance().setCookies(fsOptions, getCookies(uri));
            fileContent = fileSystem.resolveFile(uri, fsOptions).getContent();
        } else
            fileContent = fileSystem.resolveFile(uri).getContent();

        // 2016-03-22 only pure http/https auto detect encoding
        if ("http".equalsIgnoreCase(uri.substring(0, 4))) {
            fileContent.getSize(); // pass a bug {@link https://issues.apache.org/jira/browse/VFS-427}
            remoteEncoding = fileContent.getContentInfo().getContentEncoding();
        }

        if (null == remoteEncoding)
            remoteEncoding = "utf-8";

        if (!"utf".equalsIgnoreCase(remoteEncoding.substring(0, 3))) {
            log.debug("remote content encoding: " + remoteEncoding);

            // force charset encoding if setRemoteEncoding set
            if (!"utf".equalsIgnoreCase(encoding.substring(0, 3))) {
                remoteEncoding = encoding;
            } else {
                // auto detecting encoding
                remoteEncoding = detectCharset(IOUtils.toByteArray(fileContent.getInputStream()));
                log.info("real encoding: " + remoteEncoding);
            }
        }

        // 2016-02-29 fixed
        remoteContent = IOUtils.toString(fileContent.getInputStream(), remoteEncoding);

    } catch (FileSystemException fse) {
        log.warn(fse.getMessage());
        remoteContent = null;
    } catch (IOException ioe) {
        // return empty
        log.warn(ioe.getMessage());
        remoteContent = null;
    } catch (StringIndexOutOfBoundsException stre) {
        log.warn("uri: " + uri);
        log.warn(stre.getMessage());
        remoteContent = null;
    }

    clearCookies();

    // any exception will return "null"
    return remoteContent;
}

From source file:com.github.abola.crawler.CrawlerPack.java

/**
 * ?? Apache Common VFS  ???// w ww  .j a v  a2s .  c  o m
 *
 * ??
 * @see <a href="https://commons.apache.org/proper/commons-vfs/filesystems.html">commons-vfs filesystems</a>
 */
public String getFromRemote(String uri) {

    // clear cache
    fileSystem.getFilesCache().close();

    String remoteContent;
    String remoteEncoding = "utf-8";

    log.debug("getFromRemote: Loading remote URI=" + uri);
    FileContent fileContent;

    try {

        FileSystemOptions fsOptions = new FileSystemOptions();
        // set userAgent
        HttpFileSystemConfigBuilder.getInstance().setUserAgent(fsOptions, userAgent);

        // set cookie if cookies set
        if (0 < this.cookies.size()) {
            HttpFileSystemConfigBuilder.getInstance().setCookies(fsOptions, getCookies(uri));
        }

        log.debug("getFromRemote: userAgent=" + userAgent);
        log.debug("getFromRemote: cookieSize=" + cookies.size());
        log.debug("getFromRemote: cookies=" + cookies.toString());

        fileContent = fileSystem.resolveFile(uri, fsOptions).getContent();

        // 2016-03-22 only pure http/https auto detect encoding
        if ("http".equalsIgnoreCase(uri.substring(0, 4))) {
            fileContent.getSize(); // pass a bug {@link https://issues.apache.org/jira/browse/VFS-427}
            remoteEncoding = fileContent.getContentInfo().getContentEncoding();
        }

        log.debug("getFromRemote: remoteEncoding=" + remoteEncoding + "(auto detect) ");

        // 2016-03-21 zip file getContentEncoding null
        if (null == remoteEncoding)
            remoteEncoding = "utf-8";

        if (!"utf".equalsIgnoreCase(remoteEncoding.substring(0, 3))) {
            log.debug("getFromRemote: remote content encoding=" + remoteEncoding);

            // force charset encoding if setRemoteEncoding set
            if (!"utf".equalsIgnoreCase(encoding.substring(0, 3))) {
                remoteEncoding = encoding;
            } else {
                // auto detecting encoding
                remoteEncoding = detectCharset(IOUtils.toByteArray(fileContent.getInputStream()));
                log.debug("getFromRemote: real encoding=" + remoteEncoding);
            }
        }

        // ??  Apache VFS ??
        // 2016-02-29 fixed
        remoteContent = IOUtils.toString(fileContent.getInputStream(), remoteEncoding);

    } catch (FileSystemException fse) {
        log.warn("getFromRemote: FileSystemException=" + fse.getMessage());
        remoteContent = null;
    } catch (IOException ioe) {
        // return empty
        log.warn("getFromRemote: IOException=" + ioe.getMessage());
        remoteContent = null;
    } catch (StringIndexOutOfBoundsException stre) {
        log.warn("getFromRemote: StringIndexOutOfBoundsException=" + stre.getMessage());
        log.warn("getFromRemote: uri=" + uri);
        log.warn(stre.getMessage());
        remoteContent = null;
    }

    clearCookies();

    log.debug("getFromRemote: remoteContent=\n" + remoteContent);
    // any exception will return "null"
    return remoteContent;
}

From source file:org.jahia.modules.external.vfs.VFSDataSource.java

private ExternalData getFile(FileObject fileObject) throws FileSystemException {
    String type = getDataType(fileObject);

    Map<String, String[]> properties = new HashMap<String, String[]>();
    List<String> addedMixins = new ArrayList<>();
    final FileContent content = fileObject.getContent();
    if (content != null) {
        long lastModifiedTime = fileObject.getContent().getLastModifiedTime();
        if (lastModifiedTime > 0) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(lastModifiedTime);
            String[] timestamp = new String[] { ISO8601.format(calendar) };
            properties.put(Constants.JCR_CREATED, timestamp);
            properties.put(Constants.JCR_LASTMODIFIED, timestamp);
        }//from   w ww.jav a  2  s.  c  o  m
        // Add jmix:image mixin in case of the file is a picture.
        if (content.getContentInfo() != null && content.getContentInfo().getContentType() != null
                && fileObject.getContent().getContentInfo().getContentType().matches("image/(.*)")) {
            addedMixins.add(Constants.JAHIAMIX_IMAGE);
        }

    }

    String path = fileObject.getName().getPath().substring(rootPath.length());
    if (!path.startsWith("/")) {
        path = "/" + path;
    }

    ExternalData result = new ExternalData(path, path, type, properties);
    result.setMixin(addedMixins);
    return result;
}

From source file:org.jahia.modules.external.vfs.VFSDataSource.java

protected String getContentType(FileContent content) throws FileSystemException {
    String s1 = content.getContentInfo().getContentType();
    if (s1 == null) {
        s1 = JCRContentUtils.getMimeType(content.getFile().getName().getBaseName());
    }//from w  ww .j a  va 2  s  .c  o m
    if (s1 == null) {
        s1 = "application/octet-stream";
    }
    return s1;
}

From source file:pl.otros.vfs.browser.util.VFSUtils.java

private static FileObject[] extractHttpFileObjectChildren(FileObject fileObject) throws FileSystemException {
    FileObject[] result;/*w w w  . j ava2 s.  c om*/
    HttpFileObject fo = (HttpFileObject) fileObject;
    FileContent content = fo.getContent();
    String contentType = content.getContentInfo().getContentType();
    result = new FileObject[] { fileObject };
    if (contentType.equalsIgnoreCase("text/html")) {
        try {
            String html = IOUtils.toString(content.getInputStream());
            if (html.toLowerCase().contains("index of")) {
                LOGGER.info("Page contains \"index of\", resolving children");
                //a href="DSC_0410.JPG">
                Pattern p = Pattern.compile("<A .*?href=\"(.*?)\"", Pattern.CASE_INSENSITIVE);
                Matcher matcher = p.matcher(html);
                ArrayList<FileObject> list = new ArrayList<FileObject>();
                while (matcher.find()) {
                    String link = matcher.group(1);
                    LOGGER.info("Getting children from link {}", link);
                    if (StringUtils.isBlank(link)) {
                        LOGGER.debug("URL link is blank");
                        continue;
                    }
                    FileObject child;
                    URL url = fileObject.getURL();
                    child = extractHttpFileObject(link, url);
                    list.add(child);
                }
                result = list.toArray(result);
            }
            //TODO extract links
        } catch (Exception e) {
            throw new FileSystemException(e);
        }
    }
    return result;
}