Ensure HTTP classes don't close streams

Prior to this commit several HTTP classes made use of FileCopyUtils
when reading from or writing to streams. This has the unfortunate
side effect of closing streams that should really be left open.

The problem is particularly noticeable when dealing with a
FormHttpMessageConverter that is writing a multi-part response.

Relevant HTTP classes have now been refactored to make use of a new
StreamUtils class that works in a similar way FileCopyUtils but does
not close streams.

The NonClosingOutputStream class from SimpleStreamingClientHttpRequest
has also been refactored to a StreamUtils method.

Issue: SPR-10095
This commit is contained in:
Phillip Webb
2013-02-11 18:42:21 -08:00
parent 08e1cbc02b
commit 6661788748
13 changed files with 360 additions and 80 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,6 +16,8 @@
package org.springframework.http;
import static org.mockito.Mockito.spy;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
@@ -28,7 +30,7 @@ public class MockHttpOutputMessage implements HttpOutputMessage {
private final HttpHeaders headers = new HttpHeaders();
private final ByteArrayOutputStream body = new ByteArrayOutputStream();
private final ByteArrayOutputStream body = spy(new ByteArrayOutputStream());
@Override
public HttpHeaders getHeaders() {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -45,6 +45,8 @@ import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import static org.junit.Assert.*;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
/**
* @author Arjen Poutsma
@@ -157,6 +159,7 @@ public class FormHttpMessageConverterTests {
item = (FileItem) items.get(4);
assertEquals("xml", item.getFieldName());
assertEquals("text/xml", item.getContentType());
verify(outputMessage.getBody(), never()).close();
}
private static class MockHttpOutputMessageRequestContext implements RequestContext {