订阅所有JSP/Servlet的日志 订阅 | 这是最新一篇日志 上一篇 | 下一篇日志 下一篇 ]
Resin

让Resin HttpProxyServlet支持多域名

一般应用中有ajax进行跨子域代理调用时,通常需要设置HTTP代理,解决域的权限问题。
举例,blog.chinajavaworld.com中用ajax调search子系统的服务,Resin的HttpProxyServlet设置如下:
1
2
3
4
5
6
7
<servlet>
  <servlet-name>http-proxy</servlet-name>
  <servlet-class>com.caucho.servlets.HttpProxyServlet</servlet-class>
  <init host='search.chinajavaworld.com:80' />
</servlet> 
 
<servlet-mapping url-pattern="/ajax/search/*" servlet-name="http-proxy" />


在search.chinajavaworld.com/ajax/search/test.jsp中
1
<%out.print(request.getHeader("Host"));%>

现在访问:
http://blog.chinajavaworld.com/ajax/search/test.jsp,输出:
1
search.chinajavaworld.com


当应用要支持多域名访问时,问题来了:


如用 http://blog.chinajavaworld-bbb.com/ajax/search/test.jsp 进行访问,输出也是:

1
search.chinajavaworld.com

如果设置两个host
1
2
3
4
5
6
<servlet>
  <servlet-name>http-proxy</servlet-name>
  <servlet-class>com.caucho.servlets.HttpProxyServlet</servlet-class>
  <init host='search.chinajavaworld.com:80' />
  <init host='search.chinajavaworld-bbb.com:80' />
</servlet> 

则用两个域名访问的话,都将输出:
1
search.chinajavaworld-bbb.com


现在的需求是,用哪个主域名访问的,就必须返回与该主域名一致的子域

解决方法:修改 HttpProxyServlet 源码:
TcpPool.java加入以下方法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
  public Server nextServer(String mainHost) {
Server bestServer = null;
synchronized (this) {
String _URL = null;
for (int i = 0; i < _servers.length; i++) {
Server server = _servers[i];
if (server.getURL().indexOf(mainHost) != -1) {
//System.out.println("ok,found host.return server.");
bestServer = server;
break;
}
if (i == _servers.length - 1)
  _URL = server.getURL();
}
if (bestServer == null) {
//System.out.println(mainHost+" is not in server list. add now.");
String oldMainHost = getMainDomain(_URL);
_URL = _URL.replace(oldMainHost, mainHost);
String _host = getDomain(_URL);
bestServer = new Server(_host);
_hosts.add(_host);
Server[] servers = new Server[_servers.length + 1]; 
System.arraycopy(_servers, 0, servers, 0, _servers.length);
servers[_servers.length] = bestServer;
_servers = servers;
}
return bestServer;
}
}


HttpProxyServlet中加入:

String host = req.getHeader("Host");
String mainHost = TcpPool.getMainDomain(host);


然后将

TcpPool.Server server = _tcpPool.nextServer();

替换为

TcpPool.Server server = _tcpPool.nextServer(mainHost);

平均得分
(0 次评分)





文章来自: 本站原创
标签: Resin 多域名 HttpProxyServlet 
评论: 38 | 查看次数: 4610
  • 共有 38 条评论
  • 1
  • 2
  • 3
  • |
  • >>
yanlink [2010-07-31 14:17:55]
yanlink [2010-06-18 15:05:08]
flywatch [2010-06-11 10:14:44]
xiaoxue00 [2010-06-04 12:10:47]
profiter à bon marché gold wow dans GameSavor!
power leveling sont Voile!
wow lvl 70 sont bon marché et vite!
uggbootscheap [2010-05-06 19:02:24]
I do not know who wrote those nike running shoes words, but I have always liked them as a reminder that the future can be anything we want to make nike air max 95 it. We can take the mysterious, hazy future and carve out of it anything that we can nike 97 air max imagine, just like a sculptor carves a statue from a shapeless stone. We are all in the position of the farmer. If we plant a good nike 360 running shoes seed, we reap a good harvest. If our seed is poor and full of weeds, we reap a useless crop. If we plant nothing timberland shoes store at all, we harvest nothing at all. I want the future to be better than the past. I don't want it contaminated by the mistakes and cheap timberland boots errors with which history is filled. We should all be concerned about the future because that is where we will spend the reminder of our nike puma shoes lives. 06 May LH
uggbootscheap [2010-04-30 19:53:43]
With friendship, mbt shoes review life is happy and harmonious. Without friendship, life is hostile and unfortunate. I have friends in the rank and mbt chapa azul file. Some are rich and in power. Some are low and common. Some are like myself, working as a teacher, reading and writing and content with the simple mbt sport black life we have. To many of my friends, I know what to treasure, what to tolerate and what to share mbt sport white , I will never forget my old friends and keep making new friends. I will not he cold and indifferent to the poor friends and will show mbt lami black concern for them, even if it is only a comforting mbt walk black word.As life is full of strife and conflict, we need friends to support and help as out of difficulties. Our frinods give us warnings against danger. True friends share not only joy but, more often than not, they share mbt m walk silver sorrow. 30 April LH
songjlvshi [2010-03-29 10:17:29]
beeflee [2009-09-15 17:11:21]
beeflee [2009-08-25 14:45:04]
beeflee [2009-08-24 17:50:04]
beeflee [2009-08-17 17:06:52]
beeflee [2009-07-31 12:57:34]
beeflee [2009-06-09 12:04:57]
beeflee [2009-05-06 13:20:27]
pailiko001 [2009-03-12 09:22:18]
  • 共有 38 条评论
  • 1
  • 2
  • 3
  • |
  • >>
发表评论
昵 称:  登录
内 容:
选 项:
字数限制 1000 字 | UBB代码 开启 | [img]标签 开启