Skip to content

Commit 40e037b

Browse files
committed
Use SetContextClassLoader to make the ServiceLoader in scribejava work properly in retrieving the specified HttpProvider
1 parent edf430c commit 40e037b

File tree

1 file changed

+6
-10
lines changed

1 file changed

+6
-10
lines changed

src/main/java/com/cloudbees/jenkins/plugins/bitbucket/api/credentials/BitbucketOAuthAuthenticator.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,15 @@
66
import com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials;
77
import com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl;
88
import com.github.scribejava.core.builder.ServiceBuilder;
9-
import com.github.scribejava.core.httpclient.HttpClient;
109
import com.github.scribejava.core.model.OAuth2AccessToken;
1110
import com.github.scribejava.core.model.OAuthConstants;
1211
import com.github.scribejava.core.oauth.OAuth20Service;
1312
import com.github.scribejava.httpclient.apache.ApacheHttpClientConfig;
14-
import com.github.scribejava.httpclient.apache.ApacheProvider;
1513
import hudson.model.Descriptor.FormException;
1614
import java.io.IOException;
1715
import java.util.concurrent.ExecutionException;
1816
import jenkins.authentication.tokens.api.AuthenticationTokenException;
17+
import jenkins.util.SetContextClassLoader;
1918
import org.apache.commons.lang.StringUtils;
2019
import org.apache.http.HttpRequest;
2120

@@ -32,14 +31,11 @@ public class BitbucketOAuthAuthenticator extends BitbucketAuthenticator {
3231
public BitbucketOAuthAuthenticator(StandardUsernamePasswordCredentials credentials) throws AuthenticationTokenException {
3332
super(credentials);
3433

35-
HttpClient httpClient = new ApacheProvider().createClient(ApacheHttpClientConfig.defaultConfig());
36-
OAuth20Service service = new ServiceBuilder(credentials.getUsername())
37-
.apiSecret(credentials.getPassword().getPlainText())
38-
.httpClient(httpClient)
39-
// .httpClientConfig(ApacheHttpClientConfig.defaultConfig()) the ServiceLoader does not work well with Jenkins plugin classloader
40-
.build(BitbucketOAuth.instance());
41-
42-
try {
34+
try (SetContextClassLoader cl = new SetContextClassLoader(this.getClass())) {
35+
OAuth20Service service = new ServiceBuilder(credentials.getUsername())
36+
.apiSecret(credentials.getPassword().getPlainText())
37+
.httpClientConfig(ApacheHttpClientConfig.defaultConfig())
38+
.build(BitbucketOAuth.instance());
4339
token = service.getAccessTokenClientCredentialsGrant();
4440
} catch (IOException | InterruptedException | ExecutionException e) {
4541
throw new AuthenticationTokenException(e);

0 commit comments

Comments
 (0)