/
BitBucketPPRBasicAuthApiConsumer.java
68 lines (55 loc) · 2.79 KB
/
BitBucketPPRBasicAuthApiConsumer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package io.jenkins.plugins.bitbucketpushandpullrequest.client.api;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.logging.Logger;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.HttpClientBuilder;
import com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials;
import com.github.scribejava.core.model.Verb;
public class BitBucketPPRBasicAuthApiConsumer {
private static final Logger logger =
Logger.getLogger(BitBucketPPRBasicAuthApiConsumer.class.getName());
public HttpResponse send(StandardUsernamePasswordCredentials credentials, Verb verb, String url,
String payload) throws IOException, NoSuchMethodException {
logger.finest("Send state notification with StandardUsernamePasswordCredentials");
final UsernamePasswordCredentials httpAuthCredentials = new UsernamePasswordCredentials(
credentials.getUsername(), credentials.getPassword().getPlainText());
final String authHeader =
getAuthHeader(credentials.getUsername(), credentials.getPassword().getPlainText());
final org.apache.http.client.CredentialsProvider provider = new BasicCredentialsProvider();
provider.setCredentials(AuthScope.ANY, httpAuthCredentials);
final HttpClient client =
HttpClientBuilder.create().setDefaultCredentialsProvider(provider).build();
if (verb == Verb.POST) {
final HttpPost request = new HttpPost(url);
request.setHeader(HttpHeaders.AUTHORIZATION, authHeader);
request.setHeader("X-Atlassian-Token", "nocheck");
if (StringUtils.isNotBlank(payload))
request.setEntity(new StringEntity(payload, ContentType.APPLICATION_JSON));
return client.execute(request);
}
if (verb == Verb.DELETE) {
final HttpDelete request = new HttpDelete(url);
request.setHeader(HttpHeaders.AUTHORIZATION, authHeader);
request.setHeader("X-Atlassian-Token", "nocheck");
return client.execute(request);
}
throw new NoSuchMethodException();
}
private String getAuthHeader(String username, String password) {
final String auth = username + ":" + password;
byte[] encodedAuth = Base64.encodeBase64(auth.getBytes(StandardCharsets.ISO_8859_1));
return "Basic ".concat(new String(encodedAuth, StandardCharsets.ISO_8859_1));
}
}