Search in sources :

Example 6 with ResourceProxy

use of org.apache.sling.ide.transport.ResourceProxy in project sling by apache.

the class ResourceChangeCommandFactory method findSerializationDataFromCoveringParent.

/**
     * Tries to find serialization data from a resource in a covering parent
     * 
     * <p>
     * If the serialization resource is null, it's valid to look for a serialization resource higher in the filesystem,
     * given that the found serialization resource covers this resource
     * 
     * @param changedResource the resource which has changed
     * @param syncDirectory the content sync directory for the resource's project
     * @param resourceLocation the resource location relative to the sync directory
     * @param serializationFilePath the location
     * @return a <tt>ResourceProxy</tt> if there is a covering parent, or null is there is not
     * @throws CoreException
     * @throws IOException
     */
private ResourceProxy findSerializationDataFromCoveringParent(IResource changedResource, IFolder syncDirectory, String resourceLocation, IPath serializationFilePath) throws CoreException, IOException {
    // TODO - this too should be abstracted in the service layer, rather than in the Eclipse-specific code
    Logger logger = Activator.getDefault().getPluginLogger();
    logger.trace("Found plain nt:folder candidate at {0}, trying to find a covering resource for it", changedResource.getProjectRelativePath());
    // don't use isRoot() to prevent infinite loop when the final path is '//'
    while (serializationFilePath.segmentCount() != 0) {
        serializationFilePath = serializationFilePath.removeLastSegments(1);
        IFolder folderWithPossibleSerializationFile = syncDirectory.getFolder(serializationFilePath);
        if (folderWithPossibleSerializationFile == null) {
            logger.trace("No folder found at {0}, moving up to the next level", serializationFilePath);
            continue;
        }
        // it's safe to use a specific SerializationKind since this scenario is only valid for METADATA_PARTIAL
        // coverage
        String possibleSerializationFilePath = serializationManager.getSerializationFilePath(((IFolder) folderWithPossibleSerializationFile).getLocation().toOSString(), SerializationKind.METADATA_PARTIAL);
        logger.trace("Looking for serialization data in {0}", possibleSerializationFilePath);
        if (serializationManager.isSerializationFile(possibleSerializationFilePath)) {
            IPath parentSerializationFilePath = Path.fromOSString(possibleSerializationFilePath).makeRelativeTo(syncDirectory.getLocation());
            IFile possibleSerializationFile = syncDirectory.getFile(parentSerializationFilePath);
            if (!possibleSerializationFile.exists()) {
                logger.trace("Potential serialization data file {0} does not exist, moving up to the next level", possibleSerializationFile.getFullPath());
                continue;
            }
            ResourceProxy serializationData;
            try (InputStream contents = possibleSerializationFile.getContents()) {
                serializationData = serializationManager.readSerializationData(parentSerializationFilePath.toPortableString(), contents);
            }
            String repositoryPath = serializationManager.getRepositoryPath(resourceLocation);
            String potentialPath = serializationData.getPath();
            boolean covered = serializationData.covers(repositoryPath);
            logger.trace("Found possible serialization data at {0}. Resource :{1} ; our resource: {2}. Covered: {3}", parentSerializationFilePath, potentialPath, repositoryPath, covered);
            // another resource
            if (covered) {
                return serializationData.getChild(repositoryPath);
            }
            break;
        }
    }
    return null;
}
Also used : IFile(org.eclipse.core.resources.IFile) IPath(org.eclipse.core.runtime.IPath) InputStream(java.io.InputStream) Logger(org.apache.sling.ide.log.Logger) ResourceProxy(org.apache.sling.ide.transport.ResourceProxy) IFolder(org.eclipse.core.resources.IFolder)

Example 7 with ResourceProxy

use of org.apache.sling.ide.transport.ResourceProxy 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 8 with ResourceProxy

use of org.apache.sling.ide.transport.ResourceProxy in project sling by apache.

the class NodeTypeResourceBuilder method newBuilder.

public static NodeTypeResourceBuilder newBuilder(ResourceProxy parent, String name) {
    String path;
    if (parent.getPath().endsWith("/")) {
        path = parent.getPath() + name;
    } else {
        path = parent.getPath() + "/" + name;
    }
    ResourceProxy resourceProxy = new ResourceProxy(path);
    // set defaults
    resourceProxy.addProperty("jcr:nodeTypeName", name);
    resourceProxy.addProperty("jcr:primaryType", "nt:nodeType");
    resourceProxy.addProperty("jcr:isMixin", false);
    resourceProxy.addProperty("jcr:mixinTypes", new String[] {});
    return new NodeTypeResourceBuilder(resourceProxy);
}
Also used : ResourceProxy(org.apache.sling.ide.transport.ResourceProxy)

Example 9 with ResourceProxy

use of org.apache.sling.ide.transport.ResourceProxy in project sling by apache.

the class ResourceChangeCommandFactoryTest method commandForSlingOrderedFolder_extraChildrenInTheFilesystem.

@Test
public void commandForSlingOrderedFolder_extraChildrenInTheFilesystem() throws CoreException {
    // create a sling:OrderedFolder at /content/test-root
    project.createOrUpdateFile(Path.fromPortableString("jcr_root/content/test-root/.content.xml"), getClass().getResourceAsStream("sling-ordered-folder-with-children.xml"));
    // create the child folder listed in the .content.xml file
    contentProject.getFolder("jcr_root/content/test-root/folder").create(true, true, new NullProgressMonitor());
    // create an extra folder not listed in the .content.xml file
    contentProject.getFolder("jcr_root/content/test-root/folder2").create(true, true, new NullProgressMonitor());
    SpyCommand<?> command = (SpyCommand<?>) factory.newCommandForAddedOrUpdated(spyRepo, contentProject.findMember("jcr_root/content/test-root"));
    List<ResourceProxy> children = command.getResourceProxy().getChildren();
    assertThat("command.resource.children.size", children.size(), equalTo(3));
    assertThat("command.resource.children[2].name", PathUtil.getName(children.get(2).getPath()), equalTo("folder2"));
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) SpyCommand(org.apache.sling.ide.test.impl.helpers.SpyCommand) ResourceProxy(org.apache.sling.ide.transport.ResourceProxy) Test(org.junit.Test)

Example 10 with ResourceProxy

use of org.apache.sling.ide.transport.ResourceProxy in project sling by apache.

the class JcrCommand method nodeToResource.

protected ResourceProxy nodeToResource(Node node) throws RepositoryException {
    ResourceProxy resource = new ResourceProxy(node.getPath());
    resource.addAdapted(Node.class, node);
    PropertyIterator properties = node.getProperties();
    while (properties.hasNext()) {
        Property property = properties.nextProperty();
        String propertyName = property.getName();
        Object propertyValue = ConversionUtils.getPropertyValue(property);
        if (propertyValue != null) {
            resource.addProperty(propertyName, propertyValue);
        }
    }
    return resource;
}
Also used : PropertyIterator(javax.jcr.PropertyIterator) Property(javax.jcr.Property) ResourceProxy(org.apache.sling.ide.transport.ResourceProxy)

Aggregations

ResourceProxy (org.apache.sling.ide.transport.ResourceProxy)42 Test (org.junit.Test)14 Node (javax.jcr.Node)8 NodeIterator (javax.jcr.NodeIterator)5 IPath (org.eclipse.core.runtime.IPath)5 IOException (java.io.IOException)4 InputStream (java.io.InputStream)4 HashMap (java.util.HashMap)4 FilterResult (org.apache.sling.ide.filter.FilterResult)4 IFile (org.eclipse.core.resources.IFile)4 IFolder (org.eclipse.core.resources.IFolder)4 Filter (org.apache.sling.ide.filter.Filter)3 RepositoryException (org.apache.sling.ide.transport.RepositoryException)3 JsonReader (com.google.gson.stream.JsonReader)2 JsonToken (com.google.gson.stream.JsonToken)2 File (java.io.File)2 InputStreamReader (java.io.InputStreamReader)2 HashSet (java.util.HashSet)2 LinkedList (java.util.LinkedList)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2