Activator.java 3.82 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ResourceResolverFactory
 *  org.apache.sling.api.resource.ResourceUtil
 *  org.osgi.framework.Bundle
 *  org.osgi.framework.BundleActivator
 *  org.osgi.framework.BundleContext
 *  org.osgi.framework.BundleEvent
 *  org.osgi.framework.ServiceReference
 *  org.osgi.util.tracker.BundleTracker
 *  org.osgi.util.tracker.BundleTrackerCustomizer
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.granite.xss.impl;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.api.resource.ResourceUtil;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleEvent;
import org.osgi.framework.ServiceReference;
import org.osgi.util.tracker.BundleTracker;
import org.osgi.util.tracker.BundleTrackerCustomizer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Activator
implements BundleActivator {
    private static final Logger LOGGER = LoggerFactory.getLogger(Activator.class);
    private static final String SLING_XSS_BUNDLE = "org.apache.sling.xss";
    private static final String SLING_ESAPI_POLICIES = "/libs/sling/xss/config.xml";
    private BundleTracker bundleTracker;

    public void start(final BundleContext bundleContext) throws Exception {
        this.bundleTracker = new BundleTracker(bundleContext, 32, null){

            public Object addingBundle(Bundle bundle, BundleEvent event) {
                if ("org.apache.sling.xss".equals(bundle.getSymbolicName())) {
                    try {
                        ResourceResolverFactory rrf;
                        bundle.uninstall();
                        LOGGER.warn("Uninstalled the {} bundle due to its potential conflicts with com.day.cq.cq-xssprotection which embeds it.", (Object)"org.apache.sling.xss");
                        ServiceReference rrfSR = bundleContext.getServiceReference(ResourceResolverFactory.class);
                        if (rrfSR != null && (rrf = (ResourceResolverFactory)bundleContext.getService(rrfSR)) != null) {
                            ResourceResolver adminResolver = rrf.getAdministrativeResourceResolver(null);
                            Resource parent = adminResolver.getResource(ResourceUtil.getParent((String)"/libs/sling/xss/config.xml"));
                            if (parent != null && parent.getChild("config.xml") == null) {
                                Resource config = adminResolver.create(parent, ResourceUtil.getName((String)"/libs/sling/xss/config.xml"), (Map)new HashMap<String, Object>(){});
                                adminResolver.create(config, "jcr:content", (Map)new HashMap<String, Object>(){});
                            }
                            adminResolver.commit();
                            adminResolver.close();
                            bundleContext.ungetService(rrfSR);
                        }
                    }
                    catch (Exception e) {
                        LOGGER.error("Unable to uninstall bundle org.apache.sling.xss. Please remove the bundle manually due to its potential conflicts with com.day.cq.cq-xssprotection.", (Throwable)e);
                    }
                }
                return super.addingBundle(bundle, event);
            }

        };
        this.bundleTracker.open();
    }

    public void stop(BundleContext bundleContext) throws Exception {
        if (this.bundleTracker != null) {
            this.bundleTracker.close();
        }
    }

}