/** * Sets body of this message. Also sets the content type based on properties of * the given {@link org.apache.james.mime4j.dom.Body}. * * @param body * the body. */ public AbstractEntityBuilder setBody(Multipart multipart) { this.body = multipart; if (multipart != null) { List<NameValuePair> parameters = new ArrayList<NameValuePair>(multipart.getContentTypeParameters()); parameters.add(new NameValuePair("boundary", MimeUtil.createUniqueBoundary())); setField(Fields.contentType("multipart/" + multipart.getSubType(), parameters.toArray(new NameValuePair[0]))); } else { removeFields(FieldName.CONTENT_TYPE); } return this; }
private void parse() { parsed = true; RawField f = getRawField(); RawBody body = RawFieldParser.DEFAULT.parseRawBody(f); String main = body.getValue(); if (main != null) { dispositionType = main.toLowerCase(Locale.US); } else { dispositionType = null; } parameters.clear(); for (NameValuePair nmp: body.getParams()) { String name = nmp.getName().toLowerCase(Locale.US); parameters.put(name, nmp.getValue()); } }
/** * Creates a <i>Content-Disposition</i> field from the specified * disposition type and parameters. * * @param dispositionType * a disposition type (usually <code>"inline"</code> * or <code>"attachment"</code>). * @param parameters * list of disposition parameter name/value pairs. * @return the newly created <i>Content-Disposition</i> field. */ public static ContentDispositionField contentDisposition( String dispositionType, Iterable<NameValuePair> parameters) { if (!isValidDispositionType(dispositionType)) throw new IllegalArgumentException(); if (parameters == null) { return parse(ContentDispositionFieldImpl.PARSER, FieldName.CONTENT_DISPOSITION, dispositionType); } else { StringBuilder sb = new StringBuilder(dispositionType); for (NameValuePair param : parameters) { sb.append("; "); String name = param.getName(); String value = param.getValue(); sb.append(EncoderUtil.encodeHeaderParameter(name, value != null ? value : "")); } String contentDisposition = sb.toString(); return contentDisposition(contentDisposition); } }
public MultipartBuilder addTextPart(String text, Charset charset) throws IOException { Charset cs = charset != null ? charset : Charsets.ISO_8859_1; TextBody body = bodyFactory != null ? bodyFactory.textBody( InputStreams.create(text, cs), cs.name()) : BasicBodyFactory.INSTANCE.textBody(text, cs); BodyPart bodyPart = BodyPartBuilder.create() .setBody(body) .setContentType("text/plain", new NameValuePair("charset", cs.name())) .setContentTransferEncoding(Charsets.US_ASCII.equals(cs) ? "7bit" : "quoted-printable") .build(); return addBodyPart(bodyPart); }
/** * Parses the sequence of bytes containing a field parameter delimited with semicolon into * {@link NameValuePair}. * * @param buf buffer with the sequence of bytes to be parsed * @param cursor defines the bounds and current position of the buffer */ public NameValuePair parseParameter(final ByteSequence buf, final ParserCursor cursor) { String name = parseToken(buf, cursor, EQUAL_OR_SEMICOLON); if (cursor.atEnd()) { return new NameValuePair(name, null); } int delim = buf.byteAt(cursor.getPos()); cursor.updatePos(cursor.getPos() + 1); if (delim == ';') { return new NameValuePair(name, null); } String value = parseValue(buf, cursor, SEMICOLON); if (!cursor.atEnd()) { cursor.updatePos(cursor.getPos() + 1); } return new NameValuePair(name, value); }
/** * Sets body of this message. Also sets the content type based on properties of * the given {@link org.apache.james.mime4j.dom.Body}. * * @param body * the body. */ public AbstractEntityBuilder setBody(TextBody textBody) { this.body = textBody; if (textBody != null) { String mimeCharset = textBody.getMimeCharset(); if ("us-ascii".equalsIgnoreCase(mimeCharset)) { mimeCharset = null; } if (mimeCharset != null) { setField(Fields.contentType("text/plain", new NameValuePair("charset", mimeCharset))); } else { setField(Fields.contentType("text/plain")); } } else { removeFields(FieldName.CONTENT_TYPE); } return this; }
/** * Sets text of this message with the given MIME subtype and charset. * * @param text * the text. * @param charset * the charset of the text. * @param subtype * the text subtype (e.g. "plain", "html" or * "xml"). */ public Builder setBody(String text, String subtype, Charset charset) throws IOException { String mimeType = "text/" + (subtype != null ? subtype : "plain"); if (charset != null) { setField(Fields.contentType(mimeType, new NameValuePair("charset", charset.name()))); } else { setField(Fields.contentType(mimeType)); } Body textBody; if (bodyFactory != null) { textBody = bodyFactory.textBody( InputStreams.create(text, charset), charset != null ? charset.name() : null); } else { textBody = BasicBodyFactory.INSTANCE.textBody(text, charset); } return setBody(textBody); }
/** * Sets text of this message with the given MIME subtype and charset. * * @param text * the text. * @param charset * the charset of the text. * @param subtype * the text subtype (e.g. "plain", "html" or * "xml"). */ public BodyPartBuilder setBody(String text, String subtype, Charset charset) throws IOException { String mimeType = "text/" + (subtype != null ? subtype : "plain"); if (charset != null) { setField(Fields.contentType(mimeType, new NameValuePair("charset", charset.name()))); } else { setField(Fields.contentType(mimeType)); } Body textBody; if (bodyFactory != null) { textBody = bodyFactory.textBody( InputStreams.create(text, charset), charset != null ? charset.name() : null); } else { textBody = BasicBodyFactory.INSTANCE.textBody(text, charset); } return setBody(textBody); }
private Multipart asMime4JMultipart() throws IOException { MultipartBuilder builder = MultipartBuilder.create(REPORT_SUB_TYPE); builder.addContentTypeParameter(new NameValuePair("report-type", DISPOSITION_NOTIFICATION_REPORT_TYPE)); builder.addBodyPart(BodyPartBuilder.create() .use(new BasicBodyFactory()) .setBody(humanReadableText, Charsets.UTF_8) .setContentType("text/plain", UTF_8_CHARSET)); builder.addBodyPart(BodyPartBuilder.create() .use(new BasicBodyFactory()) .setBody(report.formattedValue(), Charsets.UTF_8) .setContentType(DISPOSITION_CONTENT_TYPE, UTF_8_CHARSET)); return builder.build(); }