一、分析说明
由于本站(小木人印象网)最近新上线“本地下载”和“体验中心”等功能栏目,前期由于服务器资源紧张,对于未能及时提供给用户体验使用的功能,均会跳转到相应的out-service说明页面,具体代码如下所示
package com.xwood.demo.gw;@b@@b@import java.io.IOException;@b@import java.net.Socket;@b@import java.util.HashMap;@b@import java.util.Map;@b@import java.util.concurrent.ConcurrentHashMap;@b@import java.util.concurrent.ConcurrentMap;@b@import javax.servlet.Filter;@b@import javax.servlet.FilterChain;@b@import javax.servlet.FilterConfig;@b@import javax.servlet.ServletException;@b@import javax.servlet.ServletRequest;@b@import javax.servlet.ServletResponse;@b@import javax.servlet.http.HttpServletRequest;@b@import javax.servlet.http.HttpServletResponse;@b@import org.apache.log4j.Logger;@b@import com.xwood.common.util.InitConfUtils;@b@import com.xwood.demo.util.StrUtil;@b@@b@public class OutServiceWarningFilter implements Filter {@b@ @b@ private final Logger logger = Logger.getLogger(getClass());@b@ @b@ /** id跳转对应关系表,如:p1 -> http://www.xwood.net/*/@b@ private static Map<String,String> domain_cache=new HashMap<String,String>();@b@ @b@ /**体验服务器离线提示页面*/@b@ private static final String error_page="http://www.xwood.net/out-service.html";@b@ @b@ /**下载服务器离线提示页面*/@b@ private static final String error_page_1="http://www.xwood.net/out-service-1.html";@b@ @b@ /**留用提示页面1*/@b@ private static final String error_page_2="http://www.xwood.net/out-service-2.html";@b@ @b@ /**留用提示页面2*/@b@ private static final String error_page_3="http://www.xwood.net/out-service-3.html";@b@ @b@ /**留用提示页面3*/@b@ private static final String error_page_4="http://www.xwood.net/out-service-4.html";@b@ @b@ /**错误提示页对应表*/@b@ private static String[] ep_points={error_page,error_page_1,error_page_2,error_page_3,error_page_4};@b@ @b@ /**调整地址前缀*/@b@ private static final String km_pre="p";@b@ @b@ /**代理关系对应表,如:8080 -> p1 */@b@ private static ConcurrentMap<String,String> access_proxy_cache=new ConcurrentHashMap<String,String>();@b@ @b@ /**端口ip地址对应表 ,如:8080 -> 192.168.1.2 */@b@ private static Map<Integer,String> access_port_cache=new HashMap<Integer,String>();@b@ @b@ static{@b@ String[] domainStrs=new String[]{@b@ InitConfUtils.getParamValue("access_address_1"),@b@ InitConfUtils.getParamValue("access_address_2"),@b@ InitConfUtils.getParamValue("access_address_3"),@b@ InitConfUtils.getParamValue("access_address_4");@b@ @b@ int i=1;@b@ for(String s:domainStrs){@b@ if(s==null||"".equals(s.trim()))@b@ continue;@b@ String proxy_key=String.valueOf(getUrlPort(s));@b@ //端口映射,作为地址调整,先入为主@b@ if(s.lastIndexOf(":")>6&&!access_proxy_cache.containsKey(proxy_key)){@b@ access_proxy_cache.put(proxy_key, km_pre+i);@b@ //如过是数字类型,存入端口字典@b@ if(StrUtil.isNumeric(proxy_key))@b@ access_port_cache.put(Integer.parseInt(proxy_key), getUrlIp(s));@b@ }@b@ domain_cache.put(km_pre+i, s);@b@ i++;@b@ }@b@ @b@ }@b@ @b@ /**@b@ * 返回端口关系字典port->ip,如8080 -》 192.168.1.2@b@ * @return@b@ */@b@ public static Map<Integer,String> getServicePorts(){@b@ return access_port_cache;@b@ }@b@@b@ @Override@b@ public void init(FilterConfig filterConfig) throws ServletException {@b@ }@b@ @b@ /**@b@ * 获取url中的端口@b@ * @param url@b@ * @return@b@ */@b@ public static int getUrlPort(String url){@b@ if(url.lastIndexOf(":")>6){@b@ return Integer.parseInt(url.substring(url.lastIndexOf(":")+1, url.lastIndexOf(":")+5));@b@ }@b@ return 80;@b@ }@b@ @b@ /**@b@ * 获取ip地址@b@ * @param url@b@ * @return@b@ */@b@ public static String getUrlIp(String url){@b@ if(getUrlPort(url)==80){@b@ String _url=url.substring(url.indexOf(":")+3);@b@ return _url.substring(0,_url.indexOf("/"));@b@ }else@b@ return url.substring(url.indexOf(":")+3,url.lastIndexOf(":"));@b@ }@b@ @b@ @Override@b@ public void doFilter(ServletRequest request, ServletResponse response,@b@ FilterChain chain) throws IOException, ServletException {@b@ // TODO Auto-generated method stub@b@ @b@ HttpServletRequest hreq = (HttpServletRequest) request;@b@ HttpServletResponse hres = (HttpServletResponse) response;@b@ @b@ /**跳转接口编号*/@b@ String access_id=hreq.getParameter("sp")==null?(km_pre+"0"):(String)request.getParameter("sp");@b@ Integer error_page_point=0;@b@ @b@ /**异常提示页面编号*/@b@ try { error_page_point=hreq.getParameter("ep")==null?0:Integer.parseInt(request.getParameter("ep")); } catch (Exception e1) {}@b@ @b@ /**替换式跳转url内容,如http://www.xwood.net/1.html、2.html 调整到http://online.xwood.net/1.html、2.html*/@b@ String jumpUrl=hreq.getParameter("jumpUrl")==null?"":(String)hreq.getParameter("jumpUrl");@b@ @b@ if(access_id.indexOf(km_pre)<0){@b@ access_id=access_proxy_cache.get(access_id);@b@ }@b@ String domainUrl=domain_cache.get(access_id);@b@ try {@b@ Socket client = new Socket(getUrlIp(domainUrl), getUrlPort(domainUrl));@b@ client.setSoTimeout(10000);@b@ @b@ if(StrUtil.isEmpty(jumpUrl)){@b@ hres.sendRedirect(domainUrl);@b@ }else{@b@ hres.sendRedirect(domainUrl+jumpUrl);@b@ }@b@ } catch (Exception e) {@b@ hres.sendRedirect(ep_points[error_page_point]);@b@ logger.error(domainUrl+"--------------:"+error_page);@b@ @b@ /**后台回写内容*/@b@ /*PrintWriter out = hres.getWriter();@b@ hres.setContentType("text/html;charset=utf-8"); @b@ out.println("由于体验服务器没有开机,无法完成在线体验,如需紧急需求请邮件至394345319@qq.com"); */ @b@ }@b@@b@ }@b@ @b@ @Override@b@ public void destroy() {@b@ }@b@}