Search in sources :

Example 1 with Filter

use of org.apache.sling.ide.filter.Filter in project sling by apache.

the class JcrNode method canCreateChild.

public boolean canCreateChild() {
    try {
        final IProject project = getProject();
        final Filter filter = ProjectUtil.loadFilter(project);
        final String relativeFilePath = getJcrPath();
        //            }
        if (filter == null) {
            Activator.getDefault().getPluginLogger().error("No filter.xml found for " + project);
            return true;
        } else {
            final FilterResult result = filter.filter(relativeFilePath);
            return result == FilterResult.ALLOW;
        }
    } catch (CoreException e) {
        Logger logger = Activator.getDefault().getPluginLogger();
        logger.error("Could not verify child node allowance: " + this, e);
        return false;
    }
}
Also used : CoreException(org.eclipse.core.runtime.CoreException) IActionFilter(org.eclipse.ui.IActionFilter) Filter(org.apache.sling.ide.filter.Filter) FilterResult(org.apache.sling.ide.filter.FilterResult) Logger(org.apache.sling.ide.log.Logger) IProject(org.eclipse.core.resources.IProject)

Example 2 with Filter

use of org.apache.sling.ide.filter.Filter in project sling by apache.

the class ResourceChangeCommandFactory method removeFileCommand.

private Command<?> removeFileCommand(Repository repository, IResource resource) throws CoreException, IOException {
    if (resource.isTeamPrivateMember(IResource.CHECK_ANCESTORS)) {
        Activator.getDefault().getPluginLogger().trace("Skipping team-private resource {0}", resource);
        return null;
    }
    if (ignoredFileNames.contains(resource.getName())) {
        return null;
    }
    IFolder syncDirectory = ProjectUtil.getSyncDirectory(resource.getProject());
    Filter filter = ProjectUtil.loadFilter(syncDirectory.getProject());
    FilterResult filterResult = getFilterResult(resource, null, filter);
    if (filterResult == FilterResult.DENY || filterResult == FilterResult.PREREQUISITE) {
        return null;
    }
    String resourceLocation = getRepositoryPathForDeletedResource(resource, ProjectUtil.getSyncDirectoryFile(resource.getProject()));
    // verify whether a resource being deleted does not signal that the content structure
    // was rearranged under a covering parent aggregate
    IPath serializationFilePath = Path.fromOSString(serializationManager.getSerializationFilePath(resourceLocation, SerializationKind.FOLDER));
    ResourceProxy coveringParentData = findSerializationDataFromCoveringParent(resource, syncDirectory, resourceLocation, serializationFilePath);
    if (coveringParentData != null) {
        Activator.getDefault().getPluginLogger().trace("Found covering resource data ( repository path = {0} ) for resource at {1},  skipping deletion and performing an update instead", coveringParentData.getPath(), resource.getFullPath());
        FileInfo info = createFileInfo(resource);
        return repository.newAddOrUpdateNodeCommand(new CommandContext(filter), info, coveringParentData);
    }
    return repository.newDeleteNodeCommand(serializationManager.getRepositoryPath(resourceLocation));
}
Also used : IPath(org.eclipse.core.runtime.IPath) FileInfo(org.apache.sling.ide.transport.FileInfo) CommandContext(org.apache.sling.ide.transport.CommandContext) Filter(org.apache.sling.ide.filter.Filter) FilterResult(org.apache.sling.ide.filter.FilterResult) ResourceProxy(org.apache.sling.ide.transport.ResourceProxy) IFolder(org.eclipse.core.resources.IFolder)

Example 3 with Filter

use of org.apache.sling.ide.filter.Filter in project sling by apache.

the class ResourceChangeCommandFactory method buildResourceAndInfo.

/**
     * Convenience method which builds a <tt>ResourceAndInfo</tt> info for a specific <tt>IResource</tt>
     * 
     * @param resource the resource to process
     * @param repository the repository, used to extract serialization information for different resource types
     * @return the build object, or null if one could not be built
     * @throws CoreException
     * @throws SerializationException
     * @throws IOException
     */
public ResourceAndInfo buildResourceAndInfo(IResource resource, Repository repository) throws CoreException, IOException {
    if (ignoredFileNames.contains(resource.getName())) {
        return null;
    }
    Long modificationTimestamp = (Long) resource.getSessionProperty(ResourceUtil.QN_IMPORT_MODIFICATION_TIMESTAMP);
    if (modificationTimestamp != null && modificationTimestamp >= resource.getModificationStamp()) {
        Activator.getDefault().getPluginLogger().trace("Change for resource {0} ignored as the import timestamp {1} >= modification timestamp {2}", resource, modificationTimestamp, resource.getModificationStamp());
        return null;
    }
    if (resource.isTeamPrivateMember(IResource.CHECK_ANCESTORS)) {
        Activator.getDefault().getPluginLogger().trace("Skipping team-private resource {0}", resource);
        return null;
    }
    FileInfo info = createFileInfo(resource);
    Activator.getDefault().getPluginLogger().trace("For {0} built fileInfo {1}", resource, info);
    File syncDirectoryAsFile = ProjectUtil.getSyncDirectoryFullPath(resource.getProject()).toFile();
    IFolder syncDirectory = ProjectUtil.getSyncDirectory(resource.getProject());
    Filter filter = ProjectUtil.loadFilter(resource.getProject());
    ResourceProxy resourceProxy = null;
    if (serializationManager.isSerializationFile(resource.getLocation().toOSString())) {
        IFile file = (IFile) resource;
        try (InputStream contents = file.getContents()) {
            String resourceLocation = file.getFullPath().makeRelativeTo(syncDirectory.getFullPath()).toPortableString();
            resourceProxy = serializationManager.readSerializationData(resourceLocation, contents);
            normaliseResourceChildren(file, resourceProxy, syncDirectory, repository);
            // TODO - not sure if this 100% correct, but we definitely should not refer to the FileInfo as the
            // .serialization file, since for nt:file/nt:resource nodes this will overwrite the file contents
            String primaryType = (String) resourceProxy.getProperties().get(Repository.JCR_PRIMARY_TYPE);
            if (Repository.NT_FILE.equals(primaryType)) {
                // TODO move logic to serializationManager
                File locationFile = new File(info.getLocation());
                String locationFileParent = locationFile.getParent();
                int endIndex = locationFileParent.length() - ".dir".length();
                File actualFile = new File(locationFileParent.substring(0, endIndex));
                String newLocation = actualFile.getAbsolutePath();
                String newName = actualFile.getName();
                String newRelativeLocation = actualFile.getAbsolutePath().substring(syncDirectoryAsFile.getAbsolutePath().length());
                info = new FileInfo(newLocation, newRelativeLocation, newName);
                Activator.getDefault().getPluginLogger().trace("Adjusted original location from {0} to {1}", resourceLocation, newLocation);
            }
        } catch (IOException e) {
            Status s = new Status(Status.WARNING, Activator.PLUGIN_ID, "Failed reading file at " + resource.getFullPath(), e);
            StatusManager.getManager().handle(s, StatusManager.LOG | StatusManager.SHOW);
            return null;
        }
    } else {
        // possible .dir serialization holder
        if (resource.getType() == IResource.FOLDER && resource.getName().endsWith(".dir")) {
            IFolder folder = (IFolder) resource;
            IResource contentXml = folder.findMember(".content.xml");
            // .dir serialization holder ; nothing to process here, the .content.xml will trigger the actual work
            if (contentXml != null && contentXml.exists() && serializationManager.isSerializationFile(contentXml.getLocation().toOSString())) {
                return null;
            }
        }
        resourceProxy = buildResourceProxyForPlainFileOrFolder(resource, syncDirectory, repository);
    }
    FilterResult filterResult = getFilterResult(resource, resourceProxy, filter);
    switch(filterResult) {
        case ALLOW:
            return new ResourceAndInfo(resourceProxy, info);
        case PREREQUISITE:
            // never try to 'create' the root node, we assume it exists
            if (!resourceProxy.getPath().equals("/")) {
                // suited one ( typically nt:unstructured )
                return new ResourceAndInfo(new ResourceProxy(resourceProxy.getPath()), null, true);
            }
        // falls through
        case DENY:
        default:
            return null;
    }
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) IFile(org.eclipse.core.resources.IFile) InputStream(java.io.InputStream) IOException(java.io.IOException) ResourceProxy(org.apache.sling.ide.transport.ResourceProxy) FileInfo(org.apache.sling.ide.transport.FileInfo) Filter(org.apache.sling.ide.filter.Filter) FilterResult(org.apache.sling.ide.filter.FilterResult) IFile(org.eclipse.core.resources.IFile) File(java.io.File) IResource(org.eclipse.core.resources.IResource) IFolder(org.eclipse.core.resources.IFolder)

Example 4 with Filter

use of org.apache.sling.ide.filter.Filter in project sling by apache.

the class ProjectUtil method loadFilter.

/**
     * Loads a filter for the specified project
     * 
     * @param project the project to find a filter for
     * @return the found filter or null
     * @throws CoreException
     */
public static Filter loadFilter(final IProject project) throws CoreException {
    FilterLocator filterLocator = Activator.getDefault().getFilterLocator();
    IPath filterPath = findFilterPath(project);
    if (filterPath == null) {
        return null;
    }
    IFile filterFile = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(filterPath);
    Filter filter = null;
    if (filterFile != null && filterFile.exists()) {
        try (InputStream contents = filterFile.getContents()) {
            filter = filterLocator.loadFilter(contents);
        } catch (IOException e) {
            throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Failed loading filter file for project " + project.getName() + " from location " + filterFile, e));
        }
    }
    return filter;
}
Also used : Status(org.eclipse.core.runtime.Status) IStatus(org.eclipse.core.runtime.IStatus) FilterLocator(org.apache.sling.ide.filter.FilterLocator) IFile(org.eclipse.core.resources.IFile) IPath(org.eclipse.core.runtime.IPath) CoreException(org.eclipse.core.runtime.CoreException) Filter(org.apache.sling.ide.filter.Filter) InputStream(java.io.InputStream) IOException(java.io.IOException)

Example 5 with Filter

use of org.apache.sling.ide.filter.Filter in project sling by apache.

the class AddOrUpdateNodeCommandTest method nodeNotPresentButOutsideOfFilterIsNotRemoved.

@Test
public void nodeNotPresentButOutsideOfFilterIsNotRemoved() throws Exception {
    final CommandContext context = new CommandContext(new Filter() {

        @Override
        public FilterResult filter(String repositoryPath) {
            if (repositoryPath.equals("/content/not-included-child")) {
                return FilterResult.DENY;
            }
            return FilterResult.ALLOW;
        }
    });
    doWithTransientRepository(new CallableWithSession() {

        @Override
        public Void call() throws Exception {
            Node content = session().getRootNode().addNode("content", "nt:unstructured");
            content.addNode("included-child");
            content.addNode("not-included-child");
            session().save();
            ResourceProxy resource = newResource("/content", "nt:unstructured");
            resource.addChild(newResource("/content/included-child", "nt:unstructured"));
            AddOrUpdateNodeCommand cmd = new AddOrUpdateNodeCommand(repo(), credentials(), context, null, resource, logger);
            cmd.execute().get();
            session().refresh(false);
            content = session().getRootNode().getNode("content");
            content.getNode("included-child");
            content.getNode("not-included-child");
            return null;
        }
    });
}
Also used : CommandContext(org.apache.sling.ide.transport.CommandContext) Filter(org.apache.sling.ide.filter.Filter) Node(javax.jcr.Node) FilterResult(org.apache.sling.ide.filter.FilterResult) NodeTypeExistsException(javax.jcr.nodetype.NodeTypeExistsException) InvalidNodeTypeDefinitionException(javax.jcr.nodetype.InvalidNodeTypeDefinitionException) RepositoryException(javax.jcr.RepositoryException) ParseException(org.apache.jackrabbit.commons.cnd.ParseException) IOException(java.io.IOException) UnsupportedRepositoryOperationException(javax.jcr.UnsupportedRepositoryOperationException) ResourceProxy(org.apache.sling.ide.transport.ResourceProxy) Test(org.junit.Test)

Aggregations

Filter (org.apache.sling.ide.filter.Filter)5 FilterResult (org.apache.sling.ide.filter.FilterResult)4 IOException (java.io.IOException)3 ResourceProxy (org.apache.sling.ide.transport.ResourceProxy)3 InputStream (java.io.InputStream)2 CommandContext (org.apache.sling.ide.transport.CommandContext)2 FileInfo (org.apache.sling.ide.transport.FileInfo)2 IFile (org.eclipse.core.resources.IFile)2 IFolder (org.eclipse.core.resources.IFolder)2 CoreException (org.eclipse.core.runtime.CoreException)2 IPath (org.eclipse.core.runtime.IPath)2 IStatus (org.eclipse.core.runtime.IStatus)2 Status (org.eclipse.core.runtime.Status)2 File (java.io.File)1 Node (javax.jcr.Node)1 RepositoryException (javax.jcr.RepositoryException)1 UnsupportedRepositoryOperationException (javax.jcr.UnsupportedRepositoryOperationException)1 InvalidNodeTypeDefinitionException (javax.jcr.nodetype.InvalidNodeTypeDefinitionException)1 NodeTypeExistsException (javax.jcr.nodetype.NodeTypeExistsException)1 ParseException (org.apache.jackrabbit.commons.cnd.ParseException)1