Search in sources :

Example 1 with HttpRequest

use of io.jenkins.blueocean.blueocean_bitbucket_pipeline.HttpRequest in project blueocean-plugin by jenkinsci.

the class BitbucketServerApi method getVersion.

/**
 * Gives Bitbucket server version
 * @param apiUrl API url of Bitbucket server
 * @return version of Bitbucket server
 */
@Nonnull
public static String getVersion(@Nonnull String apiUrl) {
    try {
        apiUrl = ensureTrailingSlash(apiUrl);
        HttpRequest request = new HttpRequest.HttpRequestBuilder(apiUrl).build();
        HttpResponse response = request.get(apiUrl + "rest/api/1.0/application-properties");
        int status = response.getStatus();
        if ((status >= 301 && status <= 303) || status == 307 || status == 308) {
            String location = response.getHeader("Location");
            String error = String.format("%s is invalid. Bitbucket server sent redirect response", apiUrl);
            if (StringUtils.isNotBlank(location)) {
                URL url = new URL(location);
                String host = url.getHost();
                int port = url.getPort();
                String protocol = url.getProtocol();
                String baseUrl = protocol + "://" + host + ((port == -1) ? "/" : port + "/");
                error += " with location at: " + baseUrl;
            }
            error += ". \nPlease use correct Bitbucket Server endpoint.";
            throw new ServiceException(status, error);
        }
        InputStream inputStream = response.getContent();
        Map<String, String> resp = om.readValue(inputStream, new TypeReference<Map<String, String>>() {
        });
        String version = resp.get("version");
        if (StringUtils.isBlank(version)) {
            throw new ServiceException.PreconditionRequired("Unsupported Bitbucket server, no version information could be determined");
        }
        return version;
    } catch (IOException e) {
        throw new ServiceException.UnexpectedErrorException(e.getMessage(), e);
    }
}
Also used : HttpRequest(io.jenkins.blueocean.blueocean_bitbucket_pipeline.HttpRequest) InputStream(java.io.InputStream) HttpResponse(io.jenkins.blueocean.blueocean_bitbucket_pipeline.HttpResponse) IOException(java.io.IOException) URL(java.net.URL) ServiceException(io.jenkins.blueocean.commons.ServiceException) Map(java.util.Map) Nonnull(javax.annotation.Nonnull)

Aggregations

HttpRequest (io.jenkins.blueocean.blueocean_bitbucket_pipeline.HttpRequest)1 HttpResponse (io.jenkins.blueocean.blueocean_bitbucket_pipeline.HttpResponse)1 ServiceException (io.jenkins.blueocean.commons.ServiceException)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 URL (java.net.URL)1 Map (java.util.Map)1 Nonnull (javax.annotation.Nonnull)1