Skip to content

Commit ceac4da

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

File tree

4 files changed

+38
-27
lines changed

4 files changed

+38
-27
lines changed

src/main/java/com/ning/http/client/multipart/StringPart.java

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -43,36 +43,48 @@ public class StringPart extends PartBase {
4343
private final byte[] content;
4444
private final String value;
4545

46-
public StringPart(String name, String value, Charset charset) {
47-
this(name, value, charset, null);
48-
}
49-
5046
private static Charset charsetOrDefault(Charset charset) {
5147
return charset == null ? DEFAULT_CHARSET : charset;
5248
}
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+
}
5361

54-
/**
55-
* Constructor.
56-
*
57-
* @param name
58-
* The name of the part
59-
* @param value
60-
* the string to post
61-
* @param charset
62-
* the charset to be used to encode the string, if <code>null</code> the {@link #DEFAULT_CHARSET default} is used
63-
* @param contentId
64-
* the content id
65-
*/
66-
public StringPart(String name, String value, Charset charset, String contentId) {
67-
super(name, DEFAULT_CONTENT_TYPE, charsetOrDefault(charset), contentId, DEFAULT_TRANSFER_ENCODING);
62+
public StringPart(String name, String value, String contentType) {
63+
this(name, value, contentType, null);
64+
}
65+
66+
public StringPart(String name, String value, String contentType, Charset charset) {
67+
this(name, value, contentType, charset, null);
68+
}
69+
70+
public StringPart(String name, String value, String contentType, Charset charset, String fileName) {
71+
this(name, value, contentType, charset, fileName, null);
72+
}
73+
74+
public StringPart(String name, String value, String contentType, Charset charset, String fileName, String contentId) {
75+
this(name, value, contentType, charset, fileName, contentId, null);
76+
}
77+
78+
public StringPart(String name, String value, String contentType, Charset charset, String fileName, String contentId, String transferEncoding) {
79+
super(name, contentTypeOrDefault(contentType), charsetOrDefault(charset), contentId, transferEncodingOrDefault(transferEncoding));
6880
if (value == null)
6981
throw new NullPointerException("value");
7082

7183
if (value.indexOf(0) != -1)
7284
// See RFC 2048, 2.8. "8bit Data"
7385
throw new IllegalArgumentException("NULs may not be present in string parts");
7486

75-
content = value.getBytes(charsetOrDefault(charset));
87+
content = value.getBytes(getCharset());
7688
this.value = value;
7789
}
7890

src/test/java/com/ning/http/client/async/AsyncProvidersBasicTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -677,7 +677,7 @@ public void asyncDoPostMultiPartTest() throws Throwable {
677677
try {
678678
final CountDownLatch l = new CountDownLatch(1);
679679

680-
Part p = new StringPart("foo", "bar", UTF_8);
680+
Part p = new StringPart("foo", "bar");
681681

682682
client.preparePost(getTargetUrl()).addBodyPart(p).execute(new AsyncCompletionHandlerAdapter() {
683683

@@ -707,7 +707,7 @@ public Response onCompleted(Response response) throws Exception {
707707

708708
@Test(groups = { "standalone", "default_provider", "async" })
709709
public void asyncDoPostBasicGZIPTest() throws Throwable {
710-
AsyncHttpClientConfig cf = new AsyncHttpClientConfig.Builder().build();
710+
AsyncHttpClientConfig cf = new AsyncHttpClientConfig.Builder().setCompressionEnforced(true).build();
711711
AsyncHttpClient client = getAsyncHttpClient(cf);
712712
try {
713713
final CountDownLatch l = new CountDownLatch(1);

src/test/java/com/ning/http/client/async/MultipartUploadTest.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
import com.ning.http.client.multipart.ByteArrayPart;
4343
import com.ning.http.client.multipart.FilePart;
4444
import com.ning.http.client.multipart.StringPart;
45-
import com.ning.http.util.AsyncHttpProviderUtils;
4645

4746
import javax.servlet.ServletException;
4847
import javax.servlet.http.HttpServlet;
@@ -221,12 +220,12 @@ public void testSendingSmallFilesAndByteArray() {
221220
builder.setUrl(servletEndpointRedirectUrl + "/upload/bob");
222221
builder.addBodyPart(new FilePart("file1", testResource1File, "text/plain", UTF_8));
223222
builder.addBodyPart(new FilePart("file2", testResource2File, "application/x-gzip", null));
224-
builder.addBodyPart(new StringPart("Name", "Dominic", UTF_8));
223+
builder.addBodyPart(new StringPart("Name", "Dominic"));
225224
builder.addBodyPart(new FilePart("file3", testResource3File, "text/plain", UTF_8));
226225

227-
builder.addBodyPart(new StringPart("Age", "3", AsyncHttpProviderUtils.DEFAULT_CHARSET));
228-
builder.addBodyPart(new StringPart("Height", "shrimplike", AsyncHttpProviderUtils.DEFAULT_CHARSET));
229-
builder.addBodyPart(new StringPart("Hair", "ridiculous", AsyncHttpProviderUtils.DEFAULT_CHARSET));
226+
builder.addBodyPart(new StringPart("Age", "3"));
227+
builder.addBodyPart(new StringPart("Height", "shrimplike"));
228+
builder.addBodyPart(new StringPart("Hair", "ridiculous"));
230229

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

src/test/java/com/ning/http/client/multipart/MultipartBodyTest.java

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

4848
// add a string
49-
parts.add(new StringPart("stringPart", "testString", UTF_8));
49+
parts.add(new StringPart("stringPart", "testString"));
5050

5151
compareContentLength(parts);
5252
}

0 commit comments

Comments
 (0)