Filter 起航 编程式配置 压缩响应 日志过滤器
【编程式配置】可以用web.xml配置替换
@WebListener
public class FilterListenerConfigurator implements ServletContextListener{
@Override
public void contextInitialized(ServletContextEvent e) {
ServletContext context = e.getServletContext();
FilterRegistration.Dynamic registration = context.addFilter("requestLogFilter",new RequestLogFilter());
registration.addMappingForUrlPatterns(null,false,"/*"); registration = context.addFilter("compressionFilter",new CompressionFilter());
registration.setAsyncSupported(true);//相当于xml <async-supported>
registration.addMappingForUrlPatterns(null,false,"/*");
}
}
【日志过滤器】
@Override
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain filterChain) throws IOException, ServletException {
Instant time = Instant.now();
StopWatch timer = new StopWatch();
try {
timer.start();
filterChain.doFilter(req, resp);
}finally {
timer.stop();
HttpServletRequest in = (HttpServletRequest) req;
HttpServletResponse out = (HttpServletResponse) resp;
String length = out.getHeader("Content-Length");
if (length == null || length.length()==0){
length = "-";
}
System.out.println(in.getRemoteAddr() + " - - ["+time+"]"+" \""+in.getMethod()+
" "+in.getRequestURI()+" "+in.getProtocol()+"\" "+out.getStatus()+" "+length+
" "+timer);
}
}
【压缩过滤器】
public class CompressionFilter implements Filter
{
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException
{
if(((HttpServletRequest)request).getHeader("Accept-Encoding")
.contains("gzip"))
{
System.out.println("Encoding requested.");
((HttpServletResponse)response).setHeader("Content-Encoding", "gzip");
ResponseWrapper wrapper =
new ResponseWrapper((HttpServletResponse)response);
try
{
chain.doFilter(request, wrapper);
}
finally
{
try {
wrapper.finish();
} catch(Exception e) {
e.printStackTrace();
}
}
}
else
{
System.out.println("Encoding not requested.");
chain.doFilter(request, response);
}
} private static class ResponseWrapper extends HttpServletResponseWrapper
{
private GZIPServletOutputStream outputStream;
private PrintWriter writer; public ResponseWrapper(HttpServletResponse request)
{
super(request);
} @Override
public synchronized ServletOutputStream getOutputStream()
throws IOException
{
if(this.writer != null)
throw new IllegalStateException("getWriter() already called.");
if(this.outputStream == null)
this.outputStream =
new GZIPServletOutputStream(super.getOutputStream());
return this.outputStream;
} @Override
public synchronized PrintWriter getWriter() throws IOException
{
if(this.writer == null && this.outputStream != null)
throw new IllegalStateException(
"getOutputStream() already called.");
if(this.writer == null)
{
this.outputStream =
new GZIPServletOutputStream(super.getOutputStream());
this.writer = new PrintWriter(new OutputStreamWriter(
this.outputStream, this.getCharacterEncoding()
));
}
return this.writer;
} @Override
public void flushBuffer() throws IOException
{
if(this.writer != null)
this.writer.flush();
else if(this.outputStream != null)
this.outputStream.flush();
super.flushBuffer();
} @Override
public void setContentLength(int length) { } @Override
public void setContentLengthLong(long length) { } @Override
public void setHeader(String name, String value)
{
if(!"content-length".equalsIgnoreCase(name))
super.setHeader(name, value);
} @Override
public void addHeader(String name, String value)
{
if(!"content-length".equalsIgnoreCase(name))
super.setHeader(name, value);
} @Override
public void setIntHeader(String name, int value)
{
if(!"content-length".equalsIgnoreCase(name))
super.setIntHeader(name, value);
} @Override
public void addIntHeader(String name, int value)
{
if(!"content-length".equalsIgnoreCase(name))
super.setIntHeader(name, value);
} public void finish() throws IOException
{
if(this.writer != null)
this.writer.close();
else if(this.outputStream != null)
this.outputStream.finish();
}
} private static class GZIPServletOutputStream extends ServletOutputStream
{
private final ServletOutputStream servletOutputStream;
private final GZIPOutputStream gzipStream; public GZIPServletOutputStream(ServletOutputStream servletOutputStream)
throws IOException
{
this.servletOutputStream = servletOutputStream;
this.gzipStream = new GZIPOutputStream(servletOutputStream);
} @Override
public boolean isReady()
{
return this.servletOutputStream.isReady();
} @Override
public void setWriteListener(WriteListener writeListener)
{
this.servletOutputStream.setWriteListener(writeListener);
} @Override
public void write(int b) throws IOException
{
this.gzipStream.write(b);
} @Override
public void close() throws IOException
{
this.gzipStream.close();
} @Override
public void flush() throws IOException
{
this.gzipStream.flush();
} public void finish() throws IOException
{
this.gzipStream.finish();
}
}
}
Filter 起航 编程式配置 压缩响应 日志过滤器的更多相关文章
- Spring4.0编程式定时任务配置
看过很多定时调度的配置,大多使用XML配置,觉得比较麻烦,也比较老套.这里介绍一种基于spring4.0注解编程式配置定时任务,简单清晰,使用方便.. 至于引入spring相关jar这里不多说,直接切 ...
- 事务之三:编程式事务、声明式事务(XML配置事务、注解实现事务)
Spring2.0框架的事务处理有两大类: JdbcTemplate操作采用的是JDBC默认的AutoCommit模式,也就是说我们还无法保证数据操作的原子性(要么全部生效,要么全部无效),如: Jd ...
- SpringMVC 解析(四)编程式路由
多数情况下,我们在使用Spring的Controller时,会使用@RequestMapping的形式把请求按照URL路由到指定方法上.Spring还提供了一种编程的方式去实现请求和路由方法之间的路由 ...
- spring 事物(二)—— 编程式事物实现与扩展
简介 使用TransactionTemplate 不需要显式地开始事务,甚至不需要显式地提交事务.这些步骤都由模板完成.但出现异常时,应通过TransactionStatus 的setRollback ...
- 全面分析 Spring 的编程式事务管理及声明式事务管理
开始之前 关于本教程 本教程将深入讲解 Spring 简单而强大的事务管理功能,包括编程式事务和声明式事务.通过对本教程的学习,您将能够理解 Spring 事务管理的本质,并灵活运用之. 先决条件 本 ...
- Spring学习8-Spring事务管理(编程式事务管理)
一.Spring事务的相关知识 1.事务是指一系列独立的操作,但在概念上具有原子性. 比如转账:A账号-100, B账号+100,完成.这两个操作独立是没问题的. 但在逻辑上,要么全部完成,要么一 ...
- spring 编程式事务管理和声明式事务管理
编程式事务管理 Spring 的编程式事务管理概述 在 Spring 出现以前,编程式事务管理对基于 POJO 的应用来说是唯一选择.用过 Hibernate 的人都知道,我们需要在代码中显式调用be ...
- 【spring 6】Spring和Hibernate的整合:编程式事务
一.编程式事务简介 在 Spring 出现以前,编程式事务管理对基于 POJO 的应用来说是唯一选择.用过 Hibernate 的人都知道,我们需要在代码中显式调用beginTransaction() ...
- 全面分析 Spring 的编程式事务管理及声明式事务管理--转
开始之前 关于本教程 本教程将深入讲解 Spring 简单而强大的事务管理功能,包括编程式事务和声明式事务.通过对本教程的学习,您将能够理解 Spring 事务管理的本质,并灵活运用之. 先决条件 本 ...
随机推荐
- Jira配置openLdap服务器进行用户认证
测试环境 注:进过测试,Jira6.3.6和Jira7.3.8界面和配置方法相同,不过7.3.x版本默认的用户组只有jira-software-users和jira-administrators,好在 ...
- 使用zabbix监控mariadb性能状态
0x01 前言 zabbix内置Mysql的监控模版,因为mariadb和Mysql两者的相关性,所以这个模版也能用在mariadb services上. 0x02 Mysql 首先要在mariadb ...
- webpack入门(六) API in modules
A quick summary of all methods and variables available in code compiled with webpack. 用webpack编译的一些变 ...
- hbuilder 个推 问题记录
截止版本日期: 2017/09/19 一,安卓没有角标问题: Android - 2.2+ (支持): 目前仅支持小米(MIUI v5),其它设备调用后无任何效果.官方文档 二,ios角标混乱问题: ...
- Django(十七)文件上传
http://www.cnblogs.com/wupeiqi/articles/5703697.html - 文件上传 - 普通上传 - 自定义页面上传按钮 ...
- 为什么 管理工具里没有Internet(IIS)管理器选项
如上图,localhost页能打开了,但是管理工具里没有iis管理器,主要原因是安装iis时候没有选择web管理工具,选取安装上就 有了
- Nginx上部署HTTPS + HTTP2
Nginx上部署HTTPS依赖OpenSSL库和包含文件,即须先安装好libssl-dev(或者OpenSSL),且ln -s /usr/lib/x86_64-linux-gnu/libssl.so ...
- mysql优化好文
https://segmentfault.com/a/1190000006158186
- MySQL mysqldump 导入/导出 结构&数据&存储过程&函数&事件&触发器
———————————————-库操作———————————————-1.①导出一个库结构 mysqldump -d dbname -u root -p > xxx.sql ②导出多个库结构 m ...
- libmysqlclient.so.18: cannot open shared object file
libmysqlclient.so.18: cannot open shared object file 解决libmysqlclient.so.18: cannot open shared obje ...