IP地址重定向

1:重定向
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.Map;
import java.util.regex.Pattern;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
*
*
*/
public class DispatchServlet extends HttpServlet {

/**
*
*/
private static final long serialVersionUID = 1L;

/**
*
*/
private Map unmodifiedDispatchMapping;

/*
* (非 Javadoc)
* @see javax.servlet.GenericServlet#destroy()
*/
public void destroy() {
unmodifiedDispatchMapping= null;
}

/*
* (非 Javadoc)
*
* @see javax.servlet.GenericServlet#init(javax.servlet.ServletConfig)
*/
public void init(ServletConfig config) throws ServletException {
super.init(config);
String configName = config.getInitParameter("dispatchConfigFileName");
try {
InputStream is = Thread.currentThread().getContextClassLoader()
.getResourceAsStream(configName);
BufferedReader br = new BufferedReader(new InputStreamReader(is));
Map dispatchMapping = new java.util.LinkedHashMap();
for (String line = br.readLine(); line != null; line = br
.readLine()) {
if (line.startsWith("#")) {
continue;
}
if (line != null && line.trim().length() > 0) {
line = line.trim();
String[] parts = line.split("=");
if (parts.length != 2) {
debug("Config file " + configName + " error: " + line);
continue;
}
if (parts[0].equalsIgnoreCase("rule") == true) {
// ^10\.70\.*$,http://epcis-report2.paic.com.cn
String[] kv = parts[1].split(",");
if (kv.length != 2) {
debug("Config file " + configName + " error: "
+ line);
continue;
}
try {
kv[0] = kv[0].trim();
kv[1] = kv[1].trim();
Pattern p = Pattern.compile(kv[0]);
if (isURL(kv[1])) {
dispatchMapping.put(p, kv[1]);
} else {
debug("Config file " + configName
+ " has a malformed url: " + line);
}

} catch (Exception ex) {
debug("Config file " + configName + " error: "
+ line);
ex.printStackTrace();
}
}
}
}

debug("初始化配置文件完成,已经加载的重定向配置如下:");

for (Iterator it = dispatchMapping.keySet().iterator(); it
.hasNext();) {
Pattern key = (Pattern) it.next();
debug(key.pattern() + "\t -> \t" + dispatchMapping.get(key));
}
unmodifiedDispatchMapping = java.util.Collections
.unmodifiableMap(dispatchMapping);

} catch (IOException e) {
e.printStackTrace();
}
}

/*
* (非 Javadoc)
*
* @see javax.servlet.http.HttpServlet#service(javax.servlet.ServletRequest,
* javax.servlet.ServletResponse)
*/
public void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException {
String ip = req.getRemoteAddr();
if ( ip== null || ip.trim().length()== 0) {
debug( "请求的IP为空,无法进行重定向,转发到默认的页面.");
((HttpServletRequest) req).getRequestDispatcher("index.html").forward(
req, res);
return;
}
for (Iterator it = this.unmodifiedDispatchMapping.keySet().iterator(); it
.hasNext();) {
Pattern p = (Pattern) it.next();
if (p.matcher(ip).matches()) {
String url = (String) this.unmodifiedDispatchMapping.get(p);
debug("请求的IP为 " + ip + " 在重定向配置表中找到对应的转发关系" + p.pattern()
+ ",将请求重定向到 " + url);
((HttpServletResponse) res).sendRedirect(url);
return;
}
}
debug( "在重定向配置表中找不到对应的映射关系,返回给客户端404状态.");
((HttpServletResponse) res).sendError(404);
return;
}

/**
*
* @param msg
*/
private void debug( String msg) {
getServletContext().log( msg);
System.out.println( "["+ new java.util.Date()+ "][Info]"+ msg);
}


/**
*
* @param str
* @return
*/
private boolean isURL(String str) {
try {
return (str.matches("^http://.*$") || str.matches("^https://.*$"));
} catch (Exception ex) {
ex.printStackTrace();
return false;
}
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值