11package com .github .scribejava .core .httpclient .jdk ;
22
33import com .github .scribejava .core .exceptions .OAuthException ;
4+ import com .github .scribejava .core .httpclient .BodyPartPayload ;
45import com .github .scribejava .core .httpclient .HttpClient ;
6+ import com .github .scribejava .core .httpclient .MultipartPayload ;
57import com .github .scribejava .core .model .OAuthAsyncRequestCallback ;
68import com .github .scribejava .core .model .OAuthConstants ;
79import com .github .scribejava .core .model .OAuthRequest ;
@@ -38,32 +40,46 @@ public void close() {
3840 @ Override
3941 public <T > Future <T > executeAsync (String userAgent , Map <String , String > headers , Verb httpVerb , String completeUrl ,
4042 byte [] bodyContents , OAuthAsyncRequestCallback <T > callback , OAuthRequest .ResponseConverter <T > converter ) {
41- try {
42- final Response response = execute (userAgent , headers , httpVerb , completeUrl , bodyContents );
43- @ SuppressWarnings ( "unchecked" )
44- final T t = converter == null ? ( T ) response : converter . convert ( response );
45- if ( callback != null ) {
46- callback . onCompleted ( t );
47- }
48- return new JDKHttpFuture <>( t );
49- } catch ( InterruptedException | ExecutionException | IOException e ) {
50- callback . onThrowable ( e );
51- return new JDKHttpFuture <>( e );
52- }
43+
44+ return doExecuteAsync (userAgent , headers , httpVerb , completeUrl , BodyType . BYTE_ARRAY , bodyContents , callback ,
45+ converter );
46+ }
47+
48+ @ Override
49+ public < T > Future < T > executeAsync ( String userAgent , Map < String , String > headers , Verb httpVerb , String completeUrl ,
50+ MultipartPayload bodyContents , OAuthAsyncRequestCallback < T > callback ,
51+ OAuthRequest . ResponseConverter < T > converter ) {
52+
53+ return doExecuteAsync ( userAgent , headers , httpVerb , completeUrl , BodyType . MULTIPART , bodyContents , callback ,
54+ converter );
5355 }
5456
5557 @ Override
5658 public <T > Future <T > executeAsync (String userAgent , Map <String , String > headers , Verb httpVerb , String completeUrl ,
5759 String bodyContents , OAuthAsyncRequestCallback <T > callback , OAuthRequest .ResponseConverter <T > converter ) {
60+
61+ return doExecuteAsync (userAgent , headers , httpVerb , completeUrl , BodyType .STRING , bodyContents , callback ,
62+ converter );
63+ }
64+
65+ @ Override
66+ public <T > Future <T > executeAsync (String userAgent , Map <String , String > headers , Verb httpVerb , String completeUrl ,
67+ File bodyContents , OAuthAsyncRequestCallback <T > callback , OAuthRequest .ResponseConverter <T > converter ) {
68+ throw new UnsupportedOperationException ("JDKHttpClient does not support File payload for the moment" );
69+ }
70+
71+ private <T > Future <T > doExecuteAsync (String userAgent , Map <String , String > headers , Verb httpVerb ,
72+ String completeUrl , BodyType bodyType , Object bodyContents , OAuthAsyncRequestCallback <T > callback ,
73+ OAuthRequest .ResponseConverter <T > converter ) {
5874 try {
59- final Response response = execute (userAgent , headers , httpVerb , completeUrl , bodyContents );
75+ final Response response = doExecute (userAgent , headers , httpVerb , completeUrl , bodyType , bodyContents );
6076 @ SuppressWarnings ("unchecked" )
6177 final T t = converter == null ? (T ) response : converter .convert (response );
6278 if (callback != null ) {
6379 callback .onCompleted (t );
6480 }
6581 return new JDKHttpFuture <>(t );
66- } catch (InterruptedException | ExecutionException | IOException e ) {
82+ } catch (IOException e ) {
6783 if (callback != null ) {
6884 callback .onThrowable (e );
6985 }
@@ -72,15 +88,15 @@ public <T> Future<T> executeAsync(String userAgent, Map<String, String> headers,
7288 }
7389
7490 @ Override
75- public < T > Future < T > executeAsync (String userAgent , Map <String , String > headers , Verb httpVerb , String completeUrl ,
76- File bodyContents , OAuthAsyncRequestCallback < T > callback , OAuthRequest . ResponseConverter < T > converter ) {
77- throw new UnsupportedOperationException ( "JDKHttpClient does not support File payload for the moment" );
91+ public Response execute (String userAgent , Map <String , String > headers , Verb httpVerb , String completeUrl ,
92+ byte [] bodyContents ) throws InterruptedException , ExecutionException , IOException {
93+ return doExecute ( userAgent , headers , httpVerb , completeUrl , BodyType . BYTE_ARRAY , bodyContents );
7894 }
7995
8096 @ Override
8197 public Response execute (String userAgent , Map <String , String > headers , Verb httpVerb , String completeUrl ,
82- byte [] bodyContents ) throws InterruptedException , ExecutionException , IOException {
83- return doExecute (userAgent , headers , httpVerb , completeUrl , BodyType .BYTE_ARRAY , bodyContents );
98+ MultipartPayload multipartPayloads ) throws InterruptedException , ExecutionException , IOException {
99+ return doExecute (userAgent , headers , httpVerb , completeUrl , BodyType .MULTIPART , multipartPayloads );
84100 }
85101
86102 @ Override
@@ -92,15 +108,9 @@ public Response execute(String userAgent, Map<String, String> headers, Verb http
92108 @ Override
93109 public Response execute (String userAgent , Map <String , String > headers , Verb httpVerb , String completeUrl ,
94110 File bodyContents ) throws InterruptedException , ExecutionException , IOException {
95- throw new UnsupportedOperationException ("JDKHttpClient do not support File payload for the moment" );
111+ throw new UnsupportedOperationException ("JDKHttpClient does not support File payload for the moment" );
96112 }
97113
98- @ Override
99- public Response execute (String userAgent , Map <String , String > headers , Verb httpVerb , String completeUrl ,
100- OAuthRequest .MultipartPayloads multipartPayloads ) throws InterruptedException , ExecutionException , IOException {
101- return doExecute (userAgent , headers , httpVerb , completeUrl , BodyType .MULTIPART , multipartPayloads );
102- }
103-
104114 private Response doExecute (String userAgent , Map <String , String > headers , Verb httpVerb , String completeUrl ,
105115 BodyType bodyType , Object bodyContents ) throws IOException {
106116 final HttpURLConnection connection = (HttpURLConnection ) new URL (completeUrl ).openConnection ();
@@ -136,9 +146,9 @@ void setBody(HttpURLConnection connection, Object bodyContents, boolean requires
136146 }
137147 },
138148 MULTIPART {
139- @ Override
149+ @ Override
140150 void setBody (HttpURLConnection connection , Object bodyContents , boolean requiresBody ) throws IOException {
141- addBody (connection , (OAuthRequest . MultipartPayloads ) bodyContents , requiresBody );
151+ addBody (connection , (MultipartPayload ) bodyContents , requiresBody );
142152 }
143153 },
144154 STRING {
@@ -150,7 +160,6 @@ void setBody(HttpURLConnection connection, Object bodyContents, boolean requires
150160
151161 abstract void setBody (HttpURLConnection connection , Object bodyContents , boolean requiresBody )
152162 throws IOException ;
153-
154163 }
155164
156165 private static Map <String , String > parseHeaders (HttpURLConnection conn ) {
@@ -178,41 +187,41 @@ private static void addHeaders(HttpURLConnection connection, Map<String, String>
178187 }
179188 }
180189
181- /*
182- * Multipart implementation supporting more than one payload
183- *
190+ /**
191+ * Multipart implementation supporting more than one payload
184192 */
185- private static void addBody (HttpURLConnection connection , OAuthRequest .MultipartPayloads multipartPayloads , boolean requiresBody ) throws IOException {
186- int contentLength = multipartPayloads .getContentLength ();
187- System .out .println ("length: " + contentLength );
193+ private static void addBody (HttpURLConnection connection , MultipartPayload multipartPayload , boolean requiresBody )
194+ throws IOException {
195+
196+ final int contentLength = multipartPayload .getContentLength ();
188197 if (requiresBody || contentLength > 0 ) {
189- connection .setRequestProperty (CONTENT_LENGTH , String .valueOf (contentLength ));
190- if (connection .getRequestProperty (CONTENT_TYPE ) == null ) {
191- connection .setRequestProperty (CONTENT_TYPE , DEFAULT_CONTENT_TYPE );
198+ final OutputStream os = prepareConnectionForBodyAndGetOutputStream (connection , contentLength );
199+
200+ for (BodyPartPayload bodyPart : multipartPayload .getBodyParts ()) {
201+ os .write (multipartPayload .getStartBoundary (bodyPart ));
202+ os .write (bodyPart .getPayload ());
203+ os .write (multipartPayload .getEndBoundary ());
192204 }
193- System .out .println ("content-length: " + connection .getRequestProperty (CONTENT_TYPE ));
194- connection .setDoOutput (true );
195- OutputStream os = connection .getOutputStream ();
196-
197- int totalParts = multipartPayloads .getMultipartPayloadList ().size ();
198- for (int i = 0 ; i < totalParts ; i ++) {
199- os .write (multipartPayloads .getStartBoundary (i ));
200- os .write (multipartPayloads .getMultipartPayloadList ().get (i ).getPayload (), 0 , multipartPayloads .getMultipartPayloadList ().get (i ).getLength ());
201- os .write (multipartPayloads .getEndBoundary (i ));
202- }
203- }
204- }
205-
205+ }
206+ }
207+
206208 private static void addBody (HttpURLConnection connection , byte [] content , boolean requiresBody ) throws IOException {
207209 final int contentLength = content .length ;
208210 if (requiresBody || contentLength > 0 ) {
209- connection .setRequestProperty (CONTENT_LENGTH , String .valueOf (contentLength ));
210- if (connection .getRequestProperty (CONTENT_TYPE ) == null ) {
211- connection .setRequestProperty (CONTENT_TYPE , DEFAULT_CONTENT_TYPE );
212- }
213- connection .setDoOutput (true );
214- connection .getOutputStream ().write (content );
211+ prepareConnectionForBodyAndGetOutputStream (connection , contentLength ).write (content );
215212 }
216213 }
217-
214+
215+ private static OutputStream prepareConnectionForBodyAndGetOutputStream (HttpURLConnection connection ,
216+ int contentLength ) throws IOException {
217+
218+ connection .setRequestProperty (CONTENT_LENGTH , String .valueOf (contentLength ));
219+ if (connection .getRequestProperty (CONTENT_TYPE ) == null ) {
220+ connection .setRequestProperty (CONTENT_TYPE , DEFAULT_CONTENT_TYPE );
221+
222+ }
223+ connection .setDoOutput (true );
224+ return connection .getOutputStream ();
225+ }
226+
218227}
0 commit comments