Make mode a final field of HttpMultipart

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1051681 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Sebastian Bazley 2010-12-21 22:25:45 +00:00
parent 4c24320359
commit 7bd182d481
3 changed files with 45 additions and 18 deletions

View File

@ -99,9 +99,18 @@ private static void writeField(
private final String boundary;
private final List<FormBodyPart> parts;
private HttpMultipartMode mode;
private final HttpMultipartMode mode;
public HttpMultipart(final String subType, final Charset charset, final String boundary) {
/**
* Creates an instance with the specified settings.
*
* @param subType mime subtype - must not be {@code null}
* @param charset the character set to use. May be {@code null}, in which case {@link MIME#DEFAULT_CHARSET} - i.e. US-ASCII - is used.
* @param boundary to use - must not be {@code null}
* @param mode the mode to use
* @throws IllegalArgumentException if charset is null or boundary is null
*/
public HttpMultipart(final String subType, final Charset charset, final String boundary, HttpMultipartMode mode) {
super();
if (subType == null) {
throw new IllegalArgumentException("Multipart subtype may not be null");
@ -113,7 +122,20 @@ public HttpMultipart(final String subType, final Charset charset, final String b
this.charset = charset != null ? charset : MIME.DEFAULT_CHARSET;
this.boundary = boundary;
this.parts = new ArrayList<FormBodyPart>();
this.mode = HttpMultipartMode.STRICT;
this.mode = mode;
}
/**
* Creates an instance with the specified settings.
* Mode is set to {@link HttpMultipartMode#STRICT}
*
* @param subType mime subtype - must not be {@code null}
* @param charset the character set to use. May be {@code null}, in which case {@link MIME#DEFAULT_CHARSET} - i.e. US-ASCII - is used.
* @param boundary to use - must not be {@code null}
* @throws IllegalArgumentException if charset is null or boundary is null
*/
public HttpMultipart(final String subType, final Charset charset, final String boundary) {
this(subType, charset, boundary, HttpMultipartMode.STRICT);
}
public HttpMultipart(final String subType, final String boundary) {
@ -132,10 +154,6 @@ public HttpMultipartMode getMode() {
return this.mode;
}
public void setMode(final HttpMultipartMode mode) {
this.mode = mode;
}
public List<FormBodyPart> getBodyParts() {
return this.parts;
}

View File

@ -60,6 +60,12 @@ public class MultipartEntity implements HttpEntity {
private long length;
private volatile boolean dirty; // used to decide whether to recalculate length
/**
* Creates an instance using the specified parameters
* @param mode the mode to use, may be {@code null}, in which case {@link HttpMultipartMode#STRICT} is used
* @param boundary the boundary string, may be {@code null}, in which case {@link #generateBoundary()} is invoked to create the string
* @param charset the character set to use, may be {@code null}, in which case {@link MIME#DEFAULT_CHARSET} - i.e. US-ASCII - is used.
*/
public MultipartEntity(
HttpMultipartMode mode,
String boundary,
@ -68,21 +74,28 @@ public MultipartEntity(
if (boundary == null) {
boundary = generateBoundary();
}
this.multipart = new HttpMultipart("form-data", charset, boundary);
if (mode == null) {
mode = HttpMultipartMode.STRICT;
}
this.multipart = new HttpMultipart("form-data", charset, boundary, mode);
this.contentType = new BasicHeader(
HTTP.CONTENT_TYPE,
generateContentType(boundary, charset));
this.dirty = true;
if (mode == null) {
mode = HttpMultipartMode.STRICT;
}
this.multipart.setMode(mode);
}
/**
* Creates an instance using the specified {@link HttpMultipartMode} mode.
* Boundary and charset are set to {@code null}.
* @param mode the desired mode
*/
public MultipartEntity(final HttpMultipartMode mode) {
this(mode, null, null);
}
/**
* Creates an instance using mode {@link HttpMultipartMode#STRICT}
*/
public MultipartEntity() {
this(HttpMultipartMode.STRICT, null, null);
}

View File

@ -151,7 +151,7 @@ public void testMultipartFormBrowserCompatible() throws Exception {
writer.close();
}
HttpMultipart multipart = new HttpMultipart("form-data", "foo");
HttpMultipart multipart = new HttpMultipart("form-data", null, "foo", HttpMultipartMode.STRICT);
FormBodyPart p1 = new FormBodyPart(
"field1",
new FileBody(tmpfile));
@ -166,8 +166,6 @@ public void testMultipartFormBrowserCompatible() throws Exception {
multipart.addBodyPart(p2);
multipart.addBodyPart(p3);
multipart.setMode(HttpMultipartMode.STRICT);
ByteArrayOutputStream out = new ByteArrayOutputStream();
multipart.writeTo(out);
out.close();
@ -235,7 +233,7 @@ public void testMultipartFormBrowserCompatibleNonASCIIHeaders() throws Exception
writer.close();
}
HttpMultipart multipart = new HttpMultipart("form-data", Charset.forName("UTF-8"), "foo");
HttpMultipart multipart = new HttpMultipart("form-data", Charset.forName("UTF-8"), "foo", HttpMultipartMode.BROWSER_COMPATIBLE);
FormBodyPart p1 = new FormBodyPart(
"field1",
new InputStreamBody(new FileInputStream(tmpfile), s1 + ".tmp"));
@ -246,8 +244,6 @@ public void testMultipartFormBrowserCompatibleNonASCIIHeaders() throws Exception
multipart.addBodyPart(p1);
multipart.addBodyPart(p2);
multipart.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
ByteArrayOutputStream out = new ByteArrayOutputStream();
multipart.writeTo(out);
out.close();