Search in sources :

Example 1 with LayoutException

use of org.apache.archiva.repository.content.LayoutException in project archiva by apache.

the class ManagedDefaultRepositoryContent method toItem.

@Override
public ContentItem toItem(String path) throws LayoutException {
    StorageAsset asset = getRepository().getAsset(path);
    ContentItem item = getItemFromPath(asset);
    if (item instanceof DataItem) {
        Artifact artifact = adaptItem(Artifact.class, item);
        if (asset.getParent() == null) {
            throw new LayoutException("Path too short for maven artifact " + path);
        }
        String version = asset.getParent().getName();
        if (asset.getParent().getParent() == null) {
            throw new LayoutException("Path too short for maven artifact " + path);
        }
        String project = item.getAsset().getParent().getParent().getName();
        DataItem dataItem = (DataItem) item;
        if (StringUtils.isEmpty(dataItem.getExtension())) {
            throw new LayoutException("Missing type on maven artifact");
        }
        if (!project.equals(artifact.getId())) {
            throw new LayoutException("The maven artifact id " + artifact.getId() + " does not match the project id: " + project);
        }
        boolean versionIsGenericSnapshot = VersionUtil.isGenericSnapshot(version);
        boolean artifactVersionIsSnapshot = VersionUtil.isSnapshot(artifact.getArtifactVersion());
        if (versionIsGenericSnapshot && !artifactVersionIsSnapshot) {
            throw new LayoutException("The maven artifact has no snapshot version in snapshot directory " + dataItem);
        }
        if (!versionIsGenericSnapshot && artifactVersionIsSnapshot) {
            throw new LayoutException("The maven artifact version " + artifact.getArtifactVersion() + " is a snapshot version but inside a non snapshot directory " + version);
        }
        if (!versionIsGenericSnapshot && !version.equals(artifact.getArtifactVersion())) {
            throw new LayoutException("The maven artifact version " + artifact.getArtifactVersion() + " does not match the version directory " + version);
        }
    }
    return item;
}
Also used : DataItem(org.apache.archiva.repository.content.DataItem) LayoutException(org.apache.archiva.repository.content.LayoutException) StorageAsset(org.apache.archiva.repository.storage.StorageAsset) ContentItem(org.apache.archiva.repository.content.ContentItem) ArchivaContentItem(org.apache.archiva.repository.content.base.ArchivaContentItem) Artifact(org.apache.archiva.repository.content.Artifact)

Example 2 with LayoutException

use of org.apache.archiva.repository.content.LayoutException in project archiva by apache.

the class ManagedDefaultRepositoryContent method createArtifactFromPath.

private Artifact createArtifactFromPath(final StorageAsset artifactPath) throws LayoutRuntimeException {
    if (artifactPath == null) {
        throw new LayoutRuntimeException("Path null is not valid for artifact");
    }
    final Version version;
    try {
        version = getVersionFromArtifactPath(artifactPath);
    } catch (LayoutException e) {
        throw new LayoutRuntimeException(e.getMessage(), e);
    }
    final ArtifactInfo info = getArtifactInfoFromPath(version.getId(), artifactPath);
    return org.apache.archiva.repository.content.base.ArchivaArtifact.withAsset(artifactPath).withVersion(version).withId(info.id).withClassifier(info.classifier).withRemainder(info.remainder).withType(info.type).withArtifactVersion(info.version).withContentType(info.contentType).withArtifactType(info.artifactType).build();
}
Also used : LayoutRuntimeException(org.apache.archiva.repository.content.LayoutRuntimeException) Version(org.apache.archiva.repository.content.Version) ArchivaVersion(org.apache.archiva.repository.content.base.ArchivaVersion) LayoutException(org.apache.archiva.repository.content.LayoutException)

Example 3 with LayoutException

use of org.apache.archiva.repository.content.LayoutException in project archiva by apache.

the class ManagedDefaultRepositoryContent method getArtifact.

@Override
public Artifact getArtifact(final ItemSelector selectorArg) throws ContentAccessException {
    ItemSelector selector = selectorArg;
    if (!selectorArg.hasProjectId()) {
        throw new IllegalArgumentException("Project id must be set");
    }
    if (!selectorArg.hasVersion()) {
        if (selectorArg.hasArtifactVersion() && VersionUtil.isSnapshot(selectorArg.getArtifactVersion())) {
            selector = ArchivaItemSelector.builder().withSelector(selectorArg).withVersion(VersionUtil.getBaseVersion(selectorArg.getArtifactVersion())).build();
        } else if (selectorArg.hasArtifactVersion()) {
            selector = ArchivaItemSelector.builder().withSelector(selectorArg).withVersion(selectorArg.getArtifactVersion()).build();
        } else {
            throw new IllegalArgumentException("Version must be set");
        }
    }
    if (!selectorArg.hasArtifactId()) {
        throw new IllegalArgumentException("Artifact id must be set");
    }
    final StorageAsset artifactDir = getAsset(selector.getNamespace(), selector.getProjectId(), selector.getVersion());
    final String artifactVersion = mavenContentHelper.getArtifactVersion(artifactDir, selector);
    final String classifier = MavenContentHelper.getClassifier(selector);
    final String extension = MavenContentHelper.getArtifactExtension(selector);
    final String artifactId = StringUtils.isEmpty(selector.getArtifactId()) ? selector.getProjectId() : selector.getArtifactId();
    final String fileName = MavenContentHelper.getArtifactFileName(artifactId, artifactVersion, classifier, extension);
    final StorageAsset path = getAsset(selector.getNamespace(), selector.getProjectId(), selector.getVersion(), fileName);
    try {
        return getArtifactFromPath(path);
    } catch (LayoutException e) {
        throw new IllegalArgumentException("The selector is not valid " + e.getMessage(), e);
    }
}
Also used : ItemSelector(org.apache.archiva.repository.content.ItemSelector) ArchivaItemSelector(org.apache.archiva.repository.content.base.ArchivaItemSelector) LayoutException(org.apache.archiva.repository.content.LayoutException) StorageAsset(org.apache.archiva.repository.storage.StorageAsset)

Example 4 with LayoutException

use of org.apache.archiva.repository.content.LayoutException in project archiva by apache.

the class ManagedDefaultRepositoryContent method applyCharacteristic.

@Override
public <T extends ContentItem> T applyCharacteristic(Class<T> clazz, ContentItem item) throws LayoutException {
    if (item.getAsset().isLeaf()) {
        if (clazz.isAssignableFrom(Artifact.class)) {
            Artifact artifact = getArtifactFromPath(item.getAsset());
            item.setCharacteristic(Artifact.class, artifact);
            return (T) artifact;
        } else {
            throw new LayoutException("Could not adapt file to clazz " + clazz);
        }
    } else {
        if (clazz.isAssignableFrom(Version.class)) {
            Version version = getVersionFromPath(item.getAsset());
            item.setCharacteristic(Version.class, version);
            return (T) version;
        } else if (clazz.isAssignableFrom(Project.class)) {
            Project project = getProjectFromPath(item.getAsset());
            item.setCharacteristic(Project.class, project);
            return (T) project;
        } else if (clazz.isAssignableFrom(Namespace.class)) {
            Namespace ns = getNamespaceFromPath(item.getAsset());
            item.setCharacteristic(Namespace.class, ns);
            return (T) ns;
        } else {
            throw new LayoutException("Cannot adapt directory to clazz " + clazz);
        }
    }
}
Also used : ArchivaProject(org.apache.archiva.repository.content.base.ArchivaProject) Project(org.apache.archiva.repository.content.Project) LayoutException(org.apache.archiva.repository.content.LayoutException) Version(org.apache.archiva.repository.content.Version) ArchivaVersion(org.apache.archiva.repository.content.base.ArchivaVersion) Artifact(org.apache.archiva.repository.content.Artifact) ArchivaNamespace(org.apache.archiva.repository.content.base.ArchivaNamespace) Namespace(org.apache.archiva.repository.content.Namespace)

Example 5 with LayoutException

use of org.apache.archiva.repository.content.LayoutException in project archiva by apache.

the class DefaultMavenManagedRepositoryService method copyArtifact.

@Override
public Response copyArtifact(String srcRepositoryId, String dstRepositoryId, String path) throws ArchivaRestServiceException {
    final AuditInformation auditInformation = getAuditInformation();
    final String userName = auditInformation.getUser().getUsername();
    if (StringUtils.isEmpty(userName)) {
        httpServletResponse.setHeader("WWW-Authenticate", "Bearer realm=\"archiva\"");
        throw new ArchivaRestServiceException(ErrorMessage.of(ErrorKeys.NOT_AUTHENTICATED), 401);
    }
    ManagedRepository srcRepo = repositoryRegistry.getManagedRepository(srcRepositoryId);
    if (srcRepo == null) {
        throw new ArchivaRestServiceException(ErrorMessage.of(ErrorKeys.REPOSITORY_NOT_FOUND, srcRepositoryId), 404);
    }
    ManagedRepository dstRepo = repositoryRegistry.getManagedRepository(dstRepositoryId);
    if (dstRepo == null) {
        throw new ArchivaRestServiceException(ErrorMessage.of(ErrorKeys.REPOSITORY_NOT_FOUND, dstRepositoryId), 404);
    }
    checkAuthority(auditInformation.getUser().getUsername(), srcRepositoryId, dstRepositoryId);
    try {
        ContentItem srcItem = srcRepo.getContent().toItem(path);
        ContentItem dstItem = dstRepo.getContent().toItem(path);
        if (!srcItem.getAsset().exists()) {
            throw new ArchivaRestServiceException(ErrorMessage.of(ErrorKeys.ARTIFACT_NOT_FOUND, srcRepositoryId, path), 404);
        }
        if (dstItem.getAsset().exists()) {
            throw new ArchivaRestServiceException(ErrorMessage.of(ErrorKeys.ARTIFACT_EXISTS_AT_DEST, srcRepositoryId, path), 400);
        }
        FsStorageUtil.copyAsset(srcItem.getAsset(), dstItem.getAsset(), true);
    } catch (LayoutException e) {
        throw new ArchivaRestServiceException(ErrorMessage.of(ErrorKeys.REPOSITORY_LAYOUT_ERROR, e.getMessage()));
    } catch (IOException e) {
        throw new ArchivaRestServiceException(ErrorMessage.of(ErrorKeys.ARTIFACT_COPY_ERROR, e.getMessage()));
    }
    return Response.ok().build();
}
Also used : AuditInformation(org.apache.archiva.admin.model.AuditInformation) ManagedRepository(org.apache.archiva.repository.ManagedRepository) MavenManagedRepository(org.apache.archiva.rest.api.v2.model.MavenManagedRepository) LayoutException(org.apache.archiva.repository.content.LayoutException) ArchivaRestServiceException(org.apache.archiva.rest.api.v2.svc.ArchivaRestServiceException) IOException(java.io.IOException) ContentItem(org.apache.archiva.repository.content.ContentItem)

Aggregations

LayoutException (org.apache.archiva.repository.content.LayoutException)29 ManagedRepositoryContent (org.apache.archiva.repository.ManagedRepositoryContent)14 StorageAsset (org.apache.archiva.repository.storage.StorageAsset)14 BaseRepositoryContentLayout (org.apache.archiva.repository.content.BaseRepositoryContentLayout)13 Artifact (org.apache.archiva.repository.content.Artifact)12 ArchivaItemSelector (org.apache.archiva.repository.content.base.ArchivaItemSelector)12 IOException (java.io.IOException)9 ManagedRepository (org.apache.archiva.repository.ManagedRepository)8 ContentItem (org.apache.archiva.repository.content.ContentItem)8 ItemSelector (org.apache.archiva.repository.content.ItemSelector)8 ContentAccessException (org.apache.archiva.repository.content.ContentAccessException)6 Version (org.apache.archiva.repository.content.Version)6 DavException (org.apache.jackrabbit.webdav.DavException)6 ArrayList (java.util.ArrayList)5 ArchivaRepositoryMetadata (org.apache.archiva.model.ArchivaRepositoryMetadata)4 ItemNotFoundException (org.apache.archiva.repository.content.ItemNotFoundException)4 Project (org.apache.archiva.repository.content.Project)4 DavResource (org.apache.jackrabbit.webdav.DavResource)4 Path (java.nio.file.Path)3 HashSet (java.util.HashSet)3