public InputStream getBody() throws IOException { Body body = bodyPart.getBody(); InputStream result = null; if (body instanceof TextBody) { throw new UnsupportedOperationException(); /* InputStreamReader reader = (InputStreamReader)((TextBody) body).getReader(); StringBuilder inputBuilder = new StringBuilder(); char[] buffer = new char[1024]; while (true) { int readCount = reader.read(buffer); if (readCount < 0) { break; } inputBuilder.append(buffer, 0, readCount); } String str = inputBuilder.toString(); return new ByteArrayInputStream(str.getBytes(reader.getEncoding())); */ } else if (body instanceof BinaryBody) { return ((BinaryBody)body).getInputStream(); } return result; }
@Override protected InputPart extractPart(BodyPart bodyPart) throws IOException { InputPart currPart = super.extractPart(bodyPart); Field disposition = bodyPart.getHeader().getField( FieldName.CONTENT_DISPOSITION); if (disposition == null) throw new RuntimeException(Messages.MESSAGES.couldFindNoContentDispositionHeader()); if (disposition instanceof ContentDispositionField) { String name = ((ContentDispositionField) disposition) .getParameter("name"); List<InputPart> list = formDataMap.get(name); if (list == null) { list = new LinkedList<InputPart>(); formDataMap.put(name, list); } list.add(currPart); } else { throw new RuntimeException(Messages.MESSAGES.couldNotParseContentDisposition(disposition)); } return currPart; }
@Override public void delete() throws IOException { bodyPart.dispose(); }
@Test void extractShouldRetrieveHtmlBodyWithOneInlinedHTMLAttachmentWithoutCid() throws IOException { //Given BodyPart inlinedHTMLPart = BodyPartBuilder.create() .setBody(HTML_CONTENT, "html", StandardCharsets.UTF_8) .build(); HeaderImpl inlinedHeader = new HeaderImpl(); inlinedHeader.addField(Fields.contentDisposition(MimeMessage.INLINE)); inlinedHeader.addField(Fields.contentType("text/html; charset=utf-8")); inlinedHTMLPart.setHeader(inlinedHeader); Multipart multipartAlternative = MultipartBuilder.create("alternative") .addBodyPart(inlinedHTMLPart) .build(); Message message = Message.Builder.of() .setBody(multipartAlternative) .build(); //When MessageContent actual = testee.extract(message); //Then assertThat(actual.getHtmlBody()).contains(HTML_CONTENT); }
public void startBodyPart() throws MimeException { expect(Multipart.class); BodyPart bodyPart = new BodyPart(); ((Multipart) stack.peek()).addBodyPart(bodyPart); stack.push(bodyPart); }
/** * Creates a new <code>BodyPart</code> from the specified * <code>Entity</code>. The <code>BodyPart</code> instance is initialized * with copies of header and body of the specified <code>Entity</code>. * The parent entity of the new body part is <code>null</code>. * * @param other * body part to copy. * @throws UnsupportedOperationException * if <code>other</code> contains a {@link SingleBody} that * does not support the {@link SingleBody#copy() copy()} * operation. * @throws IllegalArgumentException * if <code>other</code> contains a <code>Body</code> that * is neither a {@link Message}, {@link Multipart} or * {@link SingleBody}. */ public BodyPart copy(Entity other) { BodyPart copy = new BodyPart(); if (other.getHeader() != null) { copy.setHeader(copy(other.getHeader())); } if (other.getBody() != null) { copy.setBody(copy(other.getBody())); } return copy; }
@Test void extractShouldRetrieveTextBodyWithOneInlinedTextAttachmentWithoutCid() throws IOException { //Given BodyPart inlinedTextPart = BodyPartBuilder.create() .setBody(TEXT_CONTENT, "text", StandardCharsets.UTF_8) .build(); HeaderImpl inlinedHeader = new HeaderImpl(); inlinedHeader.addField(Fields.contentDisposition(MimeMessage.INLINE)); inlinedHeader.addField(Fields.contentType("text/plain; charset=utf-8")); inlinedTextPart.setHeader(inlinedHeader); Multipart multipartAlternative = MultipartBuilder.create("alternative") .addBodyPart(inlinedTextPart) .build(); Message message = Message.Builder.of() .setBody(multipartAlternative) .build(); //When MessageContent actual = testee.extract(message); //Then assertThat(actual.getTextBody()).contains(TEXT_CONTENT); }
final List<Entity> otherParts = other.getBodyParts(); for (Entity otherPart: otherParts) { BodyPart bodyPart = new BodyPart(); Header otherHeader = otherPart.getHeader(); if (otherHeader != null) { header.addField(otherField); bodyPart.setHeader(header); body = ((SingleBody) otherBody).copy(); bodyPart.setBody(body);
@Test public void extractShouldRetrieveHtmlBodyWithOneInlinedHTMLAttachmentWithoutCid() throws IOException { //Given BodyPart inlinedHTMLPart = BodyPartBuilder.create() .setBody(HTML_CONTENT, "html", StandardCharsets.UTF_8) .build(); HeaderImpl inlinedHeader = new HeaderImpl(); inlinedHeader.addField(Fields.contentDisposition(MimeMessage.INLINE)); inlinedHeader.addField(Fields.contentType("text/html; charset=utf-8")); inlinedHTMLPart.setHeader(inlinedHeader); Multipart multipartAlternative = MultipartBuilder.create("alternative") .addBodyPart(inlinedHTMLPart) .build(); Message message = Message.Builder.of() .setBody(multipartAlternative) .build(); //When MessageContent actual = testee.extract(message); //Then assertThat(actual.getHtmlBody()).contains(HTML_CONTENT); }
public PartImpl(final BodyPart bodyPart) { this.bodyPart = bodyPart; for (Field field : bodyPart.getHeader()) { headers.add(field.getName(), field.getBody()); if (field instanceof ContentTypeField) { contentType = MediaType.valueOf(field.getBody()); contentTypeFromMessage = true; } } if (contentType == null) contentType = defaultPartContentType; if (getCharset(contentType) == null) { if (defaultPartCharset != null) { contentType = getMediaTypeWithDefaultCharset(contentType); } else if (contentType.getType().equalsIgnoreCase("text")) { contentType = getMediaTypeWithCharset(contentType, "us-ascii"); } } }
@Test public void extractShouldRetrieveTextBodyWithOneInlinedTextAttachmentWithoutCid() throws IOException { //Given BodyPart inlinedTextPart = BodyPartBuilder.create() .setBody(TEXT_CONTENT, "text", StandardCharsets.UTF_8) .build(); HeaderImpl inlinedHeader = new HeaderImpl(); inlinedHeader.addField(Fields.contentDisposition(MimeMessage.INLINE)); inlinedHeader.addField(Fields.contentType("text/plain; charset=utf-8")); inlinedTextPart.setHeader(inlinedHeader); Multipart multipartAlternative = MultipartBuilder.create("alternative") .addBodyPart(inlinedTextPart) .build(); Message message = Message.Builder.of() .setBody(multipartAlternative) .build(); //When MessageContent actual = testee.extract(message); //Then assertThat(actual.getTextBody()).contains(TEXT_CONTENT); }
@Override public InputStream getInputStream() throws IOException { Body body = bodyPart.getBody(); InputStream result = null; if (body instanceof TextBody) { throw new UnsupportedOperationException(); /* * InputStreamReader reader = (InputStreamReader)((TextBody) * body).getReader(); StringBuilder inputBuilder = new * StringBuilder(); char[] buffer = new char[1024]; while (true) * { int readCount = reader.read(buffer); if (readCount < 0) { * break; } inputBuilder.append(buffer, 0, readCount); } String * str = inputBuilder.toString(); return new * ByteArrayInputStream(str.getBytes(reader.getEncoding())); */ } else if (body instanceof BinaryBody) { return ((BinaryBody) body).getInputStream(); } return result; }
@Override protected InputPart extractPart(BodyPart bodyPart) throws IOException { InputPart currPart = super.extractPart(bodyPart); Field disposition = bodyPart.getHeader().getField( FieldName.CONTENT_DISPOSITION); if (disposition == null) throw new RuntimeException(Messages.MESSAGES.couldFindNoContentDispositionHeader()); if (disposition instanceof ContentDispositionField) { String name = ((ContentDispositionField) disposition) .getParameter("name"); List<InputPart> list = formDataMap.get(name); if (list == null) { list = new LinkedList<InputPart>(); formData.put(name, currPart); formDataMap.put(name, list); } list.add(currPart); } else { throw new RuntimeException(Messages.MESSAGES.couldNotParseContentDisposition(disposition)); } return currPart; }
@Test void extractShouldNotRetrieveHtmlBodyWithOneInlinedHTMLAttachmentWithCid() throws IOException { //Given BodyPart inlinedHTMLPart = BodyPartBuilder.create() .setBody(HTML_CONTENT, "html", StandardCharsets.UTF_8) .build(); HeaderImpl inlinedHeader = new HeaderImpl(); inlinedHeader.addField(Fields.contentDisposition(MimeMessage.INLINE)); inlinedHeader.addField(Fields.contentType("text/html; charset=utf-8")); inlinedHeader.addField(CONTENT_ID_FIELD); inlinedHTMLPart.setHeader(inlinedHeader); Multipart multipartAlternative = MultipartBuilder.create("alternative") .addBodyPart(inlinedHTMLPart) .build(); Message message = Message.Builder.of() .setBody(multipartAlternative) .build(); //When MessageContent actual = testee.extract(message); //Then assertThat(actual.getHtmlBody()).isEmpty(); }
public InputStream getBody() throws IOException { Body body = bodyPart.getBody(); InputStream result = null; if (body instanceof TextBody) { throw new UnsupportedOperationException(); /* InputStreamReader reader = (InputStreamReader)((TextBody) body).getReader(); StringBuilder inputBuilder = new StringBuilder(); char[] buffer = new char[1024]; while (true) { int readCount = reader.read(buffer); if (readCount < 0) { break; } inputBuilder.append(buffer, 0, readCount); } String str = inputBuilder.toString(); return new ByteArrayInputStream(str.getBytes(reader.getEncoding())); */ } else if (body instanceof BinaryBody) { return ((BinaryBody)body).getInputStream(); } return result; }
public PartImpl(BodyPart bodyPart) { this.bodyPart = bodyPart; for (Field field : bodyPart.getHeader()) { headers.add(field.getName(), field.getBody()); if (field instanceof ContentTypeField) { contentType = MediaType.valueOf(field.getBody()); } } if (contentType == null) contentType = defaultPartContentType; if (getCharset(contentType) == null) { if (defaultPartCharset != null) { contentType = getMediaTypeWithDefaultCharset(contentType); } else if (contentType.getType().equalsIgnoreCase("text")) { contentType = getMediaTypeWithCharset(contentType, "us-ascii"); } } }
@Test void extractShouldNotRetrieveTextBodyWithOneInlinedTextAttachmentWithCid() throws IOException { //Given BodyPart inlinedTextPart = BodyPartBuilder.create() .setBody(TEXT_CONTENT, "text", StandardCharsets.UTF_8) .build(); HeaderImpl inlinedHeader = new HeaderImpl(); inlinedHeader.addField(Fields.contentDisposition(MimeMessage.INLINE)); inlinedHeader.addField(Fields.contentType("text/plain; charset=utf-8")); inlinedHeader.addField(CONTENT_ID_FIELD); inlinedTextPart.setHeader(inlinedHeader); Multipart multipartAlternative = MultipartBuilder.create("alternative") .addBodyPart(inlinedTextPart) .build(); Message message = Message.Builder.of() .setBody(multipartAlternative) .build(); //When MessageContent actual = testee.extract(message); //Then assertThat(actual.getTextBody()).isEmpty(); }
f.setAccessible(true); BodyPart bodyPart = (BodyPart) f.get(inputPart); SingleBody body = (SingleBody)bodyPart.getBody();