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 org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import pt.estgp.estgweb.domain.UserSession;
import pt.estgp.estgweb.utils.ConfigProperties;
import pt.estgp.estgweb.web.controllers.ApplicationController;
import pt.estgp.estgweb.web.exceptions.NotAuthorizedException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* @author Jorge Machado
* @date 26/Fev/2008
* @time 18:01:54
* @see pt.estgp.estgweb.web
*/
public class LayoutController
extends ApplicationController
{
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";
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
);
1.5.0/docs/api/java/lang/String.html">String startPath =
(1.5.0/docs/api/java/lang/String.html">String) request.
getAttribute("startPath");
1.5.0/docs/api/java/lang/String.html">String confStartPath = ConfigProperties.
getProperty("server." + serverParameter +
".start.path");
if(confStartPath
!=
null)
if(startPath ==
null)
startPath = confStartPath
;
else
startPath = confStartPath + startPath
;
if(startPath
!=
null)
path = startPath + path
;
request.
setAttribute("path", path
);
request.
setAttribute("server", server
);
request.
setAttribute("serverConfName", serverParameter
);
request.
setAttribute("requestedStartPath", startPath
);
1.5.0/docs/api/java/lang/String.html">String proxyHost = ConfigProperties.
getProperty(serverParameter +
".proxy.host");
1.5.0/docs/api/java/lang/String.html">String proxyPort = ConfigProperties.
getProperty(serverParameter +
".proxy.port");
request.
setAttribute("proxy.host",proxyHost
);
request.
setAttribute("proxy.port",proxyPort
);
try
{
// URL url = new URL(server + path);
// 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, response, path, serverParameter, server,startPath
);
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("/"))
{
1.5.0/docs/api/java/lang/String.html">String encoding = ConfigProperties.
getProperty(serverParameter +
".encoding");
request.
setAttribute("encoding", encoding
);
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
)
{
if(e
instanceof NotAuthorizedException
)
throw (NotAuthorizedException
)e
;
logger.
error(e, e
);
throw new 1.5.0/docs/api/java/io/IOException.html">IOException(e.
toString());
}
}
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
)
throws ServletException,
1.5.0/docs/api/java/io/IOException.html">IOException
{
try
{
request.
setAttribute(FTP_ACTUAL_PATH_KEY,path
);
//Check if user wants to create folders or upload files
FtpManager.
checkIfAnyFilesToUploadFromRequest(this, request, response
);
UserSession userSession = UserSessionProxy.
loadUserSessionFromRequest(request
);
//String username = userSession != null ? userSession.getUsername() : "UNKNOWN";
FTPFileProxy.
UserCredentialsForFtp u = FTPFileProxy.
getUserCredentialsForFtp(server, userSession
);
1.5.0/docs/api/java/lang/String.html">String username = u.
username;
1.5.0/docs/api/java/lang/String.html">String password = u.
password;
if (path
!=
null)
{
//boolean isStaticAccess = ConfigProperties.getBooleanProperty(serverStr+".use.default.credentials.to.updates.and.deletes");
//String user;
//String pass;
/*
if(isStaticAccess)
{
user = ConfigProperties.getProperty(serverStr + ".user");
pass = ConfigProperties.getProperty(serverStr + ".pass");
}
else
{
logger.warn("Will access using user and pass authnenticated to connect FTP " + serverStr);
user= username;
String passwordCode = (String) ((UserSessionImpl)userSession).get(FtpService.FTP_PASSWORD);
pass= DesUtils.getInstance().decrypt(passwordCode);
//pass= ((UserImpl)userSession.getUser()).getPop3PassDecrypted();
}
*/
FtpServer ftpServer = FtpServer.
getNewServer(serverUrl, username, password
);
FTPClient client = FTPFileProxy.
getFtpClient(ftpServer,userSession,server,serverUrl
);
if(client ==
null)
{
logger.
warn("###################");
logger.
warn("################### > CANT CONNECT FTP");
response.
sendError(408
);
return;
}
if(!client.
changeWorkingDirectory(path
))
{
throw new NotAuthorizedException
("nao pode aceder a esta area");
}
FTPFile
[] files = client.
listFiles();
List
<IFile
> iFiles =
new ArrayList
<IFile
>();
for (FTPFile ftpFile : files
)
{
IFile iFile
;
if (path.
endsWith("/"))
iFile =
new FtpFile
(ftpFile, serverUrl + URIUtil.
encodePath(path +ftpFile.
getName(),
"ISO-8859-1"), path
);
else iFile =
new FtpFile
(ftpFile, serverUrl + URIUtil.
encodePath(path +
"/" + ftpFile.
getName(),
"ISO-8859-1"), path
);
iFiles.
add(iFile
);
}
List
<NavPlace
> navPlaces = FtpFile.
getNavPlaces(path,startPath
);
request.
setAttribute("NavPlaces",navPlaces
);
request.
setAttribute("files", iFiles
);
client.
quit();
client.
disconnect();
}
else
{
throw new 1.5.0/docs/api/java/io/FileNotFoundException.html">FileNotFoundException("url not found for server:" + serverUrl +
" and path:" + path
);
}
}
catch (1.5.0/docs/api/java/lang/Throwable.html">Throwable e
)
{
if(e
instanceof NotAuthorizedException
)
throw (NotAuthorizedException
)e
;
logger.
error(e, e
);
}
}
}