博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
OSChina 的URL类的源代码重写过程
阅读量:6156 次
发布时间:2019-06-21

本文共 4610 字,大约阅读时间需要 15 分钟。

此代码是 oschina 到手柄形状像 http://www.oschina.net/p/tomcat 这种URL 

此类已经废弃,改用  

标签:

[1].[代码] URLMappingServlet.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package
my.view;
 
import
java.io.*;
import
java.util.*;
 
import
javax.servlet.*;
import
javax.servlet.http.*;
 
import
org.apache.commons.lang.StringUtils;
import
org.apache.commons.logging.Log;
import
org.apache.commons.logging.LogFactory;
 
/**
 
* 用于URL地址的转换
 
* http://www.oschina.net/news/list/1/3 -> {base}/news/list.vm?

p1=1&p2=3

 
* @author liudong
 
*/
public
final
class
URLMappingServlet
extends
HttpServlet {
 
    
private
final
static
Log log = LogFactory.getLog(URLMappingServlet.
class
); 
     
    
public
final
static
String CURRENT_URI =
"current_uri"
;
//{index}
    
public
final
static
String REQUEST_URI =
"request_uri"
;
//{/index}
     
    
private
final
static
String DEFAULT_INDEX_PAGE =
"index.vm"
;
    
private
final
static
String PAGE_EXTENSION =
".vm"
;
    
private
final
static
char
URL_SEPERATOR =
'/'
;
 
    
private
String default_base;
    
private
HashMap<String, String> other_base =
new
HashMap<String, String>();
     
    
private
String rootDomain =
"oschina.net"
;
     
    
@Override
    
@SuppressWarnings
(
"unchecked"
)
    
public
void
init()
throws
ServletException {
        
Enumeration<String> names = getInitParameterNames();
        
while
(names.hasMoreElements()){
            
String name = names.nextElement();
            
String v = getInitParameter(name);
            
if
(
"default"
.equalsIgnoreCase(name)){
                
default_base = v;
                
continue
;
            
}
            
for
(String n : StringUtils.split(name,
','
)){
                
other_base.put(n, v);
            
}
        
}
    
}
 
    
private
String _GetTemplateBase(HttpServletRequest req) {
        
String base =
null
;
        
String prefix = req.getServerName().toLowerCase();
        
base = other_base.get(prefix);
        
if
(base !=
null
)
            
return
base;
        
int
idx = prefix.indexOf(rootDomain);
        
if
(idx >
0
){
            
prefix = prefix.substring(
0
, idx -
1
);
            
base = other_base.get(prefix);
        
}
        
return
(base==
null
)?default_base:base;
    
}
     
    
/**
     
* 运行页面映射过程
     
* @param req
     
* @param res
     
* @throws ServletException
     
* @throws IOException
     
*/
    
protected
void
perform(HttpServletRequest req, HttpServletResponse res)
            
throws
ServletException, IOException {
 
        
StringBuilder show_page =
new
StringBuilder(_GetTemplateBase(req));
        
String prefix = req.getServletPath().substring(
1
);
        
String spath = req.getRequestURI();
        
req.setAttribute(REQUEST_URI, spath);
        
req.setAttribute(CURRENT_URI, prefix);
        
//解析URL地址
        
String[] s_result = spath.substring(
1
).split(String.valueOf(URL_SEPERATOR));
        
if
(s_result.length==
1
){
            
show_page.append(prefix);
            
show_page.append(URL_SEPERATOR);
            
show_page.append(DEFAULT_INDEX_PAGE);
        
}
        
else
{
            
show_page.append(prefix);
            
show_page.append(URL_SEPERATOR);
            
// Ex: http://www.oschina.net/admin/login/ld
            
StringBuilder testPath =
new
StringBuilder(show_page);
            
testPath.append(s_result[
1
]);
            
testPath.append(PAGE_EXTENSION);
            
boolean
isVM = _IsVmExist(testPath.toString());
            
int
param_idx =
1
;
            
if
(isVM){
                
show_page.append(s_result[
1
]);
                
show_page.append(PAGE_EXTENSION);
                
param_idx =
2
;
            
}
            
else
{
                
show_page.append(DEFAULT_INDEX_PAGE);
            
}
            
for
(
int
i=param_idx;i<s_result.length;i++){
                
if
(i==param_idx)
                    
show_page.append(
'?'
);
                
else
                    
show_page.append(
'&'
);
                
show_page.append(
'p'
);
                
show_page.append((i-param_idx+
1
));
                
show_page.append(
'='
);
                
show_page.append(s_result[i]);
            
}
            
testPath.setLength(
0
);
            
testPath =
null
;
        
}
        
if
(log.isDebugEnabled())
            
log.debug(
"request_uri="
+spath+
",servlet_path="
+
                            
req.getServletPath()+
",vm="
+show_page);
        
//运行真实的页面
        
RequestDispatcher rd = getServletContext().getRequestDispatcher(show_page.toString());
        
rd.forward(req, res);  
         
    
}
 
    
private
final
static
List<String> vm_cache =
new
Vector<String>();
     
    
/**
     
* 推断某个页面是否存在。假设存在则缓存此结果
     
* @param path
     
* @return
     
*/
    
private
boolean
_IsVmExist(String path){
        
if
(vm_cache.contains(path))
            
return
true
;
        
File testFile =
new
File(getServletContext().getRealPath(path));
        
boolean
isVM = testFile.exists() && testFile.isFile();
        
if
(isVM)
            
vm_cache.add(path);
        
return
isVM;
    
}
     
    
@Override
    
protected
void
doGet(HttpServletRequest req, HttpServletResponse resp)
            
throws
ServletException, IOException {
        
perform(req, resp);
    
}
 
    
@Override
    
protected
void
doPost(HttpServletRequest req, HttpServletResponse resp)
            
throws
ServletException, IOException {
        
perform(req, resp);
    
}
 
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

你可能感兴趣的文章
阿里云专家穆轩的《杭州九年程序员之“修炼”手册》
查看>>
JQuery:deferred对象的方法
查看>>
eyoucms问答 百度权重是什么
查看>>
win10中遇到qq视频时摄像头打不开没反应的解决方法
查看>>
介绍自己的一个Android插桩热修复框架项目QuickPatch
查看>>
关于textarea的ie9的maxlength不起作用的问题,请参考如下URL解决。
查看>>
勒索病毒GANDCRAB新变种GANDCRAB V5.2新变种来袭 你中招了吗?
查看>>
Solr Facet 查询
查看>>
C++类的继承一
查看>>
数据库分库分表(sharding)系列(五) 一种支持自由规划无须数据迁移和修改路由代码的Sharding扩容方案...
查看>>
巧用VMware Workstation的clone来制作虚拟机模板
查看>>
Spring-Mybatis MapperScannerConfigurer 取不到PropertyPlaceholderConfigurer里的值
查看>>
HP DL380G4服务器前面板指示灯的含义
查看>>
数据结构_树结构
查看>>
常用URL地址
查看>>
每天一个linux命令(19):find 命令概览
查看>>
MySQL kill操作
查看>>
windows下看端口占用
查看>>
Decommissioning a Domain Controller 降域控
查看>>
Character中的奇葩
查看>>