|
| 1 | +package org.tron.core.services.filter; |
| 2 | + |
| 3 | +import java.io.ByteArrayOutputStream; |
| 4 | +import java.io.IOException; |
| 5 | +import java.io.OutputStreamWriter; |
| 6 | +import java.io.PrintWriter; |
| 7 | +import java.nio.charset.StandardCharsets; |
| 8 | +import javax.servlet.ServletOutputStream; |
| 9 | +import javax.servlet.WriteListener; |
| 10 | +import javax.servlet.http.HttpServletResponse; |
| 11 | +import javax.servlet.http.HttpServletResponseWrapper; |
| 12 | +import lombok.Getter; |
| 13 | + |
| 14 | +/** |
| 15 | + * Buffers the response body without writing to the underlying response, |
| 16 | + * so the caller can replay it after the handler returns. |
| 17 | + * |
| 18 | + * <p>If {@code maxBytes > 0} and the response would exceed that limit, the |
| 19 | + * {@link #isOverflow()} flag is set instead of throwing. The caller should check this flag after |
| 20 | + * the handler returns and write its own error response when true. |
| 21 | + * |
| 22 | + * <p>Header-mutating methods ({@code setStatus}, {@code setContentType}) are buffered here and |
| 23 | + * only forwarded to the real response via {@link #commitToResponse()}. |
| 24 | + */ |
| 25 | +public class BufferedResponseWrapper extends HttpServletResponseWrapper { |
| 26 | + |
| 27 | + private final HttpServletResponse actual; |
| 28 | + private final ByteArrayOutputStream buffer = new ByteArrayOutputStream(); |
| 29 | + private final int maxBytes; |
| 30 | + private int status = HttpServletResponse.SC_OK; |
| 31 | + private String contentType; |
| 32 | + private boolean committed = false; |
| 33 | + @Getter |
| 34 | + private volatile boolean overflow = false; |
| 35 | + |
| 36 | + private final ServletOutputStream outputStream = new ServletOutputStream() { |
| 37 | + @Override |
| 38 | + public void write(int b) { |
| 39 | + if (overflow) { |
| 40 | + return; |
| 41 | + } |
| 42 | + if (maxBytes > 0 && buffer.size() >= maxBytes) { |
| 43 | + markOverflow(); |
| 44 | + return; |
| 45 | + } |
| 46 | + buffer.write(b); |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public void write(byte[] b, int off, int len) { |
| 51 | + if (overflow) { |
| 52 | + return; |
| 53 | + } |
| 54 | + if (maxBytes > 0 && buffer.size() + len > maxBytes) { |
| 55 | + markOverflow(); |
| 56 | + return; |
| 57 | + } |
| 58 | + buffer.write(b, off, len); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public boolean isReady() { |
| 63 | + return true; |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public void setWriteListener(WriteListener writeListener) { |
| 68 | + } |
| 69 | + }; |
| 70 | + |
| 71 | + private final PrintWriter writer = |
| 72 | + new PrintWriter(new OutputStreamWriter(outputStream, StandardCharsets.UTF_8), true); |
| 73 | + |
| 74 | + /** |
| 75 | + * @param response the wrapped response |
| 76 | + * @param maxBytes max allowed response bytes; {@code 0} means no limit |
| 77 | + */ |
| 78 | + public BufferedResponseWrapper(HttpServletResponse response, int maxBytes) { |
| 79 | + super(response); |
| 80 | + this.actual = response; |
| 81 | + this.maxBytes = maxBytes; |
| 82 | + } |
| 83 | + |
| 84 | + private void markOverflow() { |
| 85 | + overflow = true; |
| 86 | + buffer.reset(); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Early-detection path: if the framework reports the full content length before writing any |
| 91 | + * bytes, we can flag overflow without buffering anything. |
| 92 | + */ |
| 93 | + @Override |
| 94 | + public void setContentLength(int len) { |
| 95 | + if (maxBytes > 0 && len > maxBytes) { |
| 96 | + markOverflow(); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public void setContentLengthLong(long len) { |
| 102 | + if (maxBytes > 0 && len > maxBytes) { |
| 103 | + markOverflow(); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public int getStatus() { |
| 109 | + return this.status; |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public void setStatus(int sc) { |
| 114 | + this.status = sc; |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + public void setHeader(String name, String value) { |
| 119 | + if ("content-length".equalsIgnoreCase(name)) { |
| 120 | + try { |
| 121 | + setContentLengthLong(Long.parseLong(value)); |
| 122 | + } catch (NumberFormatException ignored) { |
| 123 | + // malformed value, skip overflow check |
| 124 | + } |
| 125 | + } else { |
| 126 | + super.setHeader(name, value); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + public void addHeader(String name, String value) { |
| 132 | + if ("content-length".equalsIgnoreCase(name)) { |
| 133 | + try { |
| 134 | + setContentLengthLong(Long.parseLong(value)); |
| 135 | + } catch (NumberFormatException ignored) { |
| 136 | + // malformed value, skip overflow check |
| 137 | + } |
| 138 | + } else { |
| 139 | + super.addHeader(name, value); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + @Override |
| 144 | + public void setContentType(String type) { |
| 145 | + this.contentType = type; |
| 146 | + } |
| 147 | + |
| 148 | + @Override |
| 149 | + public ServletOutputStream getOutputStream() { |
| 150 | + return outputStream; |
| 151 | + } |
| 152 | + |
| 153 | + @Override |
| 154 | + public PrintWriter getWriter() { |
| 155 | + return writer; |
| 156 | + } |
| 157 | + |
| 158 | + public void commitToResponse() throws IOException { |
| 159 | + if (committed) { |
| 160 | + throw new IllegalStateException("commitToResponse() already called"); |
| 161 | + } |
| 162 | + committed = true; |
| 163 | + // Flush the PrintWriter's OutputStreamWriter encoder into our ByteArrayOutputStream. |
| 164 | + // PrintWriter(autoFlush=true) only auto-flushes on println/printf/format, not print/write, |
| 165 | + // so bytes can remain buffered in the encoder until an explicit flush. |
| 166 | + writer.flush(); |
| 167 | + if (overflow) { |
| 168 | + return; |
| 169 | + } |
| 170 | + if (contentType != null) { |
| 171 | + actual.setContentType(contentType); |
| 172 | + } |
| 173 | + actual.setStatus(status); |
| 174 | + actual.setContentLength(buffer.size()); |
| 175 | + buffer.writeTo(actual.getOutputStream()); |
| 176 | + actual.getOutputStream().flush(); |
| 177 | + } |
| 178 | +} |
0 commit comments