[ 
https://issues.apache.org/jira/browse/CAMEL-12078?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16691648#comment-16691648
 ] 

ASF GitHub Bot commented on CAMEL-12078:
----------------------------------------

onderson closed pull request #2185: Failing test for CAMEL-12078
URL: https://github.com/apache/camel/pull/2185
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git 
a/components/camel-mail/src/test/java/org/apache/camel/dataformat/mime/multipart/MimeMultipartDataFormatTest.java
 
b/components/camel-mail/src/test/java/org/apache/camel/dataformat/mime/multipart/MimeMultipartDataFormatTest.java
index ada79c017fc..a96072de28f 100644
--- 
a/components/camel-mail/src/test/java/org/apache/camel/dataformat/mime/multipart/MimeMultipartDataFormatTest.java
+++ 
b/components/camel-mail/src/test/java/org/apache/camel/dataformat/mime/multipart/MimeMultipartDataFormatTest.java
@@ -351,6 +351,41 @@ public void unmarshalInlineHeadersNonMimeBodyStream() 
throws UnsupportedEncoding
         assertEquals("This is not a MIME-Multipart", bodyStr);
     }
 
+    @Test
+    public void attachmentReadOnce() throws IOException {
+        String attContentType = "text/plain";
+        String attText = "Attachment Text";
+        InputStream attInputStream = new 
ByteArrayInputStream(attText.getBytes());
+        String attFileName = "Attachment File Name";
+        in.setBody("Body text");
+        in.setHeader(Exchange.CONTENT_TYPE, 
"text/plain;charset=iso8859-1;other-parameter=true");
+        in.setHeader(Exchange.CONTENT_ENCODING, "UTF8");
+        Map<String, String> headers = new HashMap<String, String>();
+        headers.put("Content-Description", "Sample Attachment Data");
+        headers.put("X-AdditionalData", "additional data");
+        CountingByteArrayDataSource attachmentDs = new 
CountingByteArrayDataSource(attInputStream, attContentType);
+        addAttachment(attachmentDs, attFileName, headers);
+        Exchange result = template.send("direct:roundtrip", exchange);
+        Message out = result.getOut();
+        assertEquals("Body text", out.getBody(String.class));
+        assertThat(out.getHeader(Exchange.CONTENT_TYPE, String.class), 
startsWith("text/plain"));
+        assertEquals("UTF8", out.getHeader(Exchange.CONTENT_ENCODING));
+        assertTrue(out.hasAttachments());
+        assertEquals(1, out.getAttachmentNames().size());
+        assertThat(out.getAttachmentNames(), hasItem(attFileName));
+        Attachment att = out.getAttachmentObject(attFileName);
+        DataHandler dh = att.getDataHandler();
+        assertNotNull(dh);
+        assertEquals(attContentType, dh.getContentType());
+        InputStream is = dh.getInputStream();
+        ByteArrayOutputStream os = new ByteArrayOutputStream();
+        IOHelper.copyAndCloseInput(is, os);
+        assertEquals(attText, new String(os.toByteArray()));
+        assertEquals("Sample Attachment Data", 
att.getHeader("content-description"));
+        assertEquals("additional data", att.getHeader("X-AdditionalData"));
+        assertEquals(1, attachmentDs.readCounts); // Fails - input is read 
twice
+    }
+
     private Attachment unmarshalAndCheckAttachmentName(String matcher) throws 
IOException, UnsupportedEncodingException {
         Exchange intermediate = 
template.send("direct:unmarshalonlyinlineheaders", exchange);
         assertNotNull(intermediate.getOut());
@@ -384,6 +419,34 @@ private void addAttachment(String attContentType, String 
attText, String attFile
         in.addAttachmentObject(attFileName, attachment);
     }
 
+    private void addAttachment(DataSource ds, String attFileName, Map<String, 
String> headers) throws IOException {
+        DefaultAttachment attachment = new DefaultAttachment(ds);
+        if (headers != null) {
+            for (String headerName : headers.keySet()) {
+                attachment.addHeader(headerName, headers.get(headerName));
+            }
+        }
+        in.addAttachmentObject(attFileName, attachment);
+    }
+
+    class CountingByteArrayDataSource extends ByteArrayDataSource {
+
+        int readCounts = 0;
+
+        CountingByteArrayDataSource(InputStream is, String attContentType) 
throws IOException {
+            super(is, attContentType);
+        }
+
+        @Override
+        public InputStream getInputStream() throws IOException {
+            //Thread.dumpStack();
+            readCounts++;
+            return super.getInputStream();
+        }
+
+    }
+
+
     @Override
     protected RouteBuilder createRouteBuilder() throws Exception {
         return new RouteBuilder() {


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


> MIME-Mutipart DataFormat reads attachment DataSource twice
> ----------------------------------------------------------
>
>                 Key: CAMEL-12078
>                 URL: https://issues.apache.org/jira/browse/CAMEL-12078
>             Project: Camel
>          Issue Type: Bug
>          Components: camel-mail
>    Affects Versions: 2.20.1
>            Reporter: Tim Dudgeon
>            Priority: Minor
>             Fix For: Future
>
>
> As reported on mailing list if an attachment for MIME-Mutipart DataFormat is 
> defined using a DataSource that can only be read once you get an exception.
> http://mail-archives.apache.org/mod_mbox/camel-users/201712.mbox/%3C0e0d4b2e-dc32-c61e-3ccd-7ee14238c485%40gmail.com%3E
> For instance, this happens if the DataSource is created using an InputStream 
> that cannot be read multiple times
> It should be possible to post the data only reading the input a single time.
> I will add a test case shortly.



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to