Search in sources :

Example 1 with ContextResource

use of org.apache.sling.caconfig.resource.spi.ContextResource in project sling by apache.

the class ConfigurationWebConsolePlugin method printResolutionTestTool.

private void printResolutionTestTool(HttpServletRequest request, PrintWriter pw) {
    final String path = this.getParameter(request, "path", null);
    String configNameOther = this.getParameter(request, "configNameOther", null);
    String configName = this.getParameter(request, "configName", null);
    if (configName == null) {
        configName = configNameOther;
    } else {
        configNameOther = null;
    }
    final boolean resourceCollection = BooleanUtils.toBoolean(this.getParameter(request, "resourceCollection", "false"));
    ResourceResolver resolver = null;
    try {
        Resource contentResource = null;
        if (path != null) {
            resolver = getResolver();
            if (resolver != null) {
                contentResource = resolver.getResource(path);
            }
        }
        pw.println("<form method='get'>");
        tableStart(pw, "Test Configuration Resolution", 2);
        String alertMessage = null;
        if (path != null) {
            if (resolver == null) {
                alertMessage = "Unable to access repository - please check system configuration.";
            } else if (contentResource == null) {
                alertMessage = "Path does not exist.";
            }
        }
        textField(pw, "Content Path", "path", path, alertMessage);
        tableRows(pw);
        selectField(pw, "Config Name", "configName", configName, configurationManager.getConfigurationNames());
        tableRows(pw);
        textField(pw, "Other Config Name", "configNameOther", configNameOther);
        tableRows(pw);
        checkboxField(pw, "Resource collection", "resourceCollection", resourceCollection);
        tableRows(pw);
        pw.println("<td></td>");
        pw.println("<td><input type='submit' value='Resolve'/></td>");
        tableEnd(pw);
        pw.println("</form>");
        pw.println("<br/>");
        if (contentResource != null) {
            // context paths
            Iterator<ContextResource> contextResources = contextPathStrategyMultiplexer.findContextResources(contentResource);
            tableStart(pw, "Context paths", 3);
            pw.println("<th>Context path</th>");
            pw.println("<th>Config reference</th>");
            pw.println("<th>Ranking</th>");
            while (contextResources.hasNext()) {
                ContextResource contextResource = contextResources.next();
                tableRows(pw);
                pw.println("<td>" + xss.encodeForHTML(contextResource.getResource().getPath()) + "</td>");
                pw.println("<td>" + xss.encodeForHTML(contextResource.getConfigRef()) + "</td>");
                pw.println("<td>" + contextResource.getServiceRanking() + "</td>");
            }
            tableEnd(pw);
            pw.println("<br/>");
            // resolve configuration
            Collection<ConfigurationData> configDatas;
            if (resourceCollection) {
                configDatas = configurationManager.getConfigurationCollection(contentResource, configName).getItems();
            } else {
                ConfigurationData configData = configurationManager.getConfiguration(contentResource, configName);
                if (configData != null) {
                    configDatas = Collections.singletonList(configData);
                } else {
                    configDatas = Collections.emptyList();
                }
            }
            tableStart(pw, "Result", 6);
            if (configDatas.size() == 0) {
                pw.println("<td colspan='6'>");
                alertDiv(pw, "No matching item found.");
                pw.println("<br/>&nbsp;</td>");
            } else {
                pw.println("<th>Property</th>");
                pw.println("<th>Effective Value</th>");
                pw.println("<th>Value</th>");
                pw.println("<th>Default</th>");
                pw.println("<th>Inherited</th>");
                pw.println("<th>Overwritten</th>");
                for (ConfigurationData data : configDatas) {
                    tableRows(pw);
                    pw.println("<td colspan='6' style='background-color:#f3f3f3'>");
                    pw.print("Path: " + xss.encodeForHTML(data.getResourcePath()));
                    pw.println("</td>");
                    for (String propertyName : data.getPropertyNames()) {
                        ValueInfo<?> valueInfo = data.getValueInfo(propertyName);
                        tableRows(pw);
                        td(pw, propertyName);
                        td(pw, valueInfo.getEffectiveValue());
                        td(pw, valueInfo.getValue());
                        td(pw, valueInfo.isDefault());
                        String title = null;
                        if (valueInfo.isInherited()) {
                            title = "Source path: " + valueInfo.getConfigSourcePath();
                        }
                        td(pw, valueInfo.isInherited(), title);
                        td(pw, valueInfo.isOverridden());
                    }
                }
            }
            tableEnd(pw);
        }
    } finally {
        if (resolver != null) {
            resolver.close();
        }
    }
}
Also used : ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Resource(org.apache.sling.api.resource.Resource) ContextResource(org.apache.sling.caconfig.resource.spi.ContextResource) ConfigurationData(org.apache.sling.caconfig.management.ConfigurationData) ContextResource(org.apache.sling.caconfig.resource.spi.ContextResource)

Example 2 with ContextResource

use of org.apache.sling.caconfig.resource.spi.ContextResource in project sling by apache.

the class ResourceEliminateDuplicatesIteratorTest method testIterator.

@SuppressWarnings("unchecked")
@Test
public void testIterator() {
    context.build().resource("/content/a").resource("/content/a/b").resource("/content/a/b/c");
    ResourceResolver rr = context.resourceResolver();
    List<ContextResource> list = ImmutableList.of(new ContextResource(rr.getResource("/content/a"), "/conf/a", 0), new ContextResource(rr.getResource("/content/a/b"), "/conf/a/b", 0), new ContextResource(rr.getResource("/content/a"), "/conf/a", 0), new ContextResource(rr.getResource("/content/a"), null, 0), new ContextResource(rr.getResource("/content/a/b/c"), "/conf/a/b", 0));
    Iterator<Resource> result = toResourceIterator(new ResourceEliminateDuplicatesIterator(list.iterator()));
    assertThat(result, ResourceIteratorMatchers.paths("/content/a", "/content/a/b", "/content/a", "/content/a/b/c"));
}
Also used : ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Resource(org.apache.sling.api.resource.Resource) ContextResource(org.apache.sling.caconfig.resource.spi.ContextResource) ContextResource(org.apache.sling.caconfig.resource.spi.ContextResource) Test(org.junit.Test)

Example 3 with ContextResource

use of org.apache.sling.caconfig.resource.spi.ContextResource in project sling by apache.

the class ResourcePathCollatingIteratorTest method testWithConfigRefAndServiceRanking.

@SuppressWarnings("unchecked")
@Test
public void testWithConfigRefAndServiceRanking() {
    context.build().resource("/content/a").resource("/content/a/b").resource("/content/a/b/c").resource("/content/a/b/c/d");
    ResourceResolver rr = context.resourceResolver();
    List<ContextResource> list1 = ImmutableList.of(new ContextResource(rr.getResource("/content/a"), "/conf/z", 500));
    List<ContextResource> list2 = ImmutableList.of(new ContextResource(rr.getResource("/content/a"), "/conf/a", 100));
    Iterator<ContextResource> result = new ResourcePathCollatingIterator(ImmutableList.of(list1.iterator(), list2.iterator()));
    ContextResource item1 = result.next();
    ContextResource item2 = result.next();
    assertFalse(result.hasNext());
    assertEquals("/content/a", item1.getResource().getPath());
    assertEquals("/conf/z", item1.getConfigRef());
    assertEquals("/content/a", item2.getResource().getPath());
    assertEquals("/conf/a", item2.getConfigRef());
}
Also used : ResourceResolver(org.apache.sling.api.resource.ResourceResolver) ContextResource(org.apache.sling.caconfig.resource.spi.ContextResource) Test(org.junit.Test)

Example 4 with ContextResource

use of org.apache.sling.caconfig.resource.spi.ContextResource in project sling by apache.

the class ResourcePathCollatingIteratorTest method testWithConfigRef.

@SuppressWarnings("unchecked")
@Test
public void testWithConfigRef() {
    context.build().resource("/content/a").resource("/content/a/b").resource("/content/a/b/c").resource("/content/a/b/c/d");
    ResourceResolver rr = context.resourceResolver();
    List<ContextResource> list1 = ImmutableList.of(new ContextResource(rr.getResource("/content/a"), "/conf/z", 0));
    List<ContextResource> list2 = ImmutableList.of(new ContextResource(rr.getResource("/content/a"), "/conf/a", 0));
    Iterator<ContextResource> result = new ResourcePathCollatingIterator(ImmutableList.of(list1.iterator(), list2.iterator()));
    ContextResource item1 = result.next();
    ContextResource item2 = result.next();
    assertFalse(result.hasNext());
    assertEquals("/content/a", item1.getResource().getPath());
    assertEquals("/conf/a", item1.getConfigRef());
    assertEquals("/content/a", item2.getResource().getPath());
    assertEquals("/conf/z", item2.getConfigRef());
}
Also used : ResourceResolver(org.apache.sling.api.resource.ResourceResolver) ContextResource(org.apache.sling.caconfig.resource.spi.ContextResource) Test(org.junit.Test)

Example 5 with ContextResource

use of org.apache.sling.caconfig.resource.spi.ContextResource in project sling by apache.

the class ContextPathStrategyMultiplexerImplTest method registerContextPathStrategy.

private void registerContextPathStrategy(String... paths) {
    final List<Resource> resources = new ArrayList<>();
    for (String path : paths) {
        Resource resource = context.resourceResolver().getResource(path);
        if (resource != null) {
            resources.add(resource);
        }
    }
    context.registerService(ContextPathStrategy.class, new ContextPathStrategy() {

        @Override
        public Iterator<ContextResource> findContextResources(Resource resource) {
            return toContextResourceIterator(resources.iterator());
        }
    });
}
Also used : ArrayList(java.util.ArrayList) Resource(org.apache.sling.api.resource.Resource) ContextResource(org.apache.sling.caconfig.resource.spi.ContextResource) ContextResourceTestUtil.toContextResourceIterator(org.apache.sling.caconfig.resource.impl.util.ContextResourceTestUtil.toContextResourceIterator) Iterator(java.util.Iterator) ContextResourceTestUtil.toResourceIterator(org.apache.sling.caconfig.resource.impl.util.ContextResourceTestUtil.toResourceIterator) DefaultContextPathStrategy(org.apache.sling.caconfig.resource.impl.def.DefaultContextPathStrategy) ContextPathStrategy(org.apache.sling.caconfig.resource.spi.ContextPathStrategy)

Aggregations

ContextResource (org.apache.sling.caconfig.resource.spi.ContextResource)6 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)4 Resource (org.apache.sling.api.resource.Resource)3 Test (org.junit.Test)3 ArrayList (java.util.ArrayList)2 Iterator (java.util.Iterator)2 List (java.util.List)1 NoSuchElementException (java.util.NoSuchElementException)1 Predicate (org.apache.commons.collections.Predicate)1 ArrayIterator (org.apache.commons.collections.iterators.ArrayIterator)1 FilterIterator (org.apache.commons.collections.iterators.FilterIterator)1 ConfigurationData (org.apache.sling.caconfig.management.ConfigurationData)1 DefaultContextPathStrategy (org.apache.sling.caconfig.resource.impl.def.DefaultContextPathStrategy)1 ContextResourceTestUtil.toContextResourceIterator (org.apache.sling.caconfig.resource.impl.util.ContextResourceTestUtil.toContextResourceIterator)1 ContextResourceTestUtil.toResourceIterator (org.apache.sling.caconfig.resource.impl.util.ContextResourceTestUtil.toResourceIterator)1 PathEliminateDuplicatesIterator (org.apache.sling.caconfig.resource.impl.util.PathEliminateDuplicatesIterator)1 PathParentExpandIterator (org.apache.sling.caconfig.resource.impl.util.PathParentExpandIterator)1 ContextPathStrategy (org.apache.sling.caconfig.resource.spi.ContextPathStrategy)1