[MGNLCACHE-191] Response content is lost if buffer is not of type ByteArrayOutputStream Created: 13/Dec/17  Updated: 25/Mar/22

Status: Open
Project: Cache Modules
Component/s: cache core
Affects Version/s: 5.6
Fix Version/s: None

Type: Bug Priority: Neutral
Reporter: Cedric Reichenbach Assignee: Unassigned
Resolution: Unresolved Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Template:
Acceptance criteria:
Empty
Task DoD:
[ ]* Doc/release notes changes? Comment present?
[ ]* Downstream builds green?
[ ]* Solution information and context easily available?
[ ]* Tests
[ ]* FixVersion filled and not yet released
[ ]  Architecture Decision Record (ADR)
Bug DoR:
[ ]* Steps to reproduce, expected, and actual results filled
[ ]* Affected version filled
Testcase included:
Yes

 Description   

CacheResponseWrapper#getBufferedContent tries to cast a generic OutputStream into a ByteArrayOutputStream in order to extract already written content. Otherwise it simply returns an empty array (instead of already buffered content).

This is particularly relevant for cases where response content exceeds the given size threshold. The output buffer gets replaced by a different one (see CacheResponseWrapper.ThresholdingCacheOutputStream#thresholdReached).

Test code to reproduce

final byte[] payload = "Foo bar baz".getBytes();

final HttpServletResponse originalResponse = mock(HttpServletResponse.class);
when(originalResponse.getOutputStream()).thenReturn(new SimpleServletOutputStream(new ByteArrayOutputStream()));
final HttpServletResponse target = mock(HttpServletResponse.class);
final ByteArrayOutputStream targetStream = new ByteArrayOutputStream();
when(target.getOutputStream()).thenReturn(new SimpleServletOutputStream(targetStream));

final CacheResponseWrapper serveWrapper = new CacheResponseWrapper(originalResponse, payload.length - 1, true);

serveWrapper.getOutputStream().write(payload);
serveWrapper.replayContent(target, true);
assertThat(targetStream.toByteArray(), is(payload)); // fails

Generated at Sun Feb 11 23:53:06 CET 2024 using Jira 9.4.2#940002-sha1:46d1a51de284217efdcb32434eab47a99af2938b.