Dropped dependency on Mime4j

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@934110 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2010-04-14 18:45:17 +00:00
parent 0f538860bd
commit 379e2a6bfb
18 changed files with 407 additions and 429 deletions

View File

@ -1,6 +1,14 @@
Changes since 4.1 ALPHA1
Release 4.1 ALPHA2
-------------------
* Dropped dependency on Mime4j for HttpMime.
Contributed by Oleg Kalnichevski <olegk at apache.org>
* Extended SSLSocketFactory with a mechanism to bypass the standard certificate
trust verification (primarily to simplify dealing with self-signed
certificates)
Contributed by Oleg Kalnichevski <olegk at apache.org>
* [HTTPCLIENT-916] UsernamePasswordCredentials, NTUserPrincipal,
BasicClientCookie, BasicClientCookie2 and BasicCookieStore made Serializable.
Contributed by Oleg Kalnichevski <olegk at apache.org>
@ -8,8 +16,9 @@ Changes since 4.1 ALPHA1
* [HTTPCLIENT-914] Upgraded Commons Codec dependency to version 1.4
Contributed by Oleg Kalnichevski <olegk at apache.org>
* [HTTPCLIENT-903] Use ConcurrentHashMap instead of [Linked]HashMap for thread-safety
Improve performance of AuthSchemeRegistry, CookieSpecRegistry and SchemeRegistry classes.
* [HTTPCLIENT-903] Use ConcurrentHashMap instead of [Linked]HashMap for
thread-safety. Improve performance of AuthSchemeRegistry, CookieSpecRegistry
and SchemeRegistry classes.
Contributed by Sebastian Bazley <sebb at apache.org>
* [HTTPCLIENT-902] HttpRequestRetryHandler not called on I/O exceptions

View File

@ -54,16 +54,6 @@
<artifactId>httpcore</artifactId>
<version>${httpcore.version}</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>${pom.version}</version>
</dependency>
<dependency>
<groupId>org.apache.james</groupId>
<artifactId>apache-mime4j</artifactId>
<version>${mime4j.version}</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>

View File

@ -27,25 +27,21 @@
package org.apache.http.entity.mime;
import org.apache.http.annotation.NotThreadSafe;
import org.apache.http.entity.mime.content.ContentBody;
import org.apache.james.mime4j.descriptor.ContentDescriptor;
import org.apache.james.mime4j.message.BodyPart;
import org.apache.james.mime4j.message.Header;
/**
* An extension of the mime4j standard {@link BodyPart} class that
* automatically populates the header with standard fields based
* on the content description of the enclosed body.
*
*
* @since 4.0
*/
@NotThreadSafe // Entity is @NotThreadSafe
public class FormBodyPart extends BodyPart {
public class FormBodyPart {
private final String name;
private final Header header;
private ContentBody body;
public FormBodyPart(final String name, final ContentBody body) {
super();
@ -56,11 +52,9 @@ public class FormBodyPart extends BodyPart {
throw new IllegalArgumentException("Body may not be null");
}
this.name = name;
this.body = body;
this.header = new Header();
Header header = new Header();
setHeader(header);
setBody(body);
generateContentDisp(body);
generateContentType(body);
generateTransferEncoding(body);
@ -70,6 +64,21 @@ public class FormBodyPart extends BodyPart {
return this.name;
}
public ContentBody getBody() {
return this.body;
}
public Header getHeader() {
return this.header;
}
public void addField(final String name, final String value) {
if (name == null) {
throw new IllegalArgumentException("Field name may not be null");
}
this.header.addField(new MinimalField(name, value));
}
protected void generateContentDisp(final ContentBody body) {
StringBuilder buffer = new StringBuilder();
buffer.append("form-data; name=\"");
@ -83,26 +92,22 @@ public class FormBodyPart extends BodyPart {
addField(MIME.CONTENT_DISPOSITION, buffer.toString());
}
protected void generateContentType(final ContentDescriptor desc) {
if (desc.getMimeType() != null) {
protected void generateContentType(final ContentBody body) {
if (body.getMimeType() != null) {
StringBuilder buffer = new StringBuilder();
buffer.append(desc.getMimeType());
if (desc.getCharset() != null) {
buffer.append(body.getMimeType());
if (body.getCharset() != null) {
buffer.append("; charset=");
buffer.append(desc.getCharset());
buffer.append(body.getCharset());
}
addField(MIME.CONTENT_TYPE, buffer.toString());
}
}
protected void generateTransferEncoding(final ContentDescriptor desc) {
if (desc.getTransferEncoding() != null) {
addField(MIME.CONTENT_TRANSFER_ENC, desc.getTransferEncoding());
protected void generateTransferEncoding(final ContentBody body) {
if (body.getTransferEncoding() != null) {
addField(MIME.CONTENT_TRANSFER_ENC, body.getTransferEncoding());
}
}
private void addField(final String name, final String value) {
getHeader().addField(new MinimalField(name, value));
}
}

View File

@ -0,0 +1,145 @@
/*
* ====================================================================
* 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 java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
/**
* The header of an entity (see RFC 2045).
*/
public class Header implements Iterable<MinimalField> {
private final List<MinimalField> fields;
private final Map<String, List<MinimalField>> fieldMap;
public Header() {
super();
this.fields = new LinkedList<MinimalField>();
this.fieldMap = new HashMap<String, List<MinimalField>>();
}
public void addField(final MinimalField field) {
if (field == null) {
return;
}
String key = field.getName().toLowerCase(Locale.US);
List<MinimalField> values = this.fieldMap.get(key);
if (values == null) {
values = new LinkedList<MinimalField>();
this.fieldMap.put(key, values);
}
values.add(field);
this.fields.add(field);
}
public List<MinimalField> getFields() {
return new ArrayList<MinimalField>(this.fields);
}
public MinimalField getField(final String name) {
if (name == null) {
return null;
}
String key = name.toLowerCase(Locale.US);
List<MinimalField> list = this.fieldMap.get(key);
if (list != null && !list.isEmpty()) {
return list.get(0);
}
return null;
}
public List<MinimalField> getFields(final String name) {
if (name == null) {
return null;
}
String key = name.toLowerCase(Locale.US);
List<MinimalField> list = this.fieldMap.get(key);
if (list == null || list.isEmpty()) {
return Collections.emptyList();
} else {
return new ArrayList<MinimalField>(list);
}
}
public int removeFields(final String name) {
if (name == null) {
return 0;
}
String key = name.toLowerCase(Locale.US);
List<MinimalField> removed = fieldMap.remove(key);
if (removed == null || removed.isEmpty()) {
return 0;
}
this.fields.removeAll(removed);
return removed.size();
}
public void setField(final MinimalField field) {
if (field == null) {
return;
}
String key = field.getName().toLowerCase(Locale.US);
List<MinimalField> list = fieldMap.get(key);
if (list == null || list.isEmpty()) {
addField(field);
return;
}
list.clear();
list.add(field);
int firstOccurrence = -1;
int index = 0;
for (Iterator<MinimalField> it = this.fields.iterator(); it.hasNext(); index++) {
MinimalField f = it.next();
if (f.getName().equalsIgnoreCase(field.getName())) {
it.remove();
if (firstOccurrence == -1) {
firstOccurrence = index;
}
}
}
this.fields.add(firstOccurrence, field);
}
public Iterator<MinimalField> iterator() {
return Collections.unmodifiableList(fields).iterator();
}
@Override
public String toString() {
return this.fields.toString();
}
}

View File

@ -33,64 +33,84 @@ import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.annotation.NotThreadSafe;
import org.apache.http.entity.mime.content.ContentBody;
import org.apache.http.protocol.HTTP;
import org.apache.james.mime4j.field.ContentTypeField;
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.http.util.ByteArrayBuffer;
/**
* An extension of the mime4j standard {@link Multipart} class, which is
* capable of operating either in the strict (fully RFC 822, RFC 2045,
* RFC 2046 compliant) or the browser compatible modes.
*
* An extension of the mime4j standard {@link Multipart} class, which is capable of operating
* either in the strict (fully RFC 822, RFC 2045, RFC 2046 compliant) or the browser compatible
* modes.
*
* @since 4.0
*/
@NotThreadSafe // parent is @NotThreadSafe
public class HttpMultipart extends Multipart {
public class HttpMultipart {
private static ByteArrayBuffer encode(Charset charset, String string) {
private static ByteArrayBuffer encode(
final Charset charset, final 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 {
private static void writeBytes(
final ByteArrayBuffer b, final 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 void writeBytes(
final String s, final Charset charset, final OutputStream out) throws IOException {
ByteArrayBuffer b = encode(charset, s);
writeBytes(b, out);
}
private static void writeBytes(
final String s, final OutputStream out) throws IOException {
ByteArrayBuffer b = encode(MIME.DEFAULT_CHARSET, s);
writeBytes(b, out);
}
private static final ByteArrayBuffer FIELD_SEP = encode(MIME.DEFAULT_CHARSET, ": ");
private static final ByteArrayBuffer CR_LF = encode(MIME.DEFAULT_CHARSET, "\r\n");
private static final ByteArrayBuffer TWO_DASHES = encode(MIME.DEFAULT_CHARSET, "--");
private final String subType;
private final Charset charset;
private final String boundary;
private final List<FormBodyPart> parts;
private HttpMultipartMode mode;
public HttpMultipart(final String subType) {
super(subType);
public HttpMultipart(final String subType, final Charset charset, final String boundary) {
super();
if (subType == null) {
throw new IllegalArgumentException("Multipart subtype may not be null");
}
if (boundary == null) {
throw new IllegalArgumentException("Multipart boundary may not be null");
}
this.subType = subType;
this.charset = charset != null ? charset : MIME.DEFAULT_CHARSET;
this.boundary = boundary;
this.parts = new ArrayList<FormBodyPart>();
this.mode = HttpMultipartMode.STRICT;
}
public HttpMultipart(final String subType, final String boundary) {
this(subType, null, boundary);
}
public String getSubType() {
return this.subType;
}
public Charset getCharset() {
return this.charset;
}
public HttpMultipartMode getMode() {
return this.mode;
@ -100,32 +120,19 @@ public class HttpMultipart extends Multipart {
this.mode = mode;
}
protected Charset getCharset() {
Entity e = getParent();
ContentTypeField cField = (ContentTypeField) e.getHeader().getField(
FieldName.CONTENT_TYPE);
Charset charset = null;
switch (this.mode) {
case STRICT:
charset = MIME.DEFAULT_CHARSET;
break;
case BROWSER_COMPATIBLE:
if (cField.getCharset() != null) {
charset = CharsetUtil.getCharset(cField.getCharset());
} else {
charset = CharsetUtil.getCharset(HTTP.DEFAULT_CONTENT_CHARSET);
}
break;
public List<FormBodyPart> getBodyParts() {
return this.parts;
}
public void addBodyPart(final FormBodyPart part) {
if (part == null) {
return;
}
return charset;
this.parts.add(part);
}
protected String getBoundary() {
Entity e = getParent();
ContentTypeField cField = (ContentTypeField) e.getHeader().getField(
FieldName.CONTENT_TYPE);
return cField.getBoundary();
public String getBoundary() {
return this.boundary;
}
private void doWriteTo(
@ -133,83 +140,44 @@ public class HttpMultipart extends Multipart {
final OutputStream out,
boolean writeContent) throws IOException {
List<BodyPart> bodyParts = getBodyParts();
Charset charset = getCharset();
ByteArrayBuffer boundary = encode(this.charset, getBoundary());
for (FormBodyPart part: this.parts) {
writeBytes(TWO_DASHES, out);
writeBytes(boundary, out);
writeBytes(CR_LF, out);
ByteArrayBuffer boundary = encode(charset, getBoundary());
switch (mode) {
case STRICT:
String preamble = getPreamble();
if (preamble != null && preamble.length() != 0) {
ByteArrayBuffer b = encode(charset, preamble);
writeBytes(b, out);
writeBytes(CR_LF, out);
}
Header header = part.getHeader();
for (int i = 0; i < bodyParts.size(); i++) {
writeBytes(TWO_DASHES, out);
writeBytes(boundary, out);
writeBytes(CR_LF, out);
BodyPart part = bodyParts.get(i);
Header header = part.getHeader();
List<Field> fields = header.getFields();
for (Field field: fields) {
writeBytes(field.getRaw(), out);
switch (mode) {
case STRICT:
for (MinimalField field: header) {
writeBytes(field.getName(), out);
writeBytes(FIELD_SEP, out);
writeBytes(field.getBody(), out);
writeBytes(CR_LF, out);
}
break;
case BROWSER_COMPATIBLE:
// Only write Content-Disposition
// Use content charset
MinimalField cd = part.getHeader().getField(MIME.CONTENT_DISPOSITION);
writeBytes(cd.getName(), this.charset, out);
writeBytes(FIELD_SEP, out);
writeBytes(cd.getBody(), this.charset, out);
writeBytes(CR_LF, out);
if (writeContent) {
MessageWriter.DEFAULT.writeBody(part.getBody(), out);
}
writeBytes(CR_LF, out);
break;
}
writeBytes(TWO_DASHES, out);
writeBytes(boundary, out);
writeBytes(TWO_DASHES, out);
writeBytes(CR_LF, out);
String epilogue = getEpilogue();
if (epilogue != null && epilogue.length() != 0) {
ByteArrayBuffer b = encode(charset, epilogue);
writeBytes(b, out);
writeBytes(CR_LF, out);
}
break;
case BROWSER_COMPATIBLE:
// (1) Do not write preamble and epilogue
// (2) Only write Content-Disposition
// (3) Use content charset
for (int i = 0; i < bodyParts.size(); i++) {
writeBytes(TWO_DASHES, out);
writeBytes(boundary, out);
writeBytes(CR_LF, out);
BodyPart part = bodyParts.get(i);
Field cd = part.getHeader().getField(MIME.CONTENT_DISPOSITION);
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);
if (writeContent) {
part.getBody().writeTo(out);
}
writeBytes(TWO_DASHES, out);
writeBytes(boundary, out);
writeBytes(TWO_DASHES, out);
writeBytes(CR_LF, out);
break;
}
writeBytes(TWO_DASHES, out);
writeBytes(boundary, out);
writeBytes(TWO_DASHES, out);
writeBytes(CR_LF, out);
}
/**
@ -237,24 +205,16 @@ public class HttpMultipart extends Multipart {
* otherwise.
*/
public long getTotalLength() {
List<?> bodyParts = getBodyParts();
long contentLen = 0;
for (int i = 0; i < bodyParts.size(); i++) {
BodyPart part = (BodyPart) bodyParts.get(i);
Body body = part.getBody();
if (body instanceof ContentBody) {
long len = ((ContentBody) body).getContentLength();
if (len >= 0) {
contentLen += len;
} else {
return -1;
}
for (FormBodyPart part: this.parts) {
ContentBody body = part.getBody();
long len = ((ContentBody) body).getContentLength();
if (len >= 0) {
contentLen += len;
} else {
return -1;
}
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
doWriteTo(this.mode, out, false);

View File

@ -29,25 +29,19 @@ package org.apache.http.entity.mime;
import java.nio.charset.Charset;
import org.apache.http.annotation.Immutable;
import org.apache.james.mime4j.field.FieldName;
import org.apache.james.mime4j.util.CharsetUtil;
/**
*
* @since 4.0
*/
@Immutable
public final class MIME {
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_TYPE = "Content-Type";
public static final String CONTENT_TRANSFER_ENC = "Content-Transfer-Encoding";
public static final String CONTENT_DISPOSITION = "Content-Disposition";
public static final String ENC_8BIT = "8bit";
public static final String ENC_BINARY = "binary";
public static final Charset DEFAULT_CHARSET = CharsetUtil.getCharset("US-ASCII");
public static final Charset DEFAULT_CHARSET = Charset.forName("US-ASCII");
}

View File

@ -27,30 +27,21 @@
package org.apache.http.entity.mime;
import org.apache.http.annotation.Immutable;
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
*/
@Immutable
public class MinimalField implements Field {
//@Immutable
public class MinimalField {
private final String name;
private final String value;
private ByteSequence raw; // cache, recreated on demand
MinimalField(final String name, final String value) {
super();
this.name = name;
this.value = value;
this.raw = null;
}
public String getName() {
@ -61,13 +52,6 @@ public class MinimalField implements Field {
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();

View File

@ -31,29 +31,19 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import org.apache.http.annotation.ThreadSafe;
import org.apache.http.Header;
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.field.Fields;
import org.apache.james.mime4j.message.BodyPart;
import org.apache.james.mime4j.message.Message;
/**
* Multipart/form coded HTTP entity consisting of multiple
* body parts.
*
* Multipart/form coded HTTP entity consisting of multiple body parts.
*
* @since 4.0
*/
@ThreadSafe
public class MultipartEntity implements HttpEntity {
/**
@ -63,7 +53,6 @@ public class MultipartEntity implements HttpEntity {
"-_1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
.toCharArray();
private final Message message;
private final HttpMultipart multipart;
private final Header contentType;
@ -72,25 +61,21 @@ public class MultipartEntity implements HttpEntity {
public MultipartEntity(
HttpMultipartMode mode,
final String boundary,
final Charset charset) {
String boundary,
Charset charset) {
super();
this.multipart = new HttpMultipart("form-data");
if (boundary == null) {
boundary = generateBoundary();
}
this.multipart = new HttpMultipart("form-data", charset, boundary);
this.contentType = new BasicHeader(
HTTP.CONTENT_TYPE,
generateContentType(boundary, charset));
this.dirty = true;
this.message = new Message();
org.apache.james.mime4j.message.Header header =
new org.apache.james.mime4j.message.Header();
this.message.setHeader(header);
this.multipart.setParent(message);
if (mode == null) {
mode = HttpMultipartMode.STRICT;
}
this.multipart.setMode(mode);
this.message.getHeader().addField(Fields.contentType(this.contentType.getValue()));
}
public MultipartEntity(final HttpMultipartMode mode) {
@ -106,15 +91,7 @@ public class MultipartEntity implements HttpEntity {
final Charset charset) {
StringBuilder buffer = new StringBuilder();
buffer.append("multipart/form-data; boundary=");
if (boundary != null) {
buffer.append(boundary);
} else {
Random rand = new Random();
int count = rand.nextInt(11) + 30; // a random size from 30 to 40
for (int i = 0; i < count; i++) {
buffer.append(MULTIPART_CHARS[rand.nextInt(MULTIPART_CHARS.length)]);
}
}
buffer.append(boundary);
if (charset != null) {
buffer.append("; charset=");
buffer.append(charset.name());
@ -122,10 +99,17 @@ public class MultipartEntity implements HttpEntity {
return buffer.toString();
}
/**
* @since 4.1
*/
public void addPart(final BodyPart bodyPart) {
protected String generateBoundary() {
StringBuilder buffer = new StringBuilder();
Random rand = new Random();
int count = rand.nextInt(11) + 30; // a random size from 30 to 40
for (int i = 0; i < count; i++) {
buffer.append(MULTIPART_CHARS[rand.nextInt(MULTIPART_CHARS.length)]);
}
return buffer.toString();
}
public void addPart(final FormBodyPart bodyPart) {
this.multipart.addBodyPart(bodyPart);
this.dirty = true;
}
@ -135,10 +119,8 @@ public class MultipartEntity implements HttpEntity {
}
public boolean isRepeatable() {
List<?> parts = this.multipart.getBodyParts();
for (Iterator<?> it = parts.iterator(); it.hasNext(); ) {
FormBodyPart part = (FormBodyPart) it.next();
ContentBody body = (ContentBody) part.getBody();
for (FormBodyPart part: this.multipart.getBodyParts()) {
ContentBody body = part.getBody();
if (body.getContentLength() < 0) {
return false;
}

View File

@ -1,45 +0,0 @@
/*
* ====================================================================
* 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.MimeException;
/**
* @deprecated no longer used.
*
* @since 4.0
*/
@Deprecated
public class UnexpectedMimeException extends RuntimeException {
private static final long serialVersionUID = 1316818299528463579L;
public UnexpectedMimeException(MimeException ex) {
super(ex.getMessage(), ex);
}
}

View File

@ -27,27 +27,16 @@
package org.apache.http.entity.mime.content;
import java.util.Collections;
import java.util.Map;
import org.apache.http.annotation.NotThreadSafe;
import org.apache.james.mime4j.message.Entity;
import org.apache.james.mime4j.message.SingleBody;
/**
*
* @since 4.0
*/
@NotThreadSafe // setParent() also SingleBody and Entity are not T-S
public abstract class AbstractContentBody extends SingleBody implements ContentBody {
public abstract class AbstractContentBody 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) {
@ -64,16 +53,6 @@ public abstract class AbstractContentBody extends SingleBody implements ContentB
}
}
@Override
public Entity getParent() {
return this.parent;
}
@Override
public void setParent(final Entity parent) {
this.parent = parent;
}
public String getMimeType() {
return this.mimeType;
}
@ -86,12 +65,4 @@ public abstract class AbstractContentBody extends SingleBody implements ContentB
return this.subType;
}
public Map<String, String> getContentTypeParameters() {
return Collections.emptyMap();
}
@Override
public void dispose() {
}
}

View File

@ -27,15 +27,17 @@
package org.apache.http.entity.mime.content;
import org.apache.james.mime4j.descriptor.ContentDescriptor;
import org.apache.james.mime4j.message.Body;
import java.io.IOException;
import java.io.OutputStream;
/**
*
* @since 4.0
*/
public interface ContentBody extends Body, ContentDescriptor {
public interface ContentBody extends ContentDescriptor {
String getFilename();
void writeTo(OutputStream out) throws IOException;
}

View File

@ -0,0 +1,89 @@
/*
* ====================================================================
* 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.content;
/**
* Represents common content properties.
*/
public interface ContentDescriptor {
/**
* Returns the body descriptors MIME type.
* @see #getMediaType()
* @see #getSubType()
* @return The MIME type, which has been parsed from the
* content-type definition. Must not be null, but
* "text/plain", if no content-type was specified.
*/
String getMimeType();
/**
* Gets the defaulted MIME media type for this content.
* For example <code>TEXT</code>, <code>IMAGE</code>, <code>MULTIPART</code>
* @see #getMimeType()
* @return the MIME media type when content-type specified,
* otherwise the correct default (<code>TEXT</code>)
*/
String getMediaType();
/**
* Gets the defaulted MIME sub type for this content.
* @see #getMimeType()
* @return the MIME media type when content-type is specified,
* otherwise the correct default (<code>PLAIN</code>)
*/
String getSubType();
/**
* <p>The body descriptors character set, defaulted appropriately for the MIME type.</p>
* <p>
* For <code>TEXT</code> types, this will be defaulted to <code>us-ascii</code>.
* For other types, when the charset parameter is missing this property will be null.
* </p>
* @return Character set, which has been parsed from the
* content-type definition. Not null for <code>TEXT</code> types, when unset will
* be set to default <code>us-ascii</code>. For other types, when unset,
* null will be returned.
*/
String getCharset();
/**
* Returns the body descriptors transfer encoding.
* @return The transfer encoding. Must not be null, but "7bit",
* if no transfer-encoding was specified.
*/
String getTransferEncoding();
/**
* Returns the body descriptors content-length.
* @return Content length, if known, or -1, to indicate the absence of a
* content-length header.
*/
long getContentLength();
}

View File

@ -33,15 +33,12 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.http.annotation.NotThreadSafe;
import org.apache.http.entity.mime.MIME;
/**
*
* @since 4.0
*/
@NotThreadSafe // parent is @NotThreadSafe
public class FileBody extends AbstractContentBody {
private final File file;
@ -96,7 +93,6 @@ public class FileBody extends AbstractContentBody {
writeTo(out);
}
@Override
public void writeTo(final OutputStream out) throws IOException {
if (out == null) {
throw new IllegalArgumentException("Output stream may not be null");

View File

@ -31,15 +31,12 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.http.annotation.NotThreadSafe;
import org.apache.http.entity.mime.MIME;
/**
*
* @since 4.0
*/
@NotThreadSafe // parent is @NotThreadSafe
public class InputStreamBody extends AbstractContentBody {
private final InputStream in;
@ -70,7 +67,6 @@ public class InputStreamBody extends AbstractContentBody {
writeTo(out);
}
@Override
public void writeTo(final OutputStream out) throws IOException {
if (out == null) {
throw new IllegalArgumentException("Output stream may not be null");

View File

@ -35,10 +35,6 @@ import java.io.OutputStream;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;
import org.apache.http.annotation.NotThreadSafe;
import org.apache.http.entity.mime.MIME;
@ -46,7 +42,6 @@ import org.apache.http.entity.mime.MIME;
*
* @since 4.0
*/
@NotThreadSafe // parent is @NotThreadSafe
public class StringBody extends AbstractContentBody {
private final byte[] content;
@ -118,7 +113,6 @@ public class StringBody extends AbstractContentBody {
writeTo(out);
}
@Override
public void writeTo(final OutputStream out) throws IOException {
if (out == null) {
throw new IllegalArgumentException("Output stream may not be null");
@ -140,13 +134,6 @@ public class StringBody extends AbstractContentBody {
return this.charset.name();
}
@Override
public Map<String, String> getContentTypeParameters() {
Map<String, String> map = new HashMap<String, String>();
map.put("charset", this.charset.name());
return map;
}
public long getContentLength() {
return this.content.length;
}

View File

@ -63,7 +63,6 @@ public class TestMultipartContentBody extends TestCase {
Charset defCharset = Charset.defaultCharset();
assertEquals(defCharset.name(), b1.getCharset());
assertEquals(defCharset.name(), b1.getContentTypeParameters().get("charset"));
assertNull(b1.getFilename());
assertEquals("text/plain", b1.getMimeType());
@ -75,7 +74,6 @@ public class TestMultipartContentBody extends TestCase {
StringBody b2 = new StringBody("more text", "text/other", MIME.DEFAULT_CHARSET);
assertEquals(9, b2.getContentLength());
assertEquals(MIME.DEFAULT_CHARSET.name(), b2.getCharset());
assertEquals(MIME.DEFAULT_CHARSET.name(), b2.getContentTypeParameters().get("charset"));
assertNull(b2.getFilename());
assertEquals("text/other", b2.getMimeType());

View File

@ -44,10 +44,6 @@ 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.Fields;
import org.apache.james.mime4j.message.BodyPart;
import org.apache.james.mime4j.message.Header;
import org.apache.james.mime4j.message.Message;
public class TestMultipartForm extends TestCase {
@ -68,65 +64,8 @@ public class TestMultipartForm extends TestCase {
return new TestSuite(TestMultipartForm.class);
}
public void testMultipartFormLowLevel() throws Exception {
Message message = new Message();
Header header = new Header();
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(Fields.contentType("text/plain"));
p1.setHeader(h1);
p1.setBody(new StringBody("this stuff"));
BodyPart p2 = new BodyPart();
Header h2 = new Header();
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(Fields.contentType("text/plain"));
p3.setHeader(h3);
p3.setBody(new StringBody("all kind of stuff"));
multipart.addBodyPart(p1);
multipart.addBodyPart(p2);
multipart.addBodyPart(p3);
ByteArrayOutputStream out = new ByteArrayOutputStream();
multipart.writeTo(out);
out.close();
String expected =
"--foo\r\n" +
"Content-Type: text/plain\r\n" +
"\r\n" +
"this stuff\r\n" +
"--foo\r\n" +
"Content-Type: text/plain\r\n" +
"\r\n" +
"that stuff\r\n" +
"--foo\r\n" +
"Content-Type: text/plain\r\n" +
"\r\n" +
"all kind of stuff\r\n" +
"--foo--\r\n";
String s = out.toString("US-ASCII");
assertEquals(expected, s);
assertEquals(s.length(), multipart.getTotalLength());
}
public void testMultipartFormStringParts() throws Exception {
Message message = new Message();
Header header = new Header();
header.addField(Fields.contentType("multipart/form-data; boundary=foo"));
message.setHeader(header);
HttpMultipart multipart = new HttpMultipart("form-data");
multipart.setParent(message);
HttpMultipart multipart = new HttpMultipart("form-data", "foo");
FormBodyPart p1 = new FormBodyPart(
"field1",
new StringBody("this stuff"));
@ -173,11 +112,6 @@ public class TestMultipartForm extends TestCase {
}
public void testMultipartFormBinaryParts() throws Exception {
Message message = new Message();
Header header = new Header();
header.addField(Fields.contentType("multipart/form-data; boundary=foo"));
message.setHeader(header);
File tmpfile = File.createTempFile("tmp", ".bin");
tmpfile.deleteOnExit();
Writer writer = new FileWriter(tmpfile);
@ -187,8 +121,7 @@ public class TestMultipartForm extends TestCase {
writer.close();
}
HttpMultipart multipart = new HttpMultipart("form-data");
multipart.setParent(message);
HttpMultipart multipart = new HttpMultipart("form-data", "foo");
FormBodyPart p1 = new FormBodyPart(
"field1",
new FileBody(tmpfile));
@ -227,11 +160,6 @@ public class TestMultipartForm extends TestCase {
}
public void testMultipartFormBrowserCompatible() throws Exception {
Message message = new Message();
Header header = new Header();
header.addField(Fields.contentType("multipart/form-data; boundary=foo"));
message.setHeader(header);
File tmpfile = File.createTempFile("tmp", ".bin");
tmpfile.deleteOnExit();
Writer writer = new FileWriter(tmpfile);
@ -241,8 +169,7 @@ public class TestMultipartForm extends TestCase {
writer.close();
}
HttpMultipart multipart = new HttpMultipart("form-data");
multipart.setParent(message);
HttpMultipart multipart = new HttpMultipart("form-data", "foo");
FormBodyPart p1 = new FormBodyPart(
"field1",
new FileBody(tmpfile));
@ -316,11 +243,6 @@ public class TestMultipartForm extends TestCase {
String s1 = constructString(SWISS_GERMAN_HELLO);
String s2 = constructString(RUSSIAN_HELLO);
Message message = new Message();
Header header = new Header();
header.addField(Fields.contentType("multipart/form-data; charset=UTF-8; boundary=foo"));
message.setHeader(header);
File tmpfile = File.createTempFile("tmp", ".bin");
tmpfile.deleteOnExit();
Writer writer = new FileWriter(tmpfile);
@ -330,8 +252,7 @@ public class TestMultipartForm extends TestCase {
writer.close();
}
HttpMultipart multipart = new HttpMultipart("form-data");
multipart.setParent(message);
HttpMultipart multipart = new HttpMultipart("form-data", Charset.forName("UTF-8"), "foo");
FormBodyPart p1 = new FormBodyPart(
"field1",
new InputStreamBody(new FileInputStream(tmpfile), s1 + ".tmp"));
@ -371,13 +292,7 @@ public class TestMultipartForm extends TestCase {
String s1 = constructString(SWISS_GERMAN_HELLO);
String s2 = constructString(RUSSIAN_HELLO);
Message message = new Message();
Header header = new Header();
header.addField(Fields.contentType("multipart/form-data; boundary=foo"));
message.setHeader(header);
HttpMultipart multipart = new HttpMultipart("form-data");
multipart.setParent(message);
HttpMultipart multipart = new HttpMultipart("form-data", "foo");
FormBodyPart p1 = new FormBodyPart(
"field1",
new StringBody(s1, Charset.forName("ISO-8859-1")));

View File

@ -29,6 +29,7 @@ package org.apache.http.entity.mime;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.nio.charset.Charset;
import org.apache.http.Header;
import org.apache.http.HeaderElement;
@ -37,7 +38,6 @@ import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.InputStreamBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.james.mime4j.util.CharsetUtil;
import junit.framework.Test;
import junit.framework.TestCase;
@ -66,7 +66,7 @@ public class TestMultipartFormHttpEntity extends TestCase {
MultipartEntity entity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE,
"whatever",
CharsetUtil.getCharset("UTF-8"));
Charset.forName("UTF-8"));
assertNull(entity.getContentEncoding());
assertNotNull(entity.getContentType());