|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2022 the original author or authors. |
| 2 | + * Copyright 2002-2024 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
21 | 21 | import java.io.OutputStreamWriter;
|
22 | 22 | import java.io.PrintWriter;
|
23 | 23 | import java.io.UnsupportedEncodingException;
|
| 24 | +import java.util.ArrayList; |
| 25 | +import java.util.Collection; |
| 26 | +import java.util.Collections; |
| 27 | +import java.util.List; |
24 | 28 |
|
25 | 29 | import jakarta.servlet.ServletOutputStream;
|
26 | 30 | import jakarta.servlet.WriteListener;
|
@@ -55,6 +59,9 @@ public class ContentCachingResponseWrapper extends HttpServletResponseWrapper {
|
55 | 59 | @Nullable
|
56 | 60 | private Integer contentLength;
|
57 | 61 |
|
| 62 | + @Nullable |
| 63 | + private String contentType; |
| 64 | + |
58 | 65 |
|
59 | 66 | /**
|
60 | 67 | * Create a new ContentCachingResponseWrapper for the given servlet response.
|
@@ -139,6 +146,122 @@ public void setContentLengthLong(long len) {
|
139 | 146 | this.contentLength = lenInt;
|
140 | 147 | }
|
141 | 148 |
|
| 149 | + @Override |
| 150 | + public void setContentType(String type) { |
| 151 | + this.contentType = type; |
| 152 | + } |
| 153 | + |
| 154 | + @Override |
| 155 | + @Nullable |
| 156 | + public String getContentType() { |
| 157 | + return this.contentType; |
| 158 | + } |
| 159 | + |
| 160 | + @Override |
| 161 | + public boolean containsHeader(String name) { |
| 162 | + if (HttpHeaders.CONTENT_LENGTH.equalsIgnoreCase(name)) { |
| 163 | + return this.contentLength != null; |
| 164 | + } |
| 165 | + else if (HttpHeaders.CONTENT_TYPE.equalsIgnoreCase(name)) { |
| 166 | + return this.contentType != null; |
| 167 | + } |
| 168 | + else { |
| 169 | + return super.containsHeader(name); |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + @Override |
| 174 | + public void setHeader(String name, String value) { |
| 175 | + if (HttpHeaders.CONTENT_LENGTH.equalsIgnoreCase(name)) { |
| 176 | + this.contentLength = Integer.valueOf(value); |
| 177 | + } |
| 178 | + else if (HttpHeaders.CONTENT_TYPE.equalsIgnoreCase(name)) { |
| 179 | + this.contentType = value; |
| 180 | + } |
| 181 | + else { |
| 182 | + super.setHeader(name, value); |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + @Override |
| 187 | + public void addHeader(String name, String value) { |
| 188 | + if (HttpHeaders.CONTENT_LENGTH.equalsIgnoreCase(name)) { |
| 189 | + this.contentLength = Integer.valueOf(value); |
| 190 | + } |
| 191 | + else if (HttpHeaders.CONTENT_TYPE.equalsIgnoreCase(name)) { |
| 192 | + this.contentType = value; |
| 193 | + } |
| 194 | + else { |
| 195 | + super.addHeader(name, value); |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + @Override |
| 200 | + public void setIntHeader(String name, int value) { |
| 201 | + if (HttpHeaders.CONTENT_LENGTH.equalsIgnoreCase(name)) { |
| 202 | + this.contentLength = Integer.valueOf(value); |
| 203 | + } |
| 204 | + else { |
| 205 | + super.setIntHeader(name, value); |
| 206 | + } |
| 207 | + } |
| 208 | + |
| 209 | + @Override |
| 210 | + public void addIntHeader(String name, int value) { |
| 211 | + if (HttpHeaders.CONTENT_LENGTH.equalsIgnoreCase(name)) { |
| 212 | + this.contentLength = Integer.valueOf(value); |
| 213 | + } |
| 214 | + else { |
| 215 | + super.addIntHeader(name, value); |
| 216 | + } |
| 217 | + } |
| 218 | + |
| 219 | + @Override |
| 220 | + @Nullable |
| 221 | + public String getHeader(String name) { |
| 222 | + if (HttpHeaders.CONTENT_LENGTH.equalsIgnoreCase(name)) { |
| 223 | + return (this.contentLength != null) ? this.contentLength.toString() : null; |
| 224 | + } |
| 225 | + else if (HttpHeaders.CONTENT_TYPE.equalsIgnoreCase(name)) { |
| 226 | + return this.contentType; |
| 227 | + } |
| 228 | + else { |
| 229 | + return super.getHeader(name); |
| 230 | + } |
| 231 | + } |
| 232 | + |
| 233 | + @Override |
| 234 | + public Collection<String> getHeaders(String name) { |
| 235 | + if (HttpHeaders.CONTENT_LENGTH.equalsIgnoreCase(name)) { |
| 236 | + return this.contentLength != null ? Collections.singleton(this.contentLength.toString()) : |
| 237 | + Collections.emptySet(); |
| 238 | + } |
| 239 | + else if (HttpHeaders.CONTENT_TYPE.equalsIgnoreCase(name)) { |
| 240 | + return this.contentType != null ? Collections.singleton(this.contentType) : Collections.emptySet(); |
| 241 | + } |
| 242 | + else { |
| 243 | + return super.getHeaders(name); |
| 244 | + } |
| 245 | + } |
| 246 | + |
| 247 | + @Override |
| 248 | + public Collection<String> getHeaderNames() { |
| 249 | + Collection<String> headerNames = super.getHeaderNames(); |
| 250 | + if (this.contentLength != null || this.contentType != null) { |
| 251 | + List<String> result = new ArrayList<>(headerNames); |
| 252 | + if (this.contentLength != null) { |
| 253 | + result.add(HttpHeaders.CONTENT_LENGTH); |
| 254 | + } |
| 255 | + if (this.contentType != null) { |
| 256 | + result.add(HttpHeaders.CONTENT_TYPE); |
| 257 | + } |
| 258 | + return result; |
| 259 | + } |
| 260 | + else { |
| 261 | + return headerNames; |
| 262 | + } |
| 263 | + } |
| 264 | + |
142 | 265 | @Override
|
143 | 266 | public void setBufferSize(int size) {
|
144 | 267 | if (size > this.content.size()) {
|
@@ -197,11 +320,17 @@ public void copyBodyToResponse() throws IOException {
|
197 | 320 | protected void copyBodyToResponse(boolean complete) throws IOException {
|
198 | 321 | if (this.content.size() > 0) {
|
199 | 322 | HttpServletResponse rawResponse = (HttpServletResponse) getResponse();
|
200 |
| - if ((complete || this.contentLength != null) && !rawResponse.isCommitted()) { |
201 |
| - if (rawResponse.getHeader(HttpHeaders.TRANSFER_ENCODING) == null) { |
202 |
| - rawResponse.setContentLength(complete ? this.content.size() : this.contentLength); |
| 323 | + if (!rawResponse.isCommitted()) { |
| 324 | + if (complete || this.contentLength != null) { |
| 325 | + if (rawResponse.getHeader(HttpHeaders.TRANSFER_ENCODING) == null) { |
| 326 | + rawResponse.setContentLength(complete ? this.content.size() : this.contentLength); |
| 327 | + } |
| 328 | + this.contentLength = null; |
| 329 | + } |
| 330 | + if (complete || this.contentType != null) { |
| 331 | + rawResponse.setContentType(this.contentType); |
| 332 | + this.contentType = null; |
203 | 333 | }
|
204 |
| - this.contentLength = null; |
205 | 334 | }
|
206 | 335 | this.content.writeTo(rawResponse.getOutputStream());
|
207 | 336 | this.content.reset();
|
|
0 commit comments