1+ package net .itarray .automotion .tools .http .connections ;
2+
3+ import org .apache .http .client .HttpClient ;
4+ import org .apache .http .client .methods .*;
5+ import org .apache .http .client .utils .URIBuilder ;
6+ import org .apache .http .config .Registry ;
7+ import org .apache .http .config .RegistryBuilder ;
8+ import org .apache .http .conn .HttpClientConnectionManager ;
9+ import org .apache .http .conn .socket .ConnectionSocketFactory ;
10+ import org .apache .http .conn .socket .PlainConnectionSocketFactory ;
11+ import org .apache .http .conn .ssl .SSLConnectionSocketFactory ;
12+ import org .apache .http .conn .ssl .SSLContexts ;
13+ import org .apache .http .conn .ssl .TrustSelfSignedStrategy ;
14+ import org .apache .http .entity .StringEntity ;
15+ import org .apache .http .impl .client .HttpClientBuilder ;
16+ import org .apache .http .impl .conn .BasicHttpClientConnectionManager ;
17+
18+ import javax .net .ssl .SSLContext ;
19+ import java .io .BufferedReader ;
20+ import java .io .IOException ;
21+ import java .io .InputStreamReader ;
22+ import java .net .URISyntaxException ;
23+ import java .security .KeyManagementException ;
24+ import java .security .KeyStoreException ;
25+ import java .security .NoSuchAlgorithmException ;
26+ import java .util .HashMap ;
27+ import java .util .Map ;
28+ import java .util .logging .Logger ;
29+
30+ public class RestClient {
31+
32+ private static final Logger LOG = Logger .getLogger (RestClient .class .getName ());
33+ private Map <String , String > headers ;
34+ private String body ;
35+ private String baseUrl ;
36+
37+ public RestClient (String baseUrl ) {
38+ this .baseUrl = baseUrl ;
39+ this .headers = new HashMap ();
40+ }
41+
42+ public RestClient withHeader (String header , String value ) {
43+ this .headers .put (header , value );
44+ return this ;
45+ }
46+
47+ public RestClient withBody (String body ) {
48+ this .body = body ;
49+ return this ;
50+ }
51+
52+ public HttpResponse post (String endpointUrl ) {
53+ try {
54+ HttpClient httpClient = getHttpClientBuilder ().build ();
55+ String uri = buildUrl (endpointUrl );
56+ HttpPost request = new HttpPost (uri );
57+ for (Map .Entry <String , String > map : headers .entrySet ()) {
58+ request .addHeader (map .getKey (), map .getValue ());
59+ }
60+ StringEntity params = new StringEntity (body , "UTF-8" );
61+ request .setEntity (params );
62+
63+ CloseableHttpResponse httpResponse = (CloseableHttpResponse ) httpClient .execute (request );
64+
65+
66+ Map <Integer , String > mapResult = new HashMap <>();
67+
68+ int statusCode = httpResponse .getStatusLine ().getStatusCode ();
69+ String value = String .valueOf (getBodyResponse (httpResponse ));
70+ mapResult .put (statusCode , value );
71+
72+ log ("POST" , uri , body , statusCode , value );
73+ return new HttpResponse (statusCode , value );
74+ } catch (Exception e ) {
75+ e .printStackTrace ();
76+ return null ;
77+ }
78+ }
79+
80+ public HttpResponse get (String endpointUrl ) {
81+ try {
82+ HttpClient httpClient = getHttpClientBuilder ().build ();
83+ String uri = buildUrl (endpointUrl );
84+ HttpGet request = new HttpGet (uri );
85+
86+ for (Map .Entry <String , String > map : headers .entrySet ()) {
87+ request .addHeader (map .getKey (), map .getValue ());
88+ }
89+
90+ CloseableHttpResponse httpResponse = (CloseableHttpResponse ) httpClient .execute (request );
91+
92+ Map <Integer , String > mapResult = new HashMap <>();
93+
94+ int statusCode = httpResponse .getStatusLine ().getStatusCode ();
95+ String value = String .valueOf (getBodyResponse (httpResponse ));
96+ mapResult .put (statusCode , value );
97+
98+ log ("GET" , uri , "" , statusCode , value );
99+
100+ return new HttpResponse (statusCode , value );
101+ } catch (Exception e ) {
102+ e .printStackTrace ();
103+ return null ;
104+ }
105+ }
106+
107+ public HttpResponse delete (String endpointUrl ) {
108+ try {
109+ HttpClient httpClient = getHttpClientBuilder ().build ();
110+ String uri = buildUrl (endpointUrl );
111+ HttpDelete request = new HttpDelete (uri );
112+
113+ for (Map .Entry <String , String > map : headers .entrySet ()) {
114+ request .addHeader (map .getKey (), map .getValue ());
115+ }
116+
117+ CloseableHttpResponse httpResponse = (CloseableHttpResponse ) httpClient .execute (request );
118+
119+ Map <Integer , String > mapResult = new HashMap <>();
120+
121+ int statusCode = httpResponse .getStatusLine ().getStatusCode ();
122+ String value = String .valueOf (getBodyResponse (httpResponse ));
123+ mapResult .put (statusCode , value );
124+
125+ log ("DELETE" , uri , "" , statusCode , value );
126+
127+ return new HttpResponse (statusCode , value );
128+ } catch (Exception e ) {
129+ e .printStackTrace ();
130+ return null ;
131+ }
132+ }
133+
134+ public HttpResponse update (String endpointUrl ) {
135+ try {
136+ HttpClient httpClient = getHttpClientBuilder ().build ();
137+ String uri = buildUrl (endpointUrl );
138+ HttpPatch request = new HttpPatch (uri );
139+
140+ for (Map .Entry <String , String > map : headers .entrySet ()) {
141+ request .addHeader (map .getKey (), map .getValue ());
142+ }
143+
144+ StringEntity params = new StringEntity (body , "UTF-8" );
145+ request .setEntity (params );
146+
147+ CloseableHttpResponse httpResponse = (CloseableHttpResponse ) httpClient .execute (request );
148+
149+ Map <Integer , String > mapResult = new HashMap <>();
150+
151+ int statusCode = httpResponse .getStatusLine ().getStatusCode ();
152+ String value = String .valueOf (getBodyResponse (httpResponse ));
153+ mapResult .put (statusCode , value );
154+
155+ log ("PATCH" , uri , body , statusCode , value );
156+
157+ return new HttpResponse (statusCode , value );
158+ } catch (Exception e ) {
159+ e .printStackTrace ();
160+ return null ;
161+ }
162+ }
163+
164+ private String buildUrl (String endpointUrl ) throws URISyntaxException {
165+ String [] split = baseUrl .split ("://" );
166+ String scheme = split .length > 0 ? split [0 ] : "http" ;
167+ String host = split .length > 0 ? split [1 ] : baseUrl ;
168+ URIBuilder builder = new URIBuilder ();
169+ builder .setScheme (scheme );
170+ builder .setHost (host );
171+ builder .setPath (endpointUrl );
172+ return builder .build ().toString ();
173+
174+ }
175+
176+ private void log (String requestType , String uri , String body , int responseCode , String responseBody ) {
177+ LOG .info ("------------------------------------------- START ---------------------------------------" );
178+ LOG .info ("*** " + requestType + ": " + uri );
179+ if (!body .equals ("" )) {
180+ LOG .info ("*** Request Body" + ": " + body );
181+ }
182+ LOG .info ("*** Response Code: " + responseCode );
183+ LOG .info ("*** Response Body: " + responseBody );
184+ LOG .info ("-------------------------------------------- END ----------------------------------------\n " );
185+ }
186+
187+
188+ private StringBuffer getBodyResponse (CloseableHttpResponse response ) throws IOException {
189+ BufferedReader reader = new BufferedReader (new InputStreamReader (
190+ response .getEntity ().getContent ()));
191+
192+ String inputLine ;
193+
194+ StringBuffer body = new StringBuffer ();
195+
196+ while ((inputLine = reader .readLine ()) != null ) {
197+ body .append (inputLine );
198+ }
199+ reader .close ();
200+
201+ response .close ();
202+ return body ;
203+ }
204+
205+ private HttpClientBuilder getHttpClientBuilder () {
206+ SSLContext sslContext = null ;
207+ try {
208+ sslContext = SSLContexts .custom ().loadTrustMaterial (null , new TrustSelfSignedStrategy ()).useTLS ().build ();
209+ } catch (NoSuchAlgorithmException | KeyManagementException | KeyStoreException e ) {
210+ e .printStackTrace ();
211+ }
212+
213+ SSLConnectionSocketFactory sslConnectionFactory = new SSLConnectionSocketFactory (sslContext , SSLConnectionSocketFactory .ALLOW_ALL_HOSTNAME_VERIFIER );
214+
215+
216+ HttpClientBuilder httpClientBuilder = HttpClientBuilder .create ();
217+
218+ httpClientBuilder .setSSLSocketFactory (sslConnectionFactory );
219+ Registry <ConnectionSocketFactory > registry = RegistryBuilder .<ConnectionSocketFactory >create ()
220+ .register ("https" , sslConnectionFactory )
221+ .register ("http" , new PlainConnectionSocketFactory ())
222+ .build ();
223+
224+ HttpClientConnectionManager ccm = new BasicHttpClientConnectionManager (registry );
225+ httpClientBuilder .setConnectionManager (ccm );
226+ return httpClientBuilder ;
227+ }
228+ }
0 commit comments