Rev 851 |
Go to most recent revision |
Blame |
Compare with Previous |
Last modification |
View Log
| RSS feed
package pt.estgp.estgweb.web;
import org.apache.log4j.Logger;
import org.apache.commons.httpclient.util.URIUtil;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileNotFoundException;
import java.net.URL;
import java.net.URLConnection;
import pt.estgp.estgweb.utils.ConfigProperties;
/**
* @author Jorge Machado
* @date 26/Fev/2008
* @time 12:46:16
* @see pt.estgp.estgweb.web
*/
public class HTTPProxy
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(HTTPProxy.
class);
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 useOutputStream =
(1.5.0/docs/api/java/lang/String.html">String) request.
getAttribute("useOutputStream");
logger.
info("server:" + server
);
logger.
info("path:" + path
);
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 + URIUtil.
encodePath(path,
"ISO-8859-1"));
1.5.0/docs/api/java/net/URLConnection.html">URLConnection con = url.
openConnection();
response.
setContentType(con.
getContentType());
response.
setContentLength(con.
getContentLength());
5+0%2Fdocs%2Fapi+InputStream">InputStream stream = con.
getInputStream();
byte[] buf =
new byte[1024
];
int readedBytes
;
if(useOutputStream
!=
null && useOutputStream.
equals("true"))
{
5+0%2Fdocs%2Fapi+OutputStream">OutputStream os = response.
getOutputStream();
while((readedBytes = stream.
read(buf
)) > 0
)
{
os.
write(buf,0,readedBytes
);
}
os.
close();
}
else
{
while((readedBytes = stream.
read(buf
)) > 0
)
{
1.5.0/docs/api/java/lang/String.html">String s =
new 1.5.0/docs/api/java/lang/String.html">String(buf,
0,readedBytes,
"ISO-8859-1");
response.
getWriter().
write(s
);
}
}
}
catch(1.5.0/docs/api/java/io/FileNotFoundException.html">FileNotFoundException e
)
{
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/Exception.html">Exception 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
);
}
}
// <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>
}