SlingRepositoryImpl.java 8.47 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.crx.CRXModule
 *  com.day.crx.CRXRepository
 *  com.day.crx.License
 *  com.google.common.collect.ImmutableMap
 *  com.google.common.collect.ImmutableSet
 *  javax.jcr.Credentials
 *  javax.jcr.LoginException
 *  javax.jcr.NoSuchWorkspaceException
 *  javax.jcr.Repository
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  org.apache.jackrabbit.api.JackrabbitRepository
 *  org.apache.jackrabbit.api.management.DataStoreGarbageCollector
 *  org.apache.jackrabbit.api.management.RepositoryManager
 *  org.apache.jackrabbit.oak.spi.security.ConfigurationParameters
 *  org.apache.jackrabbit.oak.spi.security.SecurityProvider
 *  org.apache.jackrabbit.oak.spi.security.authentication.AuthInfoImpl
 *  org.apache.jackrabbit.oak.spi.security.principal.AdminPrincipal
 *  org.apache.jackrabbit.oak.spi.security.principal.SystemUserPrincipal
 *  org.apache.jackrabbit.oak.spi.security.user.UserConfiguration
 *  org.apache.sling.jcr.api.SlingRepository
 *  org.apache.sling.jcr.base.AbstractSlingRepository2
 *  org.apache.sling.jcr.base.AbstractSlingRepositoryManager
 *  org.osgi.framework.Bundle
 */
package com.adobe.granite.repository.impl;

import com.adobe.granite.repository.impl.CRX3RepositoryImpl;
import com.adobe.granite.repository.impl.ServiceUsersConfigurationManager;
import com.adobe.granite.repository.impl.SlingRepositoryManager;
import com.day.crx.CRXModule;
import com.day.crx.CRXRepository;
import com.day.crx.License;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import java.io.File;
import java.security.Principal;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import javax.jcr.Credentials;
import javax.jcr.LoginException;
import javax.jcr.NoSuchWorkspaceException;
import javax.jcr.Repository;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.security.auth.Subject;
import org.apache.jackrabbit.api.JackrabbitRepository;
import org.apache.jackrabbit.api.management.DataStoreGarbageCollector;
import org.apache.jackrabbit.api.management.RepositoryManager;
import org.apache.jackrabbit.oak.spi.security.ConfigurationParameters;
import org.apache.jackrabbit.oak.spi.security.SecurityProvider;
import org.apache.jackrabbit.oak.spi.security.authentication.AuthInfoImpl;
import org.apache.jackrabbit.oak.spi.security.principal.AdminPrincipal;
import org.apache.jackrabbit.oak.spi.security.principal.SystemUserPrincipal;
import org.apache.jackrabbit.oak.spi.security.user.UserConfiguration;
import org.apache.sling.jcr.api.SlingRepository;
import org.apache.sling.jcr.base.AbstractSlingRepository2;
import org.apache.sling.jcr.base.AbstractSlingRepositoryManager;
import org.osgi.framework.Bundle;

public class SlingRepositoryImpl
extends AbstractSlingRepository2
implements CRXRepository,
JackrabbitRepository,
Repository,
SlingRepository,
RepositoryManager {
    private final SlingRepositoryManager manager;
    private final CRX3RepositoryImpl delegate;
    private final ServiceUsersConfigurationManager serviceUsersConfigurationManager;

    public SlingRepositoryImpl(SlingRepositoryManager manager, Bundle usingBundle, CRX3RepositoryImpl delegate, ServiceUsersConfigurationManager serviceUsersConfigurationManager) {
        super((AbstractSlingRepositoryManager)manager, usingBundle);
        this.manager = manager;
        this.delegate = delegate;
        this.serviceUsersConfigurationManager = serviceUsersConfigurationManager;
    }

    public void stop() {
        throw new UnsupportedOperationException("RepositoryManager.stop()");
    }

    public DataStoreGarbageCollector createDataStoreGarbageCollector() throws RepositoryException {
        if (this.delegate instanceof RepositoryManager) {
            return ((RepositoryManager)this.delegate).createDataStoreGarbageCollector();
        }
        return null;
    }

    protected Session createAdministrativeSession(String workspace) throws RepositoryException {
        Session adminSession;
        SecurityProvider securityProvider = this.manager.getSecurityProvider();
        final String adminId = (String)((UserConfiguration)securityProvider.getConfiguration(UserConfiguration.class)).getParameters().getConfigValue("adminId", (Object)"admin");
        Set<> principals = Collections.singleton(new AdminPrincipal(){

            public String getName() {
                return adminId;
            }
        });
        AuthInfoImpl authInfo = new AuthInfoImpl(adminId, Collections.emptyMap(), principals);
        Subject subject = new Subject(true, principals, Collections.singleton(authInfo), Collections.emptySet());
        try {
            adminSession = (Session)Subject.doAsPrivileged(subject, new PrivilegedExceptionAction<Session>(){

                @Override
                public Session run() throws Exception {
                    ImmutableMap attrs = ImmutableMap.of((Object)"oak.refresh-interval", (Object)0, (Object)"oak.relaxed-locking", (Object)Boolean.TRUE);
                    return SlingRepositoryImpl.this.delegate.login(null, null, (Map<String, Object>)attrs);
                }
            }, null);
        }
        catch (PrivilegedActionException e) {
            throw new RepositoryException("failed to retrieve service session.", (Throwable)e);
        }
        return adminSession;
    }

    protected Session createServiceSession(String serviceUserName, String workspace) throws RepositoryException {
        if (!this.serviceUsersConfigurationManager.isServiceUserFastPathEnabled(serviceUserName)) {
            return super.createServiceSession(serviceUserName, workspace);
        }
        return this.createServiceSessionFastPath(serviceUserName, workspace);
    }

    protected Session createServiceSessionFastPath(final String serviceUserName, String workspace) throws RepositoryException {
        Session serviceSession;
        ImmutableSet servicePrincipals = ImmutableSet.of((Object)new SystemUserPrincipal(){

            public String getName() {
                return serviceUserName;
            }
        });
        AuthInfoImpl authInfo = new AuthInfoImpl(serviceUserName, Collections.emptyMap(), (Set)servicePrincipals);
        Subject subject = new Subject(true, (Set<? extends Principal>)servicePrincipals, Collections.singleton(authInfo), Collections.emptySet());
        try {
            serviceSession = (Session)Subject.doAsPrivileged(subject, new PrivilegedExceptionAction<Session>(){

                @Override
                public Session run() throws Exception {
                    return SlingRepositoryImpl.this.delegate.login(null, null);
                }
            }, null);
        }
        catch (PrivilegedActionException e) {
            throw new RepositoryException("failed to retrieve admin session.", (Throwable)e);
        }
        return serviceSession;
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    public Session impersonateFromService(String subServiceName, Credentials credentials, String workspaceName) throws LoginException, RepositoryException {
        Session serviceSession = null;
        try {
            serviceSession = this.createAdministrativeSession(workspaceName);
            Session session = serviceSession.impersonate(credentials);
            return session;
        }
        finally {
            if (serviceSession != null && serviceSession.isLive()) {
                serviceSession.logout();
            }
        }
    }

    public File getHomeDir() {
        return this.delegate.getHomeDir();
    }

    public License getLicense() {
        return this.delegate.getLicense();
    }

    public CRXModule getModule(String moduleName) {
        return this.delegate.getModule(moduleName);
    }

    public CRXModule[] getModules() {
        return this.delegate.getModules();
    }

    public void installModule(Session session, CRXModule module) throws RepositoryException {
        this.delegate.installModule(session, module);
    }

    public void uninstallModule(Session session, String moduleName) {
        this.delegate.uninstallModule(session, moduleName);
    }

    public Session login(Credentials credentials, String s, Map<String, Object> stringObjectMap) throws LoginException, NoSuchWorkspaceException, RepositoryException {
        return this.delegate.login(credentials, s, stringObjectMap);
    }

    public void shutdown() {
        this.delegate.shutdown();
    }

}