bjoern@3497: package de.intevation.flys.client.server; bjoern@3497: bjoern@3497: import java.io.InputStream; bjoern@3497: import java.io.IOException; bjoern@3497: import java.io.OutputStream; bjoern@3497: import java.util.Enumeration; bjoern@3497: bjoern@3497: import javax.servlet.ServletConfig; bjoern@3497: import javax.servlet.http.HttpServlet; bjoern@3497: import javax.servlet.http.HttpServletRequest; bjoern@3497: import javax.servlet.http.HttpServletResponse; bjoern@3497: import javax.servlet.http.HttpSession; bjoern@3497: bjoern@3497: import org.apache.http.Header; bjoern@3497: import org.apache.http.HttpEntity; bjoern@3497: import org.apache.http.HttpResponse; bjoern@3497: import org.apache.http.StatusLine; bjoern@3497: import org.apache.http.client.HttpClient; bjoern@3497: import org.apache.http.client.methods.HttpGet; bjoern@3497: import org.apache.http.impl.client.DefaultHttpClient; bjoern@3497: bjoern@3497: import org.apache.log4j.Logger; bjoern@3497: bjoern@3497: import de.intevation.flys.client.server.auth.User; bjoern@3497: bjoern@3497: /** bjoern@3497: * Implements a Proxy for HTTP Requests bjoern@3497: */ bjoern@3497: public class ProxyServlet bjoern@3497: extends HttpServlet bjoern@3497: { bjoern@3497: private static Logger logger = Logger.getLogger(ProxyServlet.class); bjoern@3497: private String remoteurl; bjoern@3497: bjoern@3497: @Override bjoern@3497: public void init(ServletConfig config) { bjoern@3497: this.remoteurl = config.getInitParameter("remoteurl"); bjoern@3497: } bjoern@3497: bjoern@3497: @Override bjoern@3497: public void doPost(HttpServletRequest req, HttpServletResponse resp) { bjoern@3497: } bjoern@3497: bjoern@3497: @Override sascha@3498: public void doGet(HttpServletRequest req, HttpServletResponse resp) sascha@3498: throws IOException { bjoern@3497: HttpClient httpclient = new DefaultHttpClient(); bjoern@3497: bjoern@3497: String requesturi = req.getRequestURI(); bjoern@3497: String query = req.getQueryString(); bjoern@3497: bjoern@3497: HttpGet httpget = new HttpGet(this.remoteurl + "?" + query); bjoern@3497: sascha@3498: boolean debug = logger.isDebugEnabled(); sascha@3498: bjoern@3497: for (Enumeration e = req.getHeaderNames(); e.hasMoreElements();) { bjoern@3497: String name = (String)e.nextElement(); bjoern@3497: for (Enumeration f = req.getHeaders(name); f.hasMoreElements();) { bjoern@3497: String value = (String)f.nextElement(); sascha@3498: if (debug) { sascha@3498: logger.debug( sascha@3498: "Adding request header " + name + " : " + value); sascha@3498: } bjoern@3497: httpget.addHeader(name, value); bjoern@3497: } bjoern@3497: } bjoern@3497: bjoern@3497: HttpResponse response = httpclient.execute(httpget); bjoern@3497: bjoern@3497: StatusLine statusline = response.getStatusLine(); sascha@3498: if (debug) { sascha@3498: logger.debug("Response statuscode " + statusline.getStatusCode()); sascha@3498: } bjoern@3497: resp.setStatus(statusline.getStatusCode()); bjoern@3497: bjoern@3497: Header[] headers = response.getAllHeaders(); bjoern@3497: for(Header header : headers) { sascha@3498: if (debug) { sascha@3498: logger.debug( sascha@3498: "Adding response header " + header.getName() + sascha@3498: " : " + header.getValue()); sascha@3498: } bjoern@3497: resp.setHeader(header.getName(), header.getValue()); bjoern@3497: } bjoern@3497: bjoern@3497: HttpEntity entity = response.getEntity(); bjoern@3497: if (entity != null) { bjoern@3497: InputStream instream = entity.getContent(); sascha@3498: byte [] buf = new byte[4096]; bjoern@3497: try { bjoern@3497: OutputStream outstream = resp.getOutputStream(); sascha@3498: try { sascha@3498: int read; sascha@3498: while ((read = instream.read(buf)) >= 0) { sascha@3498: outstream.write(buf, 0, read); sascha@3498: } sascha@3498: outstream.flush(); bjoern@3497: } sascha@3498: finally { sascha@3498: outstream.close(); sascha@3498: } bjoern@3497: } bjoern@3497: finally { bjoern@3497: instream.close(); bjoern@3497: } bjoern@3497: } bjoern@3497: } bjoern@3497: bjoern@3497: private User getUser(HttpServletRequest req) { bjoern@3497: HttpSession session = req.getSession(); bjoern@3497: return (User)session.getAttribute("user"); bjoern@3497: } bjoern@3497: }