设为首页 加入收藏

TOP

Java实现OpenLayers跨域代理程序(二)
2014-11-24 01:43:04 来源: 作者: 【 】 浏览:2
Tags:Java 实现 OpenLayers 代理 程序
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();
};

首页 上一页 1 2 3 4 下一页 尾页 2/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇OpenLayer加载WFS图层及C#编写ASP.. 下一篇Android 应用开发者必看的 9 个 T..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: