com.pagecrumb.proxy.ProxyServlet.java Source code

Java tutorial

Introduction

Here is the source code for com.pagecrumb.proxy.ProxyServlet.java

Source

/**
 *    
 * Copyright 2013 Pagecrumb
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *  
 */
package com.pagecrumb.proxy;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HTTP;
import org.esxx.js.protocol.GAEConnectionManager;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.UrlFetchWebConnection;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.pagecrumb.proxy.util.Util;

/**
 *  Main proxy servlet class 
 *  
 *  @author Kerby Martino
 *
 */
public class ProxyServlet extends HttpServlet {

    protected final Log log = LogFactory.getLog(getClass());

    private static final long serialVersionUID = 9L;

    private static final String SCHEME = "http";
    private static final long PUMP_TIME = 5000;
    private static String target = "";

    private String pageString = "<html><body><p>Oops, something went wrong.</p></body></html>"; // Some html to show when things go wrong

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        handleRequest(req, resp, false);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        handleRequest(req, resp, true);
    }

    @SuppressWarnings("unchecked")
    protected void handleRequest(HttpServletRequest req, HttpServletResponse resp, boolean isPost)
            throws ServletException, IOException {

        log.info("contextPath=" + req.getContextPath());

        // Setup the headless browser
        WebClient webClient = new WebClient();

        webClient.setWebConnection(new UrlFetchWebConnection(webClient));

        HttpParams httpParams = new BasicHttpParams();
        httpParams.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 10000); // GAE constraint 

        // Originally, the design was to use HttpClient to execute methods
        // Now, it uses WebClient
        ClientConnectionManager connectionManager = new GAEConnectionManager();
        HttpClient httpclient = new DefaultHttpClient(connectionManager, httpParams);

        StringBuffer sb = new StringBuffer();

        log.info(getClass().toString() + " " + "URI Component=" + req.getRequestURI());
        log.info(getClass().toString() + " " + "URL Component=" + req.getRequestURL());

        if (req.getQueryString() != null) {
            //sb.append("?" + req.getQueryString());

            String s1 = Util.decodeURIComponent(req.getQueryString());
            log.info(getClass().toString() + " " + "QueryString=" + req.getQueryString());

            target = req.getQueryString();
            int port = req.getServerPort();
            String domain = req.getServerName();

            URL url = new URL(SCHEME, domain, port, target);
            /*
            HtmlPage page = webClient.getPage(target);
                
            //gae hack because its single threaded
             webClient.getJavaScriptEngine().pumpEventLoop(PUMP_TIME);
                 
             pageString = page.asXml();
            */
        }

        sb.append(target);

        log.info(getClass().toString() + " " + "sb=" + sb.toString());

        HttpRequestBase targetRequest = null;

        if (isPost) {
            HttpPost post = new HttpPost(sb.toString());

            Enumeration<String> paramNames = req.getParameterNames();

            String paramName = null;

            List<NameValuePair> params = new ArrayList<NameValuePair>();

            while (paramNames.hasMoreElements()) {
                paramName = paramNames.nextElement();
                params.add(new BasicNameValuePair(paramName, req.getParameterValues(paramName)[0]));
            }

            post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
            targetRequest = post;
        } else {
            HttpGet get = new HttpGet(sb.toString());
            targetRequest = get;
        }

        //        log.info("Request Headers");
        //        Enumeration<String> headerNames = req.getHeaderNames();
        //        String headerName = null;
        //        while(headerNames.hasMoreElements()){
        //            headerName = headerNames.nextElement();
        //            targetRequest.addHeader(headerName, req.getHeader(headerName));
        //            log.info(headerName + " : " + req.getHeader(headerName));
        //        }

        HttpResponse targetResponse = httpclient.execute(targetRequest);
        HttpEntity entity = targetResponse.getEntity();

        // Send the Response
        InputStream input = entity.getContent();
        OutputStream output = resp.getOutputStream();

        BufferedReader reader = new BufferedReader(new InputStreamReader(input));
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(output));
        String line = reader.readLine();

        /*
         * To use the HttpClient instead the HtmlUnit's WebClient:
         */
        while (line != null) {
            writer.write(line + "\n");
            line = reader.readLine();
        }

        //        Scanner scanner = new Scanner(pageString);
        //        while (scanner.hasNextLine()) {
        //          String s = scanner.nextLine();
        //          writer.write(s + "\n");
        //        }
        if (webClient != null) {
            webClient.closeAllWindows();
        }

        reader.close();
        writer.close();

        httpclient.getConnectionManager().shutdown();
    }

}