Skip to content

Commit

Permalink
[FIXED JENKINS-50565] Use Jenkins proxy configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenc committed Apr 4, 2018
1 parent 4ff3e1e commit 2a291d5
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 37 deletions.
6 changes: 5 additions & 1 deletion CHANGES.md
Expand Up @@ -10,10 +10,14 @@
-->

## Version 1.0.8 (unreleased)
## Version 1.0.9 (unreleased)

* ...

## Version 1.0.8 (2018-04-04)

* Use Jenkins configured proxy settings to connect to Gitea ([JENKINS-50565](https://issues.jenkins-ci.org/browse/JENKINS-50565))

## Version 1.0.7 (2018-03-22)

* Fix NPE during dynamic installation of the plugin ([JENKINS-50349](https://issues.jenkins-ci.org/browse/JENKINS-50349))
Expand Down
Expand Up @@ -46,6 +46,7 @@
import java.util.List;
import java.util.Set;
import javax.net.ssl.HttpsURLConnection;
import jenkins.model.Jenkins;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
Expand Down Expand Up @@ -747,21 +748,18 @@ public List<GiteaIssue> fetchIssues(GiteaRepository repository, Set<GiteaIssueSt
@Override
public byte[] fetchFile(GiteaRepository repository, String ref, String path)
throws IOException, InterruptedException {
HttpURLConnection connection = (HttpURLConnection) new URL(
api()
.literal("/repos")
.path(UriTemplateBuilder.var("username"))
.path(UriTemplateBuilder.var("name"))
.literal("/raw")
.path(UriTemplateBuilder.var("ref", true))
.path(UriTemplateBuilder.var("path", true))
.build()
.set("username", repository.getOwner().getUsername())
.set("name", repository.getName())
.set("ref", StringUtils.split(ref, '/'))
.set("path", StringUtils.split(path, "/"))
.expand()
).openConnection();
HttpURLConnection connection = openConnection(api()
.literal("/repos")
.path(UriTemplateBuilder.var("username"))
.path(UriTemplateBuilder.var("name"))
.literal("/raw")
.path(UriTemplateBuilder.var("ref", true))
.path(UriTemplateBuilder.var("path", true))
.build()
.set("username", repository.getOwner().getUsername())
.set("name", repository.getName())
.set("ref", StringUtils.split(ref, '/'))
.set("path", StringUtils.split(path, "/")));
withAuthentication(connection);
try {
connection.connect();
Expand All @@ -783,21 +781,18 @@ public byte[] fetchFile(GiteaRepository repository, String ref, String path)
@Override
public boolean checkFile(GiteaRepository repository, String ref, String path)
throws IOException, InterruptedException {
HttpURLConnection connection = (HttpURLConnection) new URL(
api()
.literal("/repos")
.path(UriTemplateBuilder.var("username"))
.path(UriTemplateBuilder.var("name"))
.literal("/raw")
.path(UriTemplateBuilder.var("ref", true))
.path(UriTemplateBuilder.var("path", true))
.build()
.set("username", repository.getOwner().getUsername())
.set("name", repository.getName())
.set("ref", StringUtils.split(ref, '/'))
.set("path", StringUtils.split(path, "/"))
.expand()
).openConnection();
HttpURLConnection connection = openConnection(api()
.literal("/repos")
.path(UriTemplateBuilder.var("username"))
.path(UriTemplateBuilder.var("name"))
.literal("/raw")
.path(UriTemplateBuilder.var("ref", true))
.path(UriTemplateBuilder.var("path", true))
.build()
.set("username", repository.getOwner().getUsername())
.set("name", repository.getName())
.set("ref", StringUtils.split(ref, '/'))
.set("path", StringUtils.split(path, "/")));
withAuthentication(connection);
try {
connection.connect();
Expand Down Expand Up @@ -834,7 +829,7 @@ private void withAuthentication(HttpURLConnection connection) {
}

private int status(UriTemplate template) throws IOException, InterruptedException {
HttpURLConnection connection = (HttpURLConnection) new URL(template.expand()).openConnection();
HttpURLConnection connection = openConnection(template);
withAuthentication(connection);
try {
connection.connect();
Expand All @@ -845,7 +840,7 @@ private int status(UriTemplate template) throws IOException, InterruptedExceptio
}

private int delete(UriTemplate template) throws IOException, InterruptedException {
HttpURLConnection connection = (HttpURLConnection) new URL(template.expand()).openConnection();
HttpURLConnection connection = openConnection(template);
withAuthentication(connection);
connection.setRequestMethod("DELETE");
try {
Expand All @@ -857,7 +852,7 @@ private int delete(UriTemplate template) throws IOException, InterruptedExceptio
}

private <T> T getObject(UriTemplate template, final Class<T> modelClass) throws IOException, InterruptedException {
HttpURLConnection connection = (HttpURLConnection) new URL(template.expand()).openConnection();
HttpURLConnection connection = openConnection(template);
withAuthentication(connection);
try {
connection.connect();
Expand All @@ -875,7 +870,7 @@ private <T> T getObject(UriTemplate template, final Class<T> modelClass) throws

private <T> T post(UriTemplate template, Object body, final Class<T> modelClass)
throws IOException, InterruptedException {
HttpURLConnection connection = (HttpURLConnection) new URL(template.expand()).openConnection();
HttpURLConnection connection = openConnection(template);
withAuthentication(connection);
connection.setRequestMethod("POST");
byte[] bytes;
Expand Down Expand Up @@ -918,7 +913,7 @@ private <T> T post(UriTemplate template, Object body, final Class<T> modelClass)

private <T> T patch(UriTemplate template, Object body, final Class<T> modelClass)
throws IOException, InterruptedException {
HttpURLConnection connection = (HttpURLConnection) new URL(template.expand()).openConnection();
HttpURLConnection connection = openConnection(template);
withAuthentication(connection);
setRequestMethodViaJreBugWorkaround(connection, "PATCH");
byte[] bytes;
Expand Down Expand Up @@ -961,7 +956,7 @@ private <T> T patch(UriTemplate template, Object body, final Class<T> modelClass

private <T> List<T> getList(UriTemplate template, final Class<T> modelClass)
throws IOException, InterruptedException {
HttpURLConnection connection = (HttpURLConnection) new URL(template.expand()).openConnection();
HttpURLConnection connection = openConnection(template);
withAuthentication(connection);
try {
connection.connect();
Expand All @@ -985,4 +980,14 @@ private <T> List<T> getList(UriTemplate template, final Class<T> modelClass)
connection.disconnect();
}
}

private static HttpURLConnection openConnection(UriTemplate template) throws IOException {
URL url = new URL(template.expand());
Jenkins jenkins = Jenkins.getInstance();
if (jenkins == null || jenkins.proxy == null) {
return (HttpURLConnection) url.openConnection();
}
return (HttpURLConnection) url.openConnection(jenkins.proxy.createProxy(url.getHost()));
}

}

0 comments on commit 2a291d5

Please sign in to comment.