Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
JENKINS-35389: Use deprecated API will cause getting wrong certificat…
…e from gerrit https url.
  • Loading branch information
Wei Gao committed Jun 6, 2016
1 parent 395a89f commit 0e0d38b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
Expand Up @@ -28,7 +28,7 @@
import com.sonyericsson.hudson.plugins.gerrit.trigger.Messages;
import com.sonyericsson.hudson.plugins.gerrit.trigger.config.IGerritHudsonTriggerConfig;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -116,17 +116,24 @@ public static boolean isPluginEnabled(IGerritHudsonTriggerConfig config, String
}
logger.trace("{}plugins/{}/", restUrl, pluginName);

HttpResponse execute = null;
CloseableHttpResponse execute = null;
try {
execute = HttpUtils.performHTTPGet(config, restUrl + "plugins/" + pluginName + "/");
int statusCode = execute.getStatusLine().getStatusCode();
logger.debug("status code: {}", statusCode);
return decodeStatus(statusCode, pluginName);
} catch (IOException e) {
logger.warn(Messages.PluginHttpConnectionGeneralError(pluginName,
e.getMessage()), e);
return false;
} finally {
if (execute != null) {
try {
execute.close();
} catch (Exception exp) {
logger.trace("Error happened when close http client.", exp);
}
}
}

int statusCode = execute.getStatusLine().getStatusCode();
logger.debug("status code: {}", statusCode);
return decodeStatus(statusCode, pluginName);
}
}
Expand Up @@ -29,11 +29,15 @@
import java.net.URL;

import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.DefaultProxyRoutePlanner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -58,22 +62,25 @@ private HttpUtils() {
* @return httpresponse.
* @throws IOException if found.
*/
public static HttpResponse performHTTPGet(IGerritHudsonTriggerConfig config, String url) throws IOException {
DefaultHttpClient httpclient = new DefaultHttpClient();
public static CloseableHttpResponse performHTTPGet(IGerritHudsonTriggerConfig config,
String url) throws IOException {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(url);
if (config.getGerritProxy() != null && !config.getGerritProxy().isEmpty()) {
try {
URL proxyUrl = new URL(config.getGerritProxy());
HttpHost proxy = new HttpHost(proxyUrl.getHost(), proxyUrl.getPort(), proxyUrl.getProtocol());
httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
httpClient = HttpClients.custom().setRoutePlanner(routePlanner).build();
} catch (MalformedURLException e) {
logger.error("Could not parse proxy URL, attempting without proxy.", e);
}
}

httpclient.getCredentialsProvider().setCredentials(new AuthScope(null, -1),
config.getHttpCredentials());
HttpResponse execute;
return httpclient.execute(httpGet);
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope(null, -1), config.getHttpCredentials());
HttpClientContext context = HttpClientContext.create();
context.setCredentialsProvider(credsProvider);
return httpClient.execute(httpGet, context);
}
}

0 comments on commit 0e0d38b

Please sign in to comment.