1- /**
1+ /*
22 * ######
33 * ######
44 * ############ ####( ###### #####. ###### ############ ############
3434import com .adyen .Config ;
3535
3636public class HttpURLConnectionClient implements ClientInterface {
37- private HttpURLConnection httpConnection ;
3837 private static final String CHARSET = "UTF-8" ;
39-
38+
4039 /**
4140 * Does a POST request.
4241 * config is used to obtain basic auth username, password and User-Agent
43- *
44- * @param requestUrl
45- * @param requestBody
46- * @param config
47- * @return
48- * @throws IOException
4942 */
5043 @ Override
5144 public String request (String requestUrl , String requestBody , Config config ) throws IOException , HTTPClientException {
52- String response = createRequest (requestUrl , config .getApplicationName ())
53- .setBasicAuthentication (config .getUsername (), config .getPassword ())
54- .setContentType ("application/json" )
55- .doPostRequest (requestBody );
45+ HttpURLConnection httpConnection = createRequest (requestUrl , config .getApplicationName ());
46+ setBasicAuthentication (httpConnection , config .getUsername (), config .getPassword ());
47+ setContentType (httpConnection , "application/json" );
48+
49+ String response = doPostRequest (httpConnection , requestBody );
5650
5751 return response ;
5852 }
5953
60- private static String getResponseBody (InputStream responseStream )
61- throws IOException {
54+ private static String getResponseBody (InputStream responseStream ) throws IOException {
6255 //\A is the beginning of the stream boundary
6356 Scanner scanner = new Scanner (responseStream , CHARSET );
6457 String rBody = scanner .useDelimiter ("\\ A" ).next ();
@@ -69,39 +62,29 @@ private static String getResponseBody(InputStream responseStream)
6962
7063 /**
7164 * Does a POST request with HTTP key-value pairs
72- *
73- * @param requestUrl
74- * @param params
75- * @param config
76- * @return
77- * @throws IOException
78- * @throws HTTPClientException
7965 */
8066 @ Override
8167 public String post (String requestUrl , Map <String , String > params , Config config ) throws IOException , HTTPClientException {
8268 String postQuery = getQuery (params );
83- String response = createRequest (requestUrl , config .getApplicationName ())
84- . doPostRequest (postQuery );
69+ HttpURLConnection httpConnection = createRequest (requestUrl , config .getApplicationName ());
70+ String response = doPostRequest (httpConnection , postQuery );
8571
8672 return response ;
8773 }
8874
8975 /**
9076 * Get HTTP querystring from Map<String,String>
91- *
92- * @param params
93- * @return
94- * @throws UnsupportedEncodingException
9577 */
9678 private String getQuery (Map <String , String > params ) throws UnsupportedEncodingException {
9779 StringBuilder result = new StringBuilder ();
9880 boolean first = true ;
9981
10082 for (Map .Entry <String , String > pair : params .entrySet ()) {
101- if (first )
83+ if (first ) {
10284 first = false ;
103- else
85+ } else {
10486 result .append ("&" );
87+ }
10588
10689 result .append (URLEncoder .encode (pair .getKey (), CHARSET ));
10790 result .append ("=" );
@@ -113,65 +96,47 @@ private String getQuery(Map<String, String> params) throws UnsupportedEncodingEx
11396
11497 /**
11598 * Initialize the httpConnection
116- *
117- * @param requestUrl
118- * @param applicationName
119- * @return
120- * @throws IOException
12199 */
122- private HttpURLConnectionClient createRequest (String requestUrl , String applicationName ) throws IOException {
100+ private HttpURLConnection createRequest (String requestUrl , String applicationName ) throws IOException {
123101 URL targetUrl = new URL (requestUrl );
124102
125103 // set configurations
126- httpConnection = (HttpURLConnection ) targetUrl .openConnection ();
104+ HttpURLConnection httpConnection = (HttpURLConnection ) targetUrl .openConnection ();
127105 httpConnection .setUseCaches (false );
128106 httpConnection .setDoOutput (true );
129107 httpConnection .setRequestMethod ("POST" );
130108
131109 httpConnection .setRequestProperty ("Accept-Charset" , CHARSET );
132- httpConnection .setRequestProperty ("User-Agent" ,
133- String .format ("%s %s%s" , applicationName , Client .USER_AGENT_SUFFIX , Client .LIB_VERSION ));
110+ httpConnection .setRequestProperty ("User-Agent" , String .format ("%s %s%s" , applicationName , Client .USER_AGENT_SUFFIX , Client .LIB_VERSION ));
134111
135- return this ;
112+ return httpConnection ;
136113 }
137114
138115 /**
139116 * Adds Basic Authentication headers
140- *
141- * @param username
142- * @param password
143- * @return
144117 */
145- private HttpURLConnectionClient setBasicAuthentication (String username , String password ) {
118+ private HttpURLConnection setBasicAuthentication (HttpURLConnection httpConnection , String username , String password ) {
146119 // set basic authentication
147120 String authString = username + ":" + password ;
148121 byte [] authEncBytes = Base64 .encodeBase64 (authString .getBytes ());
149122 String authStringEnc = new String (authEncBytes );
150123
151124 httpConnection .setRequestProperty ("Authorization" , "Basic " + authStringEnc );
152- return this ;
125+ return httpConnection ;
153126 }
154127
155128 /**
156129 * Sets content type
157- *
158- * @param contentType
159- * @return
160130 */
161- private HttpURLConnectionClient setContentType (String contentType ) {
131+ private HttpURLConnection setContentType (HttpURLConnection httpConnection , String contentType ) {
162132 httpConnection .setRequestProperty ("Content-Type" , contentType );
163- return this ;
133+ return httpConnection ;
164134 }
165135
166136 /**
167137 * Does a POST request with raw body
168- *
169- * @param requestBody
170- * @return
171- * @throws IOException
172- * @throws HTTPClientException
173138 */
174- private String doPostRequest (String requestBody ) throws IOException , HTTPClientException {
139+ private String doPostRequest (HttpURLConnection httpConnection , String requestBody ) throws IOException , HTTPClientException {
175140 String response = null ;
176141
177142 OutputStream outputStream = httpConnection .getOutputStream ();
@@ -181,16 +146,11 @@ private String doPostRequest(String requestBody) throws IOException, HTTPClientE
181146 int responseCode = httpConnection .getResponseCode ();
182147 if (responseCode != HttpURLConnection .HTTP_OK ) {
183148 //Read the response from the error stream
184- if (httpConnection .getErrorStream () != null ) {
149+ if (httpConnection .getErrorStream () != null ) {
185150 response = getResponseBody (httpConnection .getErrorStream ());
186151 }
187152
188- HTTPClientException httpClientException = new HTTPClientException (
189- responseCode ,
190- "HTTP Exception" ,
191- httpConnection .getHeaderFields (),
192- response
193- );
153+ HTTPClientException httpClientException = new HTTPClientException (responseCode , "HTTP Exception" , httpConnection .getHeaderFields (), response );
194154
195155 throw httpClientException ;
196156 }
0 commit comments