package com.webportals.commons.uri; import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.dom4j.Attribute; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.SAXReader; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; public class UrlWriterFactory { private Log logger = LogFactory.getLog(UrlWriterFactory.class); private static final String FORWARD = "forward"; private static final String REDIRECT = "redirect"; private static final String DEFAULT_CONFIG_FILE_NAME = "urlrewrite.xml"; private static String urlWriterConfigName; /** * 版本号 */ private Long version = System.currentTimeMillis(); private static List<UrlWriter> urlPatternList = new ArrayList<UrlWriter>(); public void init() { if (StringUtils.isBlank(urlWriterConfigName)) { urlWriterConfigName = DEFAULT_CONFIG_FILE_NAME; } InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(urlWriterConfigName); if (null == inputStream) { throw new RuntimeException("没有配置urlrewrite.xml"); } urlPatternList = initUrlWriter(inputStream); } @SuppressWarnings("unchecked") private List<UrlWriter> initUrlWriter(InputStream inputStream) { List<UrlWriter> urlPatternList = new ArrayList<UrlWriter>(); SAXReader reader = new SAXReader(); try { Document doc = reader.read(inputStream); List<Element> nodes = doc.selectNodes("urlrewrite/rule"); for (Element elm : nodes) { UrlWriter urlMapping = new UrlWriter(); String from = elm.elementText("from"); from = StringUtils.isBlank(from) ? "" : from; urlMapping.setFrom(from); String to = elm.elementText("to"); to = StringUtils.isBlank(to) ? "" : to; urlMapping.setTo(to); Attribute attribute = elm.element("to").attribute("type"); String type = FORWARD; if (null != attribute) { if (StringUtils.isBlank(attribute.getValue())) { type = attribute.getValue(); } } urlMapping.setType(type); urlPatternList.add(urlMapping); } return urlPatternList; } catch (DocumentException e) { logger.error("解析url.xml时发现异常:" + e); } return urlPatternList; } public static UrlResult getUrlResult(String url) { if (StringUtils.isEmpty(url)) { return null; } url = filterUrlStyle(url); UrlResult urlResult = new UrlResult(false, ""); for (UrlWriter urlMapping : urlPatternList) { Pattern pattern = Pattern.compile(urlMapping.getFrom()); Matcher matcher = pattern.matcher(url); if (matcher.find()) { urlResult.setUrl(matcher.replaceAll(urlMapping.getTo())); if (urlMapping.getType().equals(REDIRECT)) { urlResult.setFlag(true); } return urlResult; } } return urlResult; } public static class UrlResult { private boolean flag = false; private String url = ""; public UrlResult(boolean flag, String url) { super(); this.flag = flag; this.url = url; } public boolean isFlag() { return flag; } public void setFlag(boolean flag) { this.flag = flag; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } } public String getUrlWriterConfigName() { return urlWriterConfigName; } public void setUrlWriterConfigName(String urlWriterConfigName) { UrlWriterFactory.urlWriterConfigName = urlWriterConfigName; } public Long getVersion() { return version; } public void setVersion(Long version) { this.version = version; } private static String filterUrlStyle(String url) { if (url.indexOf("://") != -1) { url = url.substring(url.indexOf("://") + 3); } if (url.length() > 1 && url.endsWith("/")) { url = url.substring(0, url.length() - 1); } return url; } }
最近下载更多
hawear LV1
2019年1月14日
mim哒哒哒 LV1
2018年9月3日
chengqiang LV13
2018年5月27日
z_kkkkk LV1
2017年9月15日
czczicheng LV6
2017年9月14日
qijian0503 LV5
2017年3月1日
msl1010895047 LV12
2016年12月20日
yp_zhou LV6
2016年7月12日
dengjianbin LV2
2016年5月9日
a4w LV1
2015年8月10日