Class LicenseDownloader

java.lang.Object
org.codehaus.mojo.license.download.LicenseDownloader
All Implemented Interfaces:
AutoCloseable

public class LicenseDownloader extends Object implements AutoCloseable
Utilities for downloading remote license files.
Since:
1.0
Author:
pgier
  • Constructor Details

    • LicenseDownloader

      public LicenseDownloader(org.apache.maven.settings.Proxy proxy, int connectTimeout, int socketTimeout, int connectionRequestTimeout, Map<String, SpdxLicenseList.Attachments.ContentSanitizer> contentSanitizers, Charset charset)
      Creates a new LicenseDownloader without authentication. Proxy settings and timeouts are still applied.
    • LicenseDownloader

      public LicenseDownloader(org.apache.maven.settings.Proxy proxy, int connectTimeout, int socketTimeout, int connectionRequestTimeout, Map<String, SpdxLicenseList.Attachments.ContentSanitizer> contentSanitizers, Charset charset, String userName, String password, String serverUrl, Map<String,String> httpHeaders)
      Creates a new LicenseDownloader with optional authentication.

      Credentials (userName/password) and httpHeaders are sent only to URLs that start with serverUrl, preventing credential leakage to third-party license servers.

      Parameters:
      proxy - optional proxy configuration from settings.xml
      connectTimeout - HTTP connection timeout in milliseconds
      socketTimeout - HTTP socket (read) timeout in milliseconds
      connectionRequestTimeout - HTTP connection-pool request timeout in milliseconds
      contentSanitizers - optional content sanitizers keyed by an id string
      charset - charset used when reading text license content
      userName - username for preemptive Basic Authentication, or null/empty to disable
      password - password for preemptive Basic Authentication, or null/empty to disable
      serverUrl - URL prefix that a license URL must start with in order to receive credentials; null disables authentication for all URLs
      httpHeaders - additional HTTP headers sent only to matching URLs (e.g. Authorization: Bearer)
  • Method Details

    • setBasedir

      public void setBasedir(Path basedir)
      Sets the directory against which a relative file: license URL is resolved. Without it, such a URL is resolved against the working directory.
      Parameters:
      basedir - the directory, usually the basedir of the project
      Since:
      2.8.1
    • setBundledTextsMode

      public void setBundledTextsMode(BundledLicenseTexts.Mode mode)
      Sets which well-known license URLs are answered from a text bundled with the plugin instead of a request. The default is BundledLicenseTexts.Mode.none, which keeps the behavior of this class unchanged.
      Parameters:
      mode - the mode
      Since:
      2.8.0
    • setRejectHtml

      public void setRejectHtml(boolean rejectHtml)
      Sets whether a response that is an HTML page, by Content-Type or by its first bytes, is rejected instead of being saved as a license text; off by default, because many licenses are published only as HTML pages.
      Parameters:
      rejectHtml - whether HTML responses are rejected
      Since:
      2.8.1
    • setLicenseCache

      public void setLicenseCache(PersistentLicenseCache licenseCache)
      Sets the persistent cache of downloaded license texts; none by default. Only http and https downloads that carry no credentials go through it.
      Parameters:
      licenseCache - the cache, or null for none
      Since:
      2.8.0
    • getBundledTextsServed

      public int getBundledTextsServed()
      Returns:
      the number of downloads that were answered from a bundled text
      Since:
      2.8.0
    • downloadLicense

      public LicenseDownloader.LicenseDownloadResult downloadLicense(String licenseUrlString, FileNameEntry fileNameEntry) throws IOException, URISyntaxException, org.apache.maven.plugin.MojoFailureException
      Downloads a license file from the given licenseUrlString stores it locally and returns the local path where the license file was stored. Note that the outputFile name can be further modified by this method, esp. the file extension can be adjusted based on the mime type of the HTTP response.
      Parameters:
      licenseUrlString - the URL
      fileNameEntry - a hint where to store the license file
      Returns:
      the path to the file where the downloaded license file was stored
      Throws:
      IOException
      URISyntaxException
      org.apache.maven.plugin.MojoFailureException
    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Throws:
      IOException
    • calculateStringChecksum

      public static String calculateStringChecksum(String contentString)