diff --git a/src/main/java/com/cloudbees/jenkins/plugins/bitbucket/client/BitbucketCloudApiClient.java b/src/main/java/com/cloudbees/jenkins/plugins/bitbucket/client/BitbucketCloudApiClient.java index 3d83d0b5e..dbec469dd 100644 --- a/src/main/java/com/cloudbees/jenkins/plugins/bitbucket/client/BitbucketCloudApiClient.java +++ b/src/main/java/com/cloudbees/jenkins/plugins/bitbucket/client/BitbucketCloudApiClient.java @@ -51,9 +51,8 @@ import edu.umd.cs.findbugs.annotations.NonNull; import hudson.ProxyConfiguration; import hudson.util.Secret; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.io.UnsupportedEncodingException; + +import java.io.*; import java.net.InetSocketAddress; import java.net.Proxy; import java.net.URLEncoder; @@ -481,6 +480,18 @@ public List getRepositories() throws IOException, Inte return getRepositories(null); } + private String getResponseBody(HttpMethod httpMethod) throws IOException { + StringBuilder response = new StringBuilder(); + BufferedReader reader = new BufferedReader(new InputStreamReader(httpMethod.getResponseBodyAsStream(), "UTF-8")); + String line = null; + while ((line = reader.readLine()) != null) + { + response.append(line).append(System.getProperty("line.separator")); + } + reader.close(); + return response.toString(); + } + private synchronized HttpClient getHttpClient() { if (this.client == null) { HttpClient client = new HttpClient(connectionManager); @@ -563,7 +574,7 @@ private String getRequest(String path) throws IOException, InterruptedException GetMethod httpget = new GetMethod(path); try { executeMethod(client, httpget); - String response = new String(httpget.getResponseBody(), "UTF-8"); + String response = getResponseBody(httpget); if (httpget.getStatusCode() == HttpStatus.SC_NOT_FOUND) { throw new FileNotFoundException("URL: " + path); } @@ -631,7 +642,7 @@ private String postRequest(PostMethod httppost) throws IOException { // 204, no content return ""; } - String response = new String(httppost.getResponseBody(), "UTF-8"); + String response = getResponseBody(httppost); if (httppost.getStatusCode() != HttpStatus.SC_OK && httppost.getStatusCode() != HttpStatus.SC_CREATED) { throw new BitbucketRequestException(httppost.getStatusCode(), "HTTP request error. Status: " + httppost.getStatusCode() + ": " + httppost.getStatusText() + ".\n" + response); } diff --git a/src/main/java/com/cloudbees/jenkins/plugins/bitbucket/server/client/BitbucketServerAPIClient.java b/src/main/java/com/cloudbees/jenkins/plugins/bitbucket/server/client/BitbucketServerAPIClient.java index b2a3802f4..b449aa3a9 100644 --- a/src/main/java/com/cloudbees/jenkins/plugins/bitbucket/server/client/BitbucketServerAPIClient.java +++ b/src/main/java/com/cloudbees/jenkins/plugins/bitbucket/server/client/BitbucketServerAPIClient.java @@ -27,14 +27,13 @@ import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketRepositoryType; import edu.umd.cs.findbugs.annotations.CheckForNull; import edu.umd.cs.findbugs.annotations.NonNull; -import java.io.FileNotFoundException; -import java.io.IOException; + +import java.io.*; import java.net.InetSocketAddress; import java.net.MalformedURLException; import java.net.Proxy; import java.net.URL; import java.util.ArrayList; -import java.util.Collections; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; @@ -60,7 +59,6 @@ import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketTeam; import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketWebHook; import com.cloudbees.jenkins.plugins.bitbucket.client.repository.UserRoleInRepository; -import com.cloudbees.jenkins.plugins.bitbucket.hooks.BitbucketSCMSourcePushHookReceiver; import com.cloudbees.jenkins.plugins.bitbucket.server.client.branch.BitbucketServerBranch; import com.cloudbees.jenkins.plugins.bitbucket.server.client.branch.BitbucketServerBranches; import com.cloudbees.jenkins.plugins.bitbucket.server.client.branch.BitbucketServerCommit; @@ -72,23 +70,7 @@ import hudson.util.Secret; import jenkins.model.Jenkins; import net.sf.json.JSONObject; -import org.apache.commons.httpclient.*; -import org.apache.commons.httpclient.auth.AuthScope; import org.apache.commons.httpclient.methods.*; -import org.apache.commons.io.IOUtils; -import org.apache.commons.lang.StringUtils; -import org.codehaus.jackson.map.ObjectMapper; - -import java.io.IOException; -import java.io.InputStream; -import java.io.UnsupportedEncodingException; -import java.net.InetSocketAddress; -import java.net.Proxy; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.logging.Level; -import java.util.logging.Logger; /** * Bitbucket API client. @@ -481,7 +463,7 @@ private String getRequest(String path) throws IOException { HttpClient client = getHttpClient(getMethodHost(httpget)); try { client.executeMethod(httpget); - String response = new String(httpget.getResponseBody(), "UTF-8"); + String response = getResponseBody(httpget); if (httpget.getStatusCode() == HttpStatus.SC_NOT_FOUND) { throw new FileNotFoundException("URL: " + path); } @@ -498,6 +480,18 @@ private String getRequest(String path) throws IOException { } } + private String getResponseBody(HttpMethod httpMethod) throws IOException { + StringBuilder response = new StringBuilder(); + BufferedReader reader = new BufferedReader(new InputStreamReader(httpMethod.getResponseBodyAsStream(), "UTF-8")); + String line = null; + while ((line = reader.readLine()) != null) + { + response.append(line).append(System.getProperty("line.separator")); + } + reader.close(); + return response.toString(); + } + private HttpClient getHttpClient(String host) { HttpClient client = new HttpClient(); @@ -595,7 +589,7 @@ private String doRequest(HttpMethod httppost) throws IOException { // 204, no content return ""; } - String response = new String(httppost.getResponseBody(), "UTF-8"); + String response = getResponseBody(httppost); if (httppost.getStatusCode() != HttpStatus.SC_OK && httppost.getStatusCode() != HttpStatus.SC_CREATED) { throw new BitbucketRequestException(httppost.getStatusCode(), "HTTP request error. Status: " + httppost.getStatusCode() + ": " + httppost.getStatusText() + ".\n" + response); }