/** * Create a new resource resolver for reading and writing the resource tree. * The resolver needs to be closed by the client. * @return A resource resolver or {@code null} if the component is already deactivated. * @throws RuntimeException if the resolver can't be created. */ public ResourceResolver createResourceResolver() { ResourceResolver resolver = null; final ResourceResolverFactory factory = this.resourceResolverFactory; if ( factory != null ) { try { resolver = this.resourceResolverFactory.getServiceResourceResolver(null); } catch ( final LoginException le) { logger.error("Unable to create new resource resolver: " + le.getMessage(), le); throw new RuntimeException(le); } } return resolver; }
/** Get the calling Bundle from auth info, fail if not provided * @throws LoginException if no calling bundle info provided */ @CheckForNull private Bundle extractCallingBundle(@Nonnull Map<String, Object> authenticationInfo) throws LoginException { final Object obj = authenticationInfo.get(ResourceProvider.AUTH_SERVICE_BUNDLE); if(obj != null && !(obj instanceof Bundle)) { throw new LoginException("Invalid calling bundle object in authentication info"); } return (Bundle)obj; }
@SuppressWarnings("deprecation") public String getValue() { // Auto-closing ResourceResolvers were made available in Sling 8 (and thus AEM 6.2) // If your code made be run on older Sling/AEM stacks, ensure you manually close any ResourceResolver you open. try (ResourceResolver rr = repo.getAdministrativeResourceResolver(null)) { return rr.getUserID(); } catch (LoginException e) { e.printStackTrace(); } return StringUtils.EMPTY; }
@Override public void run() { if (!isDataVersionStale()) { log.debug("{} - Data version '{}' is not stale, skip generation of new data version.", damPath, dataVersion); return; } try { log.debug("{} - Data version '{}' is stale, start generation of new data version.", damPath, dataVersion); generateDataVersion(); } catch (LoginException ex) { log.error("{} - Unable to get service resource resolver, please check service user configuration: {}", damPath, ex.getMessage()); } /*CHECKSTYLE:OFF*/ catch (Exception ex) { /*CHECKSTYLE:ON*/ log.error(damPath + " - Error generating data version: " + ex.getMessage(), ex); } }
private void checkIsLive() throws LoginException { if ( !isLive() ) { throw new LoginException("ResourceResolverFactory is deactivated."); } }
/** * The try-catch-finally approach can be replaced with the try-with-resources approach defined above in AEM 6.2 and later and Java7+. * * Example for getting the Bundle SubService User * * @return the user ID */ private final String getSubServiceUser() { // Create the Map to pass in the Service Account Identifier // Remember, "SERVICE_ACCOUNT_IDENTIFIER" is mapped to the CRX User via a SEPARATE ServiceUserMapper Factory OSGi Config final Map<String, Object> authInfo = Collections.singletonMap( ResourceResolverFactory.SUBSERVICE, (Object) SERVICE_ACCOUNT_IDENTIFIER); // Get the auto-closing Service resource resolver try (ResourceResolver serviceResolver = resourceResolverFactory.getServiceResourceResolver(authInfo)) { if (serviceResolver != null) { // Do some work w your service resource resolver return serviceResolver.getUserID(); } else { return "Could not obtain a User for the Service: " + SERVICE_ACCOUNT_IDENTIFIER; } } catch (LoginException e) { return "Login Exception when obtaining a User for the Bundle Service - " + e.getMessage(); } }
/** * @see org.apache.sling.api.resource.ResourceResolverFactory#getAdministrativeResourceResolver(java.util.Map) */ @Override public ResourceResolver getAdministrativeResourceResolver( Map<String, Object> authenticationInfo) throws LoginException { // usingBundle is required as bundles must now be whitelisted to use this method if (usingBundle == null) { throw new LoginException("usingBundle is null"); } if (authenticationInfo == null) { authenticationInfo = new HashMap<>(); } authenticationInfo.put(ResourceProvider.AUTH_SERVICE_BUNDLE, this.usingBundle); return commonFactory.getAdministrativeResourceResolver(authenticationInfo); }
/** * This uses the try-with-resources approach and auto-closes the resource resolver. * Available in AEM 6.2+ and Java 7+. * * Example for getting the Bundle Service User * * @return the user ID */ private final String getBundleServiceUser() { // Get the auto-closing Service resource resolver try (ResourceResolver serviceResolver = resourceResolverFactory.getServiceResourceResolver(null)) { if (serviceResolver != null) { // Do some work w your service resource resolver return serviceResolver.getUserID(); } else { return "Could not obtain a User for Bundle Service"; } } catch (LoginException e) { return "Login Exception when obtaining a User for the Bundle Service - " + e.getMessage(); } }
/** * Create a login exception from a repository exception. If the repository * exception is a {@link javax.jcr.LoginException} a {@link LoginException} * is created with the same information. Otherwise a {@link LoginException} * is created which wraps the repository exception. * * @param re * The repository exception. * @return The login exception. */ private static LoginException getLoginException(final RepositoryException re) { if (re instanceof javax.jcr.LoginException) { return new LoginException(re.getMessage(), re.getCause()); } return new LoginException("Unable to login " + re.getMessage(), re); }
log.error("Error issuing dispatcher flush rules do to repository login exception: {}", e.getMessage());
@Override public Map<String, Object> getServiceUserAuthenticationInfo(final String subServiceName) throws LoginException { // get an administrative resource resolver // Ensure a mapped user name or principal names: If no user/principal name(s) // is/are 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(); ServiceUserMapper mapper = this.activator.getServiceUserMapper(); final Iterable<String> principalNames = mapper.getServicePrincipalNames(bundle, subServiceName); final String userName = (principalNames == null) ? mapper.getServiceUserID(bundle, subServiceName) : null; if (principalNames == null && userName == null) { throw new LoginException("Cannot derive user name or principal names 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(ResourceProvider.AUTH_SERVICE_BUNDLE, bundle); if (userName != null) { authenticationInfo.put(ResourceResolverFactory.USER, userName); } return authenticationInfo; }
@Activate public void activate(final ResourcePresenterConfiguration configuration, final BundleContext bundleContext) { logger.info("activating resource presenter for {}", configuration.path()); path = configuration.path(); this.bundleContext = bundleContext; try (final ResourceResolver resourceResolver = getServiceResourceResolver()) { final Resource resource = resourceResolver.getResource(path); if (resource != null) { registerResourcePresence(); } registerResourceChangeListener(); } catch (LoginException e) { logger.error(e.getMessage(), e); } }
/** * @see org.apache.sling.api.resource.ResourceResolverFactory#getServiceResourceResolver(java.util.Map) */ @Override public ResourceResolver getServiceResourceResolver(final Map<String, Object> passedAuthenticationInfo) throws LoginException { final Map<String, Object> authenticationInfo = CommonResourceResolverFactoryImpl.sanitizeAuthenticationInfo(passedAuthenticationInfo, PASSWORD); final Object info = authenticationInfo.get(SUBSERVICE); final String subServiceName = (info instanceof String) ? (String) info : null; // Ensure a mapped user or principal name(s): If no user/principal names is/are // 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 Iterable<String> principalNames = this.serviceUserMapper.getServicePrincipalNames(this.usingBundle, subServiceName); if (principalNames == null) { final String userName = this.serviceUserMapper.getServiceUserID(this.usingBundle, subServiceName); if (userName == null) { throw new LoginException("Cannot derive user name for bundle " + this.usingBundle + " and sub service " + subServiceName); } else { // ensure proper user name authenticationInfo.put(ResourceResolverFactory.USER, userName); } } // ensure proper service bundle authenticationInfo.put(ResourceProvider.AUTH_SERVICE_BUNDLE, this.usingBundle); return commonFactory.getResourceResolverInternal(authenticationInfo, false); }
log.error("Error issuing dispatcher flush rules do to repository login exception: {}", e.getMessage()); } finally { if (resourceResolver != null) {
bundle.getSymbolicName(), bundle.getBundleId()); throw new LoginException("Repository unavailable"); throw new LoginException("Calling bundle missing in authentication info"); } else {
return "Login Exception when obtaining a User for the Service: " + SERVICE_ACCOUNT_IDENTIFIER + " - " + e.getMessage(); } finally {
/** * Get the context * @param handler The resource handler * @return The resource context or {@code null} if authentication failed previously. */ public @Nullable AuthenticatedResourceProvider getOrCreateProvider(@NotNull final ResourceProviderHandler handler, @NotNull final ResourceResolverControl control) throws LoginException { AuthenticatedResourceProvider provider = this.contextMap.get(handler); if (provider == null) { final ResourceProvider<Object> resourceProvider = handler.useResourceProvider(); if ( resourceProvider != null ) { try { provider = authenticate(handler, resourceProvider, control); this.contextMap.put(handler, provider); if ( handler.getInfo().getAuthType() == AuthType.lazy || handler.getInfo().getAuthType() == AuthType.required ) { control.registerAuthenticatedProvider(handler, provider.getResolveContext().getProviderState()); } } catch ( final LoginException le) { logger.debug("Authentication to resource provider " + resourceProvider + " failed: " + le.getMessage(), le); this.contextMap.put(handler, AuthenticatedResourceProvider.UNAUTHENTICATED_PROVIDER); throw le; } } else { this.contextMap.put(handler, AuthenticatedResourceProvider.UNAUTHENTICATED_PROVIDER); } } return provider == AuthenticatedResourceProvider.UNAUTHENTICATED_PROVIDER ? null : provider; }
/** * The try-catch-finally approach can be replaced with the try-with-resources approach defined above in AEM 6.2 and later and Java7+. * * Example for getting the Bundle Service User * * @return the user ID */ private final String getBundleServiceUser_LegacyApproach() { ResourceResolver serviceResolver = null; try { // Get the Service resource resolver serviceResolver = resourceResolverFactory.getServiceResourceResolver(null); if(serviceResolver != null) { // Do some work w your service resource resolver return serviceResolver.getUserID(); } else { return "Could not obtain a CRX User for Bundle Service"; } } catch (LoginException e) { return "Login Exception when obtaining a User for the Bundle Service - " + e.getMessage(); } finally { // As always, clean up any ResourceResolvers or JCR Sessions you open if (serviceResolver != null) { serviceResolver.close(); } } }
new Object[]{deleted, total, packageBuilder.getType()}); } catch (LoginException e) { log.error("Failed to get distribution service resolver: {}", e.getMessage()); } catch (DistributionException e) { log.error("Failed to get the list of packages", e);
log.error("Could not add package thumbnail: {}", e.getMessage()); } catch (LoginException e) { log.error("Could not add a default package thumbnail: {}", e.getMessage()); } finally { if (resourceResolver != null) {