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;
}
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;
}
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;
}
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;
}
}
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;
}
Aggregations