From 8aca8440a024c17cb8b16b02dc30dbefc951bc01 Mon Sep 17 00:00:00 2001 From: Dan Hayworth Date: Tue, 9 May 2023 20:52:14 +1200 Subject: [PATCH 1/3] fix cookie header warning --- .../license/download/LicenseDownloader.java | 88 ++++++++++--------- .../license/download/LicenseSummaryTest.java | 63 +++++++------ 2 files changed, 76 insertions(+), 75 deletions(-) diff --git a/src/main/java/org/codehaus/mojo/license/download/LicenseDownloader.java b/src/main/java/org/codehaus/mojo/license/download/LicenseDownloader.java index 3a29d9c0b..af4b24ef9 100644 --- a/src/main/java/org/codehaus/mojo/license/download/LicenseDownloader.java +++ b/src/main/java/org/codehaus/mojo/license/download/LicenseDownloader.java @@ -53,6 +53,7 @@ import org.apache.http.auth.Credentials; import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.client.CredentialsProvider; +import org.apache.http.client.config.CookieSpecs; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.config.RequestConfig.Builder; import org.apache.http.client.methods.CloseableHttpResponse; @@ -89,14 +90,15 @@ public class LicenseDownloader implements AutoCloseable private final Map contentSanitizers; private final Charset charset; - public LicenseDownloader( Proxy proxy, int connectTimeout, int socketTimeout, int connectionRequestTimeout, - Map contentSanitizers, Charset charset ) + public LicenseDownloader( final Proxy proxy, final int connectTimeout, final int socketTimeout, final int connectionRequestTimeout, + final Map contentSanitizers, final Charset charset ) { this.contentSanitizers = contentSanitizers; this.charset = charset; - final Builder configBuilder = RequestConfig.copy( RequestConfig.DEFAULT ) // - .setConnectTimeout( connectTimeout ) // - .setSocketTimeout( socketTimeout ) // + final Builder configBuilder = RequestConfig.copy( RequestConfig.DEFAULT ) + .setConnectTimeout( connectTimeout ) + .setSocketTimeout( socketTimeout ) + .setCookieSpec(CookieSpecs.STANDARD) .setConnectionRequestTimeout( connectionRequestTimeout ); if ( proxy != null ) @@ -104,7 +106,7 @@ public LicenseDownloader( Proxy proxy, int connectTimeout, int socketTimeout, in configBuilder.setProxy( new HttpHost( proxy.getHost(), proxy.getPort(), proxy.getProtocol() ) ); } - HttpClientBuilder clientBuilder = HttpClients.custom().setDefaultRequestConfig( configBuilder.build() ); + final HttpClientBuilder clientBuilder = HttpClients.custom().setDefaultRequestConfig( configBuilder.build() ); if ( proxy != null ) { if ( proxy.getUsername() != null && proxy.getPassword() != null ) @@ -121,7 +123,7 @@ public LicenseDownloader( Proxy proxy, int connectTimeout, int socketTimeout, in if ( nonProxyHosts.length > 0 ) { final List nonProxyPatterns = new ArrayList<>(); - for ( String nonProxyHost : nonProxyHosts ) + for ( final String nonProxyHost : nonProxyHosts ) { final Pattern pat = Pattern.compile( nonProxyHost.replaceAll( "\\.", "\\\\." ).replaceAll( "\\*", ".*" ), @@ -133,10 +135,10 @@ public LicenseDownloader( Proxy proxy, int connectTimeout, int socketTimeout, in { @Override - protected HttpHost determineProxy( HttpHost target, HttpRequest request, HttpContext context ) + protected HttpHost determineProxy( final HttpHost target, final HttpRequest request, final HttpContext context ) throws HttpException { - for ( Pattern pattern : nonProxyPatterns ) + for ( final Pattern pattern : nonProxyPatterns ) { if ( pattern.matcher( target.getHostName() ).matches() ) { @@ -167,7 +169,7 @@ protected HttpHost determineProxy( HttpHost target, HttpRequest request, HttpCon * @throws URISyntaxException * @throws MojoFailureException */ - public LicenseDownloadResult downloadLicense( String licenseUrlString, FileNameEntry fileNameEntry ) + public LicenseDownloadResult downloadLicense( final String licenseUrlString, final FileNameEntry fileNameEntry ) throws IOException, URISyntaxException, MojoFailureException { final File outputFile = fileNameEntry.getFile(); @@ -176,12 +178,12 @@ public LicenseDownloadResult downloadLicense( String licenseUrlString, FileNameE throw new IllegalArgumentException( "Null URL for file " + outputFile.getPath() ); } - List sanitizers = filterSanitizers( licenseUrlString ); + final List sanitizers = this.filterSanitizers( licenseUrlString ); if ( licenseUrlString.startsWith( "file://" ) ) { - LOG.debug( "Downloading '{}' -> '{}'", licenseUrlString, outputFile ); - Path in = Paths.get( new URI( licenseUrlString ) ); + LicenseDownloader.LOG.debug( "Downloading '{}' -> '{}'", licenseUrlString, outputFile ); + final Path in = Paths.get( new URI( licenseUrlString ) ); if ( sanitizers.isEmpty() ) { Files.copy( in, outputFile.toPath() ); @@ -189,16 +191,16 @@ public LicenseDownloadResult downloadLicense( String licenseUrlString, FileNameE } else { - try ( BufferedReader r = Files.newBufferedReader( in, charset ) ) + try ( BufferedReader r = Files.newBufferedReader( in, this.charset ) ) { - return sanitize( r, outputFile, charset, sanitizers, fileNameEntry.isPreferred() ); + return LicenseDownloader.sanitize( r, outputFile, this.charset, sanitizers, fileNameEntry.isPreferred() ); } } } else { - LOG.debug( "About to download '{}'", licenseUrlString ); - try ( CloseableHttpResponse response = client.execute( new HttpGet( licenseUrlString ) ) ) + LicenseDownloader.LOG.debug( "About to download '{}'", licenseUrlString ); + try ( CloseableHttpResponse response = this.client.execute( new HttpGet( licenseUrlString ) ) ) { final StatusLine statusLine = response.getStatusLine(); if ( statusLine.getStatusCode() != HttpStatus.SC_OK ) @@ -213,10 +215,10 @@ public LicenseDownloadResult downloadLicense( String licenseUrlString, FileNameE { final ContentType contentType = ContentType.get( entity ); - File updatedFile = fileNameEntry.isPreferred() ? outputFile - : updateFileExtension( outputFile, + final File updatedFile = fileNameEntry.isPreferred() ? outputFile + : LicenseDownloader.updateFileExtension( outputFile, contentType != null ? contentType.getMimeType() : null ); - LOG.debug( "Downloading '{}' -> '{}'{}", + LicenseDownloader.LOG.debug( "Downloading '{}' -> '{}'{}", licenseUrlString, updatedFile, fileNameEntry.isPreferred() ? " (preferred file name)" : "" ); @@ -249,7 +251,7 @@ public LicenseDownloadResult downloadLicense( String licenseUrlString, FileNameE return LicenseDownloadResult.success( updatedFile, actualSha1, fileNameEntry.isPreferred() ); } - catch ( NoSuchAlgorithmException e ) + catch ( final NoSuchAlgorithmException e ) { throw new RuntimeException( e ); } @@ -262,7 +264,7 @@ public LicenseDownloadResult downloadLicense( String licenseUrlString, FileNameE try ( BufferedReader r = new BufferedReader( new InputStreamReader( entity.getContent(), cs ) ) ) { - return sanitize( r, updatedFile, cs, sanitizers, fileNameEntry.isPreferred() ); + return LicenseDownloader.sanitize( r, updatedFile, cs, sanitizers, fileNameEntry.isPreferred() ); } } } @@ -274,13 +276,13 @@ public LicenseDownloadResult downloadLicense( String licenseUrlString, FileNameE } } - static LicenseDownloadResult sanitize( BufferedReader r, File out, Charset charset, - List sanitizers, boolean preferredFileName ) + static LicenseDownloadResult sanitize( final BufferedReader r, final File out, final Charset charset, + final List sanitizers, final boolean preferredFileName ) throws IOException { final StringBuilder contentBuilder = new StringBuilder(); // CHECKSTYLE_OFF: MagicNumber - char[] buffer = new char[8192]; + final char[] buffer = new char[8192]; // CHECKSTYLE_ON: MagicNumber int len = 0; while ( ( len = r.read( buffer ) ) >= 0 ) @@ -289,20 +291,20 @@ static LicenseDownloadResult sanitize( BufferedReader r, File out, Charset chars } String content = contentBuilder.toString(); - for ( ContentSanitizer sanitizer : sanitizers ) + for ( final ContentSanitizer sanitizer : sanitizers ) { content = sanitizer.sanitize( content ); } - byte[] bytes = content.getBytes( charset ); + final byte[] bytes = content.getBytes( charset ); Files.write( out.toPath(), bytes ); final String sha1 = DigestUtils.sha1Hex( bytes ); return LicenseDownloadResult.success( out, sha1, preferredFileName ); } - List filterSanitizers( String licenseUrlString ) + List filterSanitizers( final String licenseUrlString ) { - ArrayList result = new ArrayList<>(); - for ( ContentSanitizer s : contentSanitizers.values() ) + final ArrayList result = new ArrayList<>(); + for ( final ContentSanitizer s : this.contentSanitizers.values() ) { if ( s.applies( licenseUrlString ) ) { @@ -312,7 +314,7 @@ List filterSanitizers( String licenseUrlString ) return result; } - static File updateFileExtension( File outputFile, String mimeType ) + static File updateFileExtension( final File outputFile, final String mimeType ) { String realExtension = FileUtil.toExtension( mimeType, false ); if ( realExtension == null ) @@ -324,7 +326,7 @@ static File updateFileExtension( File outputFile, String mimeType ) final String oldFileName = outputFile.getName(); if ( !oldFileName.endsWith( realExtension ) ) { - final String newFileName = EXTENSION_PATTERN.matcher( oldFileName ).replaceAll( "" ) + realExtension; + final String newFileName = LicenseDownloader.EXTENSION_PATTERN.matcher( oldFileName ).replaceAll( "" ) + realExtension; return new File( outputFile.getParentFile(), newFileName ); } return outputFile; @@ -333,7 +335,7 @@ static File updateFileExtension( File outputFile, String mimeType ) @Override public void close() throws IOException { - client.close(); + this.client.close(); } /** @@ -343,17 +345,17 @@ public void close() throws IOException */ public static class LicenseDownloadResult { - public static LicenseDownloadResult success( File file, String sha1, boolean preferredFileName ) + public static LicenseDownloadResult success( final File file, final String sha1, final boolean preferredFileName ) { return new LicenseDownloadResult( file, sha1, preferredFileName, null ); } - public static LicenseDownloadResult failure( String errorMessage ) + public static LicenseDownloadResult failure( final String errorMessage ) { return new LicenseDownloadResult( null, null, false, errorMessage ); } - private LicenseDownloadResult( File file, String sha1, boolean preferredFileName, String errorMessage ) + private LicenseDownloadResult( final File file, final String sha1, final boolean preferredFileName, final String errorMessage ) { super(); this.file = file; @@ -372,32 +374,32 @@ private LicenseDownloadResult( File file, String sha1, boolean preferredFileName public File getFile() { - return file; + return this.file; } public String getErrorMessage() { - return errorMessage; + return this.errorMessage; } public boolean isSuccess() { - return errorMessage == null; + return this.errorMessage == null; } public boolean isPreferredFileName() { - return preferredFileName; + return this.preferredFileName; } public String getSha1() { - return sha1; + return this.sha1; } - public LicenseDownloadResult withFile( File otherFile ) + public LicenseDownloadResult withFile( final File otherFile ) { - return new LicenseDownloadResult( otherFile, sha1, preferredFileName, errorMessage ); + return new LicenseDownloadResult( otherFile, this.sha1, this.preferredFileName, this.errorMessage ); } } diff --git a/src/test/java/org/codehaus/mojo/license/download/LicenseSummaryTest.java b/src/test/java/org/codehaus/mojo/license/download/LicenseSummaryTest.java index 058e922f2..6aec58e57 100644 --- a/src/test/java/org/codehaus/mojo/license/download/LicenseSummaryTest.java +++ b/src/test/java/org/codehaus/mojo/license/download/LicenseSummaryTest.java @@ -1,15 +1,5 @@ package org.codehaus.mojo.license.download; -import org.codehaus.mojo.license.Eol; -import org.codehaus.mojo.license.download.LicenseSummaryReader; -import org.codehaus.mojo.license.download.LicenseSummaryWriter; -import org.junit.Assert; -import org.junit.Test; -import org.xml.sax.SAXException; - -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.transform.TransformerException; -import javax.xml.transform.TransformerFactoryConfigurationError; import java.io.File; import java.io.FileInputStream; import java.io.IOException; @@ -17,6 +7,15 @@ import java.util.ArrayList; import java.util.List; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.TransformerException; +import javax.xml.transform.TransformerFactoryConfigurationError; + +import org.codehaus.mojo.license.Eol; +import org.junit.Assert; +import org.junit.Test; +import org.xml.sax.SAXException; + /** * @since 1.0 */ @@ -34,19 +33,19 @@ public class LicenseSummaryTest public void testReadLicenseSummary() throws IOException, SAXException, ParserConfigurationException { - File licenseSummaryFile = new File( "src/test/resources/license-summary-test.xml" ); + final File licenseSummaryFile = new File( "src/test/resources/license-summary-test.xml" ); Assert.assertTrue( licenseSummaryFile.exists() ); - FileInputStream fis = new FileInputStream( licenseSummaryFile ); - List list = LicenseSummaryReader.parseLicenseSummary( fis ); + final FileInputStream fis = new FileInputStream( licenseSummaryFile ); + final List list = LicenseSummaryReader.parseLicenseSummary( fis ); fis.close(); - ProjectLicenseInfo dep = list.get( 0 ); + final ProjectLicenseInfo dep = list.get( 0 ); Assert.assertEquals( "org.codehaus.mojo", dep.getGroupId() ); Assert.assertEquals( "junk", dep.getArtifactId() ); Assert.assertEquals( "1.1", dep.getVersion() ); - List licenses = dep.getLicenses(); + final List licenses = dep.getLicenses(); Assert.assertEquals( 1, licenses.size() ); - ProjectLicense lic0 = dep.getLicenses().get(0); + final ProjectLicense lic0 = dep.getLicenses().get(0); Assert.assertEquals( "lgpl", lic0.getName() ); Assert.assertEquals( "http://www.gnu.org/licenses/lgpl-3.0.txt", lic0.getUrl() ); Assert.assertEquals( "lgpl-3.0.txt", lic0.getFile() ); @@ -69,11 +68,11 @@ public void testWriteReadLicenseSummary() throws IOException, SAXException, ParserConfigurationException, TransformerFactoryConfigurationError, TransformerException { - List licSummary = new ArrayList<>(); - ProjectLicenseInfo dep1 = new ProjectLicenseInfo( "org.test", "test1", "1.0" ); - ProjectLicenseInfo dep2 = new ProjectLicenseInfo( "org.test", "test2", "2.0" ); + final List licSummary = new ArrayList<>(); + final ProjectLicenseInfo dep1 = new ProjectLicenseInfo( "org.test", "test1", "1.0" ); + final ProjectLicenseInfo dep2 = new ProjectLicenseInfo( "org.test", "test2", "2.0" ); - ProjectLicense lic = new ProjectLicense(); + final ProjectLicense lic = new ProjectLicense(); lic.setName( "lgpl" ); lic.setUrl( "http://www.gnu.org/licenses/lgpl-3.0.txt" ); lic.setFile( "lgpl-3.0.txt" ); @@ -85,22 +84,22 @@ public void testWriteReadLicenseSummary() licSummary.add( dep2 ); { - File licenseSummaryFile = File.createTempFile( "licSummary", "tmp" ); + final File licenseSummaryFile = File.createTempFile( "licSummary", "tmp" ); LicenseSummaryWriter.writeLicenseSummary( licSummary, licenseSummaryFile, StandardCharsets.UTF_8, Eol.LF, true ); Assert.assertTrue( licenseSummaryFile.exists() ); - FileInputStream fis = new FileInputStream( licenseSummaryFile ); - List list = LicenseSummaryReader.parseLicenseSummary( fis ); + final FileInputStream fis = new FileInputStream( licenseSummaryFile ); + final List list = LicenseSummaryReader.parseLicenseSummary( fis ); fis.close(); - ProjectLicenseInfo dep = list.get( 0 ); + final ProjectLicenseInfo dep = list.get( 0 ); Assert.assertEquals( "org.test", dep.getGroupId() ); Assert.assertEquals( "test1", dep.getArtifactId() ); Assert.assertEquals( "1.0", dep.getVersion() ); - List licenses = dep.getLicenses(); + final List licenses = dep.getLicenses(); Assert.assertEquals( 1, licenses.size() ); - ProjectLicense lic0 = dep.getLicenses().get(0); + final ProjectLicense lic0 = dep.getLicenses().get(0); Assert.assertEquals( "lgpl", lic0.getName() ); Assert.assertEquals( "http://www.gnu.org/licenses/lgpl-3.0.txt", lic0.getUrl() ); Assert.assertEquals( "lgpl-3.0.txt", lic0.getFile() ); @@ -108,22 +107,22 @@ public void testWriteReadLicenseSummary() } { - File licenseSummaryFile = File.createTempFile( "licSummaryNoVersion", "tmp" ); + final File licenseSummaryFile = File.createTempFile( "licSummaryNoVersion", "tmp" ); LicenseSummaryWriter.writeLicenseSummary( licSummary, licenseSummaryFile, StandardCharsets.UTF_8, Eol.LF, false ); Assert.assertTrue( licenseSummaryFile.exists() ); - FileInputStream fis = new FileInputStream( licenseSummaryFile ); - List list = LicenseSummaryReader.parseLicenseSummary( fis ); + final FileInputStream fis = new FileInputStream( licenseSummaryFile ); + final List list = LicenseSummaryReader.parseLicenseSummary( fis ); fis.close(); - ProjectLicenseInfo dep = list.get( 0 ); + final ProjectLicenseInfo dep = list.get( 0 ); Assert.assertEquals( "org.test", dep.getGroupId() ); Assert.assertEquals( "test1", dep.getArtifactId() ); Assert.assertNull( dep.getVersion() ); - List licenses = dep.getLicenses(); + final List licenses = dep.getLicenses(); Assert.assertEquals( 1, licenses.size() ); - ProjectLicense lic0 = dep.getLicenses().get(0); + final ProjectLicense lic0 = dep.getLicenses().get(0); Assert.assertEquals( "lgpl", lic0.getName() ); Assert.assertEquals( "http://www.gnu.org/licenses/lgpl-3.0.txt", lic0.getUrl() ); Assert.assertEquals( "lgpl-3.0.txt", lic0.getFile() ); From 7e729538f269577c6ca786bdecc213f83a32e890 Mon Sep 17 00:00:00 2001 From: Dan Hayworth Date: Tue, 9 May 2023 22:07:38 +1200 Subject: [PATCH 2/3] Revert "fix cookie header warning" This reverts commit 8aca8440a024c17cb8b16b02dc30dbefc951bc01. --- .../license/download/LicenseDownloader.java | 88 +++++++++---------- .../license/download/LicenseSummaryTest.java | 63 ++++++------- 2 files changed, 75 insertions(+), 76 deletions(-) diff --git a/src/main/java/org/codehaus/mojo/license/download/LicenseDownloader.java b/src/main/java/org/codehaus/mojo/license/download/LicenseDownloader.java index af4b24ef9..3a29d9c0b 100644 --- a/src/main/java/org/codehaus/mojo/license/download/LicenseDownloader.java +++ b/src/main/java/org/codehaus/mojo/license/download/LicenseDownloader.java @@ -53,7 +53,6 @@ import org.apache.http.auth.Credentials; import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.client.CredentialsProvider; -import org.apache.http.client.config.CookieSpecs; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.config.RequestConfig.Builder; import org.apache.http.client.methods.CloseableHttpResponse; @@ -90,15 +89,14 @@ public class LicenseDownloader implements AutoCloseable private final Map contentSanitizers; private final Charset charset; - public LicenseDownloader( final Proxy proxy, final int connectTimeout, final int socketTimeout, final int connectionRequestTimeout, - final Map contentSanitizers, final Charset charset ) + public LicenseDownloader( Proxy proxy, int connectTimeout, int socketTimeout, int connectionRequestTimeout, + Map contentSanitizers, Charset charset ) { this.contentSanitizers = contentSanitizers; this.charset = charset; - final Builder configBuilder = RequestConfig.copy( RequestConfig.DEFAULT ) - .setConnectTimeout( connectTimeout ) - .setSocketTimeout( socketTimeout ) - .setCookieSpec(CookieSpecs.STANDARD) + final Builder configBuilder = RequestConfig.copy( RequestConfig.DEFAULT ) // + .setConnectTimeout( connectTimeout ) // + .setSocketTimeout( socketTimeout ) // .setConnectionRequestTimeout( connectionRequestTimeout ); if ( proxy != null ) @@ -106,7 +104,7 @@ public LicenseDownloader( final Proxy proxy, final int connectTimeout, final int configBuilder.setProxy( new HttpHost( proxy.getHost(), proxy.getPort(), proxy.getProtocol() ) ); } - final HttpClientBuilder clientBuilder = HttpClients.custom().setDefaultRequestConfig( configBuilder.build() ); + HttpClientBuilder clientBuilder = HttpClients.custom().setDefaultRequestConfig( configBuilder.build() ); if ( proxy != null ) { if ( proxy.getUsername() != null && proxy.getPassword() != null ) @@ -123,7 +121,7 @@ public LicenseDownloader( final Proxy proxy, final int connectTimeout, final int if ( nonProxyHosts.length > 0 ) { final List nonProxyPatterns = new ArrayList<>(); - for ( final String nonProxyHost : nonProxyHosts ) + for ( String nonProxyHost : nonProxyHosts ) { final Pattern pat = Pattern.compile( nonProxyHost.replaceAll( "\\.", "\\\\." ).replaceAll( "\\*", ".*" ), @@ -135,10 +133,10 @@ public LicenseDownloader( final Proxy proxy, final int connectTimeout, final int { @Override - protected HttpHost determineProxy( final HttpHost target, final HttpRequest request, final HttpContext context ) + protected HttpHost determineProxy( HttpHost target, HttpRequest request, HttpContext context ) throws HttpException { - for ( final Pattern pattern : nonProxyPatterns ) + for ( Pattern pattern : nonProxyPatterns ) { if ( pattern.matcher( target.getHostName() ).matches() ) { @@ -169,7 +167,7 @@ protected HttpHost determineProxy( final HttpHost target, final HttpRequest requ * @throws URISyntaxException * @throws MojoFailureException */ - public LicenseDownloadResult downloadLicense( final String licenseUrlString, final FileNameEntry fileNameEntry ) + public LicenseDownloadResult downloadLicense( String licenseUrlString, FileNameEntry fileNameEntry ) throws IOException, URISyntaxException, MojoFailureException { final File outputFile = fileNameEntry.getFile(); @@ -178,12 +176,12 @@ public LicenseDownloadResult downloadLicense( final String licenseUrlString, fin throw new IllegalArgumentException( "Null URL for file " + outputFile.getPath() ); } - final List sanitizers = this.filterSanitizers( licenseUrlString ); + List sanitizers = filterSanitizers( licenseUrlString ); if ( licenseUrlString.startsWith( "file://" ) ) { - LicenseDownloader.LOG.debug( "Downloading '{}' -> '{}'", licenseUrlString, outputFile ); - final Path in = Paths.get( new URI( licenseUrlString ) ); + LOG.debug( "Downloading '{}' -> '{}'", licenseUrlString, outputFile ); + Path in = Paths.get( new URI( licenseUrlString ) ); if ( sanitizers.isEmpty() ) { Files.copy( in, outputFile.toPath() ); @@ -191,16 +189,16 @@ public LicenseDownloadResult downloadLicense( final String licenseUrlString, fin } else { - try ( BufferedReader r = Files.newBufferedReader( in, this.charset ) ) + try ( BufferedReader r = Files.newBufferedReader( in, charset ) ) { - return LicenseDownloader.sanitize( r, outputFile, this.charset, sanitizers, fileNameEntry.isPreferred() ); + return sanitize( r, outputFile, charset, sanitizers, fileNameEntry.isPreferred() ); } } } else { - LicenseDownloader.LOG.debug( "About to download '{}'", licenseUrlString ); - try ( CloseableHttpResponse response = this.client.execute( new HttpGet( licenseUrlString ) ) ) + LOG.debug( "About to download '{}'", licenseUrlString ); + try ( CloseableHttpResponse response = client.execute( new HttpGet( licenseUrlString ) ) ) { final StatusLine statusLine = response.getStatusLine(); if ( statusLine.getStatusCode() != HttpStatus.SC_OK ) @@ -215,10 +213,10 @@ public LicenseDownloadResult downloadLicense( final String licenseUrlString, fin { final ContentType contentType = ContentType.get( entity ); - final File updatedFile = fileNameEntry.isPreferred() ? outputFile - : LicenseDownloader.updateFileExtension( outputFile, + File updatedFile = fileNameEntry.isPreferred() ? outputFile + : updateFileExtension( outputFile, contentType != null ? contentType.getMimeType() : null ); - LicenseDownloader.LOG.debug( "Downloading '{}' -> '{}'{}", + LOG.debug( "Downloading '{}' -> '{}'{}", licenseUrlString, updatedFile, fileNameEntry.isPreferred() ? " (preferred file name)" : "" ); @@ -251,7 +249,7 @@ public LicenseDownloadResult downloadLicense( final String licenseUrlString, fin return LicenseDownloadResult.success( updatedFile, actualSha1, fileNameEntry.isPreferred() ); } - catch ( final NoSuchAlgorithmException e ) + catch ( NoSuchAlgorithmException e ) { throw new RuntimeException( e ); } @@ -264,7 +262,7 @@ public LicenseDownloadResult downloadLicense( final String licenseUrlString, fin try ( BufferedReader r = new BufferedReader( new InputStreamReader( entity.getContent(), cs ) ) ) { - return LicenseDownloader.sanitize( r, updatedFile, cs, sanitizers, fileNameEntry.isPreferred() ); + return sanitize( r, updatedFile, cs, sanitizers, fileNameEntry.isPreferred() ); } } } @@ -276,13 +274,13 @@ public LicenseDownloadResult downloadLicense( final String licenseUrlString, fin } } - static LicenseDownloadResult sanitize( final BufferedReader r, final File out, final Charset charset, - final List sanitizers, final boolean preferredFileName ) + static LicenseDownloadResult sanitize( BufferedReader r, File out, Charset charset, + List sanitizers, boolean preferredFileName ) throws IOException { final StringBuilder contentBuilder = new StringBuilder(); // CHECKSTYLE_OFF: MagicNumber - final char[] buffer = new char[8192]; + char[] buffer = new char[8192]; // CHECKSTYLE_ON: MagicNumber int len = 0; while ( ( len = r.read( buffer ) ) >= 0 ) @@ -291,20 +289,20 @@ static LicenseDownloadResult sanitize( final BufferedReader r, final File out, f } String content = contentBuilder.toString(); - for ( final ContentSanitizer sanitizer : sanitizers ) + for ( ContentSanitizer sanitizer : sanitizers ) { content = sanitizer.sanitize( content ); } - final byte[] bytes = content.getBytes( charset ); + byte[] bytes = content.getBytes( charset ); Files.write( out.toPath(), bytes ); final String sha1 = DigestUtils.sha1Hex( bytes ); return LicenseDownloadResult.success( out, sha1, preferredFileName ); } - List filterSanitizers( final String licenseUrlString ) + List filterSanitizers( String licenseUrlString ) { - final ArrayList result = new ArrayList<>(); - for ( final ContentSanitizer s : this.contentSanitizers.values() ) + ArrayList result = new ArrayList<>(); + for ( ContentSanitizer s : contentSanitizers.values() ) { if ( s.applies( licenseUrlString ) ) { @@ -314,7 +312,7 @@ List filterSanitizers( final String licenseUrlString ) return result; } - static File updateFileExtension( final File outputFile, final String mimeType ) + static File updateFileExtension( File outputFile, String mimeType ) { String realExtension = FileUtil.toExtension( mimeType, false ); if ( realExtension == null ) @@ -326,7 +324,7 @@ static File updateFileExtension( final File outputFile, final String mimeType ) final String oldFileName = outputFile.getName(); if ( !oldFileName.endsWith( realExtension ) ) { - final String newFileName = LicenseDownloader.EXTENSION_PATTERN.matcher( oldFileName ).replaceAll( "" ) + realExtension; + final String newFileName = EXTENSION_PATTERN.matcher( oldFileName ).replaceAll( "" ) + realExtension; return new File( outputFile.getParentFile(), newFileName ); } return outputFile; @@ -335,7 +333,7 @@ static File updateFileExtension( final File outputFile, final String mimeType ) @Override public void close() throws IOException { - this.client.close(); + client.close(); } /** @@ -345,17 +343,17 @@ public void close() throws IOException */ public static class LicenseDownloadResult { - public static LicenseDownloadResult success( final File file, final String sha1, final boolean preferredFileName ) + public static LicenseDownloadResult success( File file, String sha1, boolean preferredFileName ) { return new LicenseDownloadResult( file, sha1, preferredFileName, null ); } - public static LicenseDownloadResult failure( final String errorMessage ) + public static LicenseDownloadResult failure( String errorMessage ) { return new LicenseDownloadResult( null, null, false, errorMessage ); } - private LicenseDownloadResult( final File file, final String sha1, final boolean preferredFileName, final String errorMessage ) + private LicenseDownloadResult( File file, String sha1, boolean preferredFileName, String errorMessage ) { super(); this.file = file; @@ -374,32 +372,32 @@ private LicenseDownloadResult( final File file, final String sha1, final boolean public File getFile() { - return this.file; + return file; } public String getErrorMessage() { - return this.errorMessage; + return errorMessage; } public boolean isSuccess() { - return this.errorMessage == null; + return errorMessage == null; } public boolean isPreferredFileName() { - return this.preferredFileName; + return preferredFileName; } public String getSha1() { - return this.sha1; + return sha1; } - public LicenseDownloadResult withFile( final File otherFile ) + public LicenseDownloadResult withFile( File otherFile ) { - return new LicenseDownloadResult( otherFile, this.sha1, this.preferredFileName, this.errorMessage ); + return new LicenseDownloadResult( otherFile, sha1, preferredFileName, errorMessage ); } } diff --git a/src/test/java/org/codehaus/mojo/license/download/LicenseSummaryTest.java b/src/test/java/org/codehaus/mojo/license/download/LicenseSummaryTest.java index 6aec58e57..058e922f2 100644 --- a/src/test/java/org/codehaus/mojo/license/download/LicenseSummaryTest.java +++ b/src/test/java/org/codehaus/mojo/license/download/LicenseSummaryTest.java @@ -1,5 +1,15 @@ package org.codehaus.mojo.license.download; +import org.codehaus.mojo.license.Eol; +import org.codehaus.mojo.license.download.LicenseSummaryReader; +import org.codehaus.mojo.license.download.LicenseSummaryWriter; +import org.junit.Assert; +import org.junit.Test; +import org.xml.sax.SAXException; + +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.TransformerException; +import javax.xml.transform.TransformerFactoryConfigurationError; import java.io.File; import java.io.FileInputStream; import java.io.IOException; @@ -7,15 +17,6 @@ import java.util.ArrayList; import java.util.List; -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.transform.TransformerException; -import javax.xml.transform.TransformerFactoryConfigurationError; - -import org.codehaus.mojo.license.Eol; -import org.junit.Assert; -import org.junit.Test; -import org.xml.sax.SAXException; - /** * @since 1.0 */ @@ -33,19 +34,19 @@ public class LicenseSummaryTest public void testReadLicenseSummary() throws IOException, SAXException, ParserConfigurationException { - final File licenseSummaryFile = new File( "src/test/resources/license-summary-test.xml" ); + File licenseSummaryFile = new File( "src/test/resources/license-summary-test.xml" ); Assert.assertTrue( licenseSummaryFile.exists() ); - final FileInputStream fis = new FileInputStream( licenseSummaryFile ); - final List list = LicenseSummaryReader.parseLicenseSummary( fis ); + FileInputStream fis = new FileInputStream( licenseSummaryFile ); + List list = LicenseSummaryReader.parseLicenseSummary( fis ); fis.close(); - final ProjectLicenseInfo dep = list.get( 0 ); + ProjectLicenseInfo dep = list.get( 0 ); Assert.assertEquals( "org.codehaus.mojo", dep.getGroupId() ); Assert.assertEquals( "junk", dep.getArtifactId() ); Assert.assertEquals( "1.1", dep.getVersion() ); - final List licenses = dep.getLicenses(); + List licenses = dep.getLicenses(); Assert.assertEquals( 1, licenses.size() ); - final ProjectLicense lic0 = dep.getLicenses().get(0); + ProjectLicense lic0 = dep.getLicenses().get(0); Assert.assertEquals( "lgpl", lic0.getName() ); Assert.assertEquals( "http://www.gnu.org/licenses/lgpl-3.0.txt", lic0.getUrl() ); Assert.assertEquals( "lgpl-3.0.txt", lic0.getFile() ); @@ -68,11 +69,11 @@ public void testWriteReadLicenseSummary() throws IOException, SAXException, ParserConfigurationException, TransformerFactoryConfigurationError, TransformerException { - final List licSummary = new ArrayList<>(); - final ProjectLicenseInfo dep1 = new ProjectLicenseInfo( "org.test", "test1", "1.0" ); - final ProjectLicenseInfo dep2 = new ProjectLicenseInfo( "org.test", "test2", "2.0" ); + List licSummary = new ArrayList<>(); + ProjectLicenseInfo dep1 = new ProjectLicenseInfo( "org.test", "test1", "1.0" ); + ProjectLicenseInfo dep2 = new ProjectLicenseInfo( "org.test", "test2", "2.0" ); - final ProjectLicense lic = new ProjectLicense(); + ProjectLicense lic = new ProjectLicense(); lic.setName( "lgpl" ); lic.setUrl( "http://www.gnu.org/licenses/lgpl-3.0.txt" ); lic.setFile( "lgpl-3.0.txt" ); @@ -84,22 +85,22 @@ public void testWriteReadLicenseSummary() licSummary.add( dep2 ); { - final File licenseSummaryFile = File.createTempFile( "licSummary", "tmp" ); + File licenseSummaryFile = File.createTempFile( "licSummary", "tmp" ); LicenseSummaryWriter.writeLicenseSummary( licSummary, licenseSummaryFile, StandardCharsets.UTF_8, Eol.LF, true ); Assert.assertTrue( licenseSummaryFile.exists() ); - final FileInputStream fis = new FileInputStream( licenseSummaryFile ); - final List list = LicenseSummaryReader.parseLicenseSummary( fis ); + FileInputStream fis = new FileInputStream( licenseSummaryFile ); + List list = LicenseSummaryReader.parseLicenseSummary( fis ); fis.close(); - final ProjectLicenseInfo dep = list.get( 0 ); + ProjectLicenseInfo dep = list.get( 0 ); Assert.assertEquals( "org.test", dep.getGroupId() ); Assert.assertEquals( "test1", dep.getArtifactId() ); Assert.assertEquals( "1.0", dep.getVersion() ); - final List licenses = dep.getLicenses(); + List licenses = dep.getLicenses(); Assert.assertEquals( 1, licenses.size() ); - final ProjectLicense lic0 = dep.getLicenses().get(0); + ProjectLicense lic0 = dep.getLicenses().get(0); Assert.assertEquals( "lgpl", lic0.getName() ); Assert.assertEquals( "http://www.gnu.org/licenses/lgpl-3.0.txt", lic0.getUrl() ); Assert.assertEquals( "lgpl-3.0.txt", lic0.getFile() ); @@ -107,22 +108,22 @@ public void testWriteReadLicenseSummary() } { - final File licenseSummaryFile = File.createTempFile( "licSummaryNoVersion", "tmp" ); + File licenseSummaryFile = File.createTempFile( "licSummaryNoVersion", "tmp" ); LicenseSummaryWriter.writeLicenseSummary( licSummary, licenseSummaryFile, StandardCharsets.UTF_8, Eol.LF, false ); Assert.assertTrue( licenseSummaryFile.exists() ); - final FileInputStream fis = new FileInputStream( licenseSummaryFile ); - final List list = LicenseSummaryReader.parseLicenseSummary( fis ); + FileInputStream fis = new FileInputStream( licenseSummaryFile ); + List list = LicenseSummaryReader.parseLicenseSummary( fis ); fis.close(); - final ProjectLicenseInfo dep = list.get( 0 ); + ProjectLicenseInfo dep = list.get( 0 ); Assert.assertEquals( "org.test", dep.getGroupId() ); Assert.assertEquals( "test1", dep.getArtifactId() ); Assert.assertNull( dep.getVersion() ); - final List licenses = dep.getLicenses(); + List licenses = dep.getLicenses(); Assert.assertEquals( 1, licenses.size() ); - final ProjectLicense lic0 = dep.getLicenses().get(0); + ProjectLicense lic0 = dep.getLicenses().get(0); Assert.assertEquals( "lgpl", lic0.getName() ); Assert.assertEquals( "http://www.gnu.org/licenses/lgpl-3.0.txt", lic0.getUrl() ); Assert.assertEquals( "lgpl-3.0.txt", lic0.getFile() ); From 0f5af5279f524fe4e395450cc58ba9a5d2ea5ef0 Mon Sep 17 00:00:00 2001 From: Dan Hayworth Date: Tue, 9 May 2023 22:10:44 +1200 Subject: [PATCH 3/3] Fixes Cookie Header error --- .../org/codehaus/mojo/license/download/LicenseDownloader.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/org/codehaus/mojo/license/download/LicenseDownloader.java b/src/main/java/org/codehaus/mojo/license/download/LicenseDownloader.java index 3a29d9c0b..944cdeb60 100644 --- a/src/main/java/org/codehaus/mojo/license/download/LicenseDownloader.java +++ b/src/main/java/org/codehaus/mojo/license/download/LicenseDownloader.java @@ -53,6 +53,7 @@ import org.apache.http.auth.Credentials; import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.client.CredentialsProvider; +import org.apache.http.client.config.CookieSpecs; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.config.RequestConfig.Builder; import org.apache.http.client.methods.CloseableHttpResponse; @@ -97,6 +98,7 @@ public LicenseDownloader( Proxy proxy, int connectTimeout, int socketTimeout, in final Builder configBuilder = RequestConfig.copy( RequestConfig.DEFAULT ) // .setConnectTimeout( connectTimeout ) // .setSocketTimeout( socketTimeout ) // + .setCookieSpec(CookieSpecs.STANDARD) .setConnectionRequestTimeout( connectionRequestTimeout ); if ( proxy != null )