/** * Copyright (C) 2011 JTalks.org Team * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ package org.jtalks.jcommune.web.filters.wrapper; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponseWrapper; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.nio.charset.StandardCharsets; /** * @author Mikhail Stryzhonok */ public class TaggedResponseWrapper extends HttpServletResponseWrapper { private ByteArrayPrintWriter output; public TaggedResponseWrapper(HttpServletResponse response) { super(response); output = new ByteArrayPrintWriter(); } public byte[] getByteArray() { return output.toByteArray(); } public void setByteArray(byte[] bytes) throws IOException { output.setBytes(bytes); } @Override public ServletOutputStream getOutputStream() throws IOException { return output.getStream(); } @Override public PrintWriter getWriter() throws IOException { return output.getWriter(); } public String toString() { return output.toString(); } private static class ByteArrayPrintWriter { private ByteArrayOutputStream baos = new ByteArrayOutputStream(); private PrintWriter pw = new PrintWriter(new OutputStreamWriter(baos, StandardCharsets.UTF_8)); private ServletOutputStream sos = new ByteArrayServletStream(baos); public PrintWriter getWriter() { return pw; } public ServletOutputStream getStream() { return sos; } public byte[] toByteArray() { pw.flush(); return baos.toByteArray(); } public void setBytes(byte[] bytes) throws IOException { baos.reset(); baos.write(bytes); } } private static class ByteArrayServletStream extends ServletOutputStream { private ByteArrayOutputStream baos; ByteArrayServletStream(ByteArrayOutputStream baos) { this.baos = baos; } public void write(int param) throws IOException { baos.write(param); } } }