Author: davsclaus Date: Sun Jul 26 12:11:24 2009 New Revision: 797918 URL: http://svn.apache.org/viewvc?rev=797918&view=rev Log: CAMEL-1849: stream caches spooled to disk is now deleted in case of http exception being thrown. Also http exception keeps a defensive copy of the response body stream so its detached from the real stream.
Modified: camel/branches/camel-1.x/camel-core/src/main/java/org/apache/camel/converter/stream/CachedOutputStream.java camel/branches/camel-1.x/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java camel/branches/camel-1.x/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpStreamCacheFileTest.java Modified: camel/branches/camel-1.x/camel-core/src/main/java/org/apache/camel/converter/stream/CachedOutputStream.java URL: http://svn.apache.org/viewvc/camel/branches/camel-1.x/camel-core/src/main/java/org/apache/camel/converter/stream/CachedOutputStream.java?rev=797918&r1=797917&r2=797918&view=diff ============================================================================== --- camel/branches/camel-1.x/camel-core/src/main/java/org/apache/camel/converter/stream/CachedOutputStream.java (original) +++ camel/branches/camel-1.x/camel-core/src/main/java/org/apache/camel/converter/stream/CachedOutputStream.java Sun Jul 26 12:11:24 2009 @@ -26,8 +26,6 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; -import java.util.ArrayList; -import java.util.List; import java.util.Map; import org.apache.camel.converter.IOConverter; @@ -58,10 +56,7 @@ private File tempFile; private File outputDir; - - private List<Object> streamList = new ArrayList<Object>(); - public CachedOutputStream() { currentStream = new ByteArrayOutputStream(2048); inmem = true; @@ -129,7 +124,6 @@ public void lockOutputStream() throws IOException { currentStream.flush(); outputLocked = true; - streamList.remove(currentStream); } public void close() throws IOException { @@ -181,7 +175,6 @@ if (copyOldContent) { IOHelper.copyAndCloseInput(fin, out); } - streamList.remove(currentStream); tempFile.delete(); tempFile = null; inmem = true; @@ -353,15 +346,14 @@ private void createFileOutputStream() throws IOException { ByteArrayOutputStream bout = (ByteArrayOutputStream)currentStream; if (outputDir == null) { - tempFile = FileUtil.createTempFile("cos", "tmp"); + tempFile = FileUtil.createTempFile("cos", ".tmp"); } else { - tempFile = FileUtil.createTempFile("cos", "tmp", outputDir, false); + tempFile = FileUtil.createTempFile("cos", ".tmp", outputDir, false); } currentStream = new BufferedOutputStream(new FileOutputStream(tempFile)); bout.writeTo(currentStream); inmem = false; - streamList.add(currentStream); } public File getTempFile() { @@ -384,7 +376,6 @@ maybeDeleteTempFile(this); } }; - streamList.add(fileInputStream); return fileInputStream; } catch (FileNotFoundException e) { throw new IOException("Cached file was deleted, " + e.toString()); @@ -411,8 +402,7 @@ } private void maybeDeleteTempFile(Object stream) { - streamList.remove(stream); - if (!inmem && tempFile != null && streamList.isEmpty()) { + if (!inmem && tempFile != null) { tempFile.delete(); tempFile = null; currentStream = new ByteArrayOutputStream(1024); Modified: camel/branches/camel-1.x/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java URL: http://svn.apache.org/viewvc/camel/branches/camel-1.x/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java?rev=797918&r1=797917&r2=797918&view=diff ============================================================================== --- camel/branches/camel-1.x/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java (original) +++ camel/branches/camel-1.x/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java Sun Jul 26 12:11:24 2009 @@ -16,6 +16,7 @@ */ package org.apache.camel.component.http; +import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.UnsupportedEncodingException; @@ -108,19 +109,28 @@ HttpOperationFailedException exception = null; Header[] headers = method.getResponseHeaders(); InputStream is = extractResponseBody(method, exchange); + + // make a defensive copy of the response body in the exception so its detached from the cache + InputStream copy = null; + if (is != null) { + copy = new ByteArrayInputStream(exchange.getContext().getTypeConverter().convertTo(byte[].class, is)); + // close original stream so it can delete the temporary file if it has been spooled to disk by the CachedOutputStream + is.close(); + } + if (responseCode >= 300 && responseCode < 400) { String redirectLocation; Header locationHeader = method.getResponseHeader("location"); if (locationHeader != null) { redirectLocation = locationHeader.getValue(); - exception = new HttpOperationFailedException(responseCode, method.getStatusLine(), redirectLocation, headers, is); + exception = new HttpOperationFailedException(responseCode, method.getStatusLine(), redirectLocation, headers, copy); } else { // no redirect location - exception = new HttpOperationFailedException(responseCode, method.getStatusLine(), headers, is); + exception = new HttpOperationFailedException(responseCode, method.getStatusLine(), headers, copy); } } else { // internal server error (error code 500) - exception = new HttpOperationFailedException(responseCode, method.getStatusLine(), headers, is); + exception = new HttpOperationFailedException(responseCode, method.getStatusLine(), headers, copy); } if (exception != null) { Modified: camel/branches/camel-1.x/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpStreamCacheFileTest.java URL: http://svn.apache.org/viewvc/camel/branches/camel-1.x/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpStreamCacheFileTest.java?rev=797918&r1=797917&r2=797918&view=diff ============================================================================== --- camel/branches/camel-1.x/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpStreamCacheFileTest.java (original) +++ camel/branches/camel-1.x/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpStreamCacheFileTest.java Sun Jul 26 12:11:24 2009 @@ -65,8 +65,7 @@ // the temporary files should have been deleted File file = new File("./target/cachedir"); String[] files = file.list(); - // TODO: CAMEL-1849 when fixed this one passes - //assertEquals("There should be no files", files.length, 0); + assertEquals("There should be no files", files.length, 0); } @Override