X Tutup
Skip to content

Commit fac5e70

Browse files
committed
* fix Muplipart request model and implement it for a jdk HTTP client (thanks to https://github.com/NTPape)
1 parent b4f2cf8 commit fac5e70

File tree

20 files changed

+1203
-59
lines changed

20 files changed

+1203
-59
lines changed

changelog

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
[SNAPSHOT]
2+
* fix Muplipart request model and implement it for a jdk HTTP client (thanks to https://github.com/NTPape)
3+
14
[6.2.0]
25
* add new API Microsoft Azure Active Directory (Azure AD) 2.0 (thanks to https://github.com/rzukow and https://github.com/dgrudenic)
36

pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
<dependency>
6969
<groupId>com.squareup.okhttp3</groupId>
7070
<artifactId>mockwebserver</artifactId>
71-
<version>3.12.0</version>
71+
<version>3.12.1</version>
7272
<scope>test</scope>
7373
</dependency>
7474
</dependencies>
@@ -92,7 +92,7 @@
9292
<plugin>
9393
<groupId>org.apache.maven.plugins</groupId>
9494
<artifactId>maven-jar-plugin</artifactId>
95-
<version>3.1.0</version>
95+
<version>3.1.1</version>
9696
<configuration>
9797
<archive>
9898
<manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
@@ -107,7 +107,7 @@
107107
<dependency>
108108
<groupId>com.puppycrawl.tools</groupId>
109109
<artifactId>checkstyle</artifactId>
110-
<version>8.15</version>
110+
<version>8.16</version>
111111
</dependency>
112112
</dependencies>
113113
</plugin>

scribejava-core/src/main/java/com/github/scribejava/core/httpclient/AbstractAsyncOnlyHttpClient.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,27 @@ public Response execute(String userAgent, Map<String, String> headers, Verb http
1818
(OAuthRequest.ResponseConverter<Response>) null).get();
1919
}
2020

21+
/**
22+
* @deprecated {@inheritDoc}
23+
*/
2124
@Override
25+
@Deprecated
2226
public Response execute(String userAgent, Map<String, String> headers, Verb httpVerb, String completeUrl,
2327
MultipartPayload bodyContents) throws InterruptedException, ExecutionException, IOException {
2428

2529
return executeAsync(userAgent, headers, httpVerb, completeUrl, bodyContents, null,
2630
(OAuthRequest.ResponseConverter<Response>) null).get();
2731
}
2832

33+
@Override
34+
public Response execute(String userAgent, Map<String, String> headers, Verb httpVerb, String completeUrl,
35+
com.github.scribejava.core.httpclient.multipart.MultipartPayload bodyContents)
36+
throws InterruptedException, ExecutionException, IOException {
37+
38+
return executeAsync(userAgent, headers, httpVerb, completeUrl, bodyContents, null,
39+
(OAuthRequest.ResponseConverter<Response>) null).get();
40+
}
41+
2942
@Override
3043
public Response execute(String userAgent, Map<String, String> headers, Verb httpVerb, String completeUrl,
3144
String bodyContents) throws InterruptedException, ExecutionException, IOException {
Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,69 @@
11
package com.github.scribejava.core.httpclient;
22

3+
import java.util.Collections;
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
7+
/**
8+
* @deprecated use {@link com.github.scribejava.core.httpclient.multipart.ByteArrayBodyPartPayload}
9+
*/
10+
@Deprecated
311
public class BodyPartPayload {
412

5-
private final String contentDisposition;
6-
private final String contentType;
13+
private final Map<String, String> headers;
714
private final byte[] payload;
815

9-
public BodyPartPayload(String contentDisposition, String contentType, byte[] payload) {
10-
this.contentDisposition = contentDisposition;
11-
this.contentType = contentType;
16+
public BodyPartPayload(Map<String, String> headers, byte[] payload) {
17+
this.headers = headers;
1218
this.payload = payload;
1319
}
1420

21+
public BodyPartPayload(String contentDisposition, String contentType, byte[] payload) {
22+
this(createHeadersMap(contentDisposition, contentType), payload);
23+
}
24+
25+
public BodyPartPayload(String contentDisposition, byte[] payload) {
26+
this(contentDisposition, null, payload);
27+
}
28+
29+
public Map<String, String> getHeaders() {
30+
return headers;
31+
}
32+
33+
/**
34+
* @return return
35+
* @deprecated use {@link #getHeaders() } and then get("Content-Disposition")
36+
*/
37+
@Deprecated
1538
public String getContentDisposition() {
16-
return contentDisposition;
39+
return headers.get("Content-Disposition");
1740
}
1841

42+
/**
43+
* @return return
44+
* @deprecated use {@link #getHeaders() } and then get("Content-Type")
45+
*/
46+
@Deprecated
1947
public String getContentType() {
20-
return contentType;
48+
return headers.get(HttpClient.CONTENT_TYPE);
2149
}
2250

2351
public byte[] getPayload() {
2452
return payload;
2553
}
2654

55+
private static Map<String, String> createHeadersMap(String contentDisposition, String contentType) {
56+
if (contentDisposition == null && contentType == null) {
57+
return Collections.emptyMap();
58+
}
59+
60+
final Map<String, String> headers = new HashMap<>();
61+
if (contentDisposition != null) {
62+
headers.put("Content-Disposition", contentDisposition);
63+
}
64+
if (contentType != null) {
65+
headers.put(HttpClient.CONTENT_TYPE, contentType);
66+
}
67+
return headers;
68+
}
2769
}

scribejava-core/src/main/java/com/github/scribejava/core/httpclient/HttpClient.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.github.scribejava.core.httpclient;
22

3+
import com.github.scribejava.core.httpclient.multipart.MultipartPayload;
34
import com.github.scribejava.core.model.OAuthAsyncRequestCallback;
45
import com.github.scribejava.core.model.OAuthRequest;
56
import com.github.scribejava.core.model.Response;
@@ -20,6 +21,27 @@ public interface HttpClient extends Closeable {
2021
<T> Future<T> executeAsync(String userAgent, Map<String, String> headers, Verb httpVerb, String completeUrl,
2122
byte[] bodyContents, OAuthAsyncRequestCallback<T> callback, OAuthRequest.ResponseConverter<T> converter);
2223

24+
/**
25+
* @param <T> T
26+
* @param userAgent userAgent
27+
* @param headers headers
28+
* @param httpVerb httpVerb
29+
* @param completeUrl completeUrl
30+
* @param bodyContents bodyContents
31+
* @param callback callback
32+
* @param converter converter
33+
* @return return
34+
*
35+
* @deprecated use {@link #executeAsync(java.lang.String, java.util.Map, com.github.scribejava.core.model.Verb,
36+
* java.lang.String, com.github.scribejava.core.httpclient.multipart.MultipartPayload,
37+
* com.github.scribejava.core.model.OAuthAsyncRequestCallback,
38+
* com.github.scribejava.core.model.OAuthRequest.ResponseConverter)}
39+
*/
40+
@Deprecated
41+
<T> Future<T> executeAsync(String userAgent, Map<String, String> headers, Verb httpVerb, String completeUrl,
42+
com.github.scribejava.core.httpclient.MultipartPayload bodyContents, OAuthAsyncRequestCallback<T> callback,
43+
OAuthRequest.ResponseConverter<T> converter);
44+
2345
<T> Future<T> executeAsync(String userAgent, Map<String, String> headers, Verb httpVerb, String completeUrl,
2446
MultipartPayload bodyContents, OAuthAsyncRequestCallback<T> callback,
2547
OAuthRequest.ResponseConverter<T> converter);
@@ -33,6 +55,24 @@ <T> Future<T> executeAsync(String userAgent, Map<String, String> headers, Verb h
3355
Response execute(String userAgent, Map<String, String> headers, Verb httpVerb, String completeUrl,
3456
byte[] bodyContents) throws InterruptedException, ExecutionException, IOException;
3557

58+
/**
59+
* @param userAgent userAgent
60+
* @param headers headers
61+
* @param httpVerb httpVerb
62+
* @param completeUrl completeUrl
63+
* @param bodyContents bodyContents
64+
* @return return
65+
* @throws InterruptedException InterruptedException
66+
* @throws ExecutionException ExecutionException
67+
* @throws IOException IOException
68+
* @deprecated use {@link #execute(java.lang.String, java.util.Map, com.github.scribejava.core.model.Verb,
69+
* java.lang.String, com.github.scribejava.core.httpclient.multipart.MultipartPayload)}
70+
*/
71+
@Deprecated
72+
Response execute(String userAgent, Map<String, String> headers, Verb httpVerb, String completeUrl,
73+
com.github.scribejava.core.httpclient.MultipartPayload bodyContents)
74+
throws InterruptedException, ExecutionException, IOException;
75+
3676
Response execute(String userAgent, Map<String, String> headers, Verb httpVerb, String completeUrl,
3777
MultipartPayload bodyContents) throws InterruptedException, ExecutionException, IOException;
3878

Lines changed: 66 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,80 @@
11
package com.github.scribejava.core.httpclient;
22

33
import java.util.ArrayList;
4+
import java.util.Collections;
45
import java.util.List;
6+
import java.util.Map;
57

68
/**
79
* The class containing more than one payload of multipart/form-data request
10+
*
11+
* @deprecated use {@link com.github.scribejava.core.httpclient.multipart.MultipartPayload}
812
*/
13+
@Deprecated
914
public class MultipartPayload {
1015

16+
private static final String DEFAULT_SUBTYPE = "form-data";
17+
1118
private final String boundary;
19+
private final Map<String, String> headers;
1220
private final List<BodyPartPayload> bodyParts = new ArrayList<>();
1321

1422
public MultipartPayload(String boundary) {
1523
this.boundary = boundary;
24+
headers = Collections.singletonMap(HttpClient.CONTENT_TYPE,
25+
"multipart/" + DEFAULT_SUBTYPE + "; boundary=\"" + boundary + '"');
1626
}
1727

28+
/**
29+
* @param bodyPart bodyPart
30+
* @return return
31+
* @deprecated no replace for that. implement yourself. See code here
32+
* {@link com.github.scribejava.core.httpclient.jdk.JDKHttpClient
33+
* #getPayload(com.github.scribejava.core.httpclient.MultipartPayload)}
34+
*/
35+
@Deprecated
1836
public byte[] getStartBoundary(BodyPartPayload bodyPart) {
19-
return ("--" + boundary + "\r\n"
20-
+ "Content-Disposition: " + bodyPart.getContentDisposition() + "\r\n"
21-
+ (bodyPart.getContentType() == null
22-
? "" : HttpClient.CONTENT_TYPE + ": " + bodyPart.getContentType() + "\r\n")
23-
+ "\r\n").getBytes();
37+
final StringBuilder startBoundary = new StringBuilder();
38+
startBoundary.append("\r\n--")
39+
.append(boundary)
40+
.append("\r\n");
41+
42+
for (Map.Entry<String, String> header : bodyPart.getHeaders().entrySet()) {
43+
startBoundary.append(header.getKey())
44+
.append(": ")
45+
.append(header.getValue())
46+
.append("\r\n");
47+
}
48+
return startBoundary.append("\r\n").toString().getBytes();
2449
}
2550

51+
/**
52+
* @return return
53+
* @deprecated no replace for that. implement yourself. See code here
54+
* {@link com.github.scribejava.core.httpclient.jdk.JDKHttpClient
55+
* #getPayload(com.github.scribejava.core.httpclient.MultipartPayload)}
56+
*/
57+
@Deprecated
2658
public byte[] getEndBoundary() {
2759
return ("\r\n--" + boundary + "--\r\n").getBytes();
2860
}
2961

62+
/**
63+
* @return return
64+
* @deprecated no replace for that. implement yourself. See code here
65+
* {@link com.github.scribejava.core.httpclient.jdk.JDKHttpClient
66+
* #getPayload(com.github.scribejava.core.httpclient.MultipartPayload)}
67+
*/
68+
@Deprecated
3069
public int getContentLength() {
3170
int contentLength = 0;
71+
3272
for (BodyPartPayload bodyPart : bodyParts) {
33-
contentLength += bodyPart.getPayload().length
34-
+ bodyPart.getContentDisposition().length();
35-
if (bodyPart.getContentType() != null) {
36-
contentLength += 16 //length of constant portions of contentType header
37-
+ bodyPart.getContentType().length();
38-
}
73+
contentLength += getStartBoundary(bodyPart).length + bodyPart.getPayload().length;
74+
}
75+
if (!bodyParts.isEmpty()) {
76+
contentLength += getEndBoundary().length;
3977
}
40-
41-
contentLength += (37 //length of constant portions of contentDisposition header,
42-
//see getStartBoundary and getEndBoundary methods
43-
+ boundary.length() * 2 //twice. start and end parts
44-
) * bodyParts.size(); //for every part
4578
return contentLength;
4679
}
4780

@@ -52,4 +85,21 @@ public List<BodyPartPayload> getBodyParts() {
5285
public void addMultipartPayload(String contentDisposition, String contentType, byte[] payload) {
5386
bodyParts.add(new BodyPartPayload(contentDisposition, contentType, payload));
5487
}
88+
89+
/**
90+
* @return return
91+
* @deprecated use {@link #getHeaders() } and then get("Content-Type")
92+
*/
93+
@Deprecated
94+
public String getContentType() {
95+
return headers.get(HttpClient.CONTENT_TYPE);
96+
}
97+
98+
public Map<String, String> getHeaders() {
99+
return headers;
100+
}
101+
102+
public String getBoundary() {
103+
return boundary;
104+
}
55105
}

0 commit comments

Comments
 (0)
X Tutup