Search in sources :

Example 1 with LoginException

use of org.apache.sling.api.resource.LoginException in project sling by apache.

the class CommonResourceResolverFactoryImpl method getServiceUserAuthenticationInfo.

@Override
public Map<String, Object> getServiceUserAuthenticationInfo(final String subServiceName) throws LoginException {
    // get an administrative resource resolver
    // Ensure a mapped user name: If no user is defined for a bundle
    // acting as a service, the user may be null. We can decide whether
    // this should yield guest access or no access at all. For now
    // no access is granted if there is no service user defined for
    // the bundle.
    final Bundle bundle = this.activator.getBundleContext().getBundle();
    final String userName = this.activator.getServiceUserMapper().getServiceUserID(bundle, subServiceName);
    if (userName == null) {
        throw new LoginException("Cannot derive user name for bundle " + bundle + " and sub service " + subServiceName);
    }
    final Map<String, Object> authenticationInfo = new HashMap<>();
    // ensure proper user name and service bundle
    authenticationInfo.put(ResourceResolverFactory.SUBSERVICE, subServiceName);
    authenticationInfo.put(ResourceResolverFactory.USER, userName);
    authenticationInfo.put(ResourceProvider.AUTH_SERVICE_BUNDLE, bundle);
    return authenticationInfo;
}
Also used : HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Bundle(org.osgi.framework.Bundle) LoginException(org.apache.sling.api.resource.LoginException)

Example 2 with LoginException

use of org.apache.sling.api.resource.LoginException in project sling by apache.

the class MapEntries method getVanityPaths.

/**
     * get the vanity paths  Search for all nodes having a specific vanityPath
     */
private Map<String, List<MapEntry>> getVanityPaths(String vanityPath) {
    Map<String, List<MapEntry>> entryMap = new HashMap<>();
    // sling:vanityPath (lowercase) is the property name
    final String queryString = "SELECT sling:vanityPath, sling:redirect, sling:redirectStatus FROM nt:base WHERE sling:vanityPath =" + "'" + escapeIllegalXpathSearchChars(vanityPath).replaceAll("'", "''") + "' OR sling:vanityPath =" + "'" + escapeIllegalXpathSearchChars(vanityPath.substring(1)).replaceAll("'", "''") + "' ORDER BY sling:vanityOrder DESC";
    ResourceResolver queryResolver = null;
    try {
        queryResolver = factory.getServiceResourceResolver(factory.getServiceUserAuthenticationInfo("mapping"));
        final Iterator<Resource> i = queryResolver.findResources(queryString, "sql");
        while (i.hasNext()) {
            final Resource resource = i.next();
            boolean isValid = false;
            for (final Path sPath : this.factory.getObservationPaths()) {
                if (sPath.matches(resource.getPath())) {
                    isValid = true;
                    break;
                }
            }
            if (isValid) {
                if (this.factory.isMaxCachedVanityPathEntriesStartup() || vanityCounter.longValue() < this.factory.getMaxCachedVanityPathEntries()) {
                    loadVanityPath(resource, resolveMapsMap, vanityTargets, true, false);
                    entryMap = resolveMapsMap;
                } else {
                    final Map<String, List<String>> targetPaths = new HashMap<>();
                    loadVanityPath(resource, entryMap, targetPaths, true, false);
                }
            }
        }
    } catch (LoginException e) {
        log.error("Exception while obtaining queryResolver", e);
    } finally {
        if (queryResolver != null) {
            queryResolver.close();
        }
    }
    return entryMap;
}
Also used : Path(org.apache.sling.api.resource.path.Path) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Resource(org.apache.sling.api.resource.Resource) LoginException(org.apache.sling.api.resource.LoginException) List(java.util.List) ArrayList(java.util.ArrayList)

Example 3 with LoginException

use of org.apache.sling.api.resource.LoginException in project sling by apache.

the class ResourceResolverControl method checkSourceAndDest.

private AuthenticatedResourceProvider checkSourceAndDest(final ResourceResolverContext context, final String srcAbsPath, final String destAbsPath) throws PersistenceException {
    // check source
    final Node<ResourceProviderHandler> srcNode = getResourceProviderStorage().getTree().getBestMatchingNode(srcAbsPath);
    if (srcNode == null) {
        throw new PersistenceException("Source resource does not exist.", null, srcAbsPath, null);
    }
    AuthenticatedResourceProvider srcProvider = null;
    try {
        srcProvider = context.getProviderManager().getOrCreateProvider(srcNode.getValue(), this);
    } catch (LoginException e) {
    // ignore
    }
    if (srcProvider == null) {
        throw new PersistenceException("Source resource does not exist.", null, srcAbsPath, null);
    }
    final Resource srcResource = srcProvider.getResource(srcAbsPath, null, null);
    if (srcResource == null) {
        throw new PersistenceException("Source resource does not exist.", null, srcAbsPath, null);
    }
    // check destination
    final Node<ResourceProviderHandler> destNode = getResourceProviderStorage().getTree().getBestMatchingNode(destAbsPath);
    if (destNode == null) {
        throw new PersistenceException("Destination resource does not exist.", null, destAbsPath, null);
    }
    AuthenticatedResourceProvider destProvider = null;
    try {
        destProvider = context.getProviderManager().getOrCreateProvider(destNode.getValue(), this);
    } catch (LoginException e) {
    // ignore
    }
    if (destProvider == null) {
        throw new PersistenceException("Destination resource does not exist.", null, destAbsPath, null);
    }
    final Resource destResource = destProvider.getResource(destAbsPath, null, null);
    if (destResource == null) {
        throw new PersistenceException("Destination resource does not exist.", null, destAbsPath, null);
    }
    // check for sub providers of src and dest
    if (srcProvider == destProvider && !collectProviders(context, srcNode) && !collectProviders(context, destNode)) {
        return srcProvider;
    }
    return null;
}
Also used : ResourceProviderHandler(org.apache.sling.resourceresolver.impl.providers.ResourceProviderHandler) PersistenceException(org.apache.sling.api.resource.PersistenceException) Resource(org.apache.sling.api.resource.Resource) SyntheticResource(org.apache.sling.api.resource.SyntheticResource) LoginException(org.apache.sling.api.resource.LoginException) AuthenticatedResourceProvider(org.apache.sling.resourceresolver.impl.providers.stateful.AuthenticatedResourceProvider)

Example 4 with LoginException

use of org.apache.sling.api.resource.LoginException in project sling by apache.

the class ResourceResolverControl method getResourceTypeResourceResolver.

private ResourceResolver getResourceTypeResourceResolver(final ResourceResolverFactory factory, final ResourceResolver resolver) {
    if (this.isAdmin) {
        return resolver;
    } else {
        if (this.resourceTypeResourceResolver == null) {
            try {
                // make sure we're getting the resourceTypeResourceResolver on behalf of
                // the resourceresolver bundle
                final Bundle bundle = FrameworkUtil.getBundle(ResourceResolverControl.class);
                final Map<String, Object> authenticationInfo = new HashMap<>();
                authenticationInfo.put(ResourceProvider.AUTH_SERVICE_BUNDLE, bundle);
                authenticationInfo.put(ResourceResolverFactory.SUBSERVICE, "hierarchy");
                this.resourceTypeResourceResolver = factory.getServiceResourceResolver(authenticationInfo);
            } catch (final LoginException e) {
                throw new IllegalStateException("Failed to create resource-type ResourceResolver", e);
            }
        }
        return this.resourceTypeResourceResolver;
    }
}
Also used : HashMap(java.util.HashMap) IdentityHashMap(java.util.IdentityHashMap) Bundle(org.osgi.framework.Bundle) LoginException(org.apache.sling.api.resource.LoginException)

Example 5 with LoginException

use of org.apache.sling.api.resource.LoginException in project sling by apache.

the class BasicResolveContext method getParentProviderAndContext.

private Object[] getParentProviderAndContext() {
    ResourceProvider<?> parentProvider = null;
    ResolveContext<?> parentResolveContext = null;
    if (this.parentPath != null) {
        String path = this.parentPath;
        while (path != null && parentProvider == null) {
            final Node<ResourceProviderHandler> node = this.control.getResourceProviderStorage().getTree().getBestMatchingNode(path);
            if (node != null) {
                final ResourceProviderHandler handler = node.getValue();
                try {
                    parentResolveContext = this.resolveContextManager.getOrCreateResolveContext(handler, this.control);
                    if (parentResolveContext != null) {
                        parentProvider = handler.getResourceProvider();
                    }
                } catch (final LoginException se) {
                // skip this, try next
                }
                if (parentProvider == null) {
                    parentResolveContext = null;
                    path = ResourceUtil.getParent(path);
                }
            } else {
                path = null;
            }
        }
    }
    return parentProvider != null ? new Object[] { parentProvider, parentResolveContext } : null;
}
Also used : ResourceProviderHandler(org.apache.sling.resourceresolver.impl.providers.ResourceProviderHandler) LoginException(org.apache.sling.api.resource.LoginException)

Aggregations

LoginException (org.apache.sling.api.resource.LoginException)88 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)70 Resource (org.apache.sling.api.resource.Resource)47 PersistenceException (org.apache.sling.api.resource.PersistenceException)31 HashMap (java.util.HashMap)17 RepositoryException (javax.jcr.RepositoryException)13 ModifiableValueMap (org.apache.sling.api.resource.ModifiableValueMap)11 ArrayList (java.util.ArrayList)7 ValueMap (org.apache.sling.api.resource.ValueMap)6 HashSet (java.util.HashSet)5 Map (java.util.Map)5 ResourceResolverFactory (org.apache.sling.api.resource.ResourceResolverFactory)5 IOException (java.io.IOException)4 Calendar (java.util.Calendar)4 JsonException (javax.json.JsonException)4 ServletException (javax.servlet.ServletException)4 Authorizable (org.apache.jackrabbit.api.security.user.Authorizable)4 LinkedHashMap (java.util.LinkedHashMap)3 InstanceDescription (org.apache.sling.discovery.InstanceDescription)3 Bundle (org.osgi.framework.Bundle)3