Github user neykov commented on a diff in the pull request:
https://github.com/apache/incubator-brooklyn/pull/994#discussion_r44706399
--- Diff:
core/src/main/java/org/apache/brooklyn/util/core/http/HttpTool.java ---
@@ -76,311 +76,10 @@
import com.google.common.collect.Iterables;
import com.google.common.collect.Multimap;
-public class HttpTool {
-
- private static final Logger LOG =
LoggerFactory.getLogger(HttpTool.class);
-
- /** Apache HTTP commons utility for trusting all.
- * <p>
- * For generic java HTTP usage, see {@link
SslTrustUtils#trustAll(java.net.URLConnection)}
- * and static constants in the same class. */
- public static class TrustAllStrategy implements TrustStrategy {
- @Override
- public boolean isTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
- return true;
- }
- }
-
- public static HttpClientBuilder httpClientBuilder() {
- return new HttpClientBuilder();
- }
-
- public static class HttpClientBuilder {
- private ClientConnectionManager clientConnectionManager;
- private HttpParams httpParams;
- private URI uri;
- private Integer port;
- private Credentials credentials;
- private boolean laxRedirect;
- private Boolean https;
- private SchemeSocketFactory socketFactory;
- private ConnectionReuseStrategy reuseStrategy;
- private boolean trustAll;
- private boolean trustSelfSigned;
-
- public HttpClientBuilder
clientConnectionManager(ClientConnectionManager val) {
- this.clientConnectionManager = checkNotNull(val,
"clientConnectionManager");
- return this;
- }
- public HttpClientBuilder httpParams(HttpParams val) {
- checkState(httpParams == null, "Must not call httpParams
multiple times, or after other methods like connectionTimeout");
- this.httpParams = checkNotNull(val, "httpParams");
- return this;
- }
- public HttpClientBuilder connectionTimeout(Duration val) {
- if (httpParams == null) httpParams = new BasicHttpParams();
- long millis = checkNotNull(val,
"connectionTimeout").toMilliseconds();
- if (millis > Integer.MAX_VALUE) throw new
IllegalStateException("HttpClient only accepts upto max-int millis for
connectionTimeout, but given "+val);
- HttpConnectionParams.setConnectionTimeout(httpParams, (int)
millis);
- return this;
- }
- public HttpClientBuilder socketTimeout(Duration val) {
- if (httpParams == null) httpParams = new BasicHttpParams();
- long millis = checkNotNull(val,
"socketTimeout").toMilliseconds();
- if (millis > Integer.MAX_VALUE) throw new
IllegalStateException("HttpClient only accepts upto max-int millis for
socketTimeout, but given "+val);
- HttpConnectionParams.setSoTimeout(httpParams, (int) millis);
- return this;
- }
- public HttpClientBuilder reuseStrategy(ConnectionReuseStrategy
val) {
- this.reuseStrategy = checkNotNull(val, "reuseStrategy");
- return this;
- }
- public HttpClientBuilder uri(String val) {
- return uri(URI.create(checkNotNull(val, "uri")));
- }
- public HttpClientBuilder uri(URI val) {
- this.uri = checkNotNull(val, "uri");
- if (https == null) https =
("https".equalsIgnoreCase(uri.getScheme()));
- return this;
- }
- public HttpClientBuilder port(int val) {
- this.port = val;
- return this;
- }
- public HttpClientBuilder credentials(Credentials val) {
- this.credentials = checkNotNull(val, "credentials");
- return this;
- }
- public void credential(Optional<Credentials> val) {
- if (val.isPresent()) credentials = val.get();
- }
- /** similar to curl --post301 -L` */
- public HttpClientBuilder laxRedirect(boolean val) {
- this.laxRedirect = val;
- return this;
- }
- public HttpClientBuilder https(boolean val) {
- this.https = val;
- return this;
- }
- public HttpClientBuilder socketFactory(SchemeSocketFactory val) {
- this.socketFactory = checkNotNull(val, "socketFactory");
- return this;
- }
- public HttpClientBuilder trustAll() {
- this.trustAll = true;
- return this;
- }
- public HttpClientBuilder trustSelfSigned() {
- this.trustSelfSigned = true;
- return this;
- }
- public HttpClient build() {
- final DefaultHttpClient httpClient = new
DefaultHttpClient(clientConnectionManager);
- httpClient.setParams(httpParams);
-
- // support redirects for POST (similar to `curl --post301 -L`)
- //
http://stackoverflow.com/questions/3658721/httpclient-4-error-302-how-to-redirect
- if (laxRedirect) {
- httpClient.setRedirectStrategy(new LaxRedirectStrategy());
- }
- if (reuseStrategy != null) {
- httpClient.setReuseStrategy(reuseStrategy);
- }
- if (https == Boolean.TRUE || (uri!=null &&
uri.toString().startsWith("https:"))) {
- try {
- if (port == null) {
- port = (uri != null && uri.getPort() >= 0) ?
uri.getPort() : 443;
- }
- if (socketFactory == null) {
- if (trustAll) {
- TrustStrategy trustStrategy = new
TrustAllStrategy();
- X509HostnameVerifier hostnameVerifier =
SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
- socketFactory = new
SSLSocketFactory(trustStrategy, hostnameVerifier);
- } else if (trustSelfSigned) {
- TrustStrategy trustStrategy = new
TrustSelfSignedStrategy();
- X509HostnameVerifier hostnameVerifier =
SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
- socketFactory = new
SSLSocketFactory(trustStrategy, hostnameVerifier);
- } else {
- // Using default https scheme: based on
default java truststore, which is pretty strict!
- }
- }
- if (socketFactory != null) {
- Scheme sch = new Scheme("https", port,
socketFactory);
-
httpClient.getConnectionManager().getSchemeRegistry().register(sch);
- }
- } catch (Exception e) {
- LOG.warn("Error setting trust for uri {}", uri);
- throw Exceptions.propagate(e);
- }
- }
-
- // Set credentials
- if (uri != null && credentials != null) {
- String hostname = uri.getHost();
- int port = uri.getPort();
- httpClient.getCredentialsProvider().setCredentials(new
AuthScope(hostname, port), credentials);
- }
- if (uri==null && credentials!=null) {
- LOG.warn("credentials have no effect in builder unless URI
for host is specified");
- }
-
- return httpClient;
- }
- }
-
- protected static abstract class HttpRequestBuilder<B extends
HttpRequestBuilder<B, R>, R extends HttpRequest> {
- protected R req;
-
- protected HttpRequestBuilder(R req) {
- this.req = req;
- }
- @SuppressWarnings("unchecked")
- protected B self() {
- return (B) this;
- }
- public B headers(Map<String,String> headers) {
- if (headers!=null) {
- for (Map.Entry<String,String> entry : headers.entrySet()) {
- req.addHeader(entry.getKey(), entry.getValue());
- }
- }
- return self();
- }
- public B headers(Multimap<String,String> headers) {
- if (headers!=null) {
- for (Map.Entry<String,String> entry : headers.entries()) {
- req.addHeader(entry.getKey(), entry.getValue());
- }
- }
- return self();
- }
- public R build() {
- return req;
- }
- }
-
- protected static abstract class
HttpEntityEnclosingRequestBaseBuilder<B extends
HttpEntityEnclosingRequestBaseBuilder<B,R>, R extends
HttpEntityEnclosingRequestBase> extends HttpRequestBuilder<B, R> {
- protected HttpEntityEnclosingRequestBaseBuilder(R req) {
- super(req);
- }
- public B body(byte[] body) {
- if (body != null) {
- HttpEntity httpEntity = new ByteArrayEntity(body);
- req.setEntity(httpEntity);
- }
- return self();
- }
- }
-
- public static class HttpGetBuilder extends
HttpRequestBuilder<HttpGetBuilder, HttpGet> {
- public HttpGetBuilder(URI uri) {
- super(new HttpGet(uri));
- }
- }
-
- public static class HttpHeadBuilder extends
HttpRequestBuilder<HttpHeadBuilder, HttpHead> {
- public HttpHeadBuilder(URI uri) {
- super(new HttpHead(uri));
- }
- }
-
- public static class HttpDeleteBuilder extends
HttpRequestBuilder<HttpDeleteBuilder, HttpDelete> {
- public HttpDeleteBuilder(URI uri) {
- super(new HttpDelete(uri));
- }
- }
-
- public static class HttpPostBuilder extends
HttpEntityEnclosingRequestBaseBuilder<HttpPostBuilder, HttpPost> {
- HttpPostBuilder(URI uri) {
- super(new HttpPost(uri));
- }
- }
-
- public static class HttpFormPostBuilder extends
HttpRequestBuilder<HttpFormPostBuilder, HttpPost> {
- HttpFormPostBuilder(URI uri) {
- super(new HttpPost(uri));
- }
-
- public HttpFormPostBuilder params(Map<String, String> params) {
- if (params != null) {
- Collection<NameValuePair> httpParams = new
ArrayList<NameValuePair>(params.size());
- for (Entry<String, String> param : params.entrySet()) {
- httpParams.add(new BasicNameValuePair(param.getKey(),
param.getValue()));
- }
- req.setEntity(new UrlEncodedFormEntity(httpParams));
- }
- return self();
- }
- }
-
- public static class HttpPutBuilder extends
HttpEntityEnclosingRequestBaseBuilder<HttpPutBuilder, HttpPut> {
- public HttpPutBuilder(URI uri) {
- super(new HttpPut(uri));
- }
- }
-
- public static HttpToolResponse httpGet(HttpClient httpClient, URI uri,
Map<String,String> headers) {
- HttpGet req = new HttpGetBuilder(uri).headers(headers).build();
- return execAndConsume(httpClient, req);
- }
-
- public static HttpToolResponse httpPost(HttpClient httpClient, URI
uri, Map<String,String> headers, byte[] body) {
- HttpPost req = new
HttpPostBuilder(uri).headers(headers).body(body).build();
- return execAndConsume(httpClient, req);
- }
-
- public static HttpToolResponse httpPut(HttpClient httpClient, URI uri,
Map<String, String> headers, byte[] body) {
- HttpPut req = new
HttpPutBuilder(uri).headers(headers).body(body).build();
- return execAndConsume(httpClient, req);
- }
-
- public static HttpToolResponse httpPost(HttpClient httpClient, URI
uri, Map<String,String> headers, Map<String, String> params) {
- HttpPost req = new
HttpFormPostBuilder(uri).headers(headers).params(params).build();
- return execAndConsume(httpClient, req);
- }
-
- public static HttpToolResponse httpDelete(HttpClient httpClient, URI
uri, Map<String,String> headers) {
- HttpDelete req = new
HttpDeleteBuilder(uri).headers(headers).build();
- return execAndConsume(httpClient, req);
- }
-
- public static HttpToolResponse httpHead(HttpClient httpClient, URI
uri, Map<String,String> headers) {
- HttpHead req = new HttpHeadBuilder(uri).headers(headers).build();
- return execAndConsume(httpClient, req);
- }
-
- public static HttpToolResponse execAndConsume(HttpClient httpClient,
HttpUriRequest req) {
- long startTime = System.currentTimeMillis();
- try {
- HttpResponse httpResponse = httpClient.execute(req);
-
- try {
- return new HttpToolResponse(httpResponse, startTime);
- } finally {
- EntityUtils.consume(httpResponse.getEntity());
- }
- } catch (Exception e) {
- throw Exceptions.propagate(e);
- }
- }
-
- public static boolean isStatusCodeHealthy(int code) { return
(code>=200 && code<=299); }
-
- public static String
toBasicAuthorizationValue(UsernamePasswordCredentials credentials) {
- return "Basic "+Base64.encodeBase64String(
(credentials.getUserName()+":"+credentials.getPassword()).getBytes() );
- }
+/**
+ * @deprecated since 0.9.0. Prefer org.apache.brooklyn.util.http.HttpTool
+ */
+@Deprecated
+public class HttpTool extends org.apache.brooklyn.util.http.HttpTool {
--- End diff --
This doesn't make the class backwards compatible because return types will
be coming from the new package, while users of this will be expecting the old
classes.
HttpTool is used frequently so better handle this gracefully (had a
downstream project build failure). @geomacy Can you get the old implementation
of `HttpTool` back here while keeping it marked as `@Deprecated`. Same for
`HttpToolResponse`.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---