Files
ban-maven-plugin/src/main/java/com/inteligr8/maven/ban/BanConfigurationDownloader.java
2025-07-09 10:05:28 -04:00

123 lines
5.1 KiB
Java

/*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.inteligr8.maven.ban;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.MojoFailureException;
import org.codehaus.plexus.util.xml.Xpp3Dom;
import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import org.eclipse.aether.impl.ArtifactResolver;
import org.eclipse.aether.impl.VersionRangeResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* This class parses the a configuration file that conforms with the
* specification defined by this plugin.
*/
public class BanConfigurationDownloader extends AbstractBanConfiguration {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
/**
* This constructs the downloader based on an accessible URL.
*
* @param session A Maven session object.
* @param artifactResolver A Maven Aether artifact resolver service.
* @param versionRangeResolver A Maven Aether version range resolver service.
* @param url The URL of a ban configuration file.
* @throws IOException The file could not be downloaded for any number of reasons.
* @throws MojoFailureException The file could not be parsed due to bad formatting or unexpected response.
*/
public BanConfigurationDownloader(MavenSession session, ArtifactResolver artifactResolver, VersionRangeResolver versionRangeResolver, String url) throws IOException, MojoFailureException {
super(session, artifactResolver, versionRangeResolver);
try {
Xpp3Dom rootDom = this.load(new URL(url));
this.init(rootDom);
} catch (XmlPullParserException xppe) {
throw new MojoFailureException(xppe.getMessage(), xppe);
}
}
/**
* This constructs the downloader based on a file object.
*
* @param session A Maven session object.
* @param artifactResolver A Maven Aether artifact resolver service.
* @param versionRangeResolver A Maven Aether version range resolver service.
* @param file A file, typically local, though technically may be remote.
* @throws IOException The file could not be read for any number of reasons.
* @throws MojoFailureException The file could not be parsed due to bad formatting.
*/
public BanConfigurationDownloader(MavenSession session, ArtifactResolver artifactResolver, VersionRangeResolver versionRangeResolver, File file) throws IOException, MojoFailureException {
super(session, artifactResolver, versionRangeResolver);
try {
Xpp3Dom rootDom = this.load(file);
this.init(rootDom);
} catch (XmlPullParserException xppe) {
throw new MojoFailureException(xppe.getMessage(), xppe);
}
}
/**
* This method loads the configuration from the specified URL.
*
* @param url A URL of a ban configuration file.
* @return A Maven Plexus XML DOM object.
* @throws IOException The file could not be downloaded for any number of reasons.
* @throws MojoFailureException The file could not be parsed due to bad formatting or unexpected response.
*/
private Xpp3Dom load(URL url) throws IOException, XmlPullParserException {
InputStream istream = url.openStream();
BufferedInputStream bistream = new BufferedInputStream(istream, 16384);
try {
this.logger.debug("Downloading configuration: {}", url);
return Xpp3DomBuilder.build(bistream, "utf-8");
} finally {
bistream.close();
}
}
/**
* This method loads the configuration from the specified file.
*
* @param file A file, typically local, though technically may be remote.
* @return A Maven Plexus XML DOM object.
* @throws IOException The file could not be read for any number of reasons.
* @throws MojoFailureException The file could not be parsed due to bad formatting.
*/
private Xpp3Dom load(File file) throws IOException, XmlPullParserException {
FileInputStream fistream = new FileInputStream(file);
BufferedInputStream bistream = new BufferedInputStream(fistream, 16384);
try {
this.logger.debug("Downloading configuration: {}", file);
return Xpp3DomBuilder.build(bistream, "utf-8");
} finally {
bistream.close();
}
}
}