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;
}
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();
}
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);
}
}
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);
}
}
}
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();
}
Aggregations