This is an automated email from the ASF dual-hosted git repository. martinkanters pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/maven-docck-plugin.git
The following commit(s) were added to refs/heads/master by this push: new 27483cd [MDOCCK-35] - Upgrade Http Client 27483cd is described below commit 27483cd1d58d5e2d82d14ad45460b1a109e4887c Author: Tim te Beek <tim.te.b...@jdriven.com> AuthorDate: Mon Apr 26 17:34:05 2021 +0200 [MDOCCK-35] - Upgrade Http Client --- pom.xml | 6 +- .../docck/AbstractCheckDocumentationMojo.java | 80 +++++++++++----------- 2 files changed, 43 insertions(+), 43 deletions(-) diff --git a/pom.xml b/pom.xml index e2065f7..26852f1 100644 --- a/pom.xml +++ b/pom.xml @@ -99,9 +99,9 @@ under the License. <scope>provided</scope> </dependency> <dependency> - <groupId>commons-httpclient</groupId> - <artifactId>commons-httpclient</artifactId> - <version>3.1</version> + <groupId>org.apache.httpcomponents.client5</groupId> + <artifactId>httpclient5</artifactId> + <version>5.0.3</version> </dependency> <dependency> <groupId>commons-logging</groupId> diff --git a/src/main/java/org/apache/maven/plugin/docck/AbstractCheckDocumentationMojo.java b/src/main/java/org/apache/maven/plugin/docck/AbstractCheckDocumentationMojo.java index 4f8c250..81d02f5 100644 --- a/src/main/java/org/apache/maven/plugin/docck/AbstractCheckDocumentationMojo.java +++ b/src/main/java/org/apache/maven/plugin/docck/AbstractCheckDocumentationMojo.java @@ -19,13 +19,20 @@ package org.apache.maven.plugin.docck; * under the License. */ -import org.apache.commons.httpclient.Credentials; -import org.apache.commons.httpclient.HttpClient; -import org.apache.commons.httpclient.HttpException; -import org.apache.commons.httpclient.UsernamePasswordCredentials; -import org.apache.commons.httpclient.auth.AuthScope; -import org.apache.commons.httpclient.methods.HeadMethod; -import org.apache.commons.httpclient.params.HttpMethodParams; +import org.apache.hc.client5.http.auth.AuthScope; +import org.apache.hc.client5.http.auth.UsernamePasswordCredentials; +import org.apache.hc.client5.http.classic.methods.HttpHead; +import org.apache.hc.client5.http.config.RequestConfig; +import org.apache.hc.client5.http.cookie.StandardCookieSpec; +import org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider; +import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; +import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse; +import org.apache.hc.client5.http.impl.classic.HttpClientBuilder; +import org.apache.hc.client5.http.impl.classic.HttpClients; +import org.apache.hc.core5.http.HttpHeaders; +import org.apache.hc.core5.http.HttpHost; +import org.apache.hc.core5.http.message.BasicHeader; +import org.apache.hc.core5.util.Timeout; import org.apache.maven.model.IssueManagement; import org.apache.maven.model.License; import org.apache.maven.model.Organization; @@ -98,24 +105,12 @@ public abstract class AbstractCheckDocumentationMojo @Parameter( defaultValue = "${settings}", readonly = true, required = true ) private Settings settings; - private HttpClient httpClient; + private CloseableHttpClient httpClient; private FileSetManager fileSetManager = new FileSetManager(); private List<String> validUrls = new ArrayList<>(); - protected AbstractCheckDocumentationMojo() - { - String httpUserAgent = "maven-docck-plugin/1.x" + " (Java " + System.getProperty( "java.version" ) + "; " - + System.getProperty( "os.name" ) + " " + System.getProperty( "os.version" ) + ")"; - - httpClient = new HttpClient(); - - final int connectionTimeout = 5000; - httpClient.getHttpConnectionManager().getParams().setConnectionTimeout( connectionTimeout ); - httpClient.getParams().setParameter( HttpMethodParams.USER_AGENT, httpUserAgent ); - } - protected List<MavenProject> getReactorProjects() { return reactorProjects; @@ -125,7 +120,20 @@ public abstract class AbstractCheckDocumentationMojo public void execute() throws MojoExecutionException, MojoFailureException { - setupProxy(); + + String httpUserAgent = "maven-docck-plugin/1.x" + " (Java " + System.getProperty( "java.version" ) + "; " + + System.getProperty( "os.name" ) + " " + System.getProperty( "os.version" ) + ")"; + HttpClientBuilder httpClientBuilder = HttpClients.custom() + .setDefaultRequestConfig( RequestConfig.custom() + .setConnectTimeout( Timeout.ofSeconds( 5 ) ) + .setResponseTimeout( Timeout.ofSeconds( 5 ) ) + .setCookieSpec( StandardCookieSpec.STRICT ) + .build() ) + .setDefaultHeaders( List.of( new BasicHeader( HttpHeaders.USER_AGENT, httpUserAgent ) ) ); + + setupProxy( httpClientBuilder ); + + httpClient = httpClientBuilder.build(); if ( output != null ) { @@ -194,8 +202,9 @@ public abstract class AbstractCheckDocumentationMojo /** * Setup proxy access if needed. + * @param httpClientBuilder */ - private void setupProxy() + private void setupProxy( HttpClientBuilder httpClientBuilder ) { Proxy settingsProxy = settings.getActiveProxy(); @@ -211,7 +220,7 @@ public abstract class AbstractCheckDocumentationMojo if ( StringUtils.isNotEmpty( proxyHost ) ) { - httpClient.getHostConfiguration().setProxy( proxyHost, proxyPort ); + httpClientBuilder.setProxy( new HttpHost( proxyHost, proxyPort ) ); getLog().info( "Using proxy [" + proxyHost + "] at port [" + proxyPort + "]." ); @@ -219,10 +228,12 @@ public abstract class AbstractCheckDocumentationMojo { getLog().info( "Using proxy user [" + proxyUsername + "]." ); - Credentials creds = new UsernamePasswordCredentials( proxyUsername, proxyPassword ); + BasicCredentialsProvider credsProvider = new BasicCredentialsProvider(); + credsProvider.setCredentials( + new AuthScope( proxyHost, proxyPort ), + new UsernamePasswordCredentials( proxyUsername, proxyPassword.toCharArray() ) ); - httpClient.getState().setProxyCredentials( new AuthScope( proxyHost, proxyPort ), creds ); - httpClient.getParams().setAuthenticationPreemptive( true ); + httpClientBuilder.setDefaultCredentialsProvider( credsProvider ); } } } @@ -465,14 +476,12 @@ public abstract class AbstractCheckDocumentationMojo } else if ( !validUrls.contains( url ) ) { - HeadMethod headMethod = new HeadMethod( url ); - headMethod.setFollowRedirects( true ); - headMethod.setDoAuthentication( false ); + HttpHead headMethod = new HttpHead( url ); - try + try ( CloseableHttpResponse response = httpClient.execute( headMethod ) ) { getLog().debug( "Verifying http url: " + url ); - if ( httpClient.executeMethod( headMethod ) != HTTP_STATUS_200 ) + if ( response.getCode() != HTTP_STATUS_200 ) { reporter.error( "Cannot reach " + description + " with URL: \'" + url + "\'." ); } @@ -481,20 +490,11 @@ public abstract class AbstractCheckDocumentationMojo validUrls.add( url ); } } - catch ( HttpException e ) - { - reporter.error( "Cannot reach " + description + " with URL: \'" + url + "\'.\nError: " - + e.getMessage() ); - } catch ( IOException e ) { reporter.error( "Cannot reach " + description + " with URL: \'" + url + "\'.\nError: " + e.getMessage() ); } - finally - { - headMethod.releaseConnection(); - } } } else