携帯用に全角→半角フィルタ書いた
package org.yoshiori.mobile.util; import java.io.ByteArrayOutputStream; import java.io.CharArrayWriter; import java.io.IOException; import java.io.PrintWriter; import java.io.UnsupportedEncodingException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponseWrapper; import net.sf.ehcache.constructs.web.filter.FilterServletOutputStream; import org.apache.commons.lang.StringUtils; import com.ibm.icu.text.Transliterator; public class MobileFilter implements Filter { public static final Transliterator TRANSLITERATOR = Transliterator.getInstance("Fullwidth-Halfwidth"); @Override public void destroy() { } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletResponseWrapper responseWrapper = new HttpServletResponseWrapper((HttpServletResponse) response){ private CharArrayWriter writer = new CharArrayWriter(); private ByteArrayOutputStream out = new ByteArrayOutputStream(); /* (non-Javadoc) * @see javax.servlet.ServletResponseWrapper#getWriter() */ @Override public PrintWriter getWriter() throws IOException { return new PrintWriter(writer); } /* (non-Javadoc) * @see javax.servlet.ServletResponseWrapper#getOutputStream() */ @Override public ServletOutputStream getOutputStream() throws IOException { return new FilterServletOutputStream(out); } /* (non-Javadoc) * @see java.lang.Object#toString() */ @Override public String toString() { String result = writer.toString(); // OutputStream からしか取得出来ない時がある為 if(StringUtils.isEmpty(result)){ try { result = new String(out.toByteArray(),getCharacterEncoding()); } catch (UnsupportedEncodingException e) { } } return result; }}; chain.doFilter(request, responseWrapper); String text = TRANSLITERATOR.transform(responseWrapper.toString()); response.getWriter().write(text); } @Override public void init(FilterConfig filterConfig) throws ServletException { } }
なんか chain 後に Writer に書かれてる時と Stream に書かれる時があるんだけど、
どういう時に違うのか追う時間が無かったので泥臭く処理した><
教えてエロい人!!!
あ、ICU4J の 4 以降が必要です。
あと、記号だろうがタグだろうがコメントだろうが全部おきかえちゃってます。