Subversion Repositories bacoAlunos

Rev

Rev 1310 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package jomm.web.ftp.impl;

import jomm.web.ftp.FileType;
import jomm.web.ftp.IFile;
import jomm.web.utils.NavPlace;
import org.apache.commons.httpclient.URIException;
import org.apache.commons.httpclient.util.URIUtil;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.log4j.Logger;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author Jorge Machado
 * @date 6/Fev/2008
 * @time 17:42:59
 * @see jomm.web.ftp.impl
 */

public class FtpFile implements IFile
{

    private static final 1.5.0/docs/api/java/util/logging/Logger.html">Logger logger = 1.5.0/docs/api/java/util/logging/Logger.html">Logger.getLogger(FtpFile.class);

    private long size;
    private 5+0%2Fdocs%2Fapi+Date">Date lastUpdate;
    private 1.5.0/docs/api/java/lang/String.html">String name;
    private 1.5.0/docs/api/java/lang/String.html">String path;
    private boolean directory;
    private 1.5.0/docs/api/java/lang/String.html">String completeUrl;
    private 1.5.0/docs/api/java/lang/String.html">String extension = "";
    FileType fileType;


    public FtpFile(FTPFile ftpFile, 1.5.0/docs/api/java/lang/String.html">String completeUrl, 1.5.0/docs/api/java/lang/String.html">String path)
    {
        this.path = path;
        this.completeUrl = completeUrl;
        this.lastUpdate = ftpFile.getTimestamp().getTime();
        this.name = ftpFile.getName();
        this.directory = ftpFile.isDirectory();
        this.size = ftpFile.getSize() / 1024;
        if(name.lastIndexOf('.')>=0)
            this.extension = name.substring(name.lastIndexOf('.'));
        if (ftpFile.getType() == FTPFile.DIRECTORY_TYPE)
            fileType = FileType.DIRECTORY;
        else if (ftpFile.getType() == FTPFile.FILE_TYPE)
            fileType = FileType.FILE;
        else if (ftpFile.getType() == FTPFile.UNKNOWN_TYPE)
            fileType = FileType.5+0%2Fdocs%2Fapi+UNKNOWN">UNKNOWN;
        else if (ftpFile.getType() == FTPFile.SYMBOLIC_LINK_TYPE)
            fileType = FileType.LINK;
    }

    public 5+0%2Fdocs%2Fapi+InputStream">InputStream getInputStream() throws 1.5.0/docs/api/java/io/IOException.html">IOException
    {
        try
        {
            1.5.0/docs/api/java/net/URL.html">URL url = new 1.5.0/docs/api/java/net/URL.html">URL(completeUrl);
            return url.openStream();
        }
        catch (1.5.0/docs/api/java/net/MalformedURLException.html">MalformedURLException e)
        {
            logger.error(e, e);
            return null;
        }

    }

    public 5+0%2Fdocs%2Fapi+Date">Date getLastUpdate()
    {
        return lastUpdate;
    }

    public 1.5.0/docs/api/java/lang/String.html">String getName()
    {
        return name;
    }


    public 1.5.0/docs/api/java/lang/String.html">String getNameEncoded() throws 1.5.0/docs/api/javax/print/URIException.html">URIException
    {
        return URIUtil.encodePath(name,"ISO-8859-1");
    }

    public 1.5.0/docs/api/java/lang/String.html">String getOriginalPathEncoded() throws 1.5.0/docs/api/javax/print/URIException.html">URIException
    {
        return URIUtil.encodePath(path,"ISO-8859-1");
    }


    public 1.5.0/docs/api/java/lang/String.html">String getNameEscapedHtml()
    {
        try {
            return jomm.utils.TextToHTMLEnconder.escapeHTML(new 1.5.0/docs/api/java/lang/String.html">String(name.getBytes("ISO-8859-1")));
        } catch (1.5.0/docs/api/java/io/UnsupportedEncodingException.html">UnsupportedEncodingException e) {
            logger.error(e,e);
            return name;
        }
    }
    public 1.5.0/docs/api/java/lang/String.html">String getNameHtml()
    {
        try {
            return new 1.5.0/docs/api/java/lang/String.html">String(name.getBytes("ISO-8859-1"));
        } catch (1.5.0/docs/api/java/io/UnsupportedEncodingException.html">UnsupportedEncodingException e) {
            logger.error(e,e);
            return name;
        }
    }

    public 1.5.0/docs/api/java/lang/String.html">String getExtension()
    {
        return extension;
    }
    public 1.5.0/docs/api/java/lang/String.html">String getName(HttpServletRequest request) throws 1.5.0/docs/api/javax/print/URIException.html">URIException
    {
        1.5.0/docs/api/java/lang/String.html">String queryAux = NavPlace.getAuxiliaryQueryString(request);
        if(queryAux != null)
            queryAux = "?" + queryAux;
        else queryAux = "";
        return URIUtil.encodePath(name,"ISO-8859-1") + "/" + queryAux;
        /*try {
            return URLEncoder.encode(name, "UTF-8") + "/" + queryAux;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }*/

        //return null;
    }

    public long getSize()
    {
        return size;
    }

    public 1.5.0/docs/api/java/lang/String.html">String getOriginalPath()
    {
        return path;
    }

    public boolean isDirectory()
    {
        return directory;
    }

    public 1.5.0/docs/api/java/lang/String.html">String getCompleteUrl()
    {
        return completeUrl;
    }


    public FileType getType()
    {
        return fileType;
    }

    public static List<NavPlace> getNavPlaces(1.5.0/docs/api/java/lang/String.html">String path, 1.5.0/docs/api/java/lang/String.html">String startPath)
    {
        List<NavPlace> navPlaces = new ArrayList<NavPlace>();
        1.5.0/docs/api/java/lang/String.html">String visiblePath = path.substring(startPath.length());
        1.5.0/docs/api/java/lang/String.html">String[] paths = visiblePath.split("/");
        1.5.0/docs/api/java/lang/String.html">String url = "";

        for (int i = paths.length - 1; i >= 0; i--)
        {
            if(paths[i].trim().length() > 0)
            {
                NavPlace n;
                if(navPlaces.size() == 0)
                {
                    n = new NavPlace(null,paths[i]);
                }
                else
                {
                    n = new NavPlace(url,paths[i]);
                }
                url += "../";
                navPlaces.add(0, n);
            }
        }
        return navPlaces;
    }

    public static List<IFile> init(FTPFile[] files, 1.5.0/docs/api/java/lang/String.html">String server, 1.5.0/docs/api/java/lang/String.html">String path) throws 1.5.0/docs/api/javax/print/URIException.html">URIException
    {
        List<IFile> iFiles = new ArrayList<IFile>();
        for (FTPFile ftpFile : files)
        {
            IFile iFile;
            if (path.endsWith("/"))
                iFile = new FtpFile(ftpFile, server + URIUtil.encodePath(path +ftpFile.getName(),"ISO-8859-1"), path);
            else
                iFile = new FtpFile(ftpFile, server + URIUtil.encodePath(path + "/" + ftpFile.getName(),"ISO-8859-1"), path);
            iFiles.add(iFile);
        }
        return iFiles;
    }

    public 1.5.0/docs/api/java/lang/String.html">String getServerRelativePathFile() throws 1.5.0/docs/api/javax/print/URIException.html">URIException {
        1.5.0/docs/api/java/lang/String.html">String path = getOriginalPathEncoded();
        if(!path.startsWith("/"))
            path = "/" + path;

        if(!path.endsWith("/"))
            path = path + "/";

        1.5.0/docs/api/java/lang/String.html">String name = getNameEncoded();

        return path + name;
    }
}