Skip to content

Commit 6924a42

Browse files
author
Stephane Landelle
committed
StringPart won't let configure contentType, close AsyncHttpClient#713
1 parent b568018 commit 6924a42

File tree

4 files changed

+38
-24
lines changed

4 files changed

+38
-24
lines changed

api/src/main/java/org/asynchttpclient/multipart/StringPart.java

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -41,36 +41,47 @@ public class StringPart extends PartBase {
4141
* Contents of this StringPart.
4242
*/
4343
private final byte[] content;
44+
private final String value;
4445

4546
private static Charset charsetOrDefault(Charset charset) {
4647
return charset == null ? DEFAULT_CHARSET : charset;
4748
}
49+
50+
private static String contentTypeOrDefault(String contentType) {
51+
return contentType == null ? DEFAULT_CONTENT_TYPE : contentType;
52+
}
53+
54+
private static String transferEncodingOrDefault(String transferEncoding) {
55+
return transferEncoding == null ? DEFAULT_TRANSFER_ENCODING : transferEncoding;
56+
}
57+
58+
public StringPart(String name, String value) {
59+
this(name, value, null);
60+
}
4861

49-
public StringPart(String name, String value, Charset charset) {
50-
this(name, value, charset, null);
62+
public StringPart(String name, String value, String contentType) {
63+
this(name, value, contentType, null);
5164
}
5265

53-
/**
54-
* Constructor.
55-
*
56-
* @param name
57-
* The name of the part
58-
* @param value
59-
* the string to post
60-
* @param charset
61-
* the charset to be used to encode the string, if <code>null</code> the {@link #DEFAULT_CHARSET default} is used
62-
* @param contentId
63-
* the content id
64-
*/
65-
public StringPart(String name, String value, Charset charset, String contentId) {
66+
public StringPart(String name, String value, String contentType, Charset charset) {
67+
this(name, value, contentType, charset, null);
68+
}
6669

67-
super(name, DEFAULT_CONTENT_TYPE, charsetOrDefault(charset), DEFAULT_TRANSFER_ENCODING, contentId);
70+
public StringPart(String name, String value, String contentType, Charset charset, String contentId) {
71+
this(name, value, contentType, charset, contentId, null);
72+
}
73+
74+
public StringPart(String name, String value, String contentType, Charset charset, String contentId, String transferEncoding) {
75+
super(name, contentTypeOrDefault(contentType), charsetOrDefault(charset), contentId, transferEncodingOrDefault(transferEncoding));
6876
if (value == null)
6977
throw new NullPointerException("value");
78+
7079
if (value.indexOf(0) != -1)
7180
// See RFC 2048, 2.8. "8bit Data"
7281
throw new IllegalArgumentException("NULs may not be present in string parts");
73-
content = value.getBytes(charsetOrDefault(charset));
82+
83+
content = value.getBytes(getCharset());
84+
this.value = value;
7485
}
7586

7687
/**
@@ -104,4 +115,8 @@ public byte[] getBytes(byte[] boundary) throws IOException {
104115
public long write(WritableByteChannel target, byte[] boundary) throws IOException {
105116
return MultipartUtils.writeBytesToChannel(target, getBytes(boundary));
106117
}
118+
119+
public String getValue() {
120+
return value;
121+
}
107122
}

api/src/test/java/org/asynchttpclient/async/AsyncProvidersBasicTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,7 @@ public void asyncDoPostMultiPartTest() throws Exception {
659659
try {
660660
final CountDownLatch l = new CountDownLatch(1);
661661

662-
Part p = new StringPart("foo", "bar", UTF_8);
662+
Part p = new StringPart("foo", "bar");
663663

664664
client.preparePost(getTargetUrl()).addBodyPart(p).execute(new AsyncCompletionHandlerAdapter() {
665665

api/src/test/java/org/asynchttpclient/async/MultipartUploadTest.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
import org.asynchttpclient.multipart.ByteArrayPart;
3737
import org.asynchttpclient.multipart.FilePart;
3838
import org.asynchttpclient.multipart.StringPart;
39-
import org.asynchttpclient.util.AsyncHttpProviderUtils;
4039
import org.eclipse.jetty.servlet.ServletContextHandler;
4140
import org.eclipse.jetty.servlet.ServletHolder;
4241
import org.slf4j.Logger;
@@ -176,12 +175,12 @@ public void testSendingSmallFilesAndByteArray() {
176175
builder.setUrl("http://localhost" + ":" + port1 + "/upload/bob");
177176
builder.addBodyPart(new FilePart("file1", testResource1File, "text/plain", UTF_8));
178177
builder.addBodyPart(new FilePart("file2", testResource2File, "application/x-gzip", null));
179-
builder.addBodyPart(new StringPart("Name", "Dominic", UTF_8));
178+
builder.addBodyPart(new StringPart("Name", "Dominic"));
180179
builder.addBodyPart(new FilePart("file3", testResource3File, "text/plain", UTF_8));
181180

182-
builder.addBodyPart(new StringPart("Age", "3", AsyncHttpProviderUtils.DEFAULT_CHARSET));
183-
builder.addBodyPart(new StringPart("Height", "shrimplike", AsyncHttpProviderUtils.DEFAULT_CHARSET));
184-
builder.addBodyPart(new StringPart("Hair", "ridiculous", AsyncHttpProviderUtils.DEFAULT_CHARSET));
181+
builder.addBodyPart(new StringPart("Age", "3"));
182+
builder.addBodyPart(new StringPart("Height", "shrimplike"));
183+
builder.addBodyPart(new StringPart("Hair", "ridiculous"));
185184

186185
builder.addBodyPart(new ByteArrayPart("file4", expectedContents.getBytes(UTF_8), "text/plain", UTF_8, "bytearray.txt"));
187186

api/src/test/java/org/asynchttpclient/multipart/MultipartBodyTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public void testBasics() {
4141
parts.add(new ByteArrayPart("baPart", "testMultiPart".getBytes(UTF_8), "application/test", UTF_8, "fileName"));
4242

4343
// add a string
44-
parts.add(new StringPart("stringPart", "testString", UTF_8));
44+
parts.add(new StringPart("stringPart", "testString"));
4545

4646
compareContentLength(parts);
4747
}

0 commit comments

Comments
 (0)