Mercurial > dive4elements > river
view flys-client/src/main/java/de/intevation/flys/client/server/ProxyServlet.java @ 3861:16840d849298
Cosmetics, docs.
flys-client/trunk@5626 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Felix Wolfsteller <felix.wolfsteller@intevation.de> |
---|---|
date | Thu, 27 Sep 2012 17:53:24 +0000 |
parents | 92239b53823f |
children |
line wrap: on
line source
package de.intevation.flys.client.server; 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); boolean debug = logger.isDebugEnabled(); 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(); if (debug) { logger.debug( "Adding request header " + name + " : " + value); } httpget.addHeader(name, value); } } HttpResponse response = httpclient.execute(httpget); StatusLine statusline = response.getStatusLine(); if (debug) { logger.debug("Response statuscode " + statusline.getStatusCode()); } resp.setStatus(statusline.getStatusCode()); Header[] headers = response.getAllHeaders(); for(Header header : headers) { if (debug) { 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(); byte [] buf = new byte[4096]; try { OutputStream outstream = resp.getOutputStream(); try { int read; while ((read = instream.read(buf)) >= 0) { outstream.write(buf, 0, read); } outstream.flush(); } finally { outstream.close(); } } finally { instream.close(); } } } private User getUser(HttpServletRequest req) { HttpSession session = req.getSession(); return (User)session.getAttribute("user"); } }