Skip to content

Commit f59c78b

Browse files
author
Stephane Landelle
committed
Make Parts immutable
1 parent 1fd0307 commit f59c78b

File tree

5 files changed

+29
-101
lines changed

5 files changed

+29
-101
lines changed

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

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,82 +34,82 @@ public abstract class Part {
3434
/**
3535
* Carriage return/linefeed
3636
*/
37-
protected static final String CRLF = "\r\n";
37+
public static final String CRLF = "\r\n";
3838

3939
/**
4040
* Carriage return/linefeed as a byte array
4141
*/
42-
static final byte[] CRLF_BYTES = MultipartEncodingUtil.getAsciiBytes(CRLF);
42+
public static final byte[] CRLF_BYTES = MultipartEncodingUtil.getAsciiBytes(CRLF);
4343

4444
/**
4545
* Content dispostion characters
4646
*/
47-
protected static final String QUOTE = "\"";
47+
public static final String QUOTE = "\"";
4848

4949
/**
5050
* Content dispostion as a byte array
5151
*/
52-
static final byte[] QUOTE_BYTES = MultipartEncodingUtil.getAsciiBytes(QUOTE);
52+
public static final byte[] QUOTE_BYTES = MultipartEncodingUtil.getAsciiBytes(QUOTE);
5353

5454
/**
5555
* Extra characters
5656
*/
57-
protected static final String EXTRA = "--";
57+
public static final String EXTRA = "--";
5858

5959
/**
6060
* Extra characters as a byte array
6161
*/
62-
static final byte[] EXTRA_BYTES = MultipartEncodingUtil.getAsciiBytes(EXTRA);
62+
public static final byte[] EXTRA_BYTES = MultipartEncodingUtil.getAsciiBytes(EXTRA);
6363

6464
/**
6565
* Content dispostion characters
6666
*/
67-
protected static final String CONTENT_DISPOSITION = "Content-Disposition: form-data; name=";
67+
public static final String CONTENT_DISPOSITION = "Content-Disposition: form-data; name=";
6868

6969
/**
7070
* Content dispostion as a byte array
7171
*/
72-
static final byte[] CONTENT_DISPOSITION_BYTES = MultipartEncodingUtil.getAsciiBytes(CONTENT_DISPOSITION);
72+
public static final byte[] CONTENT_DISPOSITION_BYTES = MultipartEncodingUtil.getAsciiBytes(CONTENT_DISPOSITION);
7373

7474
/**
7575
* Content type header
7676
*/
77-
protected static final String CONTENT_TYPE = "Content-Type: ";
77+
public static final String CONTENT_TYPE = "Content-Type: ";
7878

7979
/**
8080
* Content type header as a byte array
8181
*/
82-
static final byte[] CONTENT_TYPE_BYTES = MultipartEncodingUtil.getAsciiBytes(CONTENT_TYPE);
82+
public static final byte[] CONTENT_TYPE_BYTES = MultipartEncodingUtil.getAsciiBytes(CONTENT_TYPE);
8383

8484
/**
8585
* Content charset
8686
*/
87-
protected static final String CHARSET = "; charset=";
87+
public static final String CHARSET = "; charset=";
8888

8989
/**
9090
* Content charset as a byte array
9191
*/
92-
static final byte[] CHARSET_BYTES = MultipartEncodingUtil.getAsciiBytes(CHARSET);
92+
public static final byte[] CHARSET_BYTES = MultipartEncodingUtil.getAsciiBytes(CHARSET);
9393

9494
/**
9595
* Content type header
9696
*/
97-
protected static final String CONTENT_TRANSFER_ENCODING = "Content-Transfer-Encoding: ";
97+
public static final String CONTENT_TRANSFER_ENCODING = "Content-Transfer-Encoding: ";
9898

9999
/**
100100
* Content type header as a byte array
101101
*/
102-
static final byte[] CONTENT_TRANSFER_ENCODING_BYTES = MultipartEncodingUtil.getAsciiBytes(CONTENT_TRANSFER_ENCODING);
102+
public static final byte[] CONTENT_TRANSFER_ENCODING_BYTES = MultipartEncodingUtil.getAsciiBytes(CONTENT_TRANSFER_ENCODING);
103103

104104
/**
105105
* Content type header
106106
*/
107-
protected static final String CONTENT_ID = "Content-ID: ";
107+
public static final String CONTENT_ID = "Content-ID: ";
108108

109109
/**
110110
* Content type header as a byte array
111111
*/
112-
static final byte[] CONTENT_ID_BYTES = MultipartEncodingUtil.getAsciiBytes(CONTENT_ID);
112+
public static final byte[] CONTENT_ID_BYTES = MultipartEncodingUtil.getAsciiBytes(CONTENT_ID);
113113

114114
/**
115115
* Return the name of this part.

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

Lines changed: 5 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,24 @@ public abstract class PartBase extends Part {
2525
/**
2626
* Name of the file part.
2727
*/
28-
private String name;
28+
private final String name;
2929

3030
/**
3131
* Content type of the file part.
3232
*/
33-
private String contentType;
33+
private final String contentType;
3434

3535
/**
3636
* Content encoding of the file part.
3737
*/
38-
private String charSet;
38+
private final String charSet;
3939

4040
/**
4141
* The transfer encoding.
4242
*/
43-
private String transferEncoding;
43+
private final String transferEncoding;
4444

45-
private String contentId;
45+
private final String contentId;
4646

4747
/**
4848
* Constructor.
@@ -101,50 +101,7 @@ public String getTransferEncoding() {
101101
return transferEncoding;
102102
}
103103

104-
/**
105-
* Sets the character encoding.
106-
*
107-
* @param charSet the character encoding, or <code>null</code> to exclude the character encoding header
108-
*/
109-
public void setCharSet(String charSet) {
110-
this.charSet = charSet;
111-
}
112-
113-
/**
114-
* Sets the content type.
115-
*
116-
* @param contentType the content type, or <code>null</code> to exclude the content type header
117-
*/
118-
public void setContentType(String contentType) {
119-
this.contentType = contentType;
120-
}
121-
122-
/**
123-
* Sets the part name.
124-
*
125-
* @param name
126-
*/
127-
public void setName(String name) {
128-
if (name == null) {
129-
throw new IllegalArgumentException("Name must not be null");
130-
}
131-
this.name = name;
132-
}
133-
134-
/**
135-
* Sets the transfer encoding.
136-
*
137-
* @param transferEncoding the transfer encoding, or <code>null</code> to exclude the transfer encoding header
138-
*/
139-
public void setTransferEncoding(String transferEncoding) {
140-
this.transferEncoding = transferEncoding;
141-
}
142-
143104
public String getContentId() {
144105
return contentId;
145106
}
146-
147-
public void setContentId(String contentId) {
148-
this.contentId = contentId;
149-
}
150107
}

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

Lines changed: 6 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,12 @@ public class StringPart extends PartBase {
4343
/**
4444
* Contents of this StringPart.
4545
*/
46-
private byte[] content;
47-
48-
/**
49-
* The String value of this part.
50-
*/
51-
private final String value;
46+
private final byte[] content;
5247

48+
public StringPart(String name, String value, String charset) {
49+
this(name, value, charset, null);
50+
}
51+
5352
/**
5453
* Constructor.
5554
*
@@ -68,21 +67,7 @@ public StringPart(String name, String value, String charset, String contentId) {
6867
// See RFC 2048, 2.8. "8bit Data"
6968
throw new IllegalArgumentException("NULs may not be present in string parts");
7069
}
71-
this.value = value;
72-
}
73-
74-
public StringPart(String name, String value, String charset) {
75-
this(name, value, charset, null);
76-
}
77-
78-
/**
79-
* Constructor.
80-
*
81-
* @param name The name of the part
82-
* @param value the string to post
83-
*/
84-
public StringPart(String name, String value) {
85-
this(name, value, null, null);
70+
content = MultipartEncodingUtil.getBytes(value, charset);
8671
}
8772

8873
/**
@@ -91,9 +76,6 @@ public StringPart(String name, String value) {
9176
* @return the content in bytes
9277
*/
9378
private byte[] getContent() {
94-
if (content == null) {
95-
content = MultipartEncodingUtil.getBytes(value, getCharSet());
96-
}
9779
return content;
9880
}
9981

@@ -115,15 +97,4 @@ protected void sendData(OutputStream out) throws IOException {
11597
protected long lengthOfData() {
11698
return getContent().length;
11799
}
118-
119-
/*
120-
* (non-Javadoc)
121-
*
122-
* @see org.apache.commons.httpclient.methods.multipart.BasePart#setCharSet(java.lang.String)
123-
*/
124-
public void setCharSet(String charSet) {
125-
super.setCharSet(charSet);
126-
this.content = null;
127-
}
128-
129100
}

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

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

656-
Part p = new StringPart("foo", "bar");
656+
Part p = new StringPart("foo", "bar", "UTF-8");
657657

658658
client.preparePost(getTargetUrl()).addBodyPart(p).execute(new AsyncCompletionHandlerAdapter() {
659659

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ public void testSendingSmallFilesAndByteArray() {
170170
builder.setUrl("http://localhost" + ":" + port1 + "/upload/bob");
171171
builder.addBodyPart(new FilePart("file1", testResource1File, "text/plain", "UTF-8"));
172172
builder.addBodyPart(new FilePart("file2", testResource2File, "application/x-gzip", null));
173-
builder.addBodyPart(new StringPart("Name", "Dominic"));
173+
builder.addBodyPart(new StringPart("Name", "Dominic", "UTF-8"));
174174
builder.addBodyPart(new FilePart("file3", testResource3File, "text/plain", "UTF-8"));
175175

176176
builder.addBodyPart(new StringPart("Age", "3", AsyncHttpProviderUtils.DEFAULT_CHARSET));

0 commit comments

Comments
 (0)