Skip to content

Commit 2ca8a3d

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

File tree

12 files changed

+49
-43
lines changed

12 files changed

+49
-43
lines changed

api/src/main/java/org/asynchttpclient/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 transfertEncoding) {
59+
public AbstractFilePart(String name, String contentType, Charset charset, String contentId, String transfertEncoding) {
5960
super(name,//
6061
contentType == null ? DEFAULT_CONTENT_TYPE : contentType,//
6162
charset,//

api/src/main/java/org/asynchttpclient/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");

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@
1212
*/
1313
package org.asynchttpclient.multipart;
1414

15-
import org.slf4j.Logger;
16-
import org.slf4j.LoggerFactory;
17-
1815
import java.io.File;
1916
import java.io.FileInputStream;
2017
import java.io.IOException;
@@ -23,6 +20,10 @@
2320
import java.io.RandomAccessFile;
2421
import java.nio.channels.FileChannel;
2522
import java.nio.channels.WritableByteChannel;
23+
import java.nio.charset.Charset;
24+
25+
import org.slf4j.Logger;
26+
import org.slf4j.LoggerFactory;
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");

api/src/main/java/org/asynchttpclient/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.

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

Lines changed: 10 additions & 9 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.
@@ -54,14 +55,14 @@ public abstract class PartBase implements Part {
5455
*
5556
* @param name The name of the part, or <code>null</code>
5657
* @param contentType The content type, or <code>null</code>
57-
* @param charSet The character encoding, or <code>null</code>
58+
* @param charset The character encoding, or <code>null</code>
5859
* @param contentId The content id, or <code>null</code>
5960
* @param transferEncoding The transfer encoding, or <code>null</code>
6061
*/
61-
public PartBase(String name, String contentType, String charSet, String contentId, String transferEncoding) {
62+
public PartBase(String name, String contentType, Charset charset, String contentId, String transferEncoding) {
6263
this.name = name;
6364
this.contentType = contentType;
64-
this.charSet = charSet;
65+
this.charset = charset;
6566
this.contentId = contentId;
6667
this.transferEncoding = transferEncoding;
6768
}
@@ -89,10 +90,10 @@ protected void visitContentTypeHeader(PartVisitor visitor) throws IOException {
8990
visitor.withBytes(CRLF_BYTES);
9091
visitor.withBytes(CONTENT_TYPE_BYTES);
9192
visitor.withBytes(contentType.getBytes(US_ASCII));
92-
String charSet = getCharSet();
93+
Charset charSet = getCharset();
9394
if (charSet != null) {
9495
visitor.withBytes(CHARSET_BYTES);
95-
visitor.withBytes(charSet.getBytes(US_ASCII));
96+
visitor.withBytes(charset.name().getBytes(US_ASCII));
9697
}
9798
}
9899
}
@@ -186,7 +187,7 @@ public String toString() {
186187
.append(getClass().getSimpleName())//
187188
.append(" name=").append(getName())//
188189
.append(" contentType=").append(getContentType())//
189-
.append(" charset=").append(getCharSet())//
190+
.append(" charset=").append(getCharset())//
190191
.append(" tranferEncoding=").append(getTransferEncoding())//
191192
.append(" contentId=").append(getContentId())//
192193
.append(" dispositionType=").append(getDispositionType())//
@@ -204,8 +205,8 @@ public String getContentType() {
204205
}
205206

206207
@Override
207-
public String getCharSet() {
208-
return this.charSet;
208+
public Charset getCharset() {
209+
return this.charset;
209210
}
210211

211212
@Override

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ public class StringPart extends PartBase {
4242
*/
4343
private final byte[] content;
4444

45-
private static Charset charsetOrDefault(String charset) {
46-
return charset == null ? DEFAULT_CHARSET : Charset.forName(charset);
45+
private static Charset charsetOrDefault(Charset charset) {
46+
return charset == null ? DEFAULT_CHARSET : charset;
4747
}
4848

49-
public StringPart(String name, String value, String charset) {
49+
public StringPart(String name, String value, Charset charset) {
5050
this(name, value, charset, null);
5151
}
5252

@@ -62,9 +62,9 @@ public StringPart(String name, String value, String charset) {
6262
* @param contentId
6363
* the content id
6464
*/
65-
public StringPart(String name, String value, String charset, String contentId) {
65+
public StringPart(String name, String value, Charset charset, String contentId) {
6666

67-
super(name, DEFAULT_CONTENT_TYPE, charsetOrDefault(charset).name(), DEFAULT_TRANSFER_ENCODING, contentId);
67+
super(name, DEFAULT_CONTENT_TYPE, charsetOrDefault(charset), DEFAULT_TRANSFER_ENCODING, contentId);
6868
if (value == null)
6969
throw new NullPointerException("value");
7070
if (value.indexOf(0) != -1)

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", StandardCharsets.UTF_8.name());
662+
Part p = new StringPart("foo", "bar", StandardCharsets.UTF_8);
663663

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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public void testUnauthorizedWhileUploading() throws Exception {
5353

5454
AsyncHttpClient client = getAsyncHttpClient(null);
5555
try {
56-
Response response = client.preparePut(getTargetUrl()).addBodyPart(new FilePart("test", file, "application/octet-stream", StandardCharsets.UTF_8.name())).execute()
56+
Response response = client.preparePut(getTargetUrl()).addBodyPart(new FilePart("test", file, "application/octet-stream", StandardCharsets.UTF_8)).execute()
5757
.get();
5858
assertEquals(response.getStatusCode(), 401);
5959
} finally {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public void handle(String target, Request baseRequest, HttpServletRequest req, H
6464
public void testPutImageFile() throws Exception {
6565
AsyncHttpClient client = getAsyncHttpClient(new AsyncHttpClientConfig.Builder().setRequestTimeout(100 * 6000).build());
6666
try {
67-
Response response = client.preparePut(getTargetUrl()).addBodyPart(new FilePart("test", LARGE_IMAGE_FILE, "application/octet-stream", StandardCharsets.UTF_8.name())).execute().get();
67+
Response response = client.preparePut(getTargetUrl()).addBodyPart(new FilePart("test", LARGE_IMAGE_FILE, "application/octet-stream", StandardCharsets.UTF_8)).execute().get();
6868
assertEquals(response.getStatusCode(), 200);
6969
} finally {
7070
client.close();
@@ -77,7 +77,7 @@ public void testPutLargeTextFile() throws Exception {
7777

7878
AsyncHttpClient client = getAsyncHttpClient(null);
7979
try {
80-
Response response = client.preparePut(getTargetUrl()).addBodyPart(new FilePart("test", file, "application/octet-stream", StandardCharsets.UTF_8.name())).execute().get();
80+
Response response = client.preparePut(getTargetUrl()).addBodyPart(new FilePart("test", file, "application/octet-stream", StandardCharsets.UTF_8)).execute().get();
8181
assertEquals(response.getStatusCode(), 200);
8282
} finally {
8383
client.close();

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -174,16 +174,16 @@ public void testSendingSmallFilesAndByteArray() {
174174

175175
RequestBuilder builder = new RequestBuilder("POST");
176176
builder.setUrl("http://localhost" + ":" + port1 + "/upload/bob");
177-
builder.addBodyPart(new FilePart("file1", testResource1File, "text/plain", StandardCharsets.UTF_8.name()));
177+
builder.addBodyPart(new FilePart("file1", testResource1File, "text/plain", StandardCharsets.UTF_8));
178178
builder.addBodyPart(new FilePart("file2", testResource2File, "application/x-gzip", null));
179-
builder.addBodyPart(new StringPart("Name", "Dominic", StandardCharsets.UTF_8.name()));
180-
builder.addBodyPart(new FilePart("file3", testResource3File, "text/plain", StandardCharsets.UTF_8.name()));
179+
builder.addBodyPart(new StringPart("Name", "Dominic", StandardCharsets.UTF_8));
180+
builder.addBodyPart(new FilePart("file3", testResource3File, "text/plain", StandardCharsets.UTF_8));
181181

182-
builder.addBodyPart(new StringPart("Age", "3", AsyncHttpProviderUtils.DEFAULT_CHARSET.name()));
183-
builder.addBodyPart(new StringPart("Height", "shrimplike", AsyncHttpProviderUtils.DEFAULT_CHARSET.name()));
184-
builder.addBodyPart(new StringPart("Hair", "ridiculous", AsyncHttpProviderUtils.DEFAULT_CHARSET.name()));
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));
185185

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

188188
Request r = builder.build();
189189

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ public void testCloseMasterInvalidDerived() throws Exception {
307307
public void testMultiPartPut() throws Exception {
308308
SimpleAsyncHttpClient client = new SimpleAsyncHttpClient.Builder().setProviderClass(getProviderClass()).setUrl(getTargetUrl() + "/multipart").build();
309309
try {
310-
Response response = client.put(new ByteArrayPart("baPart", "testMultiPart".getBytes(StandardCharsets.UTF_8), "application/test", StandardCharsets.UTF_8.name(), "fileName")).get();
310+
Response response = client.put(new ByteArrayPart("baPart", "testMultiPart".getBytes(StandardCharsets.UTF_8), "application/test", StandardCharsets.UTF_8, "fileName")).get();
311311

312312
String body = response.getResponseBody();
313313
String contentType = response.getHeader("X-Content-Type");
@@ -331,7 +331,7 @@ public void testMultiPartPut() throws Exception {
331331
public void testMultiPartPost() throws Exception {
332332
SimpleAsyncHttpClient client = new SimpleAsyncHttpClient.Builder().setProviderClass(getProviderClass()).setUrl(getTargetUrl() + "/multipart").build();
333333
try {
334-
Response response = client.post(new ByteArrayPart("baPart", "testMultiPart".getBytes(StandardCharsets.UTF_8), "application/test", StandardCharsets.UTF_8.name(), "fileName")).get();
334+
Response response = client.post(new ByteArrayPart("baPart", "testMultiPart".getBytes(StandardCharsets.UTF_8), "application/test", StandardCharsets.UTF_8, "fileName")).get();
335335

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

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@
1212
*/
1313
package org.asynchttpclient.multipart;
1414

15+
import static org.asynchttpclient.util.StandardCharsets.UTF_8;
16+
1517
import org.asynchttpclient.Body;
1618
import org.asynchttpclient.FluentCaseInsensitiveStringsMap;
17-
import org.asynchttpclient.util.StandardCharsets;
1819
import org.testng.Assert;
1920
import org.testng.annotations.Test;
2021

@@ -37,10 +38,10 @@ public void testBasics() {
3738
parts.add(new FilePart("filePart", testFile));
3839

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

4243
// add a string
43-
parts.add(new StringPart("stringPart", "testString", StandardCharsets.UTF_8.name()));
44+
parts.add(new StringPart("stringPart", "testString", UTF_8));
4445

4546
compareContentLength(parts);
4647
}

0 commit comments

Comments
 (0)