Search in sources :

Example 1 with ResourceProvider

use of org.apache.sling.spi.resource.provider.ResourceProvider in project sling by apache.

the class ResourceResolverWebConsolePlugin method dumpDTOsHtml.

private void dumpDTOsHtml(final PrintWriter pw) {
    titleHtml(pw, "Resource Providers", "Lists all available and activate resource prodivers.");
    pw.println("<tr class='content'>");
    pw.println("<th class='content'>Provider</th>");
    pw.println("<th class='content'>Path</th>");
    pw.println("<th class='content'>Configuration</th>");
    pw.println("</tr>");
    final RuntimeDTO runtimeDTO = this.runtimeService.getRuntimeDTO();
    for (final ResourceProviderDTO dto : runtimeDTO.providers) {
        // get service reference
        final ServiceReference<ResourceProvider<?>> ref = this.getServiceReference(dto.serviceId);
        final StringBuilder sb = new StringBuilder();
        if (dto.name != null) {
            sb.append(dto.name);
            sb.append(' ');
        } else {
            sb.append("<unnamed> ");
        }
        if (ref != null) {
            sb.append("(serviceId = ");
            sb.append(dto.serviceId);
            sb.append(", bundleId = ");
            sb.append(ref.getBundle().getBundleId());
            sb.append(")");
        }
        pw.print("<tr class='content'>");
        pw.print("<td class='content' style='vertical-align: top'>");
        pw.print(ResponseUtil.escapeXml(sb.toString()));
        pw.print("</td>");
        pw.print("<td class='content' style='vertical-align: top'>");
        pw.print(ResponseUtil.escapeXml(dto.path));
        pw.print("</td>");
        pw.print("<td class='content' style='vertical-align: top'>");
        pw.print("auth=");
        pw.print(dto.authType.name());
        pw.print("<br/>");
        pw.print("adaptable=");
        pw.print(dto.adaptable);
        pw.print("<br/>");
        pw.print("attributable=");
        pw.print(dto.attributable);
        pw.print("<br/>");
        pw.print("modifiable=");
        pw.print(dto.modifiable);
        pw.print("<br/>");
        pw.print("refreshable=");
        pw.print(dto.refreshable);
        pw.print("<br/>");
        pw.print("supportsQueryLanguage=");
        pw.print(dto.supportsQueryLanguage);
        pw.print("<br/>");
        pw.print("useResourceAccessSecurity=");
        pw.print(dto.useResourceAccessSecurity);
        pw.println("</td></tr>");
    }
    if (runtimeDTO.failedProviders.length > 0) {
        titleHtml(pw, "Failed Resource Providers", "Lists all failed providers.");
        pw.println("<tr class='content'>");
        pw.println("<th class='content'>Provider</th>");
        pw.println("<th class='content'>Path</th>");
        pw.println("<th class='content'>Reason</th>");
        pw.println("</tr>");
        for (final ResourceProviderFailureDTO dto : runtimeDTO.failedProviders) {
            // get service reference
            final ServiceReference<ResourceProvider<?>> ref = this.getServiceReference(dto.serviceId);
            final StringBuilder sb = new StringBuilder();
            if (dto.name != null) {
                sb.append(dto.name);
                sb.append(' ');
            } else {
                sb.append("<unnamed> ");
            }
            if (ref != null) {
                sb.append("(serviceId = ");
                sb.append(dto.serviceId);
                sb.append(", bundleId = ");
                sb.append(ref.getBundle().getBundleId());
                sb.append(")");
            }
            pw.print("<tr class='content'>");
            pw.print("<td class='content' style='vertical-align: top'>");
            pw.print(ResponseUtil.escapeXml(sb.toString()));
            pw.print("</td>");
            pw.print("<td class='content' style='vertical-align: top'>");
            pw.print(ResponseUtil.escapeXml(dto.path));
            pw.print("</td>");
            pw.print("<td class='content' style='vertical-align: top'>");
            pw.print(dto.reason.name());
            pw.println("</td></tr>");
        }
    }
}
Also used : ResourceProviderFailureDTO(org.apache.sling.api.resource.runtime.dto.ResourceProviderFailureDTO) ResourceProviderDTO(org.apache.sling.api.resource.runtime.dto.ResourceProviderDTO) ResourceProvider(org.apache.sling.spi.resource.provider.ResourceProvider) RuntimeDTO(org.apache.sling.api.resource.runtime.dto.RuntimeDTO)

Example 2 with ResourceProvider

use of org.apache.sling.spi.resource.provider.ResourceProvider in project sling by apache.

the class FsResourceProvider method listChildren.

/**
     * Returns an iterator of resources.
     */
@SuppressWarnings("unchecked")
@Override
public Iterator<Resource> listChildren(final ResolveContext<Object> ctx, final Resource parent) {
    ResourceResolver resolver = ctx.getResourceResolver();
    List<Iterator<Resource>> allChildren = new ArrayList<>();
    Iterator<Resource> children;
    boolean askParentResourceProvider;
    if (fsMode == FsMode.FILEVAULT_XML) {
        // filevault: always ask provider, it checks itself if children matches the filters
        askParentResourceProvider = true;
        children = fileVaultMapper.getChildren(resolver, parent);
        if (children != null) {
            allChildren.add(children);
        }
    } else {
        // Sling-Initial-Content: get all matching folders/files and content files
        askParentResourceProvider = this.overlayParentResourceProvider;
        children = contentFileMapper.getChildren(resolver, parent);
        if (children != null) {
            allChildren.add(children);
        }
        children = fileMapper.getChildren(resolver, parent);
        if (children != null) {
            allChildren.add(children);
        }
    }
    // get children from from shadowed provider
    if (askParentResourceProvider) {
        final ResourceProvider parentResourceProvider = ctx.getParentResourceProvider();
        if (parentResourceProvider != null) {
            children = parentResourceProvider.listChildren(ctx.getParentResolveContext(), parent);
            if (children != null) {
                if (fsMode == FsMode.FILEVAULT_XML) {
                    // filevault: include all children from parent resource provider that do not match the filters
                    allChildren.add(IteratorUtils.filteredIterator(children, new Predicate() {

                        @Override
                        public boolean evaluate(Object object) {
                            Resource child = (Resource) object;
                            return !fileVaultMapper.pathMatches(child.getPath());
                        }
                    }));
                } else {
                    allChildren.add(children);
                }
            }
        }
    }
    if (allChildren.isEmpty()) {
        return null;
    } else if (allChildren.size() == 1) {
        return allChildren.get(0);
    } else {
        // merge all children from the different iterators, but filter out potential duplicates with same resource name
        return IteratorUtils.filteredIterator(IteratorUtils.chainedIterator(allChildren), new Predicate() {

            private Set<String> names = new HashSet<>();

            @Override
            public boolean evaluate(Object object) {
                Resource resource = (Resource) object;
                return names.add(resource.getName());
            }
        });
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) ResourceProvider(org.apache.sling.spi.resource.provider.ResourceProvider) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) Resource(org.apache.sling.api.resource.Resource) Predicate(org.apache.commons.collections.Predicate)

Example 3 with ResourceProvider

use of org.apache.sling.spi.resource.provider.ResourceProvider in project sling by apache.

the class ResourceResolverWebConsolePlugin method dumpDTOsText.

private void dumpDTOsText(final PrintWriter pw) {
    pw.println("Resource Providers");
    final String format = "%35s %25s %15s\r\n";
    pw.printf(format, "Provider", "Path", "Configuration");
    final RuntimeDTO runtimeDTO = this.runtimeService.getRuntimeDTO();
    for (final ResourceProviderDTO dto : runtimeDTO.providers) {
        // get service reference
        final ServiceReference<ResourceProvider<?>> ref = this.getServiceReference(dto.serviceId);
        final StringBuilder sb = new StringBuilder();
        if (dto.name != null) {
            sb.append(dto.name);
            sb.append(' ');
        } else {
            sb.append("<unnamed> ");
        }
        if (ref != null) {
            sb.append("(serviceId = ");
            sb.append(dto.serviceId);
            sb.append(", bundleId = ");
            sb.append(ref.getBundle().getBundleId());
            sb.append(")");
        }
        final StringBuilder config = new StringBuilder();
        config.append("auth=");
        config.append(dto.authType.name());
        config.append(", adaptable=");
        config.append(dto.adaptable);
        config.append(", attributable=");
        config.append(dto.attributable);
        config.append(", modifiable=");
        config.append(dto.modifiable);
        config.append(", refreshable=");
        config.append(dto.refreshable);
        config.append(", supportsQueryLanguage=");
        config.append(dto.supportsQueryLanguage);
        config.append(", useResourceAccessSecurity=");
        config.append(dto.useResourceAccessSecurity);
        pw.printf(format, sb.toString(), dto.path, config.toString());
    }
    pw.println();
    if (runtimeDTO.failedProviders.length > 0) {
        pw.println("Failed Resource Providers");
        pw.printf(format, "Provider", "Path", "Reason");
        for (final ResourceProviderFailureDTO dto : runtimeDTO.failedProviders) {
            // get service reference
            final ServiceReference<ResourceProvider<?>> ref = this.getServiceReference(dto.serviceId);
            final StringBuilder sb = new StringBuilder();
            if (dto.name != null) {
                sb.append(dto.name);
                sb.append(' ');
            } else {
                sb.append("<unnamed> ");
            }
            if (ref != null) {
                sb.append("(serviceId = ");
                sb.append(dto.serviceId);
                sb.append(", bundleId = ");
                sb.append(ref.getBundle().getBundleId());
                sb.append(")");
            }
            pw.printf(format, sb.toString(), dto.path, dto.reason.name());
        }
        pw.println();
    }
}
Also used : ResourceProviderFailureDTO(org.apache.sling.api.resource.runtime.dto.ResourceProviderFailureDTO) ResourceProviderDTO(org.apache.sling.api.resource.runtime.dto.ResourceProviderDTO) ResourceProvider(org.apache.sling.spi.resource.provider.ResourceProvider) RuntimeDTO(org.apache.sling.api.resource.runtime.dto.RuntimeDTO)

Example 4 with ResourceProvider

use of org.apache.sling.spi.resource.provider.ResourceProvider in project sling by apache.

the class SlingServletResolver method createServlet.

private boolean createServlet(final Servlet servlet, final ServiceReference<Servlet> reference) {
    // check for a name, this is required
    final String name = getName(reference);
    // check for Sling properties in the service registration
    final ServletResourceProvider provider = servletResourceProviderFactory.create(reference, servlet);
    if (provider == null) {
        // this is expected if the servlet is not destined for Sling
        return false;
    }
    // initialize now
    try {
        servlet.init(new SlingServletConfig(servletContext, reference, name));
        LOGGER.debug("bindServlet: Servlet {} initialized", name);
    } catch (ServletException ce) {
        LOGGER.error("bindServlet: Servlet " + ServletResourceProviderFactory.getServiceReferenceInfo(reference) + " failed to initialize", ce);
        return false;
    } catch (Throwable t) {
        LOGGER.error("bindServlet: Unexpected problem initializing servlet " + ServletResourceProviderFactory.getServiceReferenceInfo(reference), t);
        return false;
    }
    final List<ServiceRegistration<ResourceProvider<Object>>> regs = new ArrayList<>();
    for (final String root : provider.getServletPaths()) {
        @SuppressWarnings("unchecked") final ServiceRegistration<ResourceProvider<Object>> reg = (ServiceRegistration<ResourceProvider<Object>>) context.registerService(ResourceProvider.class.getName(), provider, createServiceProperties(reference, provider, root));
        regs.add(reg);
    }
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Registered {}", provider.toString());
    }
    synchronized (this.servletsByReference) {
        servletsByReference.put(reference, new ServletReg(servlet, regs));
    }
    return true;
}
Also used : SlingServletConfig(org.apache.sling.servlets.resolver.internal.helper.SlingServletConfig) ArrayList(java.util.ArrayList) ServletException(javax.servlet.ServletException) ServletResourceProvider(org.apache.sling.servlets.resolver.internal.resource.ServletResourceProvider) ResourceProvider(org.apache.sling.spi.resource.provider.ResourceProvider) ServletResourceProvider(org.apache.sling.servlets.resolver.internal.resource.ServletResourceProvider) ServiceRegistration(org.osgi.framework.ServiceRegistration)

Example 5 with ResourceProvider

use of org.apache.sling.spi.resource.provider.ResourceProvider in project sling by apache.

the class FsResourceProvider method getResource.

/**
     * Returns a resource wrapping a file system file or folder for the given
     * path. If the <code>path</code> is equal to the configured resource tree
     * location of this provider, the configured file system file or folder is
     * used for the resource. Otherwise the configured resource tree location
     * prefix is removed from the path and the remaining relative path is used
     * to access the file or folder. If no such file or folder exists, this
     * method returns <code>null</code>.
     */
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public Resource getResource(final ResolveContext<Object> ctx, final String path, final ResourceContext resourceContext, final Resource parent) {
    ResourceResolver resolver = ctx.getResourceResolver();
    boolean askParentResourceProvider;
    Resource rsrc = null;
    if (fsMode == FsMode.FILEVAULT_XML) {
        // filevault: check if path matches, if not fallback to parent resource provider
        if (fileVaultMapper.pathMatches(path)) {
            askParentResourceProvider = false;
            rsrc = fileVaultMapper.getResource(resolver, path);
        } else {
            askParentResourceProvider = true;
        }
    } else {
        // Sling-Initial-Content: mount folder/files an content files
        askParentResourceProvider = this.overlayParentResourceProvider;
        rsrc = fileMapper.getResource(resolver, path);
        if (rsrc == null) {
            rsrc = contentFileMapper.getResource(resolver, path);
        }
    }
    if (askParentResourceProvider) {
        // this allows properties like sling:resourceSuperType to take effect
        if (rsrc == null || rsrc.getResourceMetadata().containsKey(RESOURCE_METADATA_FILE_DIRECTORY)) {
            // get resource from shadowed provider
            final ResourceProvider rp = ctx.getParentResourceProvider();
            if (rp != null) {
                Resource resourceFromParentResourceProvider = rp.getResource((ResolveContext) ctx.getParentResolveContext(), path, resourceContext, parent);
                if (resourceFromParentResourceProvider != null) {
                    rsrc = resourceFromParentResourceProvider;
                }
            }
        }
    }
    return rsrc;
}
Also used : ResourceProvider(org.apache.sling.spi.resource.provider.ResourceProvider) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Resource(org.apache.sling.api.resource.Resource)

Aggregations

ResourceProvider (org.apache.sling.spi.resource.provider.ResourceProvider)8 Resource (org.apache.sling.api.resource.Resource)4 ArrayList (java.util.ArrayList)3 NonExistingResource (org.apache.sling.api.resource.NonExistingResource)2 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)2 ResourceProviderDTO (org.apache.sling.api.resource.runtime.dto.ResourceProviderDTO)2 ResourceProviderFailureDTO (org.apache.sling.api.resource.runtime.dto.ResourceProviderFailureDTO)2 RuntimeDTO (org.apache.sling.api.resource.runtime.dto.RuntimeDTO)2 ResourceProviderHandler (org.apache.sling.resourceresolver.impl.providers.ResourceProviderHandler)2 ResourceProviderStorage (org.apache.sling.resourceresolver.impl.providers.ResourceProviderStorage)2 ResolveContext (org.apache.sling.spi.resource.provider.ResolveContext)2 ResourceContext (org.apache.sling.spi.resource.provider.ResourceContext)2 Before (org.junit.Before)2 ServiceRegistration (org.osgi.framework.ServiceRegistration)2 HashSet (java.util.HashSet)1 Hashtable (java.util.Hashtable)1 Iterator (java.util.Iterator)1 Set (java.util.Set)1 ServletException (javax.servlet.ServletException)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1