diff --git a/pom.xml b/pom.xml index 95f0256..36d694e 100644 --- a/pom.xml +++ b/pom.xml @@ -120,11 +120,11 @@ io.jenkins.plugins - commons-lang3-api + apache-httpcomponents-client-5-api - org.jenkins-ci.plugins - apache-httpcomponents-client-4-api + io.jenkins.plugins + commons-lang3-api org.jenkins-ci.plugins diff --git a/src/main/java/org/jenkins/ci/plugins/jobimport/client/RestApiClient.java b/src/main/java/org/jenkins/ci/plugins/jobimport/client/RestApiClient.java index e696d1d..64c865c 100644 --- a/src/main/java/org/jenkins/ci/plugins/jobimport/client/RestApiClient.java +++ b/src/main/java/org/jenkins/ci/plugins/jobimport/client/RestApiClient.java @@ -2,7 +2,7 @@ import org.apache.commons.io.IOUtils; import org.apache.commons.lang.StringUtils; -import org.apache.http.HttpResponse; +import org.apache.hc.core5.http.ClassicHttpResponse; import org.jenkins.ci.plugins.jobimport.model.RemoteFolder; import org.jenkins.ci.plugins.jobimport.model.RemoteItem; import org.jenkins.ci.plugins.jobimport.model.RemoteJob; @@ -16,6 +16,7 @@ import javax.xml.parsers.DocumentBuilderFactory; import java.io.InputStream; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; import java.util.logging.Level; @@ -35,11 +36,11 @@ public static List getRemoteItems(RemoteFolder parent, String url, C factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true); factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); - HttpResponse response = URLUtils.getUrl(URLUtils.safeURL(url, Constants.XML_API_QUERY), credentials.username, credentials.password); + ClassicHttpResponse response = URLUtils.getUrl(URLUtils.safeURL(url, Constants.XML_API_QUERY), credentials.username, credentials.password); InputStream content = response.getEntity().getContent(); - int responseStatusCode = response.getStatusLine().getStatusCode(); + int responseStatusCode = response.getCode(); if (responseStatusCode >= 400) { - LOG.log(Level.SEVERE, "Failed to list job from remote " + url +". Response status code received " + responseStatusCode + ". Content: " + IOUtils.toString(content)); + LOG.log(Level.SEVERE, "Failed to list job from remote " + url + ". Response status code received " + responseStatusCode + ". Content: " + IOUtils.toString(content, StandardCharsets.UTF_8)); } else { Document doc = factory.newDocumentBuilder().parse(content); NodeList nl = doc.getElementsByTagName("job"); @@ -47,8 +48,7 @@ public static List getRemoteItems(RemoteFolder parent, String url, C for (int i = 0; i < nl.getLength(); i++) { Element job = (Element) nl.item(i); String impl = job.getAttribute("_class"); - boolean folder = (impl != null && - "com.cloudbees.hudson.plugins.folder.Folder".equals(impl)); + boolean folder = ("com.cloudbees.hudson.plugins.folder.Folder".equals(impl)); String desc = RemoteItemUtils.text(job, "description"); String jobUrl = RemoteItemUtils.text(job, "url"); String name = RemoteItemUtils.text(job, "name"); @@ -56,25 +56,30 @@ public static List getRemoteItems(RemoteFolder parent, String url, C final RemoteItem item = folder ? new RemoteFolder(name, impl, jobUrl, desc, parent) : new RemoteJob(name, impl, jobUrl, desc, parent); - if(parent == null) { + if (parent == null) { items.add(item); } else { parent.getChildren().add(item); items.add(item); } - if(folder && recursiveSearch) { + if (folder && recursiveSearch) { items.addAll(getRemoteItems((RemoteFolder) item, jobUrl, credentials, true)); } } } } - } catch(Exception e) { + } catch (Exception e) { LOG.log(Level.SEVERE, "Failed to list job from remote " + url, e); } return items; } - + /** + * Static-only access. + */ + private RestApiClient() { + // static-only access + } } diff --git a/src/main/java/org/jenkins/ci/plugins/jobimport/utils/URLUtils.java b/src/main/java/org/jenkins/ci/plugins/jobimport/utils/URLUtils.java index eec4dd1..e578fa4 100644 --- a/src/main/java/org/jenkins/ci/plugins/jobimport/utils/URLUtils.java +++ b/src/main/java/org/jenkins/ci/plugins/jobimport/utils/URLUtils.java @@ -24,23 +24,18 @@ package org.jenkins.ci.plugins.jobimport.utils; -import org.acegisecurity.AccessDeniedException; -import org.apache.http.HttpEntity; -import org.apache.http.HttpException; -import org.apache.http.HttpHost; -import org.apache.http.HttpResponse; -import org.apache.http.auth.AuthScope; -import org.apache.http.auth.UsernamePasswordCredentials; -import org.apache.http.client.AuthCache; -import org.apache.http.client.CredentialsProvider; -import org.apache.http.client.methods.CloseableHttpResponse; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.client.protocol.HttpClientContext; -import org.apache.http.impl.auth.BasicScheme; -import org.apache.http.impl.client.BasicAuthCache; -import org.apache.http.impl.client.BasicCredentialsProvider; -import org.apache.http.impl.client.HttpClientBuilder; -import org.apache.http.impl.client.HttpClients; +import org.apache.hc.client5.http.auth.AuthCache; +import org.apache.hc.client5.http.auth.AuthScope; +import org.apache.hc.client5.http.auth.UsernamePasswordCredentials; +import org.apache.hc.client5.http.classic.methods.HttpGet; +import org.apache.hc.client5.http.impl.auth.BasicAuthCache; +import org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider; +import org.apache.hc.client5.http.impl.auth.BasicScheme; +import org.apache.hc.client5.http.impl.classic.HttpClientBuilder; +import org.apache.hc.client5.http.impl.classic.HttpClients; +import org.apache.hc.client5.http.protocol.HttpClientContext; +import org.apache.hc.core5.http.ClassicHttpResponse; +import org.apache.hc.core5.http.HttpHost; import java.io.IOException; import java.io.InputStream; @@ -52,21 +47,20 @@ */ public final class URLUtils { - public static void notNull(final Object object) { - if (object == null) { - throw new IllegalArgumentException(); + public static void notNull(final Object object) { + if (object == null) { + throw new IllegalArgumentException(); + } } - } /** - * - * @param url The url to fetch + * @param url The url to fetch * @param username The username to use while fetching the url * @param password The password to use while fetching the url * @return The HttpResponse received. * @throws IOException If there was an issue in the communication with the server */ - public static HttpResponse getUrl(String url, String username, String password) throws IOException { + public static ClassicHttpResponse getUrl(String url, String username, String password) throws IOException { notNull(url); notNull(username); notNull(password); @@ -74,13 +68,13 @@ public static HttpResponse getUrl(String url, String username, String password) HttpClientContext localContext = HttpClientContext.create(); URL _url = new URL(url); - HttpHost target = new HttpHost(_url.getHost(), _url.getPort(), _url.getProtocol()); + HttpHost target = new HttpHost(_url.getProtocol(), _url.getHost(), _url.getPort()); - if(!username.isEmpty()) { - CredentialsProvider credsProvider = new BasicCredentialsProvider(); + if (!username.isEmpty()) { + BasicCredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(//AuthScope.ANY, new AuthScope(_url.getHost(), _url.getPort()), - new UsernamePasswordCredentials(username, password)); + new UsernamePasswordCredentials(username, password.toCharArray())); builder.setDefaultCredentialsProvider(credsProvider); @@ -91,20 +85,19 @@ public static HttpResponse getUrl(String url, String username, String password) authCache.put(target, basicAuth); localContext.setAuthCache(authCache); - } - return builder.build().execute(target, new HttpGet(url), localContext); + return builder.build().executeOpen(target, new HttpGet(url), localContext); } - + public static InputStream fetchUrl(String url, String username, String password) throws IOException { - HttpResponse response = getUrl(url, username, password); + ClassicHttpResponse response = getUrl(url, username, password); return response.getEntity().getContent(); } public static String safeURL(String base, String sufix) { if (base.endsWith("/") && sufix.startsWith("/")) { - return base.substring(0,base.length() - 1) + sufix; + return base.substring(0, base.length() - 1) + sufix; } else if (base.endsWith("/") && !sufix.startsWith("/")) { return base + sufix; } else if (!base.endsWith("/") && sufix.startsWith("/")) { @@ -113,10 +106,11 @@ public static String safeURL(String base, String sufix) { return base + "/" + sufix; } } - /** - * Static-only access. - */ - private URLUtils() { - // static-only access - } + + /** + * Static-only access. + */ + private URLUtils() { + // static-only access + } }