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.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import pt.estgp.estgweb.utils.ConfigProperties;
import javax.servlet.ServletException;
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 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
);
1.5.0/docs/api/java/lang/String.html">String startPath =
(1.5.0/docs/api/java/lang/String.html">String) request.
getAttribute("startPath");
if(startPath ==
null)
startPath = ConfigProperties.
getProperty("server." + serverParameter +
".start.path");
else
startPath = ConfigProperties.
getProperty("server." + serverParameter +
".start.path") + startPath
;
path = startPath + path
;
request.
setAttribute("path", path
);
request.
setAttribute("server", server
);
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, 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
)
{
logger.
error(e, e
);
throw new 1.5.0/docs/api/java/io/IOException.html">IOException(e.
toString());
}
}
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,
1.5.0/docs/api/java/lang/String.html">String startPath
)
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(path
);
FTPFile
[] files = client.
listFiles();
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
);
}
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:" + 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
;
}
}
}