2013年(92)
分类: 信息化
2013-02-28 19:32:17
[代码] [Java]代码 package org.apple.util.urlrewrite; import java.io.File; import java.util.ArrayList; import java.util.Iterator; import javax.servlet.http.HttpServletRequest; import javax.servlet ; .http.HttpServletResponse; import org.apple.util.log.CommonsLogging; import org.apple.util.log.Constants; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.SAXReader; /** * *@author 曹正辉 QQ:330937205 SimpleOnline 开发团队 * */ public class UrlRewriter { private Rule[] rules; private static CommonsLogging logging = new CommonsLogging(UrlRewriter.class); public UrlRewriter(String configURLPath) { ArrayList roleUrlList = getAllRileList(configURLPath); int size = roleUrlList.size(); rules = new Rule[size]; if (size != 0) { for (int i = 0; i < size; i ) { rules[i] = roleUrlList.get(i); } } } // 获取规矩 @SuppressWarnings("unchecked") public ArrayList getAllRileList(String filePath) { ArrayList roleUrlList = null; try { SAXReader reader = new SAXReader(); Document document = reader.read(new File(filePath)); // urlrewrite Element root = document.getRootElement(); roleUrlList = new ArrayList(); for (Iterator iterator = root.elementIterator(); iterator.hasNext();) { Element element = (Element) iterator.next(); Rule rule = new Rule(); rule.setFrom(element.elementText("from")); rule.setTo(element.elementText("to")); if (Constants.devMode) { logging.info(rule.toString()); } roleUrlList.add(rule); } } catch (DocumentException e) { logging.error(e); } return roleUrlList; } public boolean dealWithUrl(HttpServletRequest request, HttpServletResponse response) throws Exception { String url = request.getServletPath(); String url_copy = url; if (rules.length != 0) { int rulesLength = rules.length; Boolean isHandler = false; for (int i = 0; i < rulesLength; i ) { url = rules[i].dealWithUrl(url, isHandler); if (url != null