Skip to content

Commit 0142014

Browse files
author
Stephane Landelle
committed
Have Parts take a java.nio.Charset instead of a String, close AsyncHttpClient#652
1 parent d1d744f commit 0142014

File tree

13 files changed

+57
-47
lines changed

13 files changed

+57
-47
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import java.io.ByteArrayOutputStream;
1818
import java.io.IOException;
19+
import java.nio.charset.Charset;
1920

2021
/**
2122
* This class is an adaptation of the Apache HttpClient implementation
@@ -55,7 +56,7 @@ public abstract class AbstractFilePart extends PartBase {
5556
* @param charset
5657
* the charset encoding for this part
5758
*/
58-
public AbstractFilePart(String name, String contentType, String charset, String contentId, String transferEncoding) {
59+
public AbstractFilePart(String name, String contentType, Charset charset, String contentId, String transferEncoding) {
5960
super(name,//
6061
contentType == null ? DEFAULT_CONTENT_TYPE : contentType,//
6162
charset,//

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.io.IOException;
1616
import java.io.OutputStream;
1717
import java.nio.channels.WritableByteChannel;
18+
import java.nio.charset.Charset;
1819

1920
public class ByteArrayPart extends AbstractFilePart {
2021

@@ -28,19 +29,19 @@ public ByteArrayPart(String name, byte[] bytes, String contentType) {
2829
this(name, bytes, contentType, null);
2930
}
3031

31-
public ByteArrayPart(String name, byte[] bytes, String contentType, String charset) {
32+
public ByteArrayPart(String name, byte[] bytes, String contentType, Charset charset) {
3233
this(name, bytes, contentType, charset, null);
3334
}
3435

35-
public ByteArrayPart(String name, byte[] bytes, String contentType, String charset, String fileName) {
36+
public ByteArrayPart(String name, byte[] bytes, String contentType, Charset charset, String fileName) {
3637
this(name, bytes, contentType, charset, fileName, null);
3738
}
3839

39-
public ByteArrayPart(String name, byte[] bytes, String contentType, String charset, String fileName, String contentId) {
40+
public ByteArrayPart(String name, byte[] bytes, String contentType, Charset charset, String fileName, String contentId) {
4041
this(name, bytes, contentType, charset, fileName, contentId, null);
4142
}
4243

43-
public ByteArrayPart(String name, byte[] bytes, String contentType, String charset, String fileName, String contentId, String transferEncoding) {
44+
public ByteArrayPart(String name, byte[] bytes, String contentType, Charset charset, String fileName, String contentId, String transferEncoding) {
4445
super(name, contentType, charset, contentId, transferEncoding);
4546
if (bytes == null)
4647
throw new NullPointerException("bytes");

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.io.RandomAccessFile;
2424
import java.nio.channels.FileChannel;
2525
import java.nio.channels.WritableByteChannel;
26+
import java.nio.charset.Charset;
2627

2728
public class FilePart extends AbstractFilePart {
2829

@@ -38,19 +39,19 @@ public FilePart(String name, File file, String contentType) {
3839
this(name, file, contentType, null);
3940
}
4041

41-
public FilePart(String name, File file, String contentType, String charset) {
42+
public FilePart(String name, File file, String contentType, Charset charset) {
4243
this(name, file, contentType, charset, null);
4344
}
4445

45-
public FilePart(String name, File file, String contentType, String charset, String fileName) {
46+
public FilePart(String name, File file, String contentType, Charset charset, String fileName) {
4647
this(name, file, contentType, charset, fileName, null);
4748
}
4849

49-
public FilePart(String name, File file, String contentType, String charset, String fileName, String contentId) {
50+
public FilePart(String name, File file, String contentType, Charset charset, String fileName, String contentId) {
5051
this(name, file, contentType, charset, fileName, contentId, null);
5152
}
5253

53-
public FilePart(String name, File file, String contentType, String charset, String fileName, String contentId, String transferEncoding) {
54+
public FilePart(String name, File file, String contentType, Charset charset, String fileName, String contentId, String transferEncoding) {
5455
super(name, contentType, charset, contentId, transferEncoding);
5556
if (file == null)
5657
throw new NullPointerException("file");

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import java.io.IOException;
1818
import java.io.OutputStream;
1919
import java.nio.channels.WritableByteChannel;
20+
import java.nio.charset.Charset;
2021

2122
public interface Part {
2223

@@ -89,7 +90,7 @@ public interface Part {
8990
*
9091
* @return the character encoding, or <code>null</code> to exclude the character encoding header
9192
*/
92-
String getCharSet();
93+
Charset getCharset();
9394

9495
/**
9596
* Return the transfer encoding of this part.

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

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import java.io.IOException;
1818
import java.io.OutputStream;
19+
import java.nio.charset.Charset;
1920

2021
public abstract class PartBase implements Part {
2122

@@ -32,7 +33,7 @@ public abstract class PartBase implements Part {
3233
/**
3334
* The charset (part of Content-Type header)
3435
*/
35-
private final String charSet;
36+
private final Charset charset;
3637

3738
/**
3839
* The Content-Transfer-Encoding header value.
@@ -49,23 +50,23 @@ public abstract class PartBase implements Part {
4950
*/
5051
private String dispositionType;
5152

52-
public PartBase(String name, String contentType, String charSet, String contentId) {
53-
this(name, contentType, charSet, contentId, null);
53+
public PartBase(String name, String contentType, Charset charset, String contentId) {
54+
this(name, contentType, charset, contentId, null);
5455
}
5556

5657
/**
5758
* Constructor.
5859
*
5960
* @param name The name of the part, or <code>null</code>
6061
* @param contentType The content type, or <code>null</code>
61-
* @param charSet The character encoding, or <code>null</code>
62+
* @param charset The character encoding, or <code>null</code>
6263
* @param contentId The content id, or <code>null</code>
6364
* @param transferEncoding The transfer encoding, or <code>null</code>
6465
*/
65-
public PartBase(String name, String contentType, String charSet, String contentId, String transferEncoding) {
66+
public PartBase(String name, String contentType, Charset charset, String contentId, String transferEncoding) {
6667
this.name = name;
6768
this.contentType = contentType;
68-
this.charSet = charSet;
69+
this.charset = charset;
6970
this.contentId = contentId;
7071
this.transferEncoding = transferEncoding;
7172
}
@@ -93,10 +94,10 @@ protected void visitContentTypeHeader(PartVisitor visitor) throws IOException {
9394
visitor.withBytes(CRLF_BYTES);
9495
visitor.withBytes(CONTENT_TYPE_BYTES);
9596
visitor.withBytes(contentType.getBytes(US_ASCII));
96-
String charSet = getCharSet();
97-
if (charSet != null) {
97+
Charset charset = getCharset();
98+
if (charset != null) {
9899
visitor.withBytes(CHARSET_BYTES);
99-
visitor.withBytes(charSet.getBytes(US_ASCII));
100+
visitor.withBytes(charset.name().getBytes(US_ASCII));
100101
}
101102
}
102103
}
@@ -190,7 +191,7 @@ public String toString() {
190191
.append(getClass().getSimpleName())//
191192
.append(" name=").append(getName())//
192193
.append(" contentType=").append(getContentType())//
193-
.append(" charset=").append(getCharSet())//
194+
.append(" charset=").append(getCharset())//
194195
.append(" tranferEncoding=").append(getTransferEncoding())//
195196
.append(" contentId=").append(getContentId())//
196197
.append(" dispositionType=").append(getDispositionType())//
@@ -208,8 +209,8 @@ public String getContentType() {
208209
}
209210

210211
@Override
211-
public String getCharSet() {
212-
return this.charSet;
212+
public Charset getCharset() {
213+
return this.charset;
213214
}
214215

215216
@Override

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

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

46-
public StringPart(String name, String value, String charset) {
46+
public StringPart(String name, String value, Charset charset) {
4747
this(name, value, charset, null);
4848
}
4949

50-
private static Charset charsetOrDefault(String charset) {
51-
return charset == null ? DEFAULT_CHARSET : Charset.forName(charset);
50+
private static Charset charsetOrDefault(Charset charset) {
51+
return charset == null ? DEFAULT_CHARSET : charset;
5252
}
5353

5454
/**
@@ -63,8 +63,8 @@ private static Charset charsetOrDefault(String charset) {
6363
* @param contentId
6464
* the content id
6565
*/
66-
public StringPart(String name, String value, String charset, String contentId) {
67-
super(name, DEFAULT_CONTENT_TYPE, charsetOrDefault(charset).name(), contentId, DEFAULT_TRANSFER_ENCODING);
66+
public StringPart(String name, String value, Charset charset, String contentId) {
67+
super(name, DEFAULT_CONTENT_TYPE, charsetOrDefault(charset), contentId, DEFAULT_TRANSFER_ENCODING);
6868
if (value == null)
6969
throw new NullPointerException("value");
7070

src/main/java/com/ning/http/client/providers/apache/ApacheAsyncHttpProvider.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -689,14 +689,14 @@ private MultipartRequestEntity createMultipartRequestEntity(String charset, List
689689
parts[i] = new org.apache.commons.httpclient.methods.multipart.FilePart(part.getName(),
690690
((FilePart) part).getFile(),
691691
((FilePart) part).getContentType(),
692-
((FilePart) part).getCharSet());
692+
((FilePart) part).getCharset().name());
693693

694694
} else if (part instanceof ByteArrayPart) {
695695
PartSource source = new ByteArrayPartSource(((ByteArrayPart) part).getFileName(), ((ByteArrayPart) part).getBytes());
696696
parts[i] = new org.apache.commons.httpclient.methods.multipart.FilePart(part.getName(),
697697
source,
698698
((ByteArrayPart) part).getContentType(),
699-
((ByteArrayPart) part).getCharSet());
699+
((ByteArrayPart) part).getCharset().name());
700700

701701
} else if (part == null) {
702702
throw new NullPointerException("Part cannot be null");

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

Lines changed: 1 addition & 1 deletion
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", StandardCharsets.UTF_8.name());
680+
Part p = new StringPart("foo", "bar", StandardCharsets.UTF_8);
681681

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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public void testUnauthorizedWhileUploading() throws Exception {
5757
try {
5858
BoundRequestBuilder rb = client.preparePut(getTargetUrl());
5959

60-
rb.addBodyPart(new FilePart("test", largeFile, "application/octet-stream", StandardCharsets.UTF_8.name()));
60+
rb.addBodyPart(new FilePart("test", largeFile, "application/octet-stream", StandardCharsets.UTF_8));
6161

6262
Response response = rb.execute().get();
6363
Assert.assertEquals(401, response.getStatusCode());

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.ning.http.client.AsyncHttpClientConfig;
2525
import com.ning.http.client.Response;
2626
import com.ning.http.client.multipart.FilePart;
27+
import com.ning.http.util.StandardCharsets;
2728

2829
import javax.servlet.ServletException;
2930
import javax.servlet.ServletInputStream;
@@ -46,7 +47,7 @@ public void testPutImageFile() throws Exception {
4647
try {
4748
BoundRequestBuilder rb = client.preparePut(getTargetUrl());
4849

49-
rb.addBodyPart(new FilePart("test", largeFile, "application/octet-stream", "UTF-8"));
50+
rb.addBodyPart(new FilePart("test", largeFile, "application/octet-stream", StandardCharsets.UTF_8));
5051

5152
Response response = rb.execute().get();
5253
Assert.assertEquals(200, response.getStatusCode());
@@ -57,15 +58,15 @@ public void testPutImageFile() throws Exception {
5758

5859
@Test(groups = { "standalone", "default_provider" }, enabled = true)
5960
public void testPutLargeTextFile() throws Exception {
60-
byte[] bytes = "RatherLargeFileRatherLargeFileRatherLargeFileRatherLargeFile".getBytes("UTF-16");
61+
byte[] bytes = "RatherLargeFileRatherLargeFileRatherLargeFileRatherLargeFile".getBytes(StandardCharsets.UTF_16);
6162
long repeats = (1024 * 1024 / bytes.length) + 1;
6263
File largeFile = createTempFile(bytes, (int) repeats);
6364

6465
AsyncHttpClient client = getAsyncHttpClient(null);
6566
try {
6667
BoundRequestBuilder rb = client.preparePut(getTargetUrl());
6768

68-
rb.addBodyPart(new FilePart("test", largeFile, "application/octet-stream", "UTF-8"));
69+
rb.addBodyPart(new FilePart("test", largeFile, "application/octet-stream", StandardCharsets.UTF_8));
6970

7071
Response response = rb.execute().get();
7172
Assert.assertEquals(200, response.getStatusCode());

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
*/
1313
package com.ning.http.client.async;
1414

15+
import static com.ning.http.util.StandardCharsets.*;
16+
1517
import static org.testng.Assert.assertEquals;
1618
import static org.testng.Assert.assertNotNull;
1719
import static org.testng.Assert.assertTrue;
@@ -41,7 +43,6 @@
4143
import com.ning.http.client.multipart.FilePart;
4244
import com.ning.http.client.multipart.StringPart;
4345
import com.ning.http.util.AsyncHttpProviderUtils;
44-
import com.ning.http.util.StandardCharsets;
4546

4647
import javax.servlet.ServletException;
4748
import javax.servlet.http.HttpServlet;
@@ -218,16 +219,16 @@ public void testSendingSmallFilesAndByteArray() {
218219
try {
219220
RequestBuilder builder = new RequestBuilder("POST");
220221
builder.setUrl(servletEndpointRedirectUrl + "/upload/bob");
221-
builder.addBodyPart(new FilePart("file1", testResource1File, "text/plain", "UTF-8"));
222+
builder.addBodyPart(new FilePart("file1", testResource1File, "text/plain", UTF_8));
222223
builder.addBodyPart(new FilePart("file2", testResource2File, "application/x-gzip", null));
223-
builder.addBodyPart(new StringPart("Name", "Dominic", "UTF-8"));
224-
builder.addBodyPart(new FilePart("file3", testResource3File, "text/plain", "UTF-8"));
224+
builder.addBodyPart(new StringPart("Name", "Dominic", UTF_8));
225+
builder.addBodyPart(new FilePart("file3", testResource3File, "text/plain", UTF_8));
225226

226-
builder.addBodyPart(new StringPart("Age", "3", AsyncHttpProviderUtils.DEFAULT_CHARSET.name()));
227-
builder.addBodyPart(new StringPart("Height", "shrimplike", AsyncHttpProviderUtils.DEFAULT_CHARSET.name()));
228-
builder.addBodyPart(new StringPart("Hair", "ridiculous", AsyncHttpProviderUtils.DEFAULT_CHARSET.name()));
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));
229230

230-
builder.addBodyPart(new ByteArrayPart("file4", expectedContents.getBytes(StandardCharsets.UTF_8), "text/plain", StandardCharsets.UTF_8.name(), "bytearray.txt"));
231+
builder.addBodyPart(new ByteArrayPart("file4", expectedContents.getBytes(UTF_8), "text/plain", UTF_8, "bytearray.txt"));
231232

232233
com.ning.http.client.Request r = builder.build();
233234

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
*/
1313
package com.ning.http.client.async;
1414

15+
import static com.ning.http.util.StandardCharsets.UTF_8;
16+
1517
import static junit.framework.Assert.assertTrue;
1618
import static org.testng.Assert.assertEquals;
1719
import static org.testng.Assert.fail;
@@ -30,7 +32,6 @@
3032
import com.ning.http.client.simple.HeaderMap;
3133
import com.ning.http.client.simple.SimpleAHCTransferListener;
3234
import com.ning.http.client.uri.UriComponents;
33-
import com.ning.http.util.StandardCharsets;
3435

3536
import java.io.ByteArrayInputStream;
3637
import java.io.ByteArrayOutputStream;
@@ -266,7 +267,7 @@ public void testCloseMasterInvalidDerived() throws Exception {
266267
public void testMultiPartPut() throws Exception {
267268
SimpleAsyncHttpClient client = new SimpleAsyncHttpClient.Builder().setProviderClass(getProviderClass()).setUrl(getTargetUrl() + "/multipart").build();
268269
try {
269-
Response response = client.put(new ByteArrayPart("baPart", "testMultiPart".getBytes(StandardCharsets.UTF_8), "application/test", StandardCharsets.UTF_8.name(), "fileName")).get();
270+
Response response = client.put(new ByteArrayPart("baPart", "testMultiPart".getBytes(UTF_8), "application/test", UTF_8, "fileName")).get();
270271

271272
String body = response.getResponseBody();
272273
String contentType = response.getHeader("X-Content-Type");
@@ -290,7 +291,7 @@ public void testMultiPartPut() throws Exception {
290291
public void testMultiPartPost() throws Exception {
291292
SimpleAsyncHttpClient client = new SimpleAsyncHttpClient.Builder().setProviderClass(getProviderClass()).setUrl(getTargetUrl() + "/multipart").build();
292293
try {
293-
Response response = client.post(new ByteArrayPart("baPart", "testMultiPart".getBytes(StandardCharsets.UTF_8), "application/test", StandardCharsets.UTF_8.name(), "fileName")).get();
294+
Response response = client.post(new ByteArrayPart("baPart", "testMultiPart".getBytes(UTF_8), "application/test", UTF_8, "fileName")).get();
294295

295296
String body = response.getResponseBody();
296297
String contentType = response.getHeader("X-Content-Type");

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
*/
1313
package com.ning.http.client.multipart;
1414

15+
import static com.ning.http.util.StandardCharsets.UTF_8;
16+
1517
import org.testng.Assert;
1618
import org.testng.annotations.Test;
1719

@@ -38,10 +40,10 @@ public void testBasics() {
3840
parts.add(new FilePart("filePart", testFile));
3941

4042
// add a byte array
41-
parts.add(new ByteArrayPart("baPart", "testMultiPart".getBytes(StandardCharsets.UTF_8), "application/test", StandardCharsets.UTF_8.name(), "fileName"));
43+
parts.add(new ByteArrayPart("baPart", "testMultiPart".getBytes(UTF_8), "application/test", StandardCharsets.UTF_8, "fileName"));
4244

4345
// add a string
44-
parts.add(new StringPart("stringPart", "testString", "utf-8"));
46+
parts.add(new StringPart("stringPart", "testString", UTF_8));
4547

4648
compareContentLength(parts);
4749
}

0 commit comments

Comments
 (0)