22
33import java .io .IOException ;
44import java .net .HttpURLConnection ;
5- import java .net .URL ;
65import java .util .Map ;
76import com .github .scribejava .core .exceptions .OAuthConnectionException ;
87import com .github .scribejava .core .exceptions .OAuthException ;
98import com .github .scribejava .core .oauth .OAuthService ;
109import java .io .File ;
10+ import java .net .URL ;
1111import java .net .UnknownHostException ;
1212import java .util .HashMap ;
1313import java .util .List ;
1414
1515public class OAuthRequest extends AbstractRequest {
1616
17- private HttpURLConnection connection ;
18-
1917 private final OAuthConfig config ;
2018
2119 public OAuthRequest (Verb verb , String url , OAuthConfig config ) {
@@ -34,31 +32,32 @@ public OAuthRequest(Verb verb, String url, OAuthConfig config) {
3432 */
3533 public Response send () {
3634 try {
37- createConnection ();
38- return doSend ();
35+ return doSend (config , isFollowRedirects (), getHeaders (), getVerb (), getCompleteUrl (), this );
3936 } catch (IOException | RuntimeException e ) {
4037 throw new OAuthConnectionException (getCompleteUrl (), e );
4138 }
4239 }
4340
44- Response doSend () throws IOException {
45- final Verb verb = getVerb ();
46- connection .setRequestMethod (verb .name ());
41+ private static Response doSend (OAuthConfig config , boolean followRedirects , Map <String , String > headers ,
42+ Verb httpVerb , String completeUrl , OAuthRequest request ) throws IOException {
43+ final HttpURLConnection connection = (HttpURLConnection ) new URL (completeUrl ).openConnection ();
44+ connection .setInstanceFollowRedirects (followRedirects );
45+ connection .setRequestMethod (httpVerb .name ());
4746 if (config .getConnectTimeout () != null ) {
4847 connection .setConnectTimeout (config .getConnectTimeout ());
4948 }
5049 if (config .getReadTimeout () != null ) {
5150 connection .setReadTimeout (config .getReadTimeout ());
5251 }
53- addHeaders ();
54- if (hasBodyContent () ) {
55- final File filePayload = getFilePayload ();
52+ addHeaders (connection , headers , config . getUserAgent () );
53+ if (httpVerb == Verb . POST || httpVerb == Verb . PUT || httpVerb == Verb . DELETE ) {
54+ final File filePayload = request . getFilePayload ();
5655 if (filePayload != null ) {
5756 throw new UnsupportedOperationException ("Sync Requests do not support File payload for the moment" );
58- } else if (getStringPayload () != null ) {
59- addBody (getStringPayload ().getBytes (getCharset ()));
57+ } else if (request . getStringPayload () != null ) {
58+ addBody (connection , request . getStringPayload ().getBytes (request . getCharset ()));
6059 } else {
61- addBody (getByteArrayPayload ());
60+ addBody (connection , request . getByteArrayPayload ());
6261 }
6362 }
6463
@@ -86,25 +85,16 @@ private static Map<String, String> parseHeaders(HttpURLConnection conn) {
8685 return headers ;
8786 }
8887
89- private void createConnection () throws IOException {
90- final String completeUrl = getCompleteUrl ();
91- if (connection == null ) {
92- connection = (HttpURLConnection ) new URL (completeUrl ).openConnection ();
93- connection .setInstanceFollowRedirects (isFollowRedirects ());
94- }
95- }
96-
97- void addHeaders () {
98- for (Map .Entry <String , String > entry : getHeaders ().entrySet ()) {
88+ private static void addHeaders (HttpURLConnection connection , Map <String , String > headers , String userAgent ) {
89+ for (Map .Entry <String , String > entry : headers .entrySet ()) {
9990 connection .setRequestProperty (entry .getKey (), entry .getValue ());
10091 }
101- final String userAgent = config .getUserAgent ();
10292 if (userAgent != null ) {
10393 connection .setRequestProperty (OAuthConstants .USER_AGENT_HEADER_NAME , userAgent );
10494 }
10595 }
10696
107- void addBody (byte [] content ) throws IOException {
97+ private static void addBody (HttpURLConnection connection , byte [] content ) throws IOException {
10898 connection .setRequestProperty (CONTENT_LENGTH , String .valueOf (content .length ));
10999
110100 if (connection .getRequestProperty (CONTENT_TYPE ) == null ) {
@@ -113,8 +103,4 @@ void addBody(byte[] content) throws IOException {
113103 connection .setDoOutput (true );
114104 connection .getOutputStream ().write (content );
115105 }
116-
117- void setConnection (HttpURLConnection connection ) {
118- this .connection = connection ;
119- }
120106}
0 commit comments