Upgraded mime4j to version 0.6

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@750894 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2009-03-06 13:50:50 +00:00
parent 8849e82869
commit 9940cb5982
12 changed files with 233 additions and 122 deletions

View File

@ -32,9 +32,7 @@
package org.apache.http.entity.mime;
import org.apache.http.entity.mime.content.ContentBody;
import org.apache.james.mime4j.MimeException;
import org.apache.james.mime4j.descriptor.ContentDescriptor;
import org.apache.james.mime4j.field.Field;
import org.apache.james.mime4j.message.BodyPart;
import org.apache.james.mime4j.message.Header;
@ -75,8 +73,7 @@ public class FormBodyPart extends BodyPart {
protected void generateContentDisp(final ContentBody body) {
StringBuilder buffer = new StringBuilder();
buffer.append(MIME.CONTENT_DISPOSITION);
buffer.append(": form-data; name=\"");
buffer.append("form-data; name=\"");
buffer.append(getName());
buffer.append("\"");
if (body.getFilename() != null) {
@ -84,40 +81,29 @@ public class FormBodyPart extends BodyPart {
buffer.append(body.getFilename());
buffer.append("\"");
}
addField(buffer.toString());
addField(MIME.CONTENT_DISPOSITION, buffer.toString());
}
protected void generateContentType(final ContentDescriptor desc) {
if (desc.getMimeType() != null) {
StringBuilder buffer = new StringBuilder();
buffer.append(MIME.CONTENT_TYPE);
buffer.append(": ");
buffer.append(desc.getMimeType());
if (desc.getCharset() != null) {
buffer.append("; charset=");
buffer.append(desc.getCharset());
}
addField(buffer.toString());
addField(MIME.CONTENT_TYPE, buffer.toString());
}
}
protected void generateTransferEncoding(final ContentDescriptor desc) {
if (desc.getTransferEncoding() != null) {
StringBuilder buffer = new StringBuilder();
buffer.append(MIME.CONTENT_TRANSFER_ENC);
buffer.append(": ");
buffer.append(desc.getTransferEncoding());
addField(buffer.toString());
addField(MIME.CONTENT_TRANSFER_ENC, desc.getTransferEncoding());
}
}
private void addField(final String s) {
try {
getHeader().addField(Field.parse(s));
} catch (MimeException ex) {
// Should never happen
throw new UnexpectedMimeException(ex);
}
private void addField(final String name, final String value) {
getHeader().addField(new MinimalField(name, value));
}
}

View File

@ -31,25 +31,28 @@
package org.apache.http.entity.mime;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.util.List;
import org.apache.http.entity.mime.content.ContentBody;
import org.apache.http.protocol.HTTP;
import org.apache.james.mime4j.MimeException;
import org.apache.james.mime4j.field.ContentTypeField;
import org.apache.james.mime4j.field.Field;
import org.apache.james.mime4j.field.FieldName;
import org.apache.james.mime4j.message.Body;
import org.apache.james.mime4j.message.BodyPart;
import org.apache.james.mime4j.message.Entity;
import org.apache.james.mime4j.message.Header;
import org.apache.james.mime4j.message.MessageWriter;
import org.apache.james.mime4j.message.Multipart;
import org.apache.james.mime4j.parser.Field;
import org.apache.james.mime4j.util.ByteArrayBuffer;
import org.apache.james.mime4j.util.ByteSequence;
import org.apache.james.mime4j.util.CharsetUtil;
import org.apache.james.mime4j.util.MessageUtils;
/**
* An extension of the mime4j standard {@link Multipart} class, which is
@ -61,6 +64,28 @@ import org.apache.james.mime4j.util.MessageUtils;
*/
public class HttpMultipart extends Multipart {
private static ByteArrayBuffer encode(Charset charset, String string) {
ByteBuffer encoded = charset.encode(CharBuffer.wrap(string));
ByteArrayBuffer bab = new ByteArrayBuffer(encoded.remaining());
bab.append(encoded.array(), encoded.position(), encoded.remaining());
return bab;
}
private static void writeBytes(ByteArrayBuffer b, OutputStream out) throws IOException {
out.write(b.buffer(), 0, b.length());
}
private static void writeBytes(ByteSequence b, OutputStream out) throws IOException {
if (b instanceof ByteArrayBuffer) {
writeBytes((ByteArrayBuffer) b, out);
} else {
out.write(b.toByteArray());
}
}
private static final ByteArrayBuffer CR_LF = encode(MIME.DEFAULT_CHARSET, "\r\n");
private static final ByteArrayBuffer TWO_DASHES = encode(MIME.DEFAULT_CHARSET, "--");
private HttpMultipartMode mode;
public HttpMultipart(final String subType) {
@ -79,7 +104,7 @@ public class HttpMultipart extends Multipart {
protected Charset getCharset() {
Entity e = getParent();
ContentTypeField cField = (ContentTypeField) e.getHeader().getField(
Field.CONTENT_TYPE);
FieldName.CONTENT_TYPE);
Charset charset = null;
switch (this.mode) {
@ -100,7 +125,7 @@ public class HttpMultipart extends Multipart {
protected String getBoundary() {
Entity e = getParent();
ContentTypeField cField = (ContentTypeField) e.getHeader().getField(
Field.CONTENT_TYPE);
FieldName.CONTENT_TYPE);
return cField.getBoundary();
}
@ -109,44 +134,49 @@ public class HttpMultipart extends Multipart {
final OutputStream out,
boolean writeContent) throws IOException {
List<?> bodyParts = getBodyParts();
List<BodyPart> bodyParts = getBodyParts();
Charset charset = getCharset();
String boundary = getBoundary();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(out, charset),
8192);
ByteArrayBuffer boundary = encode(charset, getBoundary());
switch (mode) {
case STRICT:
String preamble = getPreamble();
if (preamble != null && preamble.length() != 0) {
writer.write(preamble);
writer.write("\r\n");
ByteArrayBuffer b = encode(charset, preamble);
writeBytes(b, out);
writeBytes(CR_LF, out);
}
for (int i = 0; i < bodyParts.size(); i++) {
writer.write("--");
writer.write(boundary);
writer.write("\r\n");
writer.flush();
BodyPart part = (BodyPart) bodyParts.get(i);
part.getHeader().writeTo(out, MessageUtils.STRICT_IGNORE);
if (writeContent) {
part.getBody().writeTo(out, MessageUtils.STRICT_IGNORE);
}
writer.write("\r\n");
}
writeBytes(TWO_DASHES, out);
writeBytes(boundary, out);
writeBytes(CR_LF, out);
writer.write("--");
writer.write(boundary);
writer.write("--\r\n");
BodyPart part = bodyParts.get(i);
Header header = part.getHeader();
List<Field> fields = header.getFields();
for (Field field: fields) {
writeBytes(field.getRaw(), out);
writeBytes(CR_LF, out);
}
writeBytes(CR_LF, out);
if (writeContent) {
MessageWriter.DEFAULT.writeBody(part.getBody(), out);
}
writeBytes(CR_LF, out);
}
writeBytes(TWO_DASHES, out);
writeBytes(boundary, out);
writeBytes(TWO_DASHES, out);
writeBytes(CR_LF, out);
String epilogue = getEpilogue();
if (epilogue != null && epilogue.length() != 0) {
writer.write(epilogue);
writer.write("\r\n");
ByteArrayBuffer b = encode(charset, epilogue);
writeBytes(b, out);
writeBytes(CR_LF, out);
}
writer.flush();
break;
case BROWSER_COMPATIBLE:
@ -155,28 +185,30 @@ public class HttpMultipart extends Multipart {
// (3) Use content charset
for (int i = 0; i < bodyParts.size(); i++) {
writer.write("--");
writer.write(boundary);
writer.write("\r\n");
writer.flush();
writeBytes(TWO_DASHES, out);
writeBytes(boundary, out);
writeBytes(CR_LF, out);
BodyPart part = (BodyPart) bodyParts.get(i);
Field cd = part.getHeader().getField(MIME.CONTENT_DISPOSITION);
writer.write(cd.toString());
writer.write("\r\n");
writer.write("\r\n");
writer.flush();
if (writeContent) {
part.getBody().writeTo(out, MessageUtils.LENIENT);
}
writer.write("\r\n");
StringBuilder s = new StringBuilder();
s.append(cd.getName());
s.append(": ");
s.append(cd.getBody());
writeBytes(encode(charset, s.toString()), out);
writeBytes(CR_LF, out);
writeBytes(CR_LF, out);
if (writeContent) {
MessageWriter.DEFAULT.writeBody(part.getBody(), out);
}
writeBytes(CR_LF, out);
}
writer.write("--");
writer.write(boundary);
writer.write("--\r\n");
writer.flush();
writeBytes(TWO_DASHES, out);
writeBytes(boundary, out);
writeBytes(TWO_DASHES, out);
writeBytes(CR_LF, out);
break;
}
}
@ -192,15 +224,6 @@ public class HttpMultipart extends Multipart {
doWriteTo(this.mode, out, true);
}
@Override
public void writeTo(final OutputStream out, int mode) throws IOException, MimeException {
if (mode == MessageUtils.LENIENT) {
doWriteTo(HttpMultipartMode.BROWSER_COMPATIBLE, out, true);
} else {
doWriteTo(HttpMultipartMode.STRICT, out, true);
}
}
/**
* Determines the total length of the multipart content (content length of
* individual parts plus that of extra elements required to delimit the parts

View File

@ -33,7 +33,7 @@ package org.apache.http.entity.mime;
import java.nio.charset.Charset;
import org.apache.james.mime4j.field.Field;
import org.apache.james.mime4j.field.FieldName;
import org.apache.james.mime4j.util.CharsetUtil;
/**
@ -42,8 +42,8 @@ import org.apache.james.mime4j.util.CharsetUtil;
*/
public final class MIME {
public static final String CONTENT_TYPE = Field.CONTENT_TYPE;
public static final String CONTENT_TRANSFER_ENC = Field.CONTENT_TRANSFER_ENCODING;
public static final String CONTENT_TYPE = FieldName.CONTENT_TYPE;
public static final String CONTENT_TRANSFER_ENC = FieldName.CONTENT_TRANSFER_ENCODING;
public static final String CONTENT_DISPOSITION = "Content-Disposition";
public static final String ENC_8BIT = "8bit";

View File

@ -0,0 +1,81 @@
/*
* $HeadURL:$
* $Revision:$
* $Date:$
*
* ====================================================================
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.http.entity.mime;
import org.apache.james.mime4j.parser.Field;
import org.apache.james.mime4j.util.ByteSequence;
import org.apache.james.mime4j.util.ContentUtil;
/**
* Minimal implementation of {@link Field}.
*
* @since 4.0
*/
public class MinimalField implements Field {
private final String name;
private final String value;
private ByteSequence raw;
MinimalField(final String name, final String value) {
super();
this.name = name;
this.value = value;
this.raw = null;
}
public String getName() {
return this.name;
}
public String getBody() {
return this.value;
}
public ByteSequence getRaw() {
if (this.raw == null) {
this.raw = ContentUtil.encode(toString());
}
return this.raw;
}
@Override
public String toString() {
StringBuilder buffer = new StringBuilder();
buffer.append(this.name);
buffer.append(": ");
buffer.append(this.value);
return buffer.toString();
}
}

View File

@ -44,8 +44,7 @@ import org.apache.http.HttpEntity;
import org.apache.http.entity.mime.content.ContentBody;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;
import org.apache.james.mime4j.MimeException;
import org.apache.james.mime4j.field.Field;
import org.apache.james.mime4j.field.Fields;
import org.apache.james.mime4j.message.Message;
/**
@ -91,7 +90,7 @@ public class MultipartEntity implements HttpEntity {
mode = HttpMultipartMode.STRICT;
}
this.multipart.setMode(mode);
addField("Content-Type: " + this.contentType.getValue());
this.message.getHeader().addField(Fields.contentType(this.contentType.getValue()));
}
public MultipartEntity(final HttpMultipartMode mode) {
@ -181,13 +180,4 @@ public class MultipartEntity implements HttpEntity {
this.multipart.writeTo(outstream);
}
private void addField(final String s) {
try {
this.message.getHeader().addField(Field.parse(s));
} catch (MimeException ex) {
// Should never happen
throw new UnexpectedMimeException(ex);
}
}
}

View File

@ -33,9 +33,11 @@ package org.apache.http.entity.mime;
import org.apache.james.mime4j.MimeException;
/**
* @deprecated no longer used.
*
* @since 4.0
*/
@Deprecated
public class UnexpectedMimeException extends RuntimeException {
private static final long serialVersionUID = 1316818299528463579L;

View File

@ -34,18 +34,21 @@ package org.apache.http.entity.mime.content;
import java.util.Collections;
import java.util.Map;
import org.apache.james.mime4j.message.AbstractBody;
import org.apache.james.mime4j.message.Entity;
import org.apache.james.mime4j.message.SingleBody;
/**
*
* @since 4.0
*/
public abstract class AbstractContentBody extends AbstractBody implements ContentBody {
public abstract class AbstractContentBody extends SingleBody implements ContentBody {
private final String mimeType;
private final String mediaType;
private final String subType;
private Entity parent = null;
public AbstractContentBody(final String mimeType) {
super();
if (mimeType == null) {
@ -61,7 +64,15 @@ public abstract class AbstractContentBody extends AbstractBody implements Conten
this.subType = null;
}
}
public Entity getParent() {
return this.parent;
}
public void setParent(final Entity parent) {
this.parent = parent;
}
public String getMimeType() {
return this.mimeType;
}
@ -74,8 +85,11 @@ public abstract class AbstractContentBody extends AbstractBody implements Conten
return this.subType;
}
public Map<?, ?> getContentTypeParameters() {
return Collections.EMPTY_MAP;
public Map<String, String> getContentTypeParameters() {
return Collections.emptyMap();
}
public void dispose() {
}
}

View File

@ -38,13 +38,12 @@ import java.io.InputStream;
import java.io.OutputStream;
import org.apache.http.entity.mime.MIME;
import org.apache.james.mime4j.message.BinaryBody;
/**
*
* @since 4.0
*/
public class FileBody extends AbstractContentBody implements BinaryBody {
public class FileBody extends AbstractContentBody {
private final File file;
@ -64,7 +63,15 @@ public class FileBody extends AbstractContentBody implements BinaryBody {
return new FileInputStream(this.file);
}
/**
* @deprecated use {@link #writeTo(OutputStream)}
*/
@Deprecated
public void writeTo(final OutputStream out, int mode) throws IOException {
writeTo(out);
}
public void writeTo(final OutputStream out) throws IOException {
if (out == null) {
throw new IllegalArgumentException("Output stream may not be null");
}
@ -100,5 +107,5 @@ public class FileBody extends AbstractContentBody implements BinaryBody {
public File getFile() {
return this.file;
}
}

View File

@ -36,13 +36,12 @@ import java.io.InputStream;
import java.io.OutputStream;
import org.apache.http.entity.mime.MIME;
import org.apache.james.mime4j.message.BinaryBody;
/**
*
* @since 4.0
*/
public class InputStreamBody extends AbstractContentBody implements BinaryBody {
public class InputStreamBody extends AbstractContentBody {
private final InputStream in;
private final String filename;
@ -64,7 +63,15 @@ public class InputStreamBody extends AbstractContentBody implements BinaryBody {
return this.in;
}
/**
* @deprecated use {@link #writeTo(OutputStream)}
*/
@Deprecated
public void writeTo(final OutputStream out, int mode) throws IOException {
writeTo(out);
}
public void writeTo(final OutputStream out) throws IOException {
if (out == null) {
throw new IllegalArgumentException("Output stream may not be null");
}

View File

@ -43,13 +43,12 @@ import java.util.HashMap;
import java.util.Map;
import org.apache.http.entity.mime.MIME;
import org.apache.james.mime4j.message.TextBody;
/**
*
* @since 4.0
*/
public class StringBody extends AbstractContentBody implements TextBody {
public class StringBody extends AbstractContentBody {
private final byte[] content;
private final Charset charset;
@ -83,7 +82,15 @@ public class StringBody extends AbstractContentBody implements TextBody {
this.charset);
}
/**
* @deprecated use {@link #writeTo(OutputStream)}
*/
@Deprecated
public void writeTo(final OutputStream out, int mode) throws IOException {
writeTo(out);
}
public void writeTo(final OutputStream out) throws IOException {
if (out == null) {
throw new IllegalArgumentException("Output stream may not be null");
}
@ -105,8 +112,8 @@ public class StringBody extends AbstractContentBody implements TextBody {
}
@Override
public Map<?, ?> getContentTypeParameters() {
Map<Object, Object> map = new HashMap<Object, Object>();
public Map<String, String> getContentTypeParameters() {
Map<String, String> map = new HashMap<String, String>();
map.put("charset", this.charset.name());
return map;
}

View File

@ -48,7 +48,7 @@ import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.InputStreamBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.james.mime4j.field.Field;
import org.apache.james.mime4j.field.Fields;
import org.apache.james.mime4j.message.BodyPart;
import org.apache.james.mime4j.message.Header;
import org.apache.james.mime4j.message.Message;
@ -75,25 +75,24 @@ public class TestMultipartForm extends TestCase {
public void testMultipartFormLowLevel() throws Exception {
Message message = new Message();
Header header = new Header();
header.addField(
Field.parse("Content-Type: multipart/form-data; boundary=foo"));
header.addField(Fields.contentType("multipart/form-data; boundary=foo"));
message.setHeader(header);
HttpMultipart multipart = new HttpMultipart("form-data");
multipart.setParent(message);
BodyPart p1 = new BodyPart();
Header h1 = new Header();
h1.addField(Field.parse("Content-Type: text/plain"));
h1.addField(Fields.contentType("text/plain"));
p1.setHeader(h1);
p1.setBody(new StringBody("this stuff"));
BodyPart p2 = new BodyPart();
Header h2 = new Header();
h2.addField(Field.parse("Content-Type: text/plain"));
h2.addField(Fields.contentType("text/plain"));
p2.setHeader(h2);
p2.setBody(new StringBody("that stuff"));
BodyPart p3 = new BodyPart();
Header h3 = new Header();
h3.addField(Field.parse("Content-Type: text/plain"));
h3.addField(Fields.contentType("text/plain"));
p3.setHeader(h3);
p3.setBody(new StringBody("all kind of stuff"));
@ -127,8 +126,7 @@ public class TestMultipartForm extends TestCase {
public void testMultipartFormStringParts() throws Exception {
Message message = new Message();
Header header = new Header();
header.addField(
Field.parse("Content-Type: multipart/form-data; boundary=foo"));
header.addField(Fields.contentType("multipart/form-data; boundary=foo"));
message.setHeader(header);
HttpMultipart multipart = new HttpMultipart("form-data");
@ -181,8 +179,7 @@ public class TestMultipartForm extends TestCase {
public void testMultipartFormBinaryParts() throws Exception {
Message message = new Message();
Header header = new Header();
header.addField(
Field.parse("Content-Type: multipart/form-data; boundary=foo"));
header.addField(Fields.contentType("multipart/form-data; boundary=foo"));
message.setHeader(header);
File tmpfile = File.createTempFile("tmp", ".bin");
@ -236,8 +233,7 @@ public class TestMultipartForm extends TestCase {
public void testMultipartFormBrowserCompatible() throws Exception {
Message message = new Message();
Header header = new Header();
header.addField(
Field.parse("Content-Type: multipart/form-data; boundary=foo"));
header.addField(Fields.contentType("multipart/form-data; boundary=foo"));
message.setHeader(header);
File tmpfile = File.createTempFile("tmp", ".bin");
@ -311,8 +307,7 @@ public class TestMultipartForm extends TestCase {
Message message = new Message();
Header header = new Header();
header.addField(
Field.parse("Content-Type: multipart/form-data; charset=UTF-8; boundary=foo"));
header.addField(Fields.contentType("multipart/form-data; charset=UTF-8; boundary=foo"));
message.setHeader(header);
File tmpfile = File.createTempFile("tmp", ".bin");
@ -367,8 +362,7 @@ public class TestMultipartForm extends TestCase {
Message message = new Message();
Header header = new Header();
header.addField(
Field.parse("Content-Type: multipart/form-data; boundary=foo"));
header.addField(Fields.contentType("multipart/form-data; boundary=foo"));
message.setHeader(header);
HttpMultipart multipart = new HttpMultipart("form-data");

View File

@ -75,7 +75,7 @@
<httpcore.version>4.0</httpcore.version>
<commons-logging.version>1.1.1</commons-logging.version>
<commons-codec.version>1.3</commons-codec.version>
<mime4j.version>0.5</mime4j.version>
<mime4j.version>0.6</mime4j.version>
<junit.version>3.8.2</junit.version>
</properties>