Subversion Repositories bacoAlunos

Rev

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

Rev Author Line No. Line
1 fvelez 1
package pt.estgp.estgweb.web;
2
 
865 jmachado 3
import jomm.utils.StreamsUtils;
653 jmachado 4
import org.apache.commons.httpclient.URIException;
249 jmachado 5
import org.apache.commons.httpclient.util.URIUtil;
1 fvelez 6
import org.apache.log4j.Logger;
249 jmachado 7
import pt.estgp.estgweb.utils.ConfigProperties;
865 jmachado 8
import pt.estgp.estgweb.utils.StringsUtils;
1 fvelez 9
 
249 jmachado 10
import javax.servlet.ServletException;
1 fvelez 11
import javax.servlet.http.HttpServlet;
12
import javax.servlet.http.HttpServletRequest;
13
import javax.servlet.http.HttpServletResponse;
249 jmachado 14
import javax.servlet.jsp.JspWriter;
15
import java.io.FileNotFoundException;
16
import java.io.IOException;
17
import java.io.InputStream;
18
import java.io.OutputStream;
368 jmachado 19
import java.net.*;
20
import java.util.Date;
21
import java.util.zip.ZipInputStream;
22
import java.util.zip.InflaterInputStream;
23
import java.util.zip.GZIPInputStream;
1 fvelez 24
 
25
/**
26
 * @author Jorge Machado
27
 * @date 26/Fev/2008
28
 * @time 12:46:16
29
 * @see pt.estgp.estgweb.web
30
 */
368 jmachado 31
public class WebProxy extends HttpServlet
32
{
1 fvelez 33
 
368 jmachado 34
    /**
851 jmachado 35
     * sss requests for both HTTP <code>GET</code> and <code>POST</code> methods.
368 jmachado 36
     *
1 fvelez 37
     * @param request servlet request
38
     * @param response servlet response
39
     */
40
 
41
    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(WebProxy.class);
42
 
43
 
249 jmachado 44
    protected static void processRequest(HttpServletRequest request, HttpServletResponse response)
1 fvelez 45
            throws ServletException, 1.5.0/docs/api/java/io/IOException.html">IOException
46
    {
47
        1.5.0/docs/api/java/lang/String.html">String path = (1.5.0/docs/api/java/lang/String.html">String) request.getAttribute("path");
48
        1.5.0/docs/api/java/lang/String.html">String server = (1.5.0/docs/api/java/lang/String.html">String) request.getAttribute("server");
49
        1.5.0/docs/api/java/lang/String.html">String useOutputStream = (1.5.0/docs/api/java/lang/String.html">String) request.getAttribute("useOutputStream");
29 jmachado 50
        1.5.0/docs/api/java/lang/String.html">String encoding = (1.5.0/docs/api/java/lang/String.html">String) request.getAttribute("encoding");
367 jmachado 51
        1.5.0/docs/api/java/lang/String.html">String proxyHost = (1.5.0/docs/api/java/lang/String.html">String) request.getAttribute("proxy.host");
52
        1.5.0/docs/api/java/lang/String.html">String proxyPort = (1.5.0/docs/api/java/lang/String.html">String) request.getAttribute("proxy.port");
1 fvelez 53
        logger.info("server:" + server);
54
        logger.info("path:" + path);
367 jmachado 55
        logger.info("proxyHost:" + proxyHost);
56
        logger.info("proxyPort:" + proxyPort);
368 jmachado 57
        if (path != null)
1 fvelez 58
        {
59
            try
60
            {
368 jmachado 61
                5+0%2Fdocs%2Fapi+Proxy">Proxy proxy = null;
62
                if (proxyHost != null && proxyHost.length() > 0)
63
                {
64
                    proxy = new 5+0%2Fdocs%2Fapi+Proxy">Proxy(5+0%2Fdocs%2Fapi+Proxy">Proxy.1.5.0/docs/api/java/lang/reflect/Type.html">Type.HTTP, new 1.5.0/docs/api/java/net/InetSocketAddress.html">InetSocketAddress(proxyHost, 1.5.0/docs/api/java/lang/Integer.html">Integer.parseInt(proxyPort)));
65
                }
367 jmachado 66
 
1 fvelez 67
 
368 jmachado 68
                1.5.0/docs/api/java/lang/String.html">String finalURL = server + URIUtil.encodePath(path, "ISO-8859-1");
69
                1.5.0/docs/api/java/net/URL.html">URL url = new 1.5.0/docs/api/java/net/URL.html">URL(finalURL);
70
 
1 fvelez 71
                1.5.0/docs/api/java/net/URLConnection.html">URLConnection con = url.openConnection();
72
                response.setContentType(con.getContentType());
73
                response.setContentLength(con.getContentLength());
74
 
368 jmachado 75
 
76
                5+0%2Fdocs%2Fapi+InputStream">InputStream stream = openStream(finalURL,proxy);
77
 
1 fvelez 78
                byte[] buf = new byte[1024];
79
                int readedBytes;
368 jmachado 80
                if (useOutputStream != null && useOutputStream.equals("true"))
1 fvelez 81
                {
82
 
83
                    5+0%2Fdocs%2Fapi+OutputStream">OutputStream os = response.getOutputStream();
368 jmachado 84
                    while ((readedBytes = stream.read(buf)) > 0)
1 fvelez 85
                    {
368 jmachado 86
                        os.write(buf, 0, readedBytes);
1 fvelez 87
                    }
88
                    os.close();
89
                }
90
                else
91
                {
368 jmachado 92
                    while ((readedBytes = stream.read(buf)) > 0)
1 fvelez 93
                    {
368 jmachado 94
                        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, encoding);
1 fvelez 95
                        response.getWriter().write(s);
96
                    }
97
                }
98
            }
368 jmachado 99
            catch (1.5.0/docs/api/java/io/FileNotFoundException.html">FileNotFoundException e)
1 fvelez 100
            {
313 jmachado 101
                logger.warn("url not found for server:" + server + " and path:" + path);
102
                response.sendError(404);
1 fvelez 103
            }
368 jmachado 104
            catch (1.5.0/docs/api/java/lang/Exception.html">Exception e)
1 fvelez 105
            {
106
                throw new 1.5.0/docs/api/java/io/IOException.html">IOException(e.toString());
107
            }
108
        }
109
        else
110
        {
313 jmachado 111
            logger.warn("url not found for server:" + server + " and path:" + path);
112
            response.sendError(404);
1 fvelez 113
        }
114
 
115
    }
116
 
368 jmachado 117
    private static 5+0%2Fdocs%2Fapi+InputStream">InputStream openStream(1.5.0/docs/api/java/lang/String.html">String requestURL, 5+0%2Fdocs%2Fapi+Proxy">Proxy proxy) throws 1.5.0/docs/api/java/io/IOException.html">IOException
118
    {
119
        5+0%2Fdocs%2Fapi+InputStream">InputStream in = null;
120
        1.5.0/docs/api/java/net/URL.html">URL url = new 1.5.0/docs/api/java/net/URL.html">URL(requestURL);
121
 
122
        1.5.0/docs/api/java/net/HttpURLConnection.html">HttpURLConnection con = null;
123
        int responseCode = 0;
124
        do
125
        {
126
            if (proxy != null)
127
                con = (1.5.0/docs/api/java/net/HttpURLConnection.html">HttpURLConnection) url.openConnection(proxy);
128
            else
129
                con = (1.5.0/docs/api/java/net/HttpURLConnection.html">HttpURLConnection) url.openConnection();
130
            con.setRequestProperty("User-Agent", "ESTG-Baco/1.0");
131
            con.setRequestProperty("Accept-Encoding", "compress, gzip, identify");
132
            try
133
            {
134
                responseCode = con.getResponseCode();
135
                logger.debug("responseCode=" + responseCode);
136
            }
137
            catch (1.5.0/docs/api/java/io/FileNotFoundException.html">FileNotFoundException e)
138
            {
139
                // assume it's a 503 response
140
                logger.info(requestURL, e);
141
                responseCode = 1.5.0/docs/api/java/net/HttpURLConnection.html">HttpURLConnection.HTTP_UNAVAILABLE;
142
            }
143
 
144
            if (responseCode == 1.5.0/docs/api/java/net/HttpURLConnection.html">HttpURLConnection.HTTP_UNAVAILABLE)
145
            {
146
                long retrySeconds = con.getHeaderFieldInt("Retry-After", -1);
147
                if (retrySeconds == -1)
148
                {
149
                    long now = (new 5+0%2Fdocs%2Fapi+Date">Date()).getTime();
150
                    long retryDate = con.getHeaderFieldDate("Retry-After", now);
151
                    retrySeconds = retryDate - now;
152
                }
153
                if (retrySeconds == 0)
154
                { // Apparently, it's a bad URL
155
                    throw new 1.5.0/docs/api/java/io/FileNotFoundException.html">FileNotFoundException("Bad URL?");
156
                }
157
                logger.error("Server response: Retry-After=" + retrySeconds);
158
                if (retrySeconds > 0)
159
                {
160
                    try
161
                    {
162
                        1.5.0/docs/api/java/lang/Thread.html">Thread.sleep(retrySeconds * 1000);
163
                    }
164
                    catch (1.5.0/docs/api/java/lang/InterruptedException.html">InterruptedException ex)
165
                    {
166
                        ex.printStackTrace();
167
                    }
168
                }
169
            }
170
        }
171
        while (responseCode == 1.5.0/docs/api/java/net/HttpURLConnection.html">HttpURLConnection.HTTP_UNAVAILABLE);
172
        1.5.0/docs/api/java/lang/String.html">String contentEncoding = con.getHeaderField("Content-Encoding");
173
        logger.debug("contentEncoding=" + contentEncoding);
174
        if ("compress".equals(contentEncoding))
175
        {
176
            1.5.0/docs/api/java/util/zip/ZipInputStream.html">ZipInputStream zis = new 1.5.0/docs/api/java/util/zip/ZipInputStream.html">ZipInputStream(con.getInputStream());
177
            zis.getNextEntry();
178
            in = zis;
179
        }
180
        else if ("gzip".equals(contentEncoding))
181
        {
182
            in = new 1.5.0/docs/api/java/util/zip/GZIPInputStream.html">GZIPInputStream(con.getInputStream());
183
        }
184
        else if ("deflate".equals(contentEncoding))
185
        {
186
            in = new 1.5.0/docs/api/java/util/zip/InflaterInputStream.html">InflaterInputStream(con.getInputStream());
187
        }
188
        else
189
        {
190
            in = con.getInputStream();
191
        }
192
        return in;
193
    }
194
 
653 jmachado 195
    public static void main(1.5.0/docs/api/java/lang/String.html">String[] args) throws 1.5.0/docs/api/javax/print/URIException.html">URIException {
196
        1.5.0/docs/api/java/lang/String.html">String path = "pagina.php?id=asdasd&jj=kk";
197
        1.5.0/docs/api/java/lang/String.html">String pathFinal = path.indexOf("?") >=0 ? URIUtil.encodePath(path.substring(0,path.indexOf("?")), "ISO-8859-1") + "?" +path.substring(path.indexOf("?")+1) : path;
198
        1.5.0/docs/api/java/lang/System.html">System.out.println(pathFinal);
199
    }
865 jmachado 200
    public static boolean checkExist( 1.5.0/docs/api/java/lang/String.html">String server, 1.5.0/docs/api/java/lang/String.html">String path)
201
    {
202
 
203
        1.5.0/docs/api/java/lang/String.html">String serverUrl = ConfigProperties.getProperty("server." + server);
204
        1.5.0/docs/api/java/lang/String.html">String serverEncoding = ConfigProperties.getProperty(server + ".encoding");
205
        1.5.0/docs/api/java/lang/String.html">String confStartPath = ConfigProperties.getProperty("server." + server + ".start.path");
206
        if (confStartPath != null)
207
            path = confStartPath + path;
208
 
209
 
210
        try
211
        {
212
 
213
            1.5.0/docs/api/java/lang/String.html">String pathFinal = path.indexOf("?") >=0 ? URIUtil.encodePath(path.substring(0,path.indexOf("?")), "ISO-8859-1") +  path.substring(path.indexOf("?")) : path;
214
            1.5.0/docs/api/java/lang/System.html">System.out.println("Getting: " + serverUrl + pathFinal);
215
            1.5.0/docs/api/java/net/URL.html">URL url = new 1.5.0/docs/api/java/net/URL.html">URL(serverUrl + pathFinal);
216
 
217
 
218
            1.5.0/docs/api/java/net/URLConnection.html">URLConnection con = url.openConnection();
219
            5+0%2Fdocs%2Fapi+InputStream">InputStream stream = con.getInputStream();
220
            1.5.0/docs/api/java/lang/String.html">String out = StreamsUtils.readString(stream);
221
            stream.close();
222
 
223
            if(out.trim().length() == 0)
224
                return false;
225
            return true;
226
        }
227
        catch (1.5.0/docs/api/java/io/FileNotFoundException.html">FileNotFoundException e)
228
        {
229
            return false;
230
        }
231
        catch (1.5.0/docs/api/java/lang/Exception.html">Exception e)
232
        {
233
            return false;
234
        }
235
 
236
    }
237
 
249 jmachado 238
    public static void processHttpRequestService(JspWriter out, 1.5.0/docs/api/java/lang/String.html">String server, 1.5.0/docs/api/java/lang/String.html">String path) throws 1.5.0/docs/api/java/io/IOException.html">IOException, ServletException
239
    {
652 jmachado 240
 
249 jmachado 241
        1.5.0/docs/api/java/lang/String.html">String serverUrl = ConfigProperties.getProperty("server." + server);
242
        1.5.0/docs/api/java/lang/String.html">String serverEncoding = ConfigProperties.getProperty(server + ".encoding");
243
        1.5.0/docs/api/java/lang/String.html">String confStartPath = ConfigProperties.getProperty("server." + server + ".start.path");
368 jmachado 244
        if (confStartPath != null)
249 jmachado 245
            path = confStartPath + path;
246
 
247
 
248
        try
249
        {
652 jmachado 250
 
655 jmachado 251
            1.5.0/docs/api/java/lang/String.html">String pathFinal = path.indexOf("?") >=0 ? URIUtil.encodePath(path.substring(0,path.indexOf("?")), "ISO-8859-1") +  path.substring(path.indexOf("?")) : path;
653 jmachado 252
            1.5.0/docs/api/java/lang/System.html">System.out.println("Getting: " + serverUrl + pathFinal);
253
            1.5.0/docs/api/java/net/URL.html">URL url = new 1.5.0/docs/api/java/net/URL.html">URL(serverUrl + pathFinal);
254
 
249 jmachado 255
            1.5.0/docs/api/java/net/URLConnection.html">URLConnection con = url.openConnection();
256
            5+0%2Fdocs%2Fapi+InputStream">InputStream stream = con.getInputStream();
257
 
258
            byte[] buf = new byte[1024];
259
            int readedBytes;
260
 
368 jmachado 261
            while ((readedBytes = stream.read(buf)) > 0)
249 jmachado 262
            {
368 jmachado 263
                1.5.0/docs/api/java/lang/String.html">String str = new 1.5.0/docs/api/java/lang/String.html">String(buf, 0, readedBytes, serverEncoding);
249 jmachado 264
                out.print(str);
265
            }
266
        }
368 jmachado 267
        catch (1.5.0/docs/api/java/io/FileNotFoundException.html">FileNotFoundException e)
249 jmachado 268
        {
269
            throw new 1.5.0/docs/api/java/io/FileNotFoundException.html">FileNotFoundException("url not found for server:" + server + " and path:" + path);
270
        }
368 jmachado 271
        catch (1.5.0/docs/api/java/lang/Exception.html">Exception e)
249 jmachado 272
        {
273
            throw new 1.5.0/docs/api/java/io/IOException.html">IOException(e.toString());
274
        }
275
 
276
    }
277
 
278
 
1 fvelez 279
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
368 jmachado 280
    /**
281
     * Handles the HTTP <code>GET</code> method.
282
     *
283
     * @param request  servlet request
1 fvelez 284
     * @param response servlet response
285
     */
286
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
368 jmachado 287
            throws ServletException, 1.5.0/docs/api/java/io/IOException.html">IOException
288
    {
1 fvelez 289
        processRequest(request, response);
290
    }
291
 
368 jmachado 292
    /**
293
     * Handles the HTTP <code>POST</code> method.
294
     *
295
     * @param request  servlet request
1 fvelez 296
     * @param response servlet response
297
     */
298
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
368 jmachado 299
            throws ServletException, 1.5.0/docs/api/java/io/IOException.html">IOException
300
    {
1 fvelez 301
        processRequest(request, response);
302
    }
303
 
368 jmachado 304
    /**
305
     * Returns a short description of the servlet.
1 fvelez 306
     */
368 jmachado 307
    public 1.5.0/docs/api/java/lang/String.html">String getServletInfo()
308
    {
1 fvelez 309
        return "Proxy Servlet to forward to HTTP or FTP Proxy";
310
    }
311
    // </editor-fold>
312
}