Вот мой фильтр приложения JavaEE # код doFilter
public void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
throws IOException, ServletException {
GZIPServletResponseWrapper gzipResponse = new GZIPServletResponseWrapper(response);
filterChain.doFilter(request, gzipResponse);
gzipResponse.close();
}
Вот код GZIPServletResponseWrapper
public class GZIPServletResponseWrapper extends HttpServletResponseWrapper {
private GZIPOutputStreamWrapper gzipOutputStream;
private PrintWriter printWriter;
public GZIPServletResponseWrapper(HttpServletResponse response) {
super(response);
}
public void close() throws IOException {
if (this.printWriter != null) {
this.printWriter.close();
}
if (this.gzipOutputStream != null) {
this.gzipOutputStream.close();
}
}
@Override
public void flushBuffer() throws IOException {
if (this.printWriter != null) {
this.printWriter.flush();
}
if (this.gzipOutputStream != null) {
this.gzipOutputStream.flush();
}
super.flushBuffer();
}
@Override
public ServletOutputStream getOutputStream() throws IOException {
if (printWriter != null) {
throw new IllegalStateException(GET_OUTPUT_STREAM_USED);
}
if (this.gzipOutputStream == null) {
this.gzipOutputStream = new GZIPOutputStreamWrapper(getResponse().getOutputStream(),
(HttpServletResponse) getResponse());
}
return this.gzipOutputStream;
}
@Override
public PrintWriter getWriter() throws IOException {
if (gzipOutputStream != null) {
throw new IllegalStateException(GET_WRITE_USED);
}
if (this.printWriter == null) {
this.gzipOutputStream = new GZIPOutputStreamWrapper(getResponse().getOutputStream(),
(HttpServletResponse) getResponse());
this.printWriter = new PrintWriter(new OutputStreamWriter(
this.gzipOutputStream, getResponse().getCharacterEncoding()));
}
return this.printWriter;
}
}
Вот код GZIPOutputStreamWrapper
public class GZIPOutputStreamWrapper extends ServletOutputStream {
private static final String CONTENT_ENCODING = "Content-Encoding";
private static final String TEXT = "text";
private GZIPOutputStream gzipOutputStream;
private HttpServletResponse response;
public GZIPOutputStreamWrapper(ServletOutputStream servletOutputStream, HttpServletResponse response)
throws IOException {
super();
this.gzipOutputStream = new GZIPOutputStream(servletOutputStream);
this.response = response;
}
@Override
public void write(int b) throws IOException {
this.gzipOutputStream.write(b);
}
@Override
public void write(byte[] b) throws IOException {
this.gzipOutputStream.write(b);
}
@Override
public void write(byte[] b, int off, int len) throws IOException {
if (isTextFile(response)) {
response.addHeader(CONTENT_ENCODING, GZIP);
this.gzipOutputStream.write(b, off, len);
} else {
response.getOutputStream().write(b, off, len);
}
}
private boolean isTextFile(HttpServletResponse response) {
String contentType = response.getContentType();
return contentType != null && contentType.contains(TEXT);
}
@Override
public void flush() throws IOException {
this.gzipOutputStream.flush();
}
@Override
public void close() throws IOException {
this.gzipOutputStream.close();
}
@Override
public boolean isReady() {
return false;
}
@Override
public void setWriteListener(WriteListener writeListener) {
}
}
Так в чем же задача. Мне нужно сжимать текстовые файлы в gzip в фильтре и передавать их дальше. Но он пишет мои файлы js как это
Что это может произойти? Он пишет файлы правильно с gzipOutputStream, но не с response.getOutputStream ()