Rev 1310 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1 | fvelez | 1 | package pt.estgp.estgweb.web; |
2 | |||
653 | jmachado | 3 | import org.apache.commons.httpclient.URIException; |
249 | jmachado | 4 | import org.apache.commons.httpclient.util.URIUtil; |
1 | fvelez | 5 | import org.apache.log4j.Logger; |
249 | jmachado | 6 | import pt.estgp.estgweb.utils.ConfigProperties; |
1 | fvelez | 7 | |
249 | jmachado | 8 | import javax.servlet.ServletException; |
1 | fvelez | 9 | import javax.servlet.http.HttpServlet; |
10 | import javax.servlet.http.HttpServletRequest; |
||
11 | import javax.servlet.http.HttpServletResponse; |
||
249 | jmachado | 12 | import javax.servlet.jsp.JspWriter; |
13 | import java.io.FileNotFoundException; |
||
14 | import java.io.IOException; |
||
15 | import java.io.InputStream; |
||
16 | import java.io.OutputStream; |
||
368 | jmachado | 17 | import java.net.*; |
18 | import java.util.Date; |
||
1484 | jmachado | 19 | import java.util.HashMap; |
20 | import java.util.Map; |
||
21 | import java.util.zip.GZIPInputStream; |
||
22 | import java.util.zip.InflaterInputStream; |
||
368 | jmachado | 23 | import java.util.zip.ZipInputStream; |
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 | } |
||
1484 | jmachado | 200 | |
201 | static Map<String,Boolean> urlOKCACHE = new HashMap<String, Boolean>(); |
||
202 | static Map<String,Integer> urlOKCACHE_LAST_CHECK_COUNT = new HashMap<String, Integer>(); |
||
203 | |||
865 | jmachado | 204 | 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) |
205 | { |
||
1484 | jmachado | 206 | 1.5.0/docs/api/java/lang/String.html">String serverPathKey = server + "$" + path; |
207 | 1.5.0/docs/api/java/lang/Boolean.html">Boolean urlOk = urlOKCACHE.get(serverPathKey); |
||
208 | if(urlOk != null) |
||
209 | { |
||
210 | int count = urlOKCACHE_LAST_CHECK_COUNT.get(serverPathKey); |
||
211 | if(count > 0) |
||
212 | { |
||
213 | count = count-1; |
||
214 | urlOKCACHE_LAST_CHECK_COUNT.put(serverPathKey,count); |
||
215 | return urlOk; |
||
216 | } |
||
217 | } |
||
865 | jmachado | 218 | |
219 | 1.5.0/docs/api/java/lang/String.html">String serverUrl = ConfigProperties.getProperty("server." + server); |
||
220 | 1.5.0/docs/api/java/lang/String.html">String serverEncoding = ConfigProperties.getProperty(server + ".encoding"); |
||
221 | 1.5.0/docs/api/java/lang/String.html">String confStartPath = ConfigProperties.getProperty("server." + server + ".start.path"); |
||
222 | if (confStartPath != null) |
||
223 | path = confStartPath + path; |
||
224 | |||
225 | |||
226 | try |
||
227 | { |
||
228 | |||
1484 | jmachado | 229 | 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; |
865 | jmachado | 230 | |
231 | |||
1484 | jmachado | 232 | 1.5.0/docs/api/java/lang/System.html">System.out.println("Getting: " + serverUrl + pathFinal + " CHECK TIME"); |
233 | 1.5.0/docs/api/java/net/URL.html">URL url = new 1.5.0/docs/api/java/net/URL.html">URL(serverUrl + pathFinal); |
||
234 | 1.5.0/docs/api/java/net/HttpURLConnection.html">HttpURLConnection huc = (1.5.0/docs/api/java/net/HttpURLConnection.html">HttpURLConnection) url.openConnection(); |
||
235 | huc.setRequestMethod("HEAD"); |
||
236 | urlOKCACHE_LAST_CHECK_COUNT.put(serverPathKey,10); |
||
237 | if(huc.getResponseCode() == 1.5.0/docs/api/java/net/HttpURLConnection.html">HttpURLConnection.HTTP_OK) |
||
238 | { |
||
239 | urlOKCACHE.put(serverPathKey,true); |
||
240 | huc.disconnect(); |
||
241 | return true; |
||
242 | } |
||
243 | else |
||
244 | { |
||
245 | urlOKCACHE.put(serverPathKey,false); |
||
246 | huc.disconnect(); |
||
247 | return false; |
||
248 | } |
||
865 | jmachado | 249 | |
250 | } |
||
251 | catch (1.5.0/docs/api/java/io/FileNotFoundException.html">FileNotFoundException e) |
||
252 | { |
||
253 | return false; |
||
254 | } |
||
255 | catch (1.5.0/docs/api/java/lang/Exception.html">Exception e) |
||
256 | { |
||
257 | return false; |
||
258 | } |
||
259 | |||
260 | } |
||
261 | |||
249 | jmachado | 262 | 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 |
263 | { |
||
652 | jmachado | 264 | |
249 | jmachado | 265 | 1.5.0/docs/api/java/lang/String.html">String serverUrl = ConfigProperties.getProperty("server." + server); |
266 | 1.5.0/docs/api/java/lang/String.html">String serverEncoding = ConfigProperties.getProperty(server + ".encoding"); |
||
267 | 1.5.0/docs/api/java/lang/String.html">String confStartPath = ConfigProperties.getProperty("server." + server + ".start.path"); |
||
368 | jmachado | 268 | if (confStartPath != null) |
249 | jmachado | 269 | path = confStartPath + path; |
270 | |||
271 | |||
272 | try |
||
273 | { |
||
652 | jmachado | 274 | |
655 | jmachado | 275 | 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 | 276 | 1.5.0/docs/api/java/lang/System.html">System.out.println("Getting: " + serverUrl + pathFinal); |
277 | 1.5.0/docs/api/java/net/URL.html">URL url = new 1.5.0/docs/api/java/net/URL.html">URL(serverUrl + pathFinal); |
||
278 | |||
249 | jmachado | 279 | 1.5.0/docs/api/java/net/URLConnection.html">URLConnection con = url.openConnection(); |
280 | 5+0%2Fdocs%2Fapi+InputStream">InputStream stream = con.getInputStream(); |
||
281 | |||
282 | byte[] buf = new byte[1024]; |
||
283 | int readedBytes; |
||
284 | |||
368 | jmachado | 285 | while ((readedBytes = stream.read(buf)) > 0) |
249 | jmachado | 286 | { |
368 | jmachado | 287 | 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 | 288 | out.print(str); |
289 | } |
||
290 | } |
||
368 | jmachado | 291 | catch (1.5.0/docs/api/java/io/FileNotFoundException.html">FileNotFoundException e) |
249 | jmachado | 292 | { |
293 | throw new 1.5.0/docs/api/java/io/FileNotFoundException.html">FileNotFoundException("url not found for server:" + server + " and path:" + path); |
||
294 | } |
||
368 | jmachado | 295 | catch (1.5.0/docs/api/java/lang/Exception.html">Exception e) |
249 | jmachado | 296 | { |
297 | throw new 1.5.0/docs/api/java/io/IOException.html">IOException(e.toString()); |
||
298 | } |
||
299 | |||
300 | } |
||
301 | |||
302 | |||
1 | fvelez | 303 | // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code."> |
368 | jmachado | 304 | /** |
305 | * Handles the HTTP <code>GET</code> method. |
||
306 | * |
||
307 | * @param request servlet request |
||
1 | fvelez | 308 | * @param response servlet response |
309 | */ |
||
310 | protected void doGet(HttpServletRequest request, HttpServletResponse response) |
||
368 | jmachado | 311 | throws ServletException, 1.5.0/docs/api/java/io/IOException.html">IOException |
312 | { |
||
1 | fvelez | 313 | processRequest(request, response); |
314 | } |
||
315 | |||
368 | jmachado | 316 | /** |
317 | * Handles the HTTP <code>POST</code> method. |
||
318 | * |
||
319 | * @param request servlet request |
||
1 | fvelez | 320 | * @param response servlet response |
321 | */ |
||
322 | protected void doPost(HttpServletRequest request, HttpServletResponse response) |
||
368 | jmachado | 323 | throws ServletException, 1.5.0/docs/api/java/io/IOException.html">IOException |
324 | { |
||
1 | fvelez | 325 | processRequest(request, response); |
326 | } |
||
327 | |||
368 | jmachado | 328 | /** |
329 | * Returns a short description of the servlet. |
||
1 | fvelez | 330 | */ |
368 | jmachado | 331 | public 1.5.0/docs/api/java/lang/String.html">String getServletInfo() |
332 | { |
||
1 | fvelez | 333 | return "Proxy Servlet to forward to HTTP or FTP Proxy"; |
334 | } |
||
335 | // </editor-fold> |
||
336 | } |