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; } }