|
24 | 24 |
|
25 | 25 | import java.io.BufferedReader; |
26 | 26 | import java.io.DataOutputStream; |
| 27 | +import java.io.InputStream; |
27 | 28 | import java.io.InputStreamReader; |
28 | 29 | import java.net.HttpURLConnection; |
29 | 30 | import java.net.URL; |
|
39 | 40 |
|
40 | 41 | public final class AuthHttpClient { |
41 | 42 |
|
42 | | - public String httpPost(final URL uri, final Map<String, String> headerParams, final Map<String, String> bodyParams) throws Exception { |
43 | | - if (uri == null || bodyParams == null || bodyParams.size() < 1 ) { |
44 | | - throw new AuthClientException("Invalid http request parameters"); |
45 | | - } |
46 | | - |
47 | | - final HttpsURLConnection httpsURLConnection = (HttpsURLConnection) uri.openConnection(); |
48 | | - DataOutputStream httpOutputStream = null; |
49 | | - BufferedReader br = null; |
50 | | - try { |
51 | | - // Request header |
52 | | - httpsURLConnection.setRequestMethod(ClientConstants.HTTP_REQUEST_TYPE_POST); |
53 | | - httpsURLConnection.setDoOutput(true); |
54 | | - for (Map.Entry<String, String> param: headerParams.entrySet()) { |
55 | | - httpsURLConnection.addRequestProperty(param.getKey(), param.getValue()); |
56 | | - } |
57 | | - |
58 | | - // Request body |
59 | | - StringBuilder reqBuilder = new StringBuilder(); |
60 | | - int index = bodyParams.size(); |
61 | | - for (Map.Entry<String, String> param: bodyParams.entrySet()) { |
62 | | - reqBuilder.append(URLEncoder.encode(param.getKey(), "UTF-8")).append('=').append(URLEncoder.encode(param.getValue(), "UTF-8")); |
63 | | - if(index-- > 1){ |
64 | | - reqBuilder.append('&'); |
65 | | - } |
66 | | - } |
67 | | - String requestBody = reqBuilder.toString(); |
68 | | - |
69 | | - httpOutputStream = new DataOutputStream(httpsURLConnection.getOutputStream()); |
70 | | - |
71 | | - httpOutputStream.writeBytes(requestBody); |
72 | | - httpOutputStream.flush(); |
73 | | - |
74 | | - // Parse response |
75 | | - Map<String, List<String>> respHeaders = httpsURLConnection.getHeaderFields(); |
76 | | - |
77 | | - int responseCode = httpsURLConnection.getResponseCode(); |
78 | | - |
79 | | - if (responseCode >= HttpURLConnection.HTTP_OK && |
80 | | - responseCode < HttpURLConnection.HTTP_INTERNAL_ERROR) { |
81 | | - // Return response from the http call |
82 | | - br = new BufferedReader(new InputStreamReader(httpsURLConnection.getInputStream())); |
83 | | - String line = ""; |
84 | | - StringBuilder responseOutput = new StringBuilder(); |
85 | | - while ((line = br.readLine()) != null) { |
86 | | - responseOutput.append(line); |
87 | | - } |
88 | | - return responseOutput.toString(); |
89 | | - } else { |
90 | | - // Throw a Cognito Client Exception |
91 | | - throw new AuthServiceException(httpsURLConnection.getResponseMessage()); |
92 | | - } |
93 | | - |
94 | | - } catch (final Exception e) { |
95 | | - throw e; |
96 | | - } finally { |
97 | | - if (httpOutputStream != null) { |
98 | | - httpOutputStream.close(); |
99 | | - } |
100 | | - if (br != null) { |
101 | | - br.close(); |
102 | | - } |
103 | | - } |
104 | | - } |
| 43 | + public String httpPost(final URL uri, final Map<String, String> headerParams, final Map<String, String> bodyParams) throws Exception { |
| 44 | + if (uri == null || bodyParams == null || bodyParams.size() < 1 ) { |
| 45 | + throw new AuthClientException("Invalid http request parameters"); |
| 46 | + } |
| 47 | + |
| 48 | + final HttpsURLConnection httpsURLConnection = (HttpsURLConnection) uri.openConnection(); |
| 49 | + DataOutputStream httpOutputStream = null; |
| 50 | + BufferedReader br = null; |
| 51 | + try { |
| 52 | + // Request header |
| 53 | + httpsURLConnection.setRequestMethod(ClientConstants.HTTP_REQUEST_TYPE_POST); |
| 54 | + httpsURLConnection.setDoOutput(true); |
| 55 | + for (Map.Entry<String, String> param: headerParams.entrySet()) { |
| 56 | + httpsURLConnection.addRequestProperty(param.getKey(), param.getValue()); |
| 57 | + } |
| 58 | + |
| 59 | + // Request body |
| 60 | + StringBuilder reqBuilder = new StringBuilder(); |
| 61 | + int index = bodyParams.size(); |
| 62 | + for (Map.Entry<String, String> param: bodyParams.entrySet()) { |
| 63 | + reqBuilder.append(URLEncoder.encode(param.getKey(), "UTF-8")).append('=').append(URLEncoder.encode(param.getValue(), "UTF-8")); |
| 64 | + if(index-- > 1){ |
| 65 | + reqBuilder.append('&'); |
| 66 | + } |
| 67 | + } |
| 68 | + String requestBody = reqBuilder.toString(); |
| 69 | + |
| 70 | + httpOutputStream = new DataOutputStream(httpsURLConnection.getOutputStream()); |
| 71 | + |
| 72 | + httpOutputStream.writeBytes(requestBody); |
| 73 | + httpOutputStream.flush(); |
| 74 | + |
| 75 | + // Parse response |
| 76 | + Map<String, List<String>> respHeaders = httpsURLConnection.getHeaderFields(); |
| 77 | + |
| 78 | + int responseCode = httpsURLConnection.getResponseCode(); |
| 79 | + |
| 80 | + if (responseCode >= HttpURLConnection.HTTP_OK && |
| 81 | + responseCode < HttpURLConnection.HTTP_INTERNAL_ERROR) { |
| 82 | + |
| 83 | + // Return response from the http call |
| 84 | + InputStream httpDataStream; |
| 85 | + if (responseCode < HttpURLConnection.HTTP_BAD_REQUEST) { |
| 86 | + httpDataStream = httpsURLConnection.getInputStream(); |
| 87 | + } else { |
| 88 | + httpDataStream = httpsURLConnection.getErrorStream(); |
| 89 | + } |
| 90 | + br = new BufferedReader(new InputStreamReader(httpDataStream)); |
| 91 | + |
| 92 | + String line = ""; |
| 93 | + StringBuilder responseOutput = new StringBuilder(); |
| 94 | + while ((line = br.readLine()) != null) { |
| 95 | + responseOutput.append(line); |
| 96 | + } |
| 97 | + |
| 98 | + return responseOutput.toString(); |
| 99 | + } else { |
| 100 | + // Throw a Cognito Client Exception |
| 101 | + throw new AuthServiceException(httpsURLConnection.getResponseMessage()); |
| 102 | + } |
| 103 | + |
| 104 | + } catch (final Exception e) { |
| 105 | + throw e; |
| 106 | + } finally { |
| 107 | + if (httpOutputStream != null) { |
| 108 | + httpOutputStream.close(); |
| 109 | + } |
| 110 | + if (br != null) { |
| 111 | + br.close(); |
| 112 | + } |
| 113 | + } |
| 114 | + } |
105 | 115 | } |
0 commit comments