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);
}
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());
}
}
}
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());
}
}
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());
}
}
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;
}
Aggregations