Skip to content

Commit acb0972

Browse files
committed
support chunks in JDKHttpClient's Multipart (thanks to https://github.com/eos1d3)
1 parent 01aa0b2 commit acb0972

File tree

8 files changed

+316
-26
lines changed

8 files changed

+316
-26
lines changed

changelog

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
[SNAPSHOT]
22
* add Kakao API (https://kakao.com/) (thanks to https://github.com/v0o0v)
3+
* support chunks in JDKHttpClient's Multipart (thanks to https://github.com/eos1d3)
34

45
[7.1.1]
56
* add Proxy support (via config's option) to internal JDKHttpClient (thanks to https://github.com/bjournaud)

scribejava-core/src/main/java/com/github/scribejava/core/httpclient/multipart/BodyPartPayload.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public BodyPartPayload() {
1313
}
1414

1515
public BodyPartPayload(String contentType) {
16-
this(contentType == null ? null : Collections.singletonMap(HttpClient.CONTENT_TYPE, contentType));
16+
this(convertContentTypeToHeaders(contentType));
1717
}
1818

1919
public BodyPartPayload(Map<String, String> headers) {
@@ -23,4 +23,8 @@ public BodyPartPayload(Map<String, String> headers) {
2323
public Map<String, String> getHeaders() {
2424
return headers;
2525
}
26+
27+
protected static Map<String, String> convertContentTypeToHeaders(String contentType) {
28+
return contentType == null ? null : Collections.singletonMap(HttpClient.CONTENT_TYPE, contentType);
29+
}
2630
}

scribejava-core/src/main/java/com/github/scribejava/core/httpclient/multipart/ByteArrayBodyPartPayload.java

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,46 @@
55
public class ByteArrayBodyPartPayload extends BodyPartPayload {
66

77
private final byte[] payload;
8+
private final int off;
9+
private final int len;
810

9-
public ByteArrayBodyPartPayload(byte[] payload) {
11+
public ByteArrayBodyPartPayload(byte[] payload, int off, int len, Map<String, String> headers) {
12+
super(headers);
1013
this.payload = payload;
14+
this.off = off;
15+
this.len = len;
16+
}
17+
18+
public ByteArrayBodyPartPayload(byte[] payload, Map<String, String> headers) {
19+
this(payload, 0, payload.length, headers);
1120
}
1221

1322
public ByteArrayBodyPartPayload(byte[] payload, String contentType) {
14-
super(contentType);
15-
this.payload = payload;
23+
this(payload, convertContentTypeToHeaders(contentType));
1624
}
1725

18-
public ByteArrayBodyPartPayload(byte[] payload, Map<String, String> headers) {
19-
super(headers);
20-
this.payload = payload;
26+
public ByteArrayBodyPartPayload(byte[] payload, int off, int len, String contentType) {
27+
this(payload, off, len, convertContentTypeToHeaders(contentType));
28+
}
29+
30+
public ByteArrayBodyPartPayload(byte[] payload) {
31+
this(payload, (Map<String, String>) null);
32+
}
33+
34+
public ByteArrayBodyPartPayload(byte[] payload, int off, int len) {
35+
this(payload, off, len, (Map<String, String>) null);
2136
}
2237

2338
public byte[] getPayload() {
2439
return payload;
2540
}
41+
42+
public int getOff() {
43+
return off;
44+
}
45+
46+
public int getLen() {
47+
return len;
48+
}
49+
2650
}

scribejava-core/src/main/java/com/github/scribejava/core/httpclient/multipart/FileByteArrayBodyPartPayload.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,51 @@ public FileByteArrayBodyPartPayload(byte[] payload) {
1111
this(payload, null);
1212
}
1313

14+
public FileByteArrayBodyPartPayload(byte[] payload, int off, int len) {
15+
this(payload, off, len, null);
16+
}
17+
1418
public FileByteArrayBodyPartPayload(byte[] payload, String name) {
1519
this(payload, name, null);
1620
}
1721

22+
public FileByteArrayBodyPartPayload(byte[] payload, int off, int len, String name) {
23+
this(payload, off, len, name, null);
24+
}
25+
1826
public FileByteArrayBodyPartPayload(byte[] payload, String name, String filename) {
1927
this(null, payload, name, filename);
2028
}
2129

30+
public FileByteArrayBodyPartPayload(byte[] payload, int off, int len, String name, String filename) {
31+
this(null, payload, off, len, name, filename);
32+
}
33+
2234
public FileByteArrayBodyPartPayload(String contentType, byte[] payload) {
2335
this(contentType, payload, null);
2436
}
2537

38+
public FileByteArrayBodyPartPayload(String contentType, byte[] payload, int off, int len) {
39+
this(contentType, payload, off, len, null);
40+
}
41+
2642
public FileByteArrayBodyPartPayload(String contentType, byte[] payload, String name) {
2743
this(contentType, payload, name, null);
2844
}
2945

46+
public FileByteArrayBodyPartPayload(String contentType, byte[] payload, int off, int len, String name) {
47+
this(contentType, payload, off, len, name, null);
48+
}
49+
3050
public FileByteArrayBodyPartPayload(String contentType, byte[] payload, String name, String filename) {
3151
super(payload, composeHeaders(contentType, name, filename));
3252
}
3353

54+
public FileByteArrayBodyPartPayload(String contentType, byte[] payload, int off, int len, String name,
55+
String filename) {
56+
super(payload, off, len, composeHeaders(contentType, name, filename));
57+
}
58+
3459
private static Map<String, String> composeHeaders(String contentType, String name, String filename) {
3560

3661
String contentDispositionHeader = "form-data";

scribejava-core/src/main/java/com/github/scribejava/core/httpclient/multipart/MultipartPayload.java

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,26 +72,71 @@ private static String parseOrGenerateBoundary(Map<String, String> headers) {
7272
return parsedBoundary == null ? MultipartUtils.generateDefaultBoundary() : parsedBoundary;
7373
}
7474

75+
/**
76+
*
77+
* @param fileContent fileContent
78+
* @deprecated use {@link #addBodyPart(com.github.scribejava.core.httpclient.multipart.BodyPartPayload)}
79+
*/
80+
@Deprecated
7581
public void addFileBodyPart(byte[] fileContent) {
7682
addBodyPart(new FileByteArrayBodyPartPayload(fileContent));
7783
}
7884

85+
/**
86+
*
87+
* @param fileContent fileContent
88+
* @param name name
89+
* @deprecated use {@link #addBodyPart(com.github.scribejava.core.httpclient.multipart.BodyPartPayload)}
90+
*/
91+
@Deprecated
7992
public void addFileBodyPart(byte[] fileContent, String name) {
8093
addBodyPart(new FileByteArrayBodyPartPayload(fileContent, name));
8194
}
8295

96+
/**
97+
*
98+
* @param fileContent fileContent
99+
* @param name name
100+
* @param filename filename
101+
* @deprecated use {@link #addBodyPart(com.github.scribejava.core.httpclient.multipart.BodyPartPayload)}
102+
*/
103+
@Deprecated
83104
public void addFileBodyPart(byte[] fileContent, String name, String filename) {
84105
addBodyPart(new FileByteArrayBodyPartPayload(fileContent, name, filename));
85106
}
86107

108+
/**
109+
*
110+
* @param contentType contentType
111+
* @param fileContent fileContent
112+
* @deprecated use {@link #addBodyPart(com.github.scribejava.core.httpclient.multipart.BodyPartPayload)}
113+
*/
114+
@Deprecated
87115
public void addFileBodyPart(String contentType, byte[] fileContent) {
88116
addBodyPart(new FileByteArrayBodyPartPayload(contentType, fileContent));
89117
}
90118

119+
/**
120+
*
121+
* @param contentType contentType
122+
* @param fileContent fileContent
123+
* @param name name
124+
* @deprecated use {@link #addBodyPart(com.github.scribejava.core.httpclient.multipart.BodyPartPayload)}
125+
*/
126+
@Deprecated
91127
public void addFileBodyPart(String contentType, byte[] fileContent, String name) {
92128
addBodyPart(new FileByteArrayBodyPartPayload(contentType, fileContent, name));
93129
}
94130

131+
/**
132+
*
133+
* @param contentType contentType
134+
* @param fileContent fileContent
135+
* @param name name
136+
* @param filename filename
137+
* @deprecated use {@link #addBodyPart(com.github.scribejava.core.httpclient.multipart.BodyPartPayload)}
138+
*/
139+
@Deprecated
95140
public void addFileBodyPart(String contentType, byte[] fileContent, String name, String filename) {
96141
addBodyPart(new FileByteArrayBodyPartPayload(contentType, fileContent, name, filename));
97142
}
@@ -108,14 +153,34 @@ public void addBodyPart(MultipartPayload multipartPayload) {
108153
bodyParts.add(multipartPayload);
109154
}
110155

156+
/**
157+
*
158+
* @param bodyPartPayload bodyPartPayload
159+
* @deprecated use {@link #addBodyPart(com.github.scribejava.core.httpclient.multipart.BodyPartPayload)}
160+
*/
161+
@Deprecated
111162
public void addBodyPart(byte[] bodyPartPayload) {
112163
addBodyPart(new ByteArrayBodyPartPayload(bodyPartPayload));
113164
}
114165

166+
/**
167+
*
168+
* @param bodyPartPayload bodyPartPayload
169+
* @param contentType contentType
170+
* @deprecated use {@link #addBodyPart(com.github.scribejava.core.httpclient.multipart.BodyPartPayload)}
171+
*/
172+
@Deprecated
115173
public void addBodyPart(byte[] bodyPartPayload, String contentType) {
116174
addBodyPart(new ByteArrayBodyPartPayload(bodyPartPayload, contentType));
117175
}
118176

177+
/**
178+
*
179+
* @param bodyPartPayload bodyPartPayload
180+
* @param headers headers
181+
* @deprecated use {@link #addBodyPart(com.github.scribejava.core.httpclient.multipart.BodyPartPayload)}
182+
*/
183+
@Deprecated
119184
public void addBodyPart(byte[] bodyPartPayload, Map<String, String> headers) {
120185
addBodyPart(new ByteArrayBodyPartPayload(bodyPartPayload, headers));
121186
}

scribejava-core/src/main/java/com/github/scribejava/core/httpclient/multipart/MultipartUtils.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ public static ByteArrayOutputStream getPayload(MultipartPayload multipartPayload
6464
if (bodyPart instanceof MultipartPayload) {
6565
getPayload((MultipartPayload) bodyPart).writeTo(os);
6666
} else if (bodyPart instanceof ByteArrayBodyPartPayload) {
67-
os.write(((ByteArrayBodyPartPayload) bodyPart).getPayload());
67+
final ByteArrayBodyPartPayload byteArrayBodyPart = (ByteArrayBodyPartPayload) bodyPart;
68+
os.write(byteArrayBodyPart.getPayload(), byteArrayBodyPart.getOff(), byteArrayBodyPart.getLen());
6869
} else {
6970
throw new AssertionError(bodyPart.getClass());
7071
}

0 commit comments

Comments
 (0)