Search in sources :

Example 6 with BodyPart

use of javax.mail.BodyPart in project rest.li by linkedin.

the class TestMIMEReader method executeRequestAndAssert.

///////////////////////////////////////////////////////////////////////////////////////
private void executeRequestAndAssert(final ByteString payload, final int chunkSize, final MimeMultipart mimeMultipart) throws Exception {
    mockR2AndWrite(payload, chunkSize, mimeMultipart.getContentType());
    final CountDownLatch latch = new CountDownLatch(1);
    //We simulate _client.streamRequest(request, callback);
    _reader = MultiPartMIMEReader.createAndAcquireStream(_streamRequest);
    MultiPartMIMEReaderCallbackImpl testMultiPartMIMEReaderCallback = new MultiPartMIMEReaderCallbackImpl(latch);
    _reader.registerReaderCallback(testMultiPartMIMEReaderCallback);
    latch.await(_testTimeout, TimeUnit.MILLISECONDS);
    //Verify this is unusable.
    try {
        _reader.drainAllParts();
        Assert.fail();
    } catch (MultiPartReaderFinishedException multiPartReaderFinishedException) {
    //pass
    }
    List<SinglePartMIMEReaderCallbackImpl> singlePartMIMEReaderCallbacks = testMultiPartMIMEReaderCallback._singlePartMIMEReaderCallbacks;
    Assert.assertEquals(singlePartMIMEReaderCallbacks.size(), mimeMultipart.getCount());
    for (int i = 0; i < singlePartMIMEReaderCallbacks.size(); i++) {
        //Actual
        final SinglePartMIMEReaderCallbackImpl currentCallback = singlePartMIMEReaderCallbacks.get(i);
        //Expected
        final BodyPart currentExpectedPart = mimeMultipart.getBodyPart(i);
        //Construct expected headers and verify they match
        final Map<String, String> expectedHeaders = new HashMap<String, String>();
        @SuppressWarnings("unchecked") final Enumeration<Header> allHeaders = currentExpectedPart.getAllHeaders();
        while (allHeaders.hasMoreElements()) {
            final Header header = allHeaders.nextElement();
            expectedHeaders.put(header.getName(), header.getValue());
        }
        Assert.assertEquals(currentCallback._headers, expectedHeaders);
        Assert.assertNotNull(currentCallback._finishedData);
        //Verify the body matches
        if (currentExpectedPart.getContent() instanceof byte[]) {
            Assert.assertEquals(currentCallback._finishedData.copyBytes(), currentExpectedPart.getContent());
        } else {
            //Default is String
            Assert.assertEquals(new String(currentCallback._finishedData.copyBytes()), currentExpectedPart.getContent());
        }
    }
    Assert.assertTrue(_reader.haveAllPartsFinished());
    //Mock verifies
    verify(_streamRequest, times(1)).getEntityStream();
    verify(_streamRequest, times(1)).getHeader(HEADER_CONTENT_TYPE);
    verify(_entityStream, times(1)).setReader(isA(MultiPartMIMEReader.R2MultiPartMIMEReader.class));
    final int expectedRequests = (int) Math.ceil((double) payload.length() / chunkSize);
    //One more expected request because we have to make the last call to get called onDone().
    verify(_readHandle, times(expectedRequests + 1)).request(1);
    verifyNoMoreInteractions(_streamRequest);
    verifyNoMoreInteractions(_entityStream);
    verifyNoMoreInteractions(_readHandle);
}
Also used : MultiPartReaderFinishedException(com.linkedin.multipart.exceptions.MultiPartReaderFinishedException) MimeBodyPart(javax.mail.internet.MimeBodyPart) BodyPart(javax.mail.BodyPart) HashMap(java.util.HashMap) ByteString(com.linkedin.data.ByteString) CountDownLatch(java.util.concurrent.CountDownLatch) Header(javax.mail.Header)

Example 7 with BodyPart

use of javax.mail.BodyPart in project rest.li by linkedin.

the class TestMIMEReaderExceptions method boundaryPrematurelyTerminatedNoSubsequentCRLFs.

@Test(dataProvider = "multiplePartsDataSource")
public void boundaryPrematurelyTerminatedNoSubsequentCRLFs(final int chunkSize, final List<MimeBodyPart> bodyPartList) throws Exception {
    MimeMultipart multiPartMimeBody = new MimeMultipart();
    //Add your body parts
    for (final MimeBodyPart bodyPart : bodyPartList) {
        multiPartMimeBody.addBodyPart(bodyPart);
    }
    final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    multiPartMimeBody.writeTo(byteArrayOutputStream);
    final byte[] mimePayload = byteArrayOutputStream.toByteArray();
    //At this point the mimePayload's ending looks something like the following. Consider that
    //--1234 is the boundary:
    //<ending of some part data>--1234--/r/n
    //What we want to test this particular code path is:
    //<ending of some part data>--1234678
    //So we trim off an element in the array at the end which results in:
    //<ending of some part data>--1234--/r
    //And then we modify the last three bytes to end up with:
    //<ending of some part data>--1234678
    final byte[] trimmedMimePayload = Arrays.copyOf(mimePayload, mimePayload.length - 1);
    trimmedMimePayload[trimmedMimePayload.length - 1] = 8;
    trimmedMimePayload[trimmedMimePayload.length - 2] = 7;
    trimmedMimePayload[trimmedMimePayload.length - 3] = 6;
    final ByteString requestPayload = ByteString.copy(trimmedMimePayload);
    executeRequestWithDesiredException(requestPayload, chunkSize, multiPartMimeBody.getContentType(), "Malformed multipart mime request. Premature termination of multipart " + "mime body due to a boundary without a subsequent consecutive CRLF.");
    //In this case we want all the parts to still make it over
    List<SinglePartMIMEExceptionReaderCallbackImpl> singlePartMIMEReaderCallbacks = _currentMultiPartMIMEReaderCallback.getSinglePartMIMEReaderCallbacks();
    Assert.assertEquals(singlePartMIMEReaderCallbacks.size(), multiPartMimeBody.getCount());
    //Everything should have made it over
    for (int i = 0; i < singlePartMIMEReaderCallbacks.size(); i++) {
        //Actual
        final SinglePartMIMEExceptionReaderCallbackImpl currentCallback = singlePartMIMEReaderCallbacks.get(i);
        //Expected
        final BodyPart currentExpectedPart = multiPartMimeBody.getBodyPart(i);
        //Construct expected headers and verify they match
        final Map<String, String> expectedHeaders = new HashMap<String, String>();
        @SuppressWarnings("unchecked") final Enumeration<Header> allHeaders = currentExpectedPart.getAllHeaders();
        while (allHeaders.hasMoreElements()) {
            final Header header = allHeaders.nextElement();
            expectedHeaders.put(header.getName(), header.getValue());
        }
        Assert.assertEquals(currentCallback.getHeaders(), expectedHeaders);
        //Verify the body matches
        Assert.assertNotNull(currentCallback.getFinishedData());
        if (currentExpectedPart.getContent() instanceof byte[]) {
            Assert.assertEquals(currentCallback.getFinishedData().copyBytes(), currentExpectedPart.getContent());
        } else {
            //Default is String
            Assert.assertEquals(new String(currentCallback.getFinishedData().copyBytes()), currentExpectedPart.getContent());
        }
    }
}
Also used : MimeBodyPart(javax.mail.internet.MimeBodyPart) BodyPart(javax.mail.BodyPart) HashMap(java.util.HashMap) ByteString(com.linkedin.data.ByteString) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteString(com.linkedin.data.ByteString) Header(javax.mail.Header) MimeMultipart(javax.mail.internet.MimeMultipart) MimeBodyPart(javax.mail.internet.MimeBodyPart) Test(org.testng.annotations.Test)

Example 8 with BodyPart

use of javax.mail.BodyPart in project rest.li by linkedin.

the class TestMIMEIntegrationReaderDrain method testSinglePartialTopRemaining.

@Test(dataProvider = "allTypesOfBodiesDataSource")
public void testSinglePartialTopRemaining(final int chunkSize, final List<MimeBodyPart> bodyPartList) throws Exception {
    //Execute the request, verify the correct header came back to ensure the server took the proper draining actions
    //and return the payload so we can assert deeper.
    MimeMultipart mimeMultipart = executeRequestWithDrainStrategy(chunkSize, bodyPartList, SINGLE_PARTIAL_TOP_REMAINING, "onDrainComplete");
    //Single part drains the first 6 then the top level drains all of remaining
    List<SinglePartMIMEDrainReaderCallbackImpl> singlePartMIMEReaderCallbacks = _mimeServerRequestDrainHandler.getTestMultiPartMIMEReaderCallback().getSinglePartMIMEReaderCallbacks();
    Assert.assertEquals(singlePartMIMEReaderCallbacks.size(), 6);
    for (int i = 0; i < singlePartMIMEReaderCallbacks.size(); i++) {
        //Actual

        final SinglePartMIMEDrainReaderCallbackImpl currentCallback = singlePartMIMEReaderCallbacks.get(i);
        //Expected

        final BodyPart currentExpectedPart = mimeMultipart.getBodyPart(i);
        //Construct expected headers and verify they match

        final Map<String, String> expectedHeaders = new HashMap<String, String>();
        @SuppressWarnings("unchecked") final Enumeration<Header> allHeaders = currentExpectedPart.getAllHeaders();
        while (allHeaders.hasMoreElements()) {
            final Header header = allHeaders.nextElement();
            expectedHeaders.put(header.getName(), header.getValue());
        }
        Assert.assertEquals(currentCallback._headers, expectedHeaders);
        //Verify that the bodies are empty
        Assert.assertEquals(currentCallback._finishedData, ByteString.empty());
    }
}
Also used : MimeBodyPart(javax.mail.internet.MimeBodyPart) BodyPart(javax.mail.BodyPart) Header(javax.mail.Header) MimeMultipart(javax.mail.internet.MimeMultipart) HashMap(java.util.HashMap) ByteString(com.linkedin.data.ByteString) Test(org.testng.annotations.Test)

Example 9 with BodyPart

use of javax.mail.BodyPart in project rest.li by linkedin.

the class TestMIMEIntegrationReaderDrain method testSingleAll.

@Test(dataProvider = "allTypesOfBodiesDataSource")
public void testSingleAll(final int chunkSize, final List<MimeBodyPart> bodyPartList) throws Exception {
    //Execute the request, verify the correct header came back to ensure the server took the proper drain actions
    //and return the payload so we can assert deeper.
    MimeMultipart mimeMultipart = executeRequestWithDrainStrategy(chunkSize, bodyPartList, SINGLE_ALL, "onFinished");
    //Single part drains all, one by one
    List<SinglePartMIMEDrainReaderCallbackImpl> singlePartMIMEReaderCallbacks = _mimeServerRequestDrainHandler.getTestMultiPartMIMEReaderCallback().getSinglePartMIMEReaderCallbacks();
    Assert.assertEquals(singlePartMIMEReaderCallbacks.size(), 12);
    //Verify everything was drained
    for (int i = 0; i < singlePartMIMEReaderCallbacks.size(); i++) {
        //Actual

        final SinglePartMIMEDrainReaderCallbackImpl currentCallback = singlePartMIMEReaderCallbacks.get(i);
        //Expected

        final BodyPart currentExpectedPart = mimeMultipart.getBodyPart(i);
        //Construct expected headers and verify they match

        final Map<String, String> expectedHeaders = new HashMap<String, String>();
        @SuppressWarnings("unchecked") final Enumeration<Header> allHeaders = currentExpectedPart.getAllHeaders();
        while (allHeaders.hasMoreElements()) {
            final Header header = allHeaders.nextElement();
            expectedHeaders.put(header.getName(), header.getValue());
        }
        Assert.assertEquals(currentCallback._headers, expectedHeaders);
        //Verify that the bodies are empty
        Assert.assertEquals(currentCallback._finishedData, ByteString.empty());
    }
}
Also used : MimeBodyPart(javax.mail.internet.MimeBodyPart) BodyPart(javax.mail.BodyPart) Header(javax.mail.Header) MimeMultipart(javax.mail.internet.MimeMultipart) HashMap(java.util.HashMap) ByteString(com.linkedin.data.ByteString) Test(org.testng.annotations.Test)

Example 10 with BodyPart

use of javax.mail.BodyPart in project spring-framework by spring-projects.

the class MimeMessageHelper method getMainPart.

private MimeBodyPart getMainPart() throws MessagingException {
    MimeMultipart mimeMultipart = getMimeMultipart();
    MimeBodyPart bodyPart = null;
    for (int i = 0; i < mimeMultipart.getCount(); i++) {
        BodyPart bp = mimeMultipart.getBodyPart(i);
        if (bp.getFileName() == null) {
            bodyPart = (MimeBodyPart) bp;
        }
    }
    if (bodyPart == null) {
        MimeBodyPart mimeBodyPart = new MimeBodyPart();
        mimeMultipart.addBodyPart(mimeBodyPart);
        bodyPart = mimeBodyPart;
    }
    return bodyPart;
}
Also used : MimeBodyPart(javax.mail.internet.MimeBodyPart) BodyPart(javax.mail.BodyPart) MimeMultipart(javax.mail.internet.MimeMultipart) MimeBodyPart(javax.mail.internet.MimeBodyPart)

Aggregations

BodyPart (javax.mail.BodyPart)71 MimeMultipart (javax.mail.internet.MimeMultipart)52 MimeBodyPart (javax.mail.internet.MimeBodyPart)41 MessagingException (javax.mail.MessagingException)23 MimeMessage (javax.mail.internet.MimeMessage)19 Header (javax.mail.Header)15 Multipart (javax.mail.Multipart)15 DispositionNotification (com.sun.mail.dsn.DispositionNotification)13 IOException (java.io.IOException)13 HashMap (java.util.HashMap)13 InternetHeaders (javax.mail.internet.InternetHeaders)13 ByteString (com.linkedin.data.ByteString)12 DataHandler (javax.activation.DataHandler)10 Test (org.testng.annotations.Test)10 Session (javax.mail.Session)9 InternetAddress (javax.mail.internet.InternetAddress)8 Properties (java.util.Properties)7 File (java.io.File)6 InputStream (java.io.InputStream)6 FileDataSource (javax.activation.FileDataSource)6