Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate to httpclient5 #47

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,11 @@
<dependencies>
<dependency>
<groupId>io.jenkins.plugins</groupId>
<artifactId>commons-lang3-api</artifactId>
<artifactId>apache-httpcomponents-client-5-api</artifactId>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>apache-httpcomponents-client-4-api</artifactId>
<groupId>io.jenkins.plugins</groupId>
<artifactId>commons-lang3-api</artifactId>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -35,46 +36,50 @@
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");

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

Check warning on line 51 in src/main/java/org/jenkins/ci/plugins/jobimport/client/RestApiClient.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 39-51 are not covered by tests
String desc = RemoteItemUtils.text(job, "description");
String jobUrl = RemoteItemUtils.text(job, "url");
String name = RemoteItemUtils.text(job, "name");

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
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -52,59 +47,57 @@
*/
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);
HttpClientBuilder builder = HttpClients.custom();
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);

AuthCache authCache = new BasicAuthCache();
// Generate BASIC scheme object and add it to the local
// auth cache
BasicScheme basicAuth = new BasicScheme();
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);

Check warning on line 93 in src/main/java/org/jenkins/ci/plugins/jobimport/utils/URLUtils.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 71-93 are not covered by tests
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("/")) {
Expand All @@ -113,10 +106,11 @@
return base + "/" + sufix;
}
}
/**
* Static-only access.
*/
private URLUtils() {
// static-only access
}

/**
* Static-only access.
*/
private URLUtils() {
// static-only access
}
}
Loading