changeset 3497:88feb3347aa5

Implement a ProxyServlet Implement a ProxyServlet to be able to restrict the access to the mapserver too. All queries to the provided map services should go throught this new ProxyServlet. Currently the ProxyServlet can only handle HTTP GET requests. flys-client/trunk@5221 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Bjoern Ricks <bjoern.ricks@intevation.de>
date Thu, 16 Aug 2012 14:42:36 +0000
parents f1814efec714
children 92239b53823f
files flys-client/ChangeLog flys-client/src/main/java/de/intevation/flys/client/server/ProxyServlet.java
diffstat 2 files changed, 105 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/flys-client/ChangeLog	Thu Aug 16 08:14:09 2012 +0000
+++ b/flys-client/ChangeLog	Thu Aug 16 14:42:36 2012 +0000
@@ -1,3 +1,11 @@
+2012-08-16	Björn Ricks <bjoern.ricks@intevation.de>
+
+	* src/main/java/de/intevation/flys/client/server/ProxyServlet.java:
+	  Implement a ProxyServlet to be able to restrict the access to the
+	  mapserver too. All queries to the provided map services should go throught
+	  this new ProxyServlet. Currently the ProxyServlet can only handle HTTP GET
+	  requests.
+
 2012-08-16	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
 
 	* src/main/java/de/intevation/flys/client/server/RiverServiceImpl.java:
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/server/ProxyServlet.java	Thu Aug 16 14:42:36 2012 +0000
@@ -0,0 +1,97 @@
+package de.intevation.flys.client.server;
+
+import java.io.BufferedInputStream;
+import java.io.InputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.util.Enumeration;
+
+import javax.servlet.ServletConfig;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.servlet.http.HttpSession;
+
+import org.apache.http.Header;
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpResponse;
+import org.apache.http.StatusLine;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.impl.client.DefaultHttpClient;
+
+import org.apache.log4j.Logger;
+
+import de.intevation.flys.client.server.auth.User;
+
+/**
+ * Implements a Proxy for HTTP Requests
+ */
+public class ProxyServlet
+extends HttpServlet
+{
+    private static Logger logger = Logger.getLogger(ProxyServlet.class);
+    private String remoteurl;
+
+    @Override
+    public void init(ServletConfig config) {
+        this.remoteurl = config.getInitParameter("remoteurl");
+    }
+
+    @Override
+    public void doPost(HttpServletRequest req, HttpServletResponse resp) {
+    }
+
+    @Override
+    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
+        HttpClient httpclient = new DefaultHttpClient();
+
+        String requesturi = req.getRequestURI();
+        String query = req.getQueryString();
+
+        HttpGet httpget = new HttpGet(this.remoteurl + "?" + query);
+
+        for (Enumeration e = req.getHeaderNames(); e.hasMoreElements();) {
+            String name = (String)e.nextElement();
+            for (Enumeration f = req.getHeaders(name); f.hasMoreElements();) {
+                String value = (String)f.nextElement();
+                logger.debug("Adding request header " + name + " : " + value);
+                httpget.addHeader(name, value);
+            }
+        }
+
+        HttpResponse response = httpclient.execute(httpget);
+
+        StatusLine statusline = response.getStatusLine();
+        logger.debug("Response statuscode " + statusline.getStatusCode());
+        resp.setStatus(statusline.getStatusCode());
+
+        Header[] headers = response.getAllHeaders();
+        for(Header header : headers) {
+            logger.debug("Adding response header " + header.getName() + " : " + header.getValue());
+            resp.setHeader(header.getName(), header.getValue());
+        }
+
+        HttpEntity entity = response.getEntity();
+        if (entity != null) {
+            InputStream instream = entity.getContent();
+            BufferedInputStream binstream = new BufferedInputStream(instream);
+            try {
+                OutputStream outstream = resp.getOutputStream();
+                int read;
+                while ( ( read = binstream.read() ) != -1 ) {
+                    outstream.write(read);
+                }
+                outstream.close();
+            }
+            finally {
+                instream.close();
+            }
+        }
+    }
+
+    private User getUser(HttpServletRequest req) {
+            HttpSession session = req.getSession();
+            return (User)session.getAttribute("user");
+    }
+}

http://dive4elements.wald.intevation.org