框架对静态资源的扩展

为了提高美工的工作效率,提高网站运行效率,将静态资源和代码进行了分离。结合组件化,资源就可能存在于JAR包中、主工程和资源工程三处。为了确保各处能够正确识别资源,做了一个静态资源过滤器:

 

public class StaticResourceFilter implements Filter {
	public void doFilter(ServletRequest req, ServletResponse res,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest)req;
		String strServletPath = request.getServletPath();
		
		if(!canSolve(request, strServletPath)) {
			byte[] bBuf = getBuffer(request, strServletPath);

			if(bBuf!=null && bBuf.length>0) {
				res.getOutputStream().write(bBuf);
				res.flushBuffer();
			} else {
				HttpServletResponse response = (HttpServletResponse)res;
				response.sendRedirect(ApplicationParameter.RESOURCE_PATH + strServletPath);
			}
		} else {
			chain.doFilter(req, res);
		}
	}

	public void init(FilterConfig filterConfig) throws ServletException {
		
	}
	
	public void destroy() {

	}
	
	protected boolean canSolve(HttpServletRequest request, String strServletPath) {
		if(strArrIgnoreServletPath != null) {
			for(String strIgnoreServletPath : strArrIgnoreServletPath) {
				if(strIgnoreServletPath.equalsIgnoreCase(strServletPath)) {
					return true;
				}
			}
		}
		
		@SuppressWarnings("deprecation")
		String strRealPath = request.getRealPath(strServletPath);
		return new File(strRealPath).exists();
	}
	
	protected byte[] getBuffer(HttpServletRequest request, String strServletPath) throws IOException {
		byte[] bArrRet = (byte[])mapCache.get(strServletPath);
		
		if(bArrRet == null) {
			File file = getFileFromOverridePath(request, strServletPath);
			
			if(file == null) {
				file = getFileFromClassPath(request, strServletPath);
			}

			if(file != null) {
				bArrRet = new byte[(int)file.length()];
				FileInputStream fis = new FileInputStream(file);
				fis.read(bArrRet);
				fis.close();
			} else {
				InputStream fis = getFileFromJar(strServletPath);
				
				if(fis != null) {
					bArrRet = new byte[10240];
					int iPos = 0, iCount = 0;
					
					while ((iCount = fis.read(bArrRet, iPos, bArrRet.length-iPos)) != -1) {
						iPos += iCount;
						
						if(iPos >= bArrRet.length) {
							bArrRet = Arrays.copyOf(bArrRet, bArrRet.length+10240);
						}
					}
					
					fis.close();
					bArrRet = Arrays.copyOf(bArrRet, iPos);
				} else {
					bArrRet = new byte[0];
				}
			}
			
			mapCache.put(strServletPath, bArrRet);
		}
		
		return bArrRet!=null&&bArrRet.length>0 ? bArrRet : null;
	}
	
	protected File getFileFromOverridePath(HttpServletRequest request, String strServletPath) throws IOException {
		String strRelativePath = strServletPath.toLowerCase().startsWith(strBasePathLocation.toLowerCase())
				? strServletPath.substring(strBasePathLocation.length()) : strServletPath;
		String strOverridePath = strBasePathLocation + strOverridePathPrefix
				+ (strRelativePath.charAt(0)=='/' ? strRelativePath.substring(1) : strRelativePath);
		
		File file = new File(request.getRealPath(strOverridePath));
		return file.exists() ? file : null;
	}
	
	protected File getFileFromClassPath(HttpServletRequest request, String strServletPath) throws IOException {
		String strClassPath = strBasePathLocation + strClassPathPrefix
				+ (strServletPath.charAt(0)=='/' ? strServletPath.substring(1) : strServletPath);
		
		File file = new File(request.getRealPath(strClassPath));
		return file.exists() ? file : null;
	}
	
	protected InputStream getFileFromJar(String strServletPath) throws IOException {
		ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
		String pattern = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + strServletPath;
		Resource[] resources = resourcePatternResolver.getResources(pattern);

		if(resources!=null && resources.length>0) {
			return resources[0].getInputStream();
		}
		
		return null;
	}
	
	private static final Map<String, Object> mapCache = new Hashtable<String, Object>();
	
	private static final String[] strArrIgnoreServletPath = new String[] {
		"/struts/utils.js",
	};
	
	private static final String strOverridePathPrefix = "static-override/";
	private static final String strBasePathLocation = "/WEB-INF/";
	private static final String strClassPathPrefix = "classes/";
}


比较简单,细节就不多说了。web.xml中的相关配置:

 

 

	<!-- 静态资源处理 -->
	<filter> 
		<filter-name>staticResFilter</filter-name>
		<filter-class>com.ekingstar.framework.util.StaticResourceFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>staticResFilter</filter-name>
		<url-pattern>*.js</url-pattern>
	</filter-mapping>
	<filter-mapping>
		<filter-name>staticResFilter</filter-name>
		<url-pattern>*.css</url-pattern>
	</filter-mapping>
	<filter-mapping>
		<filter-name>staticResFilter</filter-name>
		<url-pattern>*.json</url-pattern>
	</filter-mapping>
	<filter-mapping>
		<filter-name>staticResFilter</filter-name>
		<url-pattern>*.jpg</url-pattern>
	</filter-mapping>
	<filter-mapping>
		<filter-name>staticResFilter</filter-name>
		<url-pattern>*.png</url-pattern>
	</filter-mapping>
	<filter-mapping>
		<filter-name>staticResFilter</filter-name>
		<url-pattern>*.gif</url-pattern>
	</filter-mapping>

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值