⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fileobjectdavresource.java

📁 这是linux下ssl vpn的实现程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package com.sslexplorer.vfs.webdav;

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.vfs.FileObject;
import org.apache.commons.vfs.FileType;

import com.sslexplorer.vfs.NetworkPlace;
import com.sslexplorer.vfs.webdav.methods.GET;

/**
 * <p>
 * This class implements {@link com.sslexplorer.vfs.webdav.DAVResource} and and
 * provides axcess to the file system.
 * 
 * @author James D Robinson
 * 
 * @mail <a href="mailto:james@3sp.com">&lt;james@3sp.com&gt;</a>
 */
public class FileObjectDAVResource implements DAVResource {
    final static Log log = LogFactory.getLog(DAVResource.class);

    private DAVMount mount = null;

    private FileObject file = null;

    protected DAVTransaction transaction = null;

    protected String relativePath;

    protected DAVResource parent;

    /**
     * @param mount The Mount acociated with this resource.
     * @param file The file accociated with this resource.
     * @param transaction the transaction the resource was created under
     * @param parent parent
     * @param relativePath path relative to root of mount
     * @throws IOException on any error
     */
    public FileObjectDAVResource(DAVMount mount, FileObject file, DAVTransaction transaction, FileObjectDAVResource parent,
                                 String relativePath) throws IOException {
        if (mount == null)
            throw new NullPointerException("Null mount");
        this.mount = mount;
        this.file = file;
        this.transaction = transaction;
        this.parent = parent;
        this.relativePath = relativePath;

        // get the parent now if possible
        if (this.parent == null) {
            if (this.relativePath != null) {
                String parentPath = DAVUtilities.getParentPath(relativePath);
                if (parentPath == null) {
                } else {
                    this.parent = mount.getResource(parentPath, transaction);
                }
            }
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Object#hashCode()
     */
    public int hashCode() {
        try {
            return getFile().hashCode();
        } catch (IOException e) {
            return hashCode();
        }
    }

    public boolean isMount() {
        return false;
    }

    public String getWebFolderPath() {
        return "/fs/" + mount.getMountString() + "/" + relativePath;
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Object#equals(java.lang.Object)
     */
    public boolean equals(Object object) {
        if (object == null)
            return (false);
        if (object instanceof FileObjectDAVResource) {
            FileObjectDAVResource resource = (FileObjectDAVResource) object;
            try {
                boolean u = getFile().equals(resource.getFile());
                boolean r = this.getMount() == resource.mount;
                return (u && r);
            } catch (IOException ioe) {
                return false;
            }
        } else {
            return (false);
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Comparable#compareTo(Object)
     */
    public int compareTo(Object object) {
        FileObjectDAVResource resource = (FileObjectDAVResource) object;
        try {
            return (getFile().getURL().toExternalForm().compareTo(resource.getFile().getURL().toExternalForm()));
        } catch (IOException ioe) {
            log.warn("Failed to compare two files.", ioe);
            return -999;
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.sslexplorer.vfs.webdav.DAVResource#verifyAccess()
     */
    public void verifyAccess() throws Exception, DAVAuthenticationRequiredException {
        this.getFile().exists();
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.sslexplorer.vfs.webdav.DAVResource#isNull()
     */
    public boolean isNull() {
        try {
            return !getFile().exists();
        } catch (IOException ioe) {
            if (log.isDebugEnabled()) {
                log.warn("Failed to test if resource exists.", ioe);
            } else {
                log.warn("Failed to test if resource exists : " + ioe.getMessage());
            }
            return true;
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.sslexplorer.vfs.webdav.DAVResource#isCollection()
     */
    public boolean isCollection() {
        if (this.isNull())
            return false;
        try {
            FileObject temp = getFile();
            FileType type = temp.getType();
            if (type == null) {
                type = temp.getName().getType();
            }
            return (type.equals(FileType.FOLDER));
        } catch (IOException e) {
            if (log.isDebugEnabled()) {
                log.warn("Failed to test if resource is a collection.", e);
            } else {
                log.warn("Failed to test if resource is a collection : " + e.getMessage());
            }
            return false;
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.sslexplorer.vfs.webdav.DAVResource#isResource()
     */
    public boolean isResource() {
        if (this.isNull()) {
            return false;
        } else {
            return (!this.isCollection());
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.sslexplorer.vfs.webdav.DAVResource#getFile()
     */
    public FileObject getFile() throws IOException {
        if (file == null) {
            file = ((AbstractNetworkPlaceMount) mount).createAuthenticatedVFSFileObject(getRelativePath(), transaction);
            if (file == null) {
                throw new IOException("Could not create file object.");
            }
        }
        return file;
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.sslexplorer.vfs.webdav.DAVResource#getMount()
     */
    public DAVMount getMount() {
        return this.mount;
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.sslexplorer.vfs.webdav.DAVResource#getDisplayName()
     */
    public String getDisplayName() {
        try {
            String name = getFile().getName().getBaseName();
            if (this.isCollection())
                return (name + "/");
            return name;
        } catch (IOException ioe) {
            return getBasename();
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.sslexplorer.vfs.webdav.DAVResource#getBasename()
     */
    public String getBasename() {
        String p = ((AbstractNetworkPlaceMount) this.getMount()).getNetworkPlace().getUri();
        int idx = p.length() < 2 ? -1 : (p.lastIndexOf('/', p.endsWith("/") ? p.length() - 2 : p.length() - 1));
        if (idx != 1) {
            p = p.substring(idx + 1);
        }
        return p;
    }

    /**
     * <p>
     * Return the path of this {@link DAVResource} relative to the root of the
     * associated {@link DAVRepository}.
     * </p>
     * 
     * @return a <b>non null</b> {@link String}.
     */
    public String getRelativePath() {
        return relativePath;
    }

    /**
     * <p>
     * Return the {@link URI} of this {@link DAVResource} relative to the root
     * of the associated {@link DAVRepository}.
     * </p>
     * 
     * @return a <b>non-null</b> {@link URI} instance.
     */
    public URI getRelativeURI() {
        try {
            return new URI(DAVUtilities.encodePath(getRelativePath()));
        } catch (Exception e) {
            log.warn("Failed to get the relativeURI for the resource", e);
            e.printStackTrace();
            return null;
        }
    }

    /**
     * <p>
     * Return the parent {@link DAVResource} of this instance.
     * </p>
     * 
     * @return a <b>non-null</b> {@link DAVResource} instance or <b>null</b>
     *         if this {@link DAVResource} is the repository root.
     */
    public DAVResource getParent() {
        try {
            if (parent == null) {
                String parentPath = DAVUtilities.stripLeadingSlash(DAVUtilities.getParentPath(getFullPath()));
                if (parentPath == null || parentPath.equals("/")) {
                    return null;
                } else {
                    return DAVRepository.getInstance().getResource(parentPath, transaction);
                }
            }
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return parent;
    }

    /**
     * <p>
     * Return an {@link Iterator} over all children of this instance.
     * </p>
     * 
     * @return a <b>non-null</b> {@link Iterator} instance or <b>null</b> if
     *         this {@link DAVResource} is not a collection.
     * @throws IOException
     */
    public Iterator getChildren() throws IOException {
        if (!this.isCollection())
            return null;

        // this forces VFS to re cashe.
        this.getFile().close();

        FileObject children[] = this.getFile().getChildren();
        List resources = new ArrayList(children.length);

        NetworkPlace np = ((AbstractNetworkPlaceMount) getMount()).getNetworkPlace();

        for (int x = 0; x < children.length; x++) {
            String fileName = children[x].getName().getBaseName();
            if (fileName.startsWith(PREFIX) && fileName.endsWith(SUFFIX))
                continue;
            if (!np.isShowHidden() && children[x].getName().getBaseName().startsWith("."))
                continue;
            if (!isCollection() && !isResource())
                continue;
            try {
                /*
                 * TODO BPS - I think we have a problem here.
                 * 
                 * When getting children that require further authentication we
                 * get a {@link DAVAuthenticationRequiredException} exception.
                 * We do not want to throw an exception at this point but we do
                 * want add the child. Its only when children of the child are
                 * accessed that we want to throw the exception. Because
                 * DAVMount.getResource() is the one that throws this, a
                 * resource object can never be created.
                 * 
                 * This will happen for example when listomg /fs/[store] and a
                 * network place that requires auth. is hit.
                 */

                DAVResource r = getMount().getResource(DAVUtilities.concatenatePaths(relativePath, fileName), transaction);
                if (!np.isAllowResursive() && r.isCollection())
                    continue;
                resources.add(r);
            } catch (Exception e) {
                /*
                 * NOTE - BPS - We cannot log this exception as it may have user
                 * information in the URI.
                 */
                // log.warn("Failed to get resource.", e);
            }
        }
        return resources.iterator();
    }

    /**
     * Create
     */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -