Search in sources :

Example 11 with ResourceProxy

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

the class ReorderChildNodesCommand method reorderChildNodes.

private void reorderChildNodes(Node nodeToReorder, ResourceProxy resourceToReorder) throws RepositoryException {
    List<ResourceProxy> children = resourceToReorder.getChildren();
    ListIterator<ResourceProxy> childrenIterator = children.listIterator();
    // do not process
    if (!childrenIterator.hasNext()) {
        getLogger().trace("Resource at {0} has no children, skipping child node reordering", resourceToReorder.getPath());
        return;
    }
    Set<String> resourceChildNames = new HashSet<>(children.size());
    Set<String> nodeChildNames = new HashSet<>(children.size());
    List<Node> nodeChildren = new LinkedList<>();
    NodeIterator nodeChildrenIt = nodeToReorder.getNodes();
    while (nodeChildrenIt.hasNext()) {
        Node node = nodeChildrenIt.nextNode();
        nodeChildren.add(node);
        nodeChildNames.add(node.getName());
    }
    for (ResourceProxy childResources : children) {
        resourceChildNames.add(Text.getName(childResources.getPath()));
    }
    ListIterator<Node> nodeChildrenListIt = nodeChildren.listIterator();
    // the sorting is conditioned on the local workspace and the repository having the same child names
    // if that precondition is not met abort processing since there can be no meaningful result
    traceResourcesAndNodes(children, nodeChildren);
    if (!resourceChildNames.equals(nodeChildNames)) {
        getLogger().warn("Different child names between the local workspace ( " + resourceChildNames + ") and the repository (" + nodeChildNames + ") for path " + resource.getPath() + ". Reordering will not be performed");
        return;
    }
    while (childrenIterator.hasNext() || nodeChildrenListIt.hasNext()) {
        ResourceProxy childResource = childrenIterator.next();
        Node childNode = nodeChildrenListIt.next();
        // order is as expected, skip reordering
        if (Text.getName(childResource.getPath()).equals(childNode.getName())) {
            // descend into covered child resources once they are properly arranged and perform reordering
            if (resourceToReorder.covers(childResource.getPath())) {
                reorderChildNodes(childNode, childResource);
            }
            continue;
        }
        // don't perform any reordering if this particular node does not have reorderable children
        if (!nodeToReorder.getPrimaryNodeType().hasOrderableChildNodes()) {
            getLogger().trace("Node at {0} does not have orderable child nodes, skipping reordering of {1}", nodeToReorder.getPath(), childResource.getPath());
            continue;
        }
        String expectedParentName;
        if (childrenIterator.hasNext()) {
            expectedParentName = Text.getName(childrenIterator.next().getPath());
            // move back
            childrenIterator.previous();
        } else {
            expectedParentName = null;
        }
        getLogger().trace("For node at {0} ordering {1} before {2}", nodeToReorder.getPath(), Text.getName(childResource.getPath()), expectedParentName);
        nodeToReorder.orderBefore(Text.getName(childResource.getPath()), expectedParentName);
    }
}
Also used : NodeIterator(javax.jcr.NodeIterator) Node(javax.jcr.Node) ResourceProxy(org.apache.sling.ide.transport.ResourceProxy) LinkedList(java.util.LinkedList) HashSet(java.util.HashSet)

Example 12 with ResourceProxy

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

the class ListTreeCommand method execute0.

@Override
protected ResourceProxy execute0(Session session) throws RepositoryException {
    Node node = session.getNode(getPath());
    ResourceProxy parent = nodeToResource(node);
    addChildren(parent, node, levels - 1);
    return parent;
}
Also used : Node(javax.jcr.Node) ResourceProxy(org.apache.sling.ide.transport.ResourceProxy)

Example 13 with ResourceProxy

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

the class ListTreeCommand method addChildren.

private void addChildren(ResourceProxy parent, Node node, int remainingLevels) throws RepositoryException {
    if (remainingLevels < 0) {
        // paranoia check
        throw new IllegalArgumentException("remainingLevels must be >=0, not: " + remainingLevels);
    }
    final long start = System.currentTimeMillis();
    NodeIterator nodes = node.getNodes();
    final long end = System.currentTimeMillis();
    log("ListTreeCommand.child -> " + node.getPath(), start, end);
    while (nodes.hasNext()) {
        Node childNode = nodes.nextNode();
        // TODO - this should not be needed if we obey the vlt filters
        if (childNode.getPath().equals("/jcr:system")) {
            continue;
        }
        final ResourceProxy childResourceProxy = nodeToResource(childNode);
        parent.addChild(childResourceProxy);
        if (remainingLevels > 0) {
            addChildren(childResourceProxy, childNode, remainingLevels - 1);
        }
    }
}
Also used : NodeIterator(javax.jcr.NodeIterator) Node(javax.jcr.Node) ResourceProxy(org.apache.sling.ide.transport.ResourceProxy)

Example 14 with ResourceProxy

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

the class VltNodeTypeFactory method init.

void init(VltRepository repository) throws RepositoryException {
    Result<ResourceProxy> jcrSystem = repository.newListTreeNodeCommand("/jcr:system/jcr:nodeTypes", 3).execute();
    // phase 1: create all node types
    for (ResourceProxy child : jcrSystem.get().getChildren()) {
        VltNodeType nt = createNodeType(child);
        nodeTypes.put(nt.getName(), nt);
    }
    // phase 2: init declared fields
    for (VltNodeType nt : nodeTypes.values()) {
        initDeclaredFields(nt);
    }
    //   hence initTypeDependencyTree can assume all nodetypes exist
    for (VltNodeType nt : nodeTypes.values()) {
        initTypeDependencyTree(nt);
    }
    // phase 3: init property definitions
    for (VltNodeType nt : nodeTypes.values()) {
        final ResourceProxy child = nt.getResourceProxy();
        initPropertyDefinitions(nt);
        initProperties(nt, child);
    }
    // phase 4: initialize the allowed primary childnodetypes
    for (VltNodeType nt : nodeTypes.values()) {
        initAllowedPrimaryChildNodeTypes(nt);
    }
}
Also used : ResourceProxy(org.apache.sling.ide.transport.ResourceProxy)

Example 15 with ResourceProxy

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

the class ListChildrenCommand method execute.

@Override
public Result<ResourceProxy> execute() {
    GetMethod get = new GetMethod(getPath());
    try {
        httpClient.getParams().setAuthenticationPreemptive(true);
        Credentials defaultcreds = new UsernamePasswordCredentials(repositoryInfo.getUsername(), repositoryInfo.getPassword());
        httpClient.getState().setCredentials(new AuthScope(repositoryInfo.getHost(), repositoryInfo.getPort(), AuthScope.ANY_REALM), defaultcreds);
        int responseStatus = httpClient.executeMethod(get);
        //return EncodingUtil.getString(rawdata, m.getResponseCharSet());
        if (!isSuccessStatus(responseStatus))
            return failureResultForStatusCode(responseStatus);
        ResourceProxy resource = new ResourceProxy(path);
        Gson gson = new Gson();
        try (JsonReader jsonReader = new JsonReader(new InputStreamReader(get.getResponseBodyAsStream(), get.getResponseCharSet()))) {
            jsonReader.beginObject();
            while (jsonReader.hasNext()) {
                String name = jsonReader.nextName();
                JsonToken token = jsonReader.peek();
                if (token == JsonToken.BEGIN_OBJECT) {
                    ResourceProxy child = new ResourceProxy(PathUtil.join(path, name));
                    ResourceWithPrimaryType resourceWithPrimaryType = gson.fromJson(jsonReader, ResourceWithPrimaryType.class);
                    // evaluate its jcr:primaryType as well!
                    child.addProperty(Repository.JCR_PRIMARY_TYPE, resourceWithPrimaryType.getPrimaryType());
                    resource.addChild(child);
                } else if (token == JsonToken.STRING) {
                    if (Repository.JCR_PRIMARY_TYPE.equals(name)) {
                        String primaryType = jsonReader.nextString();
                        if (primaryType != null) {
                            // TODO - needed?
                            resource.addProperty(Repository.JCR_PRIMARY_TYPE, primaryType);
                        }
                    }
                } else {
                    jsonReader.skipValue();
                }
            }
            jsonReader.endObject();
        }
        return AbstractResult.success(resource);
    } catch (Exception e) {
        return AbstractResult.failure(new RepositoryException(e));
    } finally {
        get.releaseConnection();
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) Gson(com.google.gson.Gson) RepositoryException(org.apache.sling.ide.transport.RepositoryException) ResourceProxy(org.apache.sling.ide.transport.ResourceProxy) RepositoryException(org.apache.sling.ide.transport.RepositoryException) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials) GetMethod(org.apache.commons.httpclient.methods.GetMethod) AuthScope(org.apache.commons.httpclient.auth.AuthScope) JsonReader(com.google.gson.stream.JsonReader) JsonToken(com.google.gson.stream.JsonToken) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials) Credentials(org.apache.commons.httpclient.Credentials)

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