Mercurial > dive4elements > river
annotate flys-client/src/main/java/de/intevation/flys/client/server/ProxyServlet.java @ 5720:aa1a43f5c03a
Send errors as plaintext and not as pdf documents
author | Andre Heinecke <aheinecke@intevation.de> |
---|---|
date | Mon, 15 Apr 2013 18:32:37 +0200 |
parents | 92239b53823f |
children |
rev | line source |
---|---|
3497
88feb3347aa5
Implement a ProxyServlet
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
1 package de.intevation.flys.client.server; |
88feb3347aa5
Implement a ProxyServlet
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
2 |
88feb3347aa5
Implement a ProxyServlet
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
3 import java.io.InputStream; |
88feb3347aa5
Implement a ProxyServlet
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
4 import java.io.IOException; |
88feb3347aa5
Implement a ProxyServlet
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
5 import java.io.OutputStream; |
88feb3347aa5
Implement a ProxyServlet
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
6 import java.util.Enumeration; |
88feb3347aa5
Implement a ProxyServlet
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
7 |
88feb3347aa5
Implement a ProxyServlet
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
8 import javax.servlet.ServletConfig; |
88feb3347aa5
Implement a ProxyServlet
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
9 import javax.servlet.http.HttpServlet; |
88feb3347aa5
Implement a ProxyServlet
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
10 import javax.servlet.http.HttpServletRequest; |
88feb3347aa5
Implement a ProxyServlet
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
11 import javax.servlet.http.HttpServletResponse; |
88feb3347aa5
Implement a ProxyServlet
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
12 import javax.servlet.http.HttpSession; |
88feb3347aa5
Implement a ProxyServlet
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
13 |
88feb3347aa5
Implement a ProxyServlet
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
14 import org.apache.http.Header; |
88feb3347aa5
Implement a ProxyServlet
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
15 import org.apache.http.HttpEntity; |
88feb3347aa5
Implement a ProxyServlet
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
16 import org.apache.http.HttpResponse; |
88feb3347aa5
Implement a ProxyServlet
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
17 import org.apache.http.StatusLine; |
88feb3347aa5
Implement a ProxyServlet
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
18 import org.apache.http.client.HttpClient; |
88feb3347aa5
Implement a ProxyServlet
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
19 import org.apache.http.client.methods.HttpGet; |
88feb3347aa5
Implement a ProxyServlet
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
20 import org.apache.http.impl.client.DefaultHttpClient; |
88feb3347aa5
Implement a ProxyServlet
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
21 |
88feb3347aa5
Implement a ProxyServlet
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
22 import org.apache.log4j.Logger; |
88feb3347aa5
Implement a ProxyServlet
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
23 |
88feb3347aa5
Implement a ProxyServlet
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
24 import de.intevation.flys.client.server.auth.User; |
88feb3347aa5
Implement a ProxyServlet
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
25 |
88feb3347aa5
Implement a ProxyServlet
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
26 /** |
88feb3347aa5
Implement a ProxyServlet
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
27 * Implements a Proxy for HTTP Requests |
88feb3347aa5
Implement a ProxyServlet
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
28 */ |
88feb3347aa5
Implement a ProxyServlet
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
29 public class ProxyServlet |
88feb3347aa5
Implement a ProxyServlet
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
30 extends HttpServlet |
88feb3347aa5
Implement a ProxyServlet
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
31 { |
88feb3347aa5
Implement a ProxyServlet
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
32 private static Logger logger = Logger.getLogger(ProxyServlet.class); |
88feb3347aa5
Implement a ProxyServlet
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
33 private String remoteurl; |
88feb3347aa5
Implement a ProxyServlet
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
34 |
88feb3347aa5
Implement a ProxyServlet
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
35 @Override |
88feb3347aa5
Implement a ProxyServlet
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
36 public void init(ServletConfig config) { |
88feb3347aa5
Implement a ProxyServlet
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
37 this.remoteurl = config.getInitParameter("remoteurl"); |
88feb3347aa5
Implement a ProxyServlet
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
38 } |
88feb3347aa5
Implement a ProxyServlet
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
39 |
88feb3347aa5
Implement a ProxyServlet
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
40 @Override |
88feb3347aa5
Implement a ProxyServlet
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
41 public void doPost(HttpServletRequest req, HttpServletResponse resp) { |
88feb3347aa5
Implement a ProxyServlet
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
42 } |
88feb3347aa5
Implement a ProxyServlet
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
43 |
88feb3347aa5
Implement a ProxyServlet
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
44 @Override |
3498
92239b53823f
ProxyServlet: Use block instead of byte wise copy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3497
diff
changeset
|
45 public void doGet(HttpServletRequest req, HttpServletResponse resp) |
92239b53823f
ProxyServlet: Use block instead of byte wise copy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3497
diff
changeset
|
46 throws IOException { |
3497
88feb3347aa5
Implement a ProxyServlet
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
47 HttpClient httpclient = new DefaultHttpClient(); |
88feb3347aa5
Implement a ProxyServlet
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
48 |
88feb3347aa5
Implement a ProxyServlet
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
49 String requesturi = req.getRequestURI(); |
88feb3347aa5
Implement a ProxyServlet
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
50 String query = req.getQueryString(); |
88feb3347aa5
Implement a ProxyServlet
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
51 |
88feb3347aa5
Implement a ProxyServlet
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
52 HttpGet httpget = new HttpGet(this.remoteurl + "?" + query); |
88feb3347aa5
Implement a ProxyServlet
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
53 |
3498
92239b53823f
ProxyServlet: Use block instead of byte wise copy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3497
diff
changeset
|
54 boolean debug = logger.isDebugEnabled(); |
92239b53823f
ProxyServlet: Use block instead of byte wise copy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3497
diff
changeset
|
55 |
3497
88feb3347aa5
Implement a ProxyServlet
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
56 for (Enumeration e = req.getHeaderNames(); e.hasMoreElements();) { |
88feb3347aa5
Implement a ProxyServlet
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
57 String name = (String)e.nextElement(); |
88feb3347aa5
Implement a ProxyServlet
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
58 for (Enumeration f = req.getHeaders(name); f.hasMoreElements();) { |
88feb3347aa5
Implement a ProxyServlet
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
59 String value = (String)f.nextElement(); |
3498
92239b53823f
ProxyServlet: Use block instead of byte wise copy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3497
diff
changeset
|
60 if (debug) { |
92239b53823f
ProxyServlet: Use block instead of byte wise copy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3497
diff
changeset
|
61 logger.debug( |
92239b53823f
ProxyServlet: Use block instead of byte wise copy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3497
diff
changeset
|
62 "Adding request header " + name + " : " + value); |
92239b53823f
ProxyServlet: Use block instead of byte wise copy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3497
diff
changeset
|
63 } |
3497
88feb3347aa5
Implement a ProxyServlet
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
64 httpget.addHeader(name, value); |
88feb3347aa5
Implement a ProxyServlet
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
65 } |
88feb3347aa5
Implement a ProxyServlet
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
66 } |
88feb3347aa5
Implement a ProxyServlet
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
67 |
88feb3347aa5
Implement a ProxyServlet
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
68 HttpResponse response = httpclient.execute(httpget); |
88feb3347aa5
Implement a ProxyServlet
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
69 |
88feb3347aa5
Implement a ProxyServlet
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
70 StatusLine statusline = response.getStatusLine(); |
3498
92239b53823f
ProxyServlet: Use block instead of byte wise copy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3497
diff
changeset
|
71 if (debug) { |
92239b53823f
ProxyServlet: Use block instead of byte wise copy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3497
diff
changeset
|
72 logger.debug("Response statuscode " + statusline.getStatusCode()); |
92239b53823f
ProxyServlet: Use block instead of byte wise copy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3497
diff
changeset
|
73 } |
3497
88feb3347aa5
Implement a ProxyServlet
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
74 resp.setStatus(statusline.getStatusCode()); |
88feb3347aa5
Implement a ProxyServlet
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
75 |
88feb3347aa5
Implement a ProxyServlet
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
76 Header[] headers = response.getAllHeaders(); |
88feb3347aa5
Implement a ProxyServlet
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
77 for(Header header : headers) { |
3498
92239b53823f
ProxyServlet: Use block instead of byte wise copy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3497
diff
changeset
|
78 if (debug) { |
92239b53823f
ProxyServlet: Use block instead of byte wise copy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3497
diff
changeset
|
79 logger.debug( |
92239b53823f
ProxyServlet: Use block instead of byte wise copy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3497
diff
changeset
|
80 "Adding response header " + header.getName() + |
92239b53823f
ProxyServlet: Use block instead of byte wise copy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3497
diff
changeset
|
81 " : " + header.getValue()); |
92239b53823f
ProxyServlet: Use block instead of byte wise copy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3497
diff
changeset
|
82 } |
3497
88feb3347aa5
Implement a ProxyServlet
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
83 resp.setHeader(header.getName(), header.getValue()); |
88feb3347aa5
Implement a ProxyServlet
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
84 } |
88feb3347aa5
Implement a ProxyServlet
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
85 |
88feb3347aa5
Implement a ProxyServlet
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
86 HttpEntity entity = response.getEntity(); |
88feb3347aa5
Implement a ProxyServlet
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
87 if (entity != null) { |
88feb3347aa5
Implement a ProxyServlet
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
88 InputStream instream = entity.getContent(); |
3498
92239b53823f
ProxyServlet: Use block instead of byte wise copy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3497
diff
changeset
|
89 byte [] buf = new byte[4096]; |
3497
88feb3347aa5
Implement a ProxyServlet
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
90 try { |
88feb3347aa5
Implement a ProxyServlet
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
91 OutputStream outstream = resp.getOutputStream(); |
3498
92239b53823f
ProxyServlet: Use block instead of byte wise copy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3497
diff
changeset
|
92 try { |
92239b53823f
ProxyServlet: Use block instead of byte wise copy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3497
diff
changeset
|
93 int read; |
92239b53823f
ProxyServlet: Use block instead of byte wise copy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3497
diff
changeset
|
94 while ((read = instream.read(buf)) >= 0) { |
92239b53823f
ProxyServlet: Use block instead of byte wise copy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3497
diff
changeset
|
95 outstream.write(buf, 0, read); |
92239b53823f
ProxyServlet: Use block instead of byte wise copy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3497
diff
changeset
|
96 } |
92239b53823f
ProxyServlet: Use block instead of byte wise copy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3497
diff
changeset
|
97 outstream.flush(); |
3497
88feb3347aa5
Implement a ProxyServlet
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
98 } |
3498
92239b53823f
ProxyServlet: Use block instead of byte wise copy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3497
diff
changeset
|
99 finally { |
92239b53823f
ProxyServlet: Use block instead of byte wise copy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3497
diff
changeset
|
100 outstream.close(); |
92239b53823f
ProxyServlet: Use block instead of byte wise copy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3497
diff
changeset
|
101 } |
3497
88feb3347aa5
Implement a ProxyServlet
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
102 } |
88feb3347aa5
Implement a ProxyServlet
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
103 finally { |
88feb3347aa5
Implement a ProxyServlet
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
104 instream.close(); |
88feb3347aa5
Implement a ProxyServlet
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
105 } |
88feb3347aa5
Implement a ProxyServlet
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
106 } |
88feb3347aa5
Implement a ProxyServlet
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
107 } |
88feb3347aa5
Implement a ProxyServlet
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
108 |
88feb3347aa5
Implement a ProxyServlet
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
109 private User getUser(HttpServletRequest req) { |
88feb3347aa5
Implement a ProxyServlet
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
110 HttpSession session = req.getSession(); |
88feb3347aa5
Implement a ProxyServlet
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
111 return (User)session.getAttribute("user"); |
88feb3347aa5
Implement a ProxyServlet
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
112 } |
88feb3347aa5
Implement a ProxyServlet
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
113 } |