Subversion Repositories bacoAlunos

Rev

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

Rev Author Line No. Line
1 fvelez 1
package pt.estgp.estgweb.web;
2
 
3
import org.apache.log4j.Logger;
4
import org.apache.commons.httpclient.util.URIUtil;
5
 
6
import javax.servlet.http.HttpServlet;
7
import javax.servlet.http.HttpServletRequest;
8
import javax.servlet.http.HttpServletResponse;
9
import javax.servlet.ServletException;
10
import java.io.IOException;
11
import java.io.InputStream;
12
import java.io.OutputStream;
13
import java.io.FileNotFoundException;
14
import java.net.URL;
15
import java.net.URLConnection;
16
 
17
import pt.estgp.estgweb.utils.ConfigProperties;
18
 
19
/**
20
 * @author Jorge Machado
21
 * @date 26/Fev/2008
22
 * @time 12:46:16
23
 * @see pt.estgp.estgweb.web
24
 */
25
public class HTTPProxy extends HttpServlet
26
{
27
 
851 jmachado 28
    /** sss requests for both HTTP <code>GET</code> and <code>POST</code> methods.
1 fvelez 29
     * @param request servlet request
30
     * @param response servlet response
31
     */
32
 
33
    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);
34
 
35
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
36
            throws ServletException, 1.5.0/docs/api/java/io/IOException.html">IOException
37
    {
38
        1.5.0/docs/api/java/lang/String.html">String path = (1.5.0/docs/api/java/lang/String.html">String) request.getAttribute("path");
39
        1.5.0/docs/api/java/lang/String.html">String server = (1.5.0/docs/api/java/lang/String.html">String) request.getAttribute("server");
40
        1.5.0/docs/api/java/lang/String.html">String useOutputStream = (1.5.0/docs/api/java/lang/String.html">String) request.getAttribute("useOutputStream");
41
        logger.info("server:" + server);
42
        logger.info("path:" + path);
43
        if(path != null)
44
        {
45
            try
46
            {
47
                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"));
48
 
49
                1.5.0/docs/api/java/net/URLConnection.html">URLConnection con = url.openConnection();
50
                response.setContentType(con.getContentType());
51
                response.setContentLength(con.getContentLength());
52
                5+0%2Fdocs%2Fapi+InputStream">InputStream stream = con.getInputStream();
53
 
54
                byte[] buf = new byte[1024];
55
                int readedBytes;
56
                if(useOutputStream!=null && useOutputStream.equals("true"))
57
                {
58
 
59
                    5+0%2Fdocs%2Fapi+OutputStream">OutputStream os = response.getOutputStream();
60
                    while((readedBytes = stream.read(buf)) > 0)
61
                    {
62
                        os.write(buf,0,readedBytes);
63
                    }
64
                    os.close();
65
                }
66
                else
67
                {
68
                    while((readedBytes = stream.read(buf)) > 0)
69
                    {
70
                        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");
71
                        response.getWriter().write(s);
72
                    }
73
                }
74
            }
75
            catch(1.5.0/docs/api/java/io/FileNotFoundException.html">FileNotFoundException e)
76
            {
77
                throw new 1.5.0/docs/api/java/io/FileNotFoundException.html">FileNotFoundException("url not found for server:" + server + " and path:" + path);
78
            }
79
            catch(1.5.0/docs/api/java/lang/Exception.html">Exception e)
80
            {
81
                throw new 1.5.0/docs/api/java/io/IOException.html">IOException(e.toString());
82
            }
83
        }
84
        else
85
        {
86
            throw new 1.5.0/docs/api/java/io/FileNotFoundException.html">FileNotFoundException("url not found for server:" + server + " and path:" + path);
87
        }
88
    }
89
 
90
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
91
    /** Handles the HTTP <code>GET</code> method.
92
     * @param request servlet request
93
     * @param response servlet response
94
     */
95
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
96
            throws ServletException, 1.5.0/docs/api/java/io/IOException.html">IOException {
97
        processRequest(request, response);
98
    }
99
 
100
    /** Handles the HTTP <code>POST</code> method.
101
     * @param request servlet request
102
     * @param response servlet response
103
     */
104
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
105
            throws ServletException, 1.5.0/docs/api/java/io/IOException.html">IOException {
106
        processRequest(request, response);
107
    }
108
 
109
    /** Returns a short description of the servlet.
110
     */
111
    public 1.5.0/docs/api/java/lang/String.html">String getServletInfo() {
112
        return "Proxy Servlet to get pages form static web page servers";
113
    }
114
    // </editor-fold>
115
}