Chinaunix首页 | 论坛 | 博客
  • 博客访问: 189264
  • 博文数量: 92
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1413
  • 用 户 组: 普通用户
  • 注册时间: 2013-02-04 21:12
文章分类
文章存档

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 
阅读(595) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~