Search in sources :

Example 6 with XMLException

use of org.apache.archiva.xml.XMLException in project archiva by apache.

the class MetadataTools method updateMetadata.

/**
 * Update the metadata to represent the all versions/plugins of
 * the provided groupId:artifactId project or group reference,
 * based off of information present in the repository,
 * the maven-metadata.xml files, and the proxy/repository specific
 * metadata file contents.
 * <p>
 * We must treat this as a group or a project metadata file as there is no way to know in advance
 *
 * @param managedRepository the managed repository where the metadata is kept.
 * @param reference         the reference to update.
 * @throws LayoutException
 * @throws RepositoryMetadataException
 * @throws IOException
 * @throws ContentNotFoundException
 * @deprecated
 */
public void updateMetadata(ManagedRepositoryContent managedRepository, ProjectReference reference) throws LayoutException, RepositoryMetadataException, IOException, ContentNotFoundException {
    Path metadataFile = Paths.get(managedRepository.getRepoRoot(), toPath(reference));
    long lastUpdated = getExistingLastUpdated(metadataFile);
    ArchivaRepositoryMetadata metadata = new ArchivaRepositoryMetadata();
    metadata.setGroupId(reference.getGroupId());
    metadata.setArtifactId(reference.getArtifactId());
    // Gather up all versions found in the managed repository.
    Set<String> allVersions = managedRepository.getVersions(reference);
    // Gather up all plugins found in the managed repository.
    // TODO: do we know this information instead?
    // Set<Plugin> allPlugins = managedRepository.getPlugins( reference );
    Set<Plugin> allPlugins;
    if (Files.exists(metadataFile)) {
        try {
            allPlugins = new LinkedHashSet<Plugin>(MavenMetadataReader.read(metadataFile).getPlugins());
        } catch (XMLException e) {
            throw new RepositoryMetadataException(e.getMessage(), e);
        }
    } else {
        allPlugins = new LinkedHashSet<Plugin>();
    }
    // Does this repository have a set of remote proxied repositories?
    Set<String> proxiedRepoIds = this.proxies.get(managedRepository.getId());
    if (CollectionUtils.isNotEmpty(proxiedRepoIds)) {
        // Add in the proxied repo version ids too.
        Iterator<String> it = proxiedRepoIds.iterator();
        while (it.hasNext()) {
            String proxyId = it.next();
            ArchivaRepositoryMetadata proxyMetadata = readProxyMetadata(managedRepository, reference, proxyId);
            if (proxyMetadata != null) {
                allVersions.addAll(proxyMetadata.getAvailableVersions());
                allPlugins.addAll(proxyMetadata.getPlugins());
                long proxyLastUpdated = getLastUpdated(proxyMetadata);
                lastUpdated = Math.max(lastUpdated, proxyLastUpdated);
            }
        }
    }
    if (!allVersions.isEmpty()) {
        updateMetadataVersions(allVersions, metadata);
    } else {
        // Add the plugins to the metadata model.
        metadata.setPlugins(new ArrayList<>(allPlugins));
        // artifact ID was actually the last part of the group
        metadata.setGroupId(metadata.getGroupId() + "." + metadata.getArtifactId());
        metadata.setArtifactId(null);
    }
    if (lastUpdated > 0) {
        metadata.setLastUpdatedTimestamp(toLastUpdatedDate(lastUpdated));
    }
    // Save the metadata model to disk.
    RepositoryMetadataWriter.write(metadata, metadataFile);
    ChecksummedFile checksum = new ChecksummedFile(metadataFile);
    checksum.fixChecksums(algorithms);
}
Also used : Path(java.nio.file.Path) ChecksummedFile(org.apache.archiva.checksum.ChecksummedFile) XMLException(org.apache.archiva.xml.XMLException) ArchivaRepositoryMetadata(org.apache.archiva.model.ArchivaRepositoryMetadata) Plugin(org.apache.archiva.model.Plugin)

Example 7 with XMLException

use of org.apache.archiva.xml.XMLException in project archiva by apache.

the class MetadataTools method readProxyMetadata.

public ArchivaRepositoryMetadata readProxyMetadata(ManagedRepositoryContent managedRepository, String logicalResource, String proxyId) {
    String metadataPath = getRepositorySpecificName(proxyId, logicalResource);
    Path metadataFile = Paths.get(managedRepository.getRepoRoot(), metadataPath);
    if (!Files.exists(metadataFile) || !Files.isRegularFile(metadataFile)) {
        // Nothing to do. return null.
        return null;
    }
    try {
        return MavenMetadataReader.read(metadataFile);
    } catch (XMLException e) {
        // TODO: [monitor] consider a monitor for this event.
        // TODO: consider a read-redo on monitor return code?
        log.warn("Unable to read metadata: {}", metadataFile.toAbsolutePath(), e);
        return null;
    }
}
Also used : Path(java.nio.file.Path) XMLException(org.apache.archiva.xml.XMLException)

Example 8 with XMLException

use of org.apache.archiva.xml.XMLException in project archiva by apache.

the class MetadataTools method readProxyMetadata.

public ArchivaRepositoryMetadata readProxyMetadata(ManagedRepositoryContent managedRepository, ProjectReference reference, String proxyId) {
    String metadataPath = getRepositorySpecificName(proxyId, toPath(reference));
    Path metadataFile = Paths.get(managedRepository.getRepoRoot(), metadataPath);
    if (!Files.exists(metadataFile) || !Files.isRegularFile(metadataFile)) {
        // Nothing to do. return null.
        return null;
    }
    try {
        return MavenMetadataReader.read(metadataFile);
    } catch (XMLException e) {
        // TODO: [monitor] consider a monitor for this event.
        // TODO: consider a read-redo on monitor return code?
        log.warn("Unable to read metadata: {}", metadataFile.toAbsolutePath(), e);
        return null;
    }
}
Also used : Path(java.nio.file.Path) XMLException(org.apache.archiva.xml.XMLException)

Example 9 with XMLException

use of org.apache.archiva.xml.XMLException in project archiva by apache.

the class RepositoryMetadataWriter method write.

public static void write(ArchivaRepositoryMetadata metadata, Writer writer) throws RepositoryMetadataException {
    Document doc = DocumentHelper.createDocument();
    Element root = DocumentHelper.createElement("metadata");
    doc.setRootElement(root);
    addOptionalElementText(root, "groupId", metadata.getGroupId());
    addOptionalElementText(root, "artifactId", metadata.getArtifactId());
    addOptionalElementText(root, "version", metadata.getVersion());
    if (CollectionUtils.isNotEmpty(metadata.getPlugins())) {
        Element plugins = root.addElement("plugins");
        List<Plugin> pluginList = metadata.getPlugins();
        Collections.sort(pluginList, PluginComparator.INSTANCE);
        for (Plugin plugin : metadata.getPlugins()) {
            Element p = plugins.addElement("plugin");
            p.addElement("prefix").setText(plugin.getPrefix());
            p.addElement("artifactId").setText(plugin.getArtifactId());
            addOptionalElementText(p, "name", plugin.getName());
        }
    }
    if (// 
    CollectionUtils.isNotEmpty(metadata.getAvailableVersions()) || // 
    StringUtils.isNotBlank(metadata.getReleasedVersion()) || // 
    StringUtils.isNotBlank(metadata.getLatestVersion()) || // 
    StringUtils.isNotBlank(metadata.getLastUpdated()) || (metadata.getSnapshotVersion() != null)) {
        Element versioning = root.addElement("versioning");
        addOptionalElementText(versioning, "latest", metadata.getLatestVersion());
        addOptionalElementText(versioning, "release", metadata.getReleasedVersion());
        if (metadata.getSnapshotVersion() != null) {
            Element snapshot = versioning.addElement("snapshot");
            String bnum = String.valueOf(metadata.getSnapshotVersion().getBuildNumber());
            addOptionalElementText(snapshot, "buildNumber", bnum);
            addOptionalElementText(snapshot, "timestamp", metadata.getSnapshotVersion().getTimestamp());
        }
        if (CollectionUtils.isNotEmpty(metadata.getAvailableVersions())) {
            Element versions = versioning.addElement("versions");
            Iterator<String> it = metadata.getAvailableVersions().iterator();
            while (it.hasNext()) {
                String version = it.next();
                versions.addElement("version").setText(version);
            }
        }
        addOptionalElementText(versioning, "lastUpdated", metadata.getLastUpdated());
    }
    try {
        XMLWriter.write(doc, writer);
    } catch (XMLException e) {
        throw new RepositoryMetadataException("Unable to write xml contents to writer: " + e.getMessage(), e);
    }
}
Also used : XMLException(org.apache.archiva.xml.XMLException) Element(org.dom4j.Element) Document(org.dom4j.Document) Plugin(org.apache.archiva.model.Plugin)

Example 10 with XMLException

use of org.apache.archiva.xml.XMLException in project archiva by apache.

the class ArchivaDavResourceFactory method evaluatePathWithVersion.

private // 
String evaluatePathWithVersion(// 
ArchivaDavResourceLocator archivaLocator, // 
ManagedRepositoryContent managedRepositoryContent, String contextPath) throws DavException {
    String layout = managedRepositoryContent.getRepository() == null ? "default" : managedRepositoryContent.getRepository().getLayout();
    RepositoryStorage repositoryStorage = this.applicationContext.getBean("repositoryStorage#" + layout, RepositoryStorage.class);
    try {
        return // 
        repositoryStorage.getFilePathWithVersion(// 
        archivaLocator.getResourcePath(), managedRepositoryContent);
    } catch (RelocationException e) {
        String path = e.getPath();
        log.debug("Relocation to {}", path);
        throw new BrowserRedirectException(addHrefPrefix(contextPath, path), e.getRelocationType());
    } catch (XMLException e) {
        log.error(e.getMessage(), e);
        throw new DavException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e);
    }
}
Also used : RepositoryStorage(org.apache.archiva.metadata.repository.storage.RepositoryStorage) XMLException(org.apache.archiva.xml.XMLException) RelocationException(org.apache.archiva.metadata.repository.storage.RelocationException) DavException(org.apache.jackrabbit.webdav.DavException)

Aggregations

XMLException (org.apache.archiva.xml.XMLException)13 Path (java.nio.file.Path)11 ArchivaRepositoryMetadata (org.apache.archiva.model.ArchivaRepositoryMetadata)8 ArrayList (java.util.ArrayList)3 RepositoryAdminException (org.apache.archiva.admin.model.RepositoryAdminException)3 ManagedRepository (org.apache.archiva.admin.model.beans.ManagedRepository)2 Plugin (org.apache.archiva.model.Plugin)2 SnapshotVersion (org.apache.archiva.model.SnapshotVersion)2 ManagedRepository (org.apache.archiva.repository.ManagedRepository)2 ManagedRepositoryContent (org.apache.archiva.repository.ManagedRepositoryContent)2 DavException (org.apache.jackrabbit.webdav.DavException)2 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 NoSuchFileException (java.nio.file.NoSuchFileException)1 HashMap (java.util.HashMap)1 List (java.util.List)1 NetworkProxy (org.apache.archiva.admin.model.beans.NetworkProxy)1 ProxyConnector (org.apache.archiva.admin.model.beans.ProxyConnector)1 RemoteRepository (org.apache.archiva.admin.model.beans.RemoteRepository)1 ChecksummedFile (org.apache.archiva.checksum.ChecksummedFile)1