Subversion Repositories bacoAlunos

Rev

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

package pt.estgp.estgweb.web;

import jomm.web.ftp.IFile;
import jomm.web.ftp.impl.FtpFile;
import jomm.web.utils.NavPlace;
import org.apache.commons.httpclient.util.URIUtil;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.log4j.Logger;
import pt.estgp.estgweb.web.exceptions.NotAuthorizedException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author Jorge Machado
 * @date 26/Fev/2008
 * @time 12:46:16
 * @see pt.estgp.estgweb.web
 */

public class FTPProxy extends HttpServlet {

    /** sss requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     */

    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(FTPProxy.class);

    private static HashMap<String,FtpServer> servers = new HashMap<String,FtpServer>();

    /**
     * Add a ftpServer to Cache
     * @param urlStr to parse
     * @param user username
     * @param pass password
     * @return ftpServer
     */

    private static synchronized FtpServer addServer(1.5.0/docs/api/java/lang/String.html">String urlStr, 1.5.0/docs/api/java/lang/String.html">String user, 1.5.0/docs/api/java/lang/String.html">String pass)
    {
        try
        {
            1.5.0/docs/api/java/net/URL.html">URL url = new 1.5.0/docs/api/java/net/URL.html">URL(urlStr);
            1.5.0/docs/api/java/lang/String.html">String host = url.getHost();
            int port = url.getPort();
            if(port <= 0)
                port = 21;
            logger.info("Connecting to " + host + "in port:" + port);
            FtpServer ftpServer = new FTPProxy.FtpServer(host,port,user,pass);
            servers.put(urlStr,ftpServer);
            return ftpServer;
        }
        catch (1.5.0/docs/api/java/net/MalformedURLException.html">MalformedURLException e)
        {
            logger.error(e,e);
        }
        catch (1.5.0/docs/api/java/io/IOException.html">IOException e)
        {
            logger.error(e,e);
        }
        return null;
    }

    private static FtpServer getServer(1.5.0/docs/api/java/lang/String.html">String urlStr, 1.5.0/docs/api/java/lang/String.html">String user, 1.5.0/docs/api/java/lang/String.html">String pass)
    {
        FtpServer ftpServer = servers.get(urlStr);
        if(ftpServer == null)
            ftpServer = addServer(urlStr,user,pass);
        return ftpServer;
    }

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, 1.5.0/docs/api/java/io/IOException.html">IOException
    {
        1.5.0/docs/api/java/lang/String.html">String path = (1.5.0/docs/api/java/lang/String.html">String) request.getAttribute("path");
        1.5.0/docs/api/java/lang/String.html">String server = (1.5.0/docs/api/java/lang/String.html">String) request.getAttribute("server");
        1.5.0/docs/api/java/lang/String.html">String user = (1.5.0/docs/api/java/lang/String.html">String) request.getAttribute("user");
        1.5.0/docs/api/java/lang/String.html">String pass = (1.5.0/docs/api/java/lang/String.html">String) request.getAttribute("pass");
        logger.info("server:" + server);
        logger.info("path:" + path);
        if(path != null)
        {
            FtpServer ftpServer = getServer(server,user,pass);
            FTPClient client = ftpServer.getClient();
            if(!client.changeWorkingDirectory(URIUtil.encodePath(path,"ISO-8859-1")))
            {
                throw new NotAuthorizedException();
            }
            FTPFile[] files =  client.listFiles();
            List<IFile> iFiles = new ArrayList<IFile>();
            for(FTPFile ftpFile : files)
            {
                IFile iFile;
                if(path.endsWith("/"))
                    iFile = new FtpFile(ftpFile,server + path + ftpFile.getName(),path);
                else
                    iFile = new FtpFile(ftpFile,server + path + "/" + ftpFile.getName(),path);

                iFiles.add(iFile);
            }
            List<NavPlace> navPlaces = FtpFile.getNavPlaces(path,"");
            request.setAttribute("NavPlaces",navPlaces);
            request.setAttribute("files",iFiles);
            getServletContext().getRequestDispatcher("/user/ftpclient/ftpclient.jsp").forward(request,response);
            client.quit();
        }
        else
        {
            throw new 1.5.0/docs/api/java/io/FileNotFoundException.html">FileNotFoundException("url not found for server:" + server + " and path:" + path);
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, 1.5.0/docs/api/java/io/IOException.html">IOException {
        processRequest(request, response);
    }

    /** Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, 1.5.0/docs/api/java/io/IOException.html">IOException {
        processRequest(request, response);
    }

    /** Returns a short description of the servlet.
     */

    public 1.5.0/docs/api/java/lang/String.html">String getServletInfo() {
        return "Proxy Servlet to get pages form static web page servers";
    }
    // </editor-fold>

    public static class FtpServer
    {
        1.5.0/docs/api/java/lang/String.html">String host = null;
        int port = 21;
        1.5.0/docs/api/java/lang/String.html">String username = null;
        1.5.0/docs/api/java/lang/String.html">String password = null;


        public FtpServer(1.5.0/docs/api/java/lang/String.html">String host, int port)
        {
            this.host = host;
            if(port > 0)
                this.port = port;
        }

        public FtpServer(1.5.0/docs/api/java/lang/String.html">String host, int port, 1.5.0/docs/api/java/lang/String.html">String username, 1.5.0/docs/api/java/lang/String.html">String password)
        {
            this.host = host;
            this.port = port;
            this.username = username;
            this.password = password;
        }

        public FTPClient getClient() throws 1.5.0/docs/api/java/io/IOException.html">IOException
        {
            FTPClient ftp = new FTPClient();
            ftp.connect(host,port);
            if(username != null)
                ftp.user(username);
            if(password != null)
                ftp.pass(password);
            return ftp;
        }

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

        public void setHost(1.5.0/docs/api/java/lang/String.html">String host) {
            this.host = host;
        }

        public int getPort() {
            return port;
        }

        public void setPort(int port) {
            this.port = port;
        }

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

        public void setUsername(1.5.0/docs/api/java/lang/String.html">String username) {
            this.username = username;
        }

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

        public void setPassword(1.5.0/docs/api/java/lang/String.html">String password) {
            this.password = password;
        }
    }
}