Java实现OpenLayers跨域代理程序(二)

2014-11-24 01:43:04 · 作者: · 浏览: 13
a.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
* This is a transparent HTTP proxy written in Java that is similar to the proxy in
* the OpenLayers examples, which is written in Python. These proxies are used
* to circumvent browser restrictions on cross-domain requests with java script.
*


*


* To use the proxy you need to 1) configure the proxy servlet in your web.xml
* and 2) use OpenLayers.setProxyHost to set the url-path to the proxy. If the
* proxy is configured to listen to the url-pattern '/gwtOpenLayersProxy/*' then
* the proxy host should be set to 'gwtOpenLayersProxy targetURL='.
*


* Initial code for this proxy is based upon * "the">http://trac.openlayers.org/changeset/8099/sandbox format=diff&new=8099">the
* following code

* see also * "http://java.sun.com/docs/books/tutorial/networking/urls/readingWriting.html"
* title="this networking tutorial">this networking tutorial
*


*/
@SuppressWarnings("serial")
public class GwtOpenLayersProxyServlet extends HttpServlet {


protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
processRequest(request,response);
}


protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
processRequest(request, response);
}


private void processRequest(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {


HttpURLConnection connection = null;
InputStream istream = null; //input to proxy
OutputStream ostream = null; //output from proxy
InputStream connectionIstream = null; //output for the target is input for the connection
OutputStream connectionOstream = null; //input for the target is output for the connection


String remoteHost = request.getRemoteHost(); // get host address of client - for checking allowedHosts
boolean allowedHost = isAllowedHost(remoteHost); //The allowedHosts are the hosts that are allowed to use the Open Proxy.


try {
// easy way to ignore case of param
if(request.getParameter("targetURL") != null && request.getParameter("targetURL") != "" && allowedHost) {


// HTTPUrlConnection looks at http.proxyHost and http.proxyPort system properties.
// Make sure these properties are set these if you are behind a proxy.


//step 1: initialize
String requestMethod = request.getMethod();


URL targetURL = new URL(request.getParameter("targetURL"));
connection = (HttpURLConnection) targetURL.openConnection();
connection.setRequestMethod(requestMethod);
transferHTTPRequestHeaders(connection, request);


//step 2: proxy requests
if (requestMethod.equals("GET")){
//default for setDoInput is true
connectionIstream = connection.getInputStream();
};