Search in sources :

Example 1 with DavException

use of org.apache.jackrabbit.webdav.DavException in project archiva by apache.

the class DavResourceTest method testDeleteNonExistantResourceShould404.

@Test
public void testDeleteNonExistantResourceShould404() throws Exception {
    Path dir = baseDir.resolve("testdir");
    try {
        DavResource directoryResource = getDavResource("/testdir", dir);
        directoryResource.getCollection().removeMember(directoryResource);
        fail("Did not throw DavException");
    } catch (DavException e) {
        assertEquals(DavServletResponse.SC_NOT_FOUND, e.getErrorCode());
    }
}
Also used : Path(java.nio.file.Path) DavResource(org.apache.jackrabbit.webdav.DavResource) DavException(org.apache.jackrabbit.webdav.DavException) Test(org.junit.Test)

Example 2 with DavException

use of org.apache.jackrabbit.webdav.DavException in project archiva by apache.

the class ArchivaDavResource method refreshLock.

@Override
public ActiveLock refreshLock(LockInfo lockInfo, String lockToken) throws DavException {
    if (!exists()) {
        throw new DavException(DavServletResponse.SC_NOT_FOUND);
    }
    ActiveLock lock = getLock(lockInfo.getType(), lockInfo.getScope());
    if (lock == null) {
        throw new DavException(DavServletResponse.SC_PRECONDITION_FAILED, "No lock with the given type/scope present on resource " + getResourcePath());
    }
    lock = lockManager.refreshLock(lockInfo, lockToken, this);
    return lock;
}
Also used : ActiveLock(org.apache.jackrabbit.webdav.lock.ActiveLock) DavException(org.apache.jackrabbit.webdav.DavException)

Example 3 with DavException

use of org.apache.jackrabbit.webdav.DavException in project archiva by apache.

the class ArchivaDavResource method addMember.

@Override
public void addMember(DavResource resource, InputContext inputContext) throws DavException {
    Path localFile = localResource.resolve(resource.getDisplayName());
    boolean exists = Files.exists(localFile);
    if (// New File
    isCollection() && inputContext.hasStream()) {
        try (OutputStream stream = Files.newOutputStream(localFile)) {
            IOUtils.copy(inputContext.getInputStream(), stream);
        } catch (IOException e) {
            throw new DavException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e);
        }
        // TODO: a bad deployment shouldn't delete an existing file - do we need to write to a temporary location first?
        long expectedContentLength = inputContext.getContentLength();
        long actualContentLength = 0;
        try {
            actualContentLength = Files.size(localFile);
        } catch (IOException e) {
            log.error("Could not get length of file {}: {}", localFile, e.getMessage(), e);
        }
        // length of -1 is given for a chunked request or unknown length, in which case we accept what was uploaded
        if (expectedContentLength >= 0 && expectedContentLength != actualContentLength) {
            String msg = "Content Header length was " + expectedContentLength + " but was " + actualContentLength;
            log.debug("Upload failed: {}", msg);
            org.apache.archiva.common.utils.FileUtils.deleteQuietly(localFile);
            throw new DavException(HttpServletResponse.SC_BAD_REQUEST, msg);
        }
        queueRepositoryTask(localFile);
        log.debug("File '{}{}(current user '{}')", resource.getDisplayName(), (exists ? "' modified " : "' created "), this.principal);
        triggerAuditEvent(resource, exists ? AuditEvent.MODIFY_FILE : AuditEvent.CREATE_FILE);
    } else if (// New directory
    !inputContext.hasStream() && isCollection()) {
        try {
            Files.createDirectories(localFile);
        } catch (IOException e) {
            log.error("Could not create directory {}: {}", localFile, e.getMessage(), e);
        }
        log.debug("Directory '{}' (current user '{}')", resource.getDisplayName(), this.principal);
        triggerAuditEvent(resource, AuditEvent.CREATE_DIR);
    } else {
        String msg = "Could not write member " + resource.getResourcePath() + " at " + getResourcePath() + " as this is not a DAV collection";
        log.debug(msg);
        throw new DavException(HttpServletResponse.SC_BAD_REQUEST, msg);
    }
}
Also used : Path(java.nio.file.Path) DavException(org.apache.jackrabbit.webdav.DavException) OutputStream(java.io.OutputStream) IOException(java.io.IOException)

Example 4 with DavException

use of org.apache.jackrabbit.webdav.DavException in project archiva by apache.

the class ArchivaDavResourceFactory method processRepositoryGroup.

private DavResource processRepositoryGroup(final DavServletRequest request, ArchivaDavResourceLocator archivaLocator, List<String> repositories, String activePrincipal, List<String> resourcesInAbsolutePath, RepositoryGroupConfiguration repoGroupConfig) throws DavException {
    DavResource resource = null;
    List<DavException> storedExceptions = new ArrayList<>();
    String pathInfo = StringUtils.removeEnd(request.getPathInfo(), "/");
    String rootPath = StringUtils.substringBeforeLast(pathInfo, "/");
    if (StringUtils.endsWith(rootPath, repoGroupConfig.getMergedIndexPath())) {
        // we are in the case of index file request
        String requestedFileName = StringUtils.substringAfterLast(pathInfo, "/");
        Path temporaryIndexDirectory = buildMergedIndexDirectory(repositories, activePrincipal, request, repoGroupConfig);
        Path resourceFile = temporaryIndexDirectory.resolve(requestedFileName);
        resource = new ArchivaDavResource(resourceFile.toAbsolutePath().toString(), requestedFileName, null, request.getRemoteAddr(), activePrincipal, request.getDavSession(), archivaLocator, this, mimeTypes, auditListeners, scheduler, fileLockManager);
    } else {
        for (String repositoryId : repositories) {
            ManagedRepositoryContent managedRepositoryContent;
            ManagedRepository managedRepository = repositoryRegistry.getManagedRepository(repositoryId);
            if (managedRepository == null) {
                throw new DavException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Could not find repository with id " + repositoryId);
            }
            managedRepositoryContent = managedRepository.getContent();
            if (managedRepositoryContent == null) {
                log.error("Inconsistency detected. Repository content not found for '{}'", repositoryId);
                throw new DavException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Could not find repository content with id " + repositoryId);
            }
            try {
                DavResource updatedResource = processRepository(request, archivaLocator, activePrincipal, managedRepositoryContent, managedRepository);
                if (resource == null) {
                    resource = updatedResource;
                }
                String logicalResource = getLogicalResource(archivaLocator, null, false);
                if (logicalResource.endsWith("/")) {
                    logicalResource = logicalResource.substring(1);
                }
                resourcesInAbsolutePath.add(Paths.get(managedRepositoryContent.getRepoRoot(), logicalResource).toAbsolutePath().toString());
            } catch (DavException e) {
                storedExceptions.add(e);
            }
        }
    }
    if (resource == null) {
        if (!storedExceptions.isEmpty()) {
            // MRM-1232
            for (DavException e : storedExceptions) {
                if (401 == e.getErrorCode()) {
                    throw e;
                }
            }
            throw new DavException(HttpServletResponse.SC_NOT_FOUND);
        } else {
            throw new DavException(HttpServletResponse.SC_NOT_FOUND);
        }
    }
    return resource;
}
Also used : Path(java.nio.file.Path) DavResource(org.apache.jackrabbit.webdav.DavResource) ManagedRepository(org.apache.archiva.repository.ManagedRepository) DavException(org.apache.jackrabbit.webdav.DavException) ArrayList(java.util.ArrayList) ManagedRepositoryContent(org.apache.archiva.repository.ManagedRepositoryContent)

Example 5 with DavException

use of org.apache.jackrabbit.webdav.DavException in project archiva by apache.

the class ArchivaDavResourceFactoryTest method testRepositoryGroupLastRepositoryRequiresAuthentication.

@Test
public void testRepositoryGroupLastRepositoryRequiresAuthentication() throws Exception {
    DavResourceLocator locator = new ArchivaDavResourceLocator("", "/repository/" + LOCAL_REPO_GROUP + "/org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar", LOCAL_REPO_GROUP, new ArchivaDavLocatorFactory());
    List<RepositoryGroupConfiguration> repoGroups = new ArrayList<>();
    RepositoryGroupConfiguration repoGroup = new RepositoryGroupConfiguration();
    repoGroup.setId(LOCAL_REPO_GROUP);
    repoGroup.addRepository(INTERNAL_REPO);
    repoGroup.addRepository(RELEASES_REPO);
    repoGroups.add(repoGroup);
    config.setRepositoryGroups(repoGroups);
    ManagedRepositoryContent internalRepo = createManagedRepositoryContent(INTERNAL_REPO);
    ManagedRepositoryContent releasesRepo = createManagedRepositoryContent(RELEASES_REPO);
    try {
        archivaConfigurationControl.reset();
        expect(archivaConfiguration.getConfiguration()).andReturn(config).times(3);
        expect(request.getMethod()).andReturn("GET").times(3);
        expect(request.getPathInfo()).andReturn("org/apache/archiva").times(0, 2);
        expect(repoFactory.getManagedRepositoryContent(INTERNAL_REPO)).andReturn(internalRepo);
        expect(repoFactory.getManagedRepositoryContent(RELEASES_REPO)).andReturn(releasesRepo);
        expect(request.getRemoteAddr()).andReturn("http://localhost:8080").times(2);
        expect(request.getDavSession()).andReturn(new ArchivaDavSession()).times(2);
        expect(request.getContextPath()).andReturn("").times(2);
        expect(repoRequest.isSupportFile("org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar")).andReturn(false);
        expect(repoRequest.isDefault("org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar")).andReturn(false);
        expect(repoRequest.toArtifactReference("org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar")).andReturn(null);
        expect(repoRequest.toNativePath("org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar", internalRepo)).andReturn(Paths.get(config.findManagedRepositoryById(INTERNAL_REPO).getLocation(), "target/test-classes/internal/org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar").toString());
        expect(repoRequest.isArchetypeCatalog("org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar")).andReturn(false);
        archivaConfigurationControl.replay();
        requestControl.replay();
        repoContentFactoryControl.replay();
        repoRequestControl.replay();
        resourceFactory.createResource(locator, request, response);
        archivaConfigurationControl.verify();
        requestControl.verify();
        repoContentFactoryControl.verify();
        repoRequestControl.verify();
        fail("A DavException with 401 error code should have been thrown.");
    } catch (DavException e) {
        assertEquals(401, e.getErrorCode());
    }
}
Also used : RepositoryGroupConfiguration(org.apache.archiva.configuration.RepositoryGroupConfiguration) DavException(org.apache.jackrabbit.webdav.DavException) ArrayList(java.util.ArrayList) ManagedRepositoryContent(org.apache.archiva.repository.ManagedRepositoryContent) DavResourceLocator(org.apache.jackrabbit.webdav.DavResourceLocator) Test(org.junit.Test)

Aggregations

DavException (org.apache.jackrabbit.webdav.DavException)157 RepositoryException (javax.jcr.RepositoryException)89 IOException (java.io.IOException)61 HttpResponse (org.apache.http.HttpResponse)47 DavResourceLocator (org.apache.jackrabbit.webdav.DavResourceLocator)34 DavResource (org.apache.jackrabbit.webdav.DavResource)30 Element (org.w3c.dom.Element)29 ArrayList (java.util.ArrayList)25 JcrDavException (org.apache.jackrabbit.webdav.jcr.JcrDavException)25 MultiStatusResponse (org.apache.jackrabbit.webdav.MultiStatusResponse)18 Node (javax.jcr.Node)16 DavPropertyNameSet (org.apache.jackrabbit.webdav.property.DavPropertyNameSet)15 DavPropertySet (org.apache.jackrabbit.webdav.property.DavPropertySet)14 ItemNotFoundException (javax.jcr.ItemNotFoundException)13 HttpPropfind (org.apache.jackrabbit.webdav.client.methods.HttpPropfind)12 ElementIterator (org.apache.jackrabbit.webdav.xml.ElementIterator)12 Path (java.nio.file.Path)11 ManagedRepositoryContent (org.apache.archiva.repository.ManagedRepositoryContent)10 Test (org.junit.Test)9 HrefProperty (org.apache.jackrabbit.webdav.property.HrefProperty)8