view flys-client/src/main/java/de/intevation/flys/client/server/ProxyServlet.java @ 4794:a7d080347ac3

MINFO: Allow two methods for outlier test in SQ relation. * Methods can be switched as option in conf.xml. * Methods: - Find outliers via multiples of the standard deviation. - Grubbs (used in Fix-Analysis)
author Raimund Renkert <rrenkert@intevation.de>
date Fri, 11 Jan 2013 13:57:38 +0100
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");
    }
}

http://dive4elements.wald.intevation.org