2020import java .security .cert .X509Certificate ;
2121import java .util .Map ;
2222
23- import org .apache .http . HttpEntity ;
24- import org .apache .http .HttpResponse ;
25- import org .apache .http . auth . AuthScope ;
23+ import org .apache .brooklyn . util . exceptions . Exceptions ;
24+ import org .apache .brooklyn . util . http .HttpTool ;
25+ import org .apache .brooklyn . util . http . HttpToolResponse ;
2626import org .apache .http .auth .Credentials ;
2727import org .apache .http .client .HttpClient ;
28- import org .apache .http .client .methods .HttpGet ;
29- import org .apache .http .client .methods .HttpPost ;
30- import org .apache .http .conn .scheme .Scheme ;
31- import org .apache .http .conn .ssl .SSLSocketFactory ;
3228import org .apache .http .conn .ssl .TrustStrategy ;
33- import org .apache .http .entity .ByteArrayEntity ;
34- import org .apache .http .impl .client .DefaultHttpClient ;
35- import org .apache .http .util .EntityUtils ;
3629import org .bouncycastle .util .io .Streams ;
3730import org .slf4j .Logger ;
3831import org .slf4j .LoggerFactory ;
4134import com .google .common .collect .Multimap ;
4235import com .google .common .collect .Multimaps ;
4336
44- import org .apache .brooklyn .util .http .HttpToolResponse ;
45- import org .apache .brooklyn .util .exceptions .Exceptions ;
46-
4737/**
4838 * HTTP convenience static methods.
4939 * <p>
5040 * Uses the Apache {@link HttpClient} for connections.
41+ *
42+ * @deprecated since 0.10.0; instead use {@link HttpTool}.
5143 */
44+ @ Deprecated
5245public class HttpUtil {
5346
5447 public static final Logger LOG = LoggerFactory .getLogger (HttpUtil .class );
5548
5649 public static HttpClient createHttpClient (URI uri , Optional <Credentials > credentials ) {
57- final DefaultHttpClient httpClient = new DefaultHttpClient ();
58-
59- // TODO if supplier returns null, we may wish to defer initialization until url available?
60- if (uri != null && "https" .equalsIgnoreCase (uri .getScheme ())) {
61- try {
62- int port = (uri .getPort () >= 0 ) ? uri .getPort () : 443 ;
63- SSLSocketFactory socketFactory = new SSLSocketFactory (
64- new TrustAllStrategy (), SSLSocketFactory .ALLOW_ALL_HOSTNAME_VERIFIER );
65- Scheme sch = new Scheme ("https" , port , socketFactory );
66- httpClient .getConnectionManager ().getSchemeRegistry ().register (sch );
67- } catch (Exception e ) {
68- LOG .warn ("Error in HTTP Feed of {}, setting trust for uri {}" , uri );
69- throw Exceptions .propagate (e );
70- }
71- }
72-
73- // Set credentials
74- if (uri != null && credentials .isPresent ()) {
75- String hostname = uri .getHost ();
76- int port = uri .getPort ();
77- httpClient .getCredentialsProvider ().setCredentials (new AuthScope (hostname , port ), credentials .get ());
78- }
79-
80- return httpClient ;
50+ return HttpTool .httpClientBuilder ()
51+ .uri (uri )
52+ .credential (credentials )
53+ .build ();
8154 }
8255
8356 public static HttpToolResponse invoke (org .jclouds .http .HttpRequest request ) {
84- HttpClient client = HttpUtil .createHttpClient (request .getEndpoint (), Optional .<Credentials >absent ());
57+ HttpClient client = HttpTool .httpClientBuilder ()
58+ .uri (request .getEndpoint ())
59+ .build ();
8560 String method = request .getMethod ();
8661 try {
8762 if ("GET" .equalsIgnoreCase (method )) {
88- return HttpUtil .httpGet (client , request .getEndpoint (), request .getHeaders ());
63+ return HttpTool .httpGet (client , request .getEndpoint (), request .getHeaders ());
8964 } else if ("POST" .equalsIgnoreCase (method )) {
90- return HttpUtil .httpPost (client , request .getEndpoint (), request .getHeaders (), Streams .readAll (request .getPayload ().openStream ()));
65+ return HttpTool .httpPost (client , request .getEndpoint (), request .getHeaders (), Streams .readAll (request .getPayload ().openStream ()));
66+ // return HttpUtil.httpPost(client, request.getEndpoint(), request.getHeaders(), Streams.readAll(request.getPayload().openStream()));
9167 } else {
9268 // TODO being lazy!
9369 throw new UnsupportedOperationException ("Unsupported method: " +method +" for " +request );
@@ -99,58 +75,23 @@ public static HttpToolResponse invoke(org.jclouds.http.HttpRequest request) {
9975
10076 public static HttpToolResponse httpGet (URI uri , Multimap <String ,String > headers ) {
10177 HttpClient client = HttpUtil .createHttpClient (uri , Optional .<Credentials >absent ());
102- return HttpUtil .httpGet (client , uri , headers );
78+ return HttpTool .httpGet (client , uri , headers );
10379 }
10480
10581 public static HttpToolResponse httpGet (HttpClient httpClient , URI uri , Map <String ,String > headers ) {
106- return httpGet (httpClient , uri , Multimaps .forMap (headers ));
82+ return HttpTool . httpGet (httpClient , uri , Multimaps .forMap (headers ));
10783 }
10884
10985 public static HttpToolResponse httpGet (HttpClient httpClient , URI uri , Multimap <String ,String > headers ) {
110- HttpGet httpGet = new HttpGet (uri );
111- for (Map .Entry <String ,String > entry : headers .entries ()) {
112- httpGet .addHeader (entry .getKey (), entry .getValue ());
113- }
114-
115- long startTime = System .currentTimeMillis ();
116- try {
117- HttpResponse httpResponse = httpClient .execute (httpGet );
118- try {
119- return new HttpToolResponse (httpResponse , startTime );
120- } finally {
121- EntityUtils .consume (httpResponse .getEntity ());
122- }
123- } catch (Exception e ) {
124- throw Exceptions .propagate (e );
125- }
86+ return HttpTool .httpGet (httpClient , uri , headers );
12687 }
12788
12889 public static HttpToolResponse httpPost (HttpClient httpClient , URI uri , Map <String ,String > headers , byte [] body ) {
129- return httpPost (httpClient , uri , Multimaps .forMap (headers ), body );
90+ return HttpTool . httpPost (httpClient , uri , Multimaps .forMap (headers ), body );
13091 }
13192
13293 public static HttpToolResponse httpPost (HttpClient httpClient , URI uri , Multimap <String ,String > headers , byte [] body ) {
133- HttpPost httpPost = new HttpPost (uri );
134- for (Map .Entry <String ,String > entry : headers .entries ()) {
135- httpPost .addHeader (entry .getKey (), entry .getValue ());
136- }
137- if (body != null ) {
138- HttpEntity httpEntity = new ByteArrayEntity (body );
139- httpPost .setEntity (httpEntity );
140- }
141-
142- long startTime = System .currentTimeMillis ();
143- try {
144- HttpResponse httpResponse = httpClient .execute (httpPost );
145-
146- try {
147- return new HttpToolResponse (httpResponse , startTime );
148- } finally {
149- EntityUtils .consume (httpResponse .getEntity ());
150- }
151- } catch (Exception e ) {
152- throw Exceptions .propagate (e );
153- }
94+ return HttpTool .httpPost (httpClient , uri , headers , body );
15495 }
15596
15697 public static class TrustAllStrategy implements TrustStrategy {
0 commit comments