[英]Jira REST API with proxy (Java)
我想在需要通过代理访问所需的 Jira 实例的 go 的应用程序中使用 Jira REST Client API for Java。 不幸的是,在使用该库中的给定工厂时,我没有找到设置它的方法:
JiraRestClientFactory factory = new AsynchronousJiraRestClientFactory();
String authentication = Base64.getEncoder().encodeToString("username:password".toBytes());
return factory.createWithAuthenticationHandler(URI.create(JIRA_URL), new BasicAuthenticationHandler(authentication));
我们如何使用 Jira API 并设置代理?
我在 inte.net 上找到的唯一解决方案是使用系统参数对其进行设置(参见下面的解决方案 1)。 不幸的是,这不符合我的要求,因为在我工作的公司中,有多个代理,并且根据要调用的服务,它必须使用另一个代理配置。 在那种情况下,我无法在不破坏对需要另一个代理的其他服务的所有调用的情况下设置系统属性。
尽管如此,我还是找到了一种通过重新实现一些类来设置它的方法(参见解决方案 2)。
重要限制:代理服务器不得要求凭据。
也许作为上下文,我创建了一个包含代理配置的 class:
@Data
@AllArgsConstructor
public class ProxyConfiguration {
public static final Pattern PROXY_PATTERN = Pattern.compile("(https?):\\/\\/(.*):(\\d+)");
private String scheme;
private String host;
private Integer port;
public static ProxyConfiguration fromPath(String path) {
Matcher matcher = PROXY_PATTERN.matcher(path);
if (matcher.find()) {
return new ProxyConfiguration(matcher.group(1), matcher.group(2), toInt(matcher.group(3)));
}
return null;
}
public String getPath() {
return scheme + "://" + host + ":" + port;
}
}
在应用程序启动时或在使用 Jira REST API 之前使用您的代理配置调用以下方法:
public static void configureProxy(ProxyConfiguration proxy) {
if (proxy != null) {
System.getProperties().setProperty("http.proxyHost", proxy.getHost());
System.getProperties().setProperty("http.proxyPort", proxy.getPort().toString());
System.getProperties().setProperty("https.proxyHost", proxy.getHost());
System.getProperties().setProperty("https.proxyPort", proxy.getPort().toString());
}
}
AsynchronousHttpClientFactory
不幸的是,由于这个 class 有许多私有内部类和方法,您将不得不进行丑陋的复制粘贴并更改以下代码以提供所需的代理配置:
public DisposableHttpClient createClient(URI serverUri, ProxyConfiguration proxy, AuthenticationHandler authenticationHandler) {
HttpClientOptions options = new HttpClientOptions();
if (proxy != null) {
options.setProxyOptions(new ProxyOptions.ProxyOptionsBuilder()
.withProxy(HTTP, new Host(proxy.getHost(), proxy.getPort()))
.withProxy(HTTPS, new Host(proxy.getHost(), proxy.getPort()))
.build());
}
DefaultHttpClientFactory<?> defaultHttpClientFactory = ...
}
然后您可以使用它(在以下示例中,我对AsynchronousHttpClientFactory
的重新实现称为AtlassianHttpClientFactory
):
URI url = URI.create(JIRA_URL);
String authentication = Base64.getEncoder().encodeToString("username:password".toBytes());
DisposableHttpClient client = new AtlassianHttpClientFactory().createClient(url, proxy, new BasicAuthenticationHandler(authentication));
return new AsynchronousJiraRestClient(url, client);
请注意,在所有这些问题之后,我还决定编写一个支持身份验证、代理、多个 HTTP 客户端并与CompletableFuture
异步工作的Jira 客户端库。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.