jenkins自动构建-java后端

String jksIndexUrl = jenkinsOperate.getJksIndexUrl();
String jksBuildUrl = jenkinsOperate.getJksJobUrl() + JenkinsConstants.JENKINS_BUILD_SUFFIX;

String errorMsg = JenkinsApiUtil.build(jksBuildUrl, jksIndexUrl, jksLoginName, password);
public static String build(String requestUrl, String jenkinsIndexUrl, String username, String password) {
    String errorMsg = null;
    try {
        HttpResponse response = executeHttp(requestUrl, jenkinsIndexUrl, username, password);
        String result = EntityUtils.toString(response.getEntity());
        LOGGER.info("###JenkinsApiUtil.build result is " + result);
    } catch (Exception ex) {
        ex.printStackTrace();
        errorMsg = ex.getMessage();
    }
    return errorMsg;
}
private static HttpResponse executeHttp(String requestUrl, String jenkinsIndexUrl, String username, String password) throws IOException {
    // 构造httpClient
    URI uri = URI.create(requestUrl);
    HttpHost host = new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme());
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(new AuthScope(uri.getHost(), uri.getPort()), new UsernamePasswordCredentials(username, password));
    HttpClientBuilder httpClientBuilder = getHttpClientBuilder();
    httpClientBuilder.setDefaultCredentialsProvider(credsProvider);
    HttpClient httpClient = httpClientBuilder.build();

    HttpPost httpPost = new HttpPost(uri);
    httpPost.setHeader("Content-Type", "application/xml;charset=UTF-8");
    String crumbCode = getJenkinsCrumb(jenkinsIndexUrl, username, password);
    if (!StringUtils.isEmpty(crumbCode)) {
        httpPost.addHeader("Jenkins-Crumb", crumbCode);
    }

    // 构造localContext
    HttpClientContext localContext = HttpClientContext.create();
    AuthCache authCache = new BasicAuthCache();
    BasicScheme basicAuth = new BasicScheme();
    authCache.put(host, basicAuth);
    localContext.setAuthCache(authCache);
    return httpClient.execute(host, httpPost, localContext);
}
public static String getJenkinsCrumb(String jenkinsIndexUrl, String username, String password) {
    try {
        String urlString = jenkinsIndexUrl + "/crumbIssuer/api/json";

        // 构造httpClient
        URI uri = URI.create(urlString);
        HttpHost host = new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme());
        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(new AuthScope(uri.getHost(), uri.getPort()), new UsernamePasswordCredentials(username, password));
        HttpClientBuilder httpClientBuilder = getHttpClientBuilder();
        httpClientBuilder.setDefaultCredentialsProvider(credsProvider);
        HttpClient httpClient = httpClientBuilder.build();

        // 构造httpGet
        HttpGet httpGet = new HttpGet(uri);

        // 构造localContext
        HttpClientContext localContext = HttpClientContext.create();
        AuthCache authCache = new BasicAuthCache();
        BasicScheme basicAuth = new BasicScheme();
        authCache.put(host, basicAuth);
        localContext.setAuthCache(authCache);

        HttpResponse response = httpClient.execute(host, httpGet, localContext);
        String result = EntityUtils.toString(response.getEntity());
        LOGGER.error("####JenkinsApiUtil.getJenkinsCrumb result is ", result);
        if (StringUtils.isEmpty(result)) {
            return null;
        }
        Map maps = (Map) JSONObject.parse(result);
        return maps.get("crumb").toString();
    } catch (Exception e) {
        LOGGER.error("####JenkinsApiUtil.getJenkinsCrumb failed.");
        return null;
    }
}
private final static RequestConfig.Builder DefaultRequestConfigBuilder() {
    return RequestConfig.custom()
            .setSocketTimeout(20000)
            .setConnectTimeout(10000)
            .setConnectionRequestTimeout(10000)
            .setRedirectsEnabled(true);
}

private final static RequestConfig DEFAULT_REQUEST_CONFIG = DefaultRequestConfigBuilder().build();

private static HttpClientBuilder getHttpClientBuilder() {
    try {
        SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, (a, b) -> true).build();
        Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
                .register("http", PlainConnectionSocketFactory.INSTANCE)
                .register("https", new SSLConnectionSocketFactory(sslContext))
                .build();

        PoolingHttpClientConnectionManager connectionManager;
        connectionManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
        connectionManager.setMaxTotal(200);
        connectionManager.setDefaultMaxPerRoute(40);

        RequestConfig requestConfig = DEFAULT_REQUEST_CONFIG;
        HttpClientBuilder clientBuilder = HttpClients.custom()
                .setConnectionManager(connectionManager)
                .setDefaultRequestConfig(requestConfig);
        return clientBuilder;
    } catch (Exception e) {
        return null;
    }
}

转载于:https://my.oschina.net/u/3824443/blog/1833095

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值