Subversion Repositories bacoAlunos

Rev

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

package pt.estgp.estgweb.web;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import org.apache.log4j.Logger;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.httpclient.util.URIUtil;

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

import pt.estgp.estgweb.utils.ConfigProperties;
import jomm.web.ftp.IFile;
import jomm.web.ftp.impl.FtpFile;

/**
 * @author Jorge Machado
 * @date 26/Fev/2008
 * @time 18:01:54
 * @see pt.estgp.estgweb.web
 */

public class LayoutController extends 1.5.0/docs/api/javax/swing/Action.html">Action
{

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

    public ActionForward execute(ActionMapping mapping,
                                 ActionForm form ,
                                 HttpServletRequest request,
                                 HttpServletResponse response)
            throws 1.5.0/docs/api/java/io/IOException.html">IOException, ServletException
    {

        1.5.0/docs/api/java/lang/String.html">String layout = request.getParameter("layout");
        1.5.0/docs/api/java/lang/String.html">String path = request.getParameter("path");
        1.5.0/docs/api/java/lang/String.html">String serverParameter = request.getParameter("server");
        1.5.0/docs/api/java/lang/String.html">String server = ConfigProperties.getProperty("server." + serverParameter);


        if(serverParameter== null || server == null || server.length()==0 || path == null || layout == null)
            throw new 1.5.0/docs/api/java/io/FileNotFoundException.html">FileNotFoundException("url not found for server:" + server + " and path:" + path);

        logger.info("requested path:" + path);
        logger.info("requested server:" + server);

        request.setAttribute("path",path);
        request.setAttribute("server",server);
        if(path != null)
        {
            try
            {
                1.5.0/docs/api/java/net/URL.html">URL url = new 1.5.0/docs/api/java/net/URL.html">URL(server + path);
                1.5.0/docs/api/java/net/URLConnection.html">URLConnection con = url.openConnection();
                con.connect();
                logger.info("contentPath:" + con.getContentType());
//                if(con.getContentType() != null && con.getContentType().toLowerCase().indexOf("html") >= 0)
                if(server.startsWith("ftp://"))
                {
                    processRequestFtp(request, path,serverParameter,server);
                    return mapping.findForward(layout + ".ftp");
                }
                else if(path.endsWith("html") || path.endsWith("htm") || path.endsWith("shtml") || path.endsWith("asp") || path.endsWith("jsp") || path.endsWith("aspx") || path.endsWith("/"))
                {
                    return mapping.findForward(layout);
                }
                else
                {
                    ActionForward forward = new ActionForward();
                    request.setAttribute("useOutputStream","true");
                    forward.setPath("/WebProxy");
                    return forward;
                }
            }
            catch(1.5.0/docs/api/java/io/FileNotFoundException.html">FileNotFoundException e)
            {
                throw e;
            }
            catch(1.5.0/docs/api/java/lang/Throwable.html">Throwable e)
            {
                logger.error(e,e);
                throw new 1.5.0/docs/api/java/io/IOException.html">IOException(e.toString());
            }
        }
        else
        {
            throw new 1.5.0/docs/api/java/io/FileNotFoundException.html">FileNotFoundException("url not found for server:" + server + " and path:" + path);
        }
    }


    protected void processRequestFtp(HttpServletRequest request, 1.5.0/docs/api/java/lang/String.html">String path,1.5.0/docs/api/java/lang/String.html">String serverStr,1.5.0/docs/api/java/lang/String.html">String server)
            throws ServletException, 1.5.0/docs/api/java/io/IOException.html">IOException
    {
        try
        {
            1.5.0/docs/api/java/lang/String.html">String user = ConfigProperties.getProperty(serverStr + ".user");
            1.5.0/docs/api/java/lang/String.html">String pass = ConfigProperties.getProperty(serverStr + ".pass");
            if(path != null)
            {
                FtpServer ftpServer = getServer(server,user,pass);
                FTPClient client = ftpServer.getClient();
                client.changeWorkingDirectory(URIUtil.encodePath(path,"ISO-8859-1"));
                FTPFile[] files =  client.listFiles();
                List<IFile> iFiles = new ArrayList<IFile>();
                for(FTPFile ftpFile : files)
                {
                    IFile iFile = new FtpFile(ftpFile,server + path + "/" + ftpFile.getName(),path);
                    iFiles.add(iFile);
                }
                request.setAttribute("files",iFiles);
                client.disconnect();
                client.quit();
            }
            else
            {
                throw new 1.5.0/docs/api/java/io/FileNotFoundException.html">FileNotFoundException("url not found for server:" + server + " and path:" + path);
            }
        }
        catch(1.5.0/docs/api/java/lang/Throwable.html">Throwable e)
        {
            logger.error(e,e);
        }
    }

    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 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;
    }

    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;
        }
    }


}