Subversion Repositories bacoAlunos

Rev

Rev 1312 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 fvelez 1
package pt.estgp.estgweb.web;
2
 
165 jmachado 3
import jomm.web.ftp.IFile;
4
import jomm.web.ftp.impl.FtpFile;
5
import jomm.web.utils.NavPlace;
6
import org.apache.commons.httpclient.util.URIUtil;
7
import org.apache.commons.net.ftp.FTPClient;
8
import org.apache.commons.net.ftp.FTPFile;
9
import org.apache.log4j.Logger;
10
import org.apache.struts.action.ActionForm;
1 fvelez 11
import org.apache.struts.action.ActionForward;
12
import org.apache.struts.action.ActionMapping;
698 jmachado 13
import pt.estgp.estgweb.domain.UserSession;
165 jmachado 14
import pt.estgp.estgweb.utils.ConfigProperties;
1314 jmachado 15
import pt.estgp.estgweb.web.controllers.ApplicationController;
696 jmachado 16
import pt.estgp.estgweb.web.exceptions.NotAuthorizedException;
1 fvelez 17
 
165 jmachado 18
import javax.servlet.ServletException;
1 fvelez 19
import javax.servlet.http.HttpServletRequest;
20
import javax.servlet.http.HttpServletResponse;
21
import java.io.FileNotFoundException;
165 jmachado 22
import java.io.IOException;
1 fvelez 23
import java.util.ArrayList;
165 jmachado 24
import java.util.List;
1 fvelez 25
 
26
/**
27
 * @author Jorge Machado
28
 * @date 26/Fev/2008
29
 * @time 18:01:54
30
 * @see pt.estgp.estgweb.web
31
 */
1314 jmachado 32
public class LayoutController extends ApplicationController
1 fvelez 33
{
34
 
406 jmachado 35
    public static final 1.5.0/docs/api/java/lang/String.html">String FTP_ACTUAL_PATH_KEY = "pt.estgp.estgweb.web.LayoutController.FTP_ACTUAL_PATH_KEY";
36
 
1 fvelez 37
    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);
38
 
39
    public ActionForward execute(ActionMapping mapping,
144 jmachado 40
                                 ActionForm form,
1 fvelez 41
                                 HttpServletRequest request,
42
                                 HttpServletResponse response)
43
            throws 1.5.0/docs/api/java/io/IOException.html">IOException, ServletException
44
    {
45
 
46
        1.5.0/docs/api/java/lang/String.html">String layout = request.getParameter("layout");
47
        1.5.0/docs/api/java/lang/String.html">String path = request.getParameter("path");
48
        1.5.0/docs/api/java/lang/String.html">String serverParameter = request.getParameter("server");
49
        1.5.0/docs/api/java/lang/String.html">String server = ConfigProperties.getProperty("server." + serverParameter);
50
 
51
 
489 jmachado 52
 
144 jmachado 53
        if (serverParameter == null || server == null || server.length() == 0 || path == null || layout == null)
1 fvelez 54
            throw new 1.5.0/docs/api/java/io/FileNotFoundException.html">FileNotFoundException("url not found for server:" + server + " and path:" + path);
55
 
56
        logger.info("requested path:" + path);
57
        logger.info("requested server:" + server);
223 jmachado 58
        1.5.0/docs/api/java/lang/String.html">String startPath = (1.5.0/docs/api/java/lang/String.html">String) request.getAttribute("startPath");
226 jmachado 59
        1.5.0/docs/api/java/lang/String.html">String confStartPath = ConfigProperties.getProperty("server." + serverParameter + ".start.path");
60
        if(confStartPath != null)
61
            if(startPath == null)
62
                startPath = confStartPath;
63
            else
64
                startPath = confStartPath + startPath;
65
        if(startPath != null)
66
            path = startPath + path;
144 jmachado 67
        request.setAttribute("path", path);
68
        request.setAttribute("server", server);
489 jmachado 69
        request.setAttribute("serverConfName", serverParameter);
70
        request.setAttribute("requestedStartPath", startPath);
222 jmachado 71
 
367 jmachado 72
        1.5.0/docs/api/java/lang/String.html">String proxyHost = ConfigProperties.getProperty(serverParameter + ".proxy.host");
73
        1.5.0/docs/api/java/lang/String.html">String proxyPort = ConfigProperties.getProperty(serverParameter + ".proxy.port");
74
        request.setAttribute("proxy.host",proxyHost);
75
        request.setAttribute("proxy.port",proxyPort);
76
 
165 jmachado 77
        try
1 fvelez 78
        {
165 jmachado 79
//                URL url = new URL(server + path);
80
//                URLConnection con = url.openConnection();
81
//
82
//                con.connect();
83
//                logger.info("contentPath:" + con.getContentType());
1 fvelez 84
//                if(con.getContentType() != null && con.getContentType().toLowerCase().indexOf("html") >= 0)
165 jmachado 85
            if (server.startsWith("ftp://"))
86
            {
406 jmachado 87
                processRequestFtp(request, response, path, serverParameter, server,startPath);
165 jmachado 88
                return mapping.findForward(layout + ".ftp");
1 fvelez 89
            }
165 jmachado 90
            else
91
            if (path.endsWith("html") || path.endsWith("htm") || path.endsWith("shtml") || path.endsWith("asp") || path.endsWith("jsp") || path.endsWith("aspx") || path.endsWith("/"))
1 fvelez 92
            {
165 jmachado 93
                1.5.0/docs/api/java/lang/String.html">String encoding = ConfigProperties.getProperty(serverParameter + ".encoding");
94
                request.setAttribute("encoding", encoding);
95
                return mapping.findForward(layout);
1 fvelez 96
            }
165 jmachado 97
            else
1 fvelez 98
            {
165 jmachado 99
                ActionForward forward = new ActionForward();
100
                request.setAttribute("useOutputStream", "true");
101
                forward.setPath("/WebProxy");
102
                return forward;
1 fvelez 103
            }
104
        }
165 jmachado 105
        catch (1.5.0/docs/api/java/io/FileNotFoundException.html">FileNotFoundException e)
1 fvelez 106
        {
165 jmachado 107
            throw e;
1 fvelez 108
        }
165 jmachado 109
        catch (1.5.0/docs/api/java/lang/Throwable.html">Throwable e)
110
        {
698 jmachado 111
            if(e instanceof NotAuthorizedException)
112
                throw (NotAuthorizedException)e;
165 jmachado 113
            logger.error(e, e);
114
            throw new 1.5.0/docs/api/java/io/IOException.html">IOException(e.toString());
115
        }
1 fvelez 116
    }
117
 
118
 
249 jmachado 119
 
1098 jmachado 120
    protected void processRequestFtp(HttpServletRequest request, HttpServletResponse response, 1.5.0/docs/api/java/lang/String.html">String path, 1.5.0/docs/api/java/lang/String.html">String server, 1.5.0/docs/api/java/lang/String.html">String serverUrl, 1.5.0/docs/api/java/lang/String.html">String startPath)
1 fvelez 121
            throws ServletException, 1.5.0/docs/api/java/io/IOException.html">IOException
122
    {
123
        try
124
        {
406 jmachado 125
            request.setAttribute(FTP_ACTUAL_PATH_KEY,path);
126
 
127
            //Check if user wants to create folders or upload files
1312 jmachado 128
            FtpManager.checkIfAnyFilesToUploadFromRequest(this, request, response);
406 jmachado 129
 
698 jmachado 130
            UserSession userSession = UserSessionProxy.loadUserSessionFromRequest(request);
1098 jmachado 131
            //String username = userSession != null ? userSession.getUsername() : "UNKNOWN";
698 jmachado 132
 
1098 jmachado 133
            FTPFileProxy.UserCredentialsForFtp u = FTPFileProxy.getUserCredentialsForFtp(server, userSession);
134
            1.5.0/docs/api/java/lang/String.html">String username = u.username;
135
            1.5.0/docs/api/java/lang/String.html">String password = u.password;
222 jmachado 136
 
144 jmachado 137
            if (path != null)
1 fvelez 138
            {
222 jmachado 139
 
1098 jmachado 140
                //boolean isStaticAccess = ConfigProperties.getBooleanProperty(serverStr+".use.default.credentials.to.updates.and.deletes");
141
                //String user;
142
                //String pass;
908 jmachado 143
 
1098 jmachado 144
                /*
908 jmachado 145
                if(isStaticAccess)
146
                {
147
                    user = ConfigProperties.getProperty(serverStr + ".user");
148
                    pass = ConfigProperties.getProperty(serverStr + ".pass");
149
                }
150
                else
151
                {
152
                    logger.warn("Will access using user and pass authnenticated to connect FTP " + serverStr);
153
                    user= username;
923 jmachado 154
                    String passwordCode = (String) ((UserSessionImpl)userSession).get(FtpService.FTP_PASSWORD);
155
                    pass= DesUtils.getInstance().decrypt(passwordCode);
156
                    //pass= ((UserImpl)userSession.getUser()).getPop3PassDecrypted();
908 jmachado 157
                }
1098 jmachado 158
                */
159
                FtpServer ftpServer = FtpServer.getNewServer(serverUrl, username, password);
160
                FTPClient client = FTPFileProxy.getFtpClient(ftpServer,userSession,server,serverUrl);
1005 jmachado 161
                if(client == null)
162
                {
163
                    logger.warn("###################");
164
                    logger.warn("################### > CANT CONNECT FTP");
165
                    response.sendError(408);
166
                    return;
167
                }
168
 
696 jmachado 169
                if(!client.changeWorkingDirectory(path))
170
                {
908 jmachado 171
                    throw new NotAuthorizedException("nao pode aceder a esta area");
696 jmachado 172
                }
144 jmachado 173
                FTPFile[] files = client.listFiles();
1 fvelez 174
                List<IFile> iFiles = new ArrayList<IFile>();
144 jmachado 175
                for (FTPFile ftpFile : files)
1 fvelez 176
                {
144 jmachado 177
                    IFile iFile;
178
                    if (path.endsWith("/"))
1098 jmachado 179
                        iFile = new FtpFile(ftpFile, serverUrl + URIUtil.encodePath(path +ftpFile.getName(),"ISO-8859-1"), path);
180
                    else iFile = new FtpFile(ftpFile, serverUrl + URIUtil.encodePath(path + "/" + ftpFile.getName(),"ISO-8859-1"), path);
1 fvelez 181
                    iFiles.add(iFile);
1283 jmachado 182
 
1 fvelez 183
                }
222 jmachado 184
                List<NavPlace> navPlaces = FtpFile.getNavPlaces(path,startPath);
165 jmachado 185
                request.setAttribute("NavPlaces",navPlaces);
144 jmachado 186
                request.setAttribute("files", iFiles);
187
                client.quit();
1 fvelez 188
                client.disconnect();
189
            }
190
            else
191
            {
1098 jmachado 192
                throw new 1.5.0/docs/api/java/io/FileNotFoundException.html">FileNotFoundException("url not found for server:" + serverUrl + " and path:" + path);
1 fvelez 193
            }
194
        }
144 jmachado 195
        catch (1.5.0/docs/api/java/lang/Throwable.html">Throwable e)
1 fvelez 196
        {
698 jmachado 197
            if(e instanceof NotAuthorizedException)
198
                throw (NotAuthorizedException)e;
144 jmachado 199
            logger.error(e, e);
1 fvelez 200
        }
201
    }
202
}