ContextHubImpl.java 22.6 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.granite.security.user.UserManagementService
 *  com.adobe.granite.security.user.UserPropertiesService
 *  com.day.cq.widget.ClientLibrary
 *  com.day.cq.widget.HtmlLibraryManager
 *  com.day.cq.widget.LibraryType
 *  javax.jcr.Node
 *  javax.jcr.Property
 *  javax.jcr.RepositoryException
 *  javax.servlet.RequestDispatcher
 *  javax.servlet.ServletRequest
 *  javax.servlet.ServletResponse
 *  org.apache.commons.collections.Predicate
 *  org.apache.commons.collections.iterators.FilterIterator
 *  org.apache.commons.io.IOUtils
 *  org.apache.commons.lang3.StringUtils
 *  org.apache.felix.scr.annotations.Activate
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Property
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.api.SlingHttpServletRequest
 *  org.apache.sling.api.SlingHttpServletResponse
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ResourceUtil
 *  org.apache.sling.api.resource.ValueMap
 *  org.apache.sling.commons.osgi.PropertiesUtil
 *  org.apache.sling.settings.SlingSettingsService
 *  org.osgi.service.component.ComponentContext
 */
package com.adobe.granite.contexthub.impl;

import com.adobe.granite.contexthub.api.ContextHub;
import com.adobe.granite.contexthub.api.Mode;
import com.adobe.granite.contexthub.api.Module;
import com.adobe.granite.contexthub.api.Store;
import com.adobe.granite.contexthub.impl.ClientLib;
import com.adobe.granite.contexthub.impl.CustomResponseWrapper;
import com.adobe.granite.contexthub.impl.ModeConfigFilter;
import com.adobe.granite.contexthub.impl.ModuleConfigFilter;
import com.adobe.granite.contexthub.impl.StoreConfigFilter;
import com.adobe.granite.security.user.UserManagementService;
import com.adobe.granite.security.user.UserPropertiesService;
import com.day.cq.widget.ClientLibrary;
import com.day.cq.widget.HtmlLibraryManager;
import com.day.cq.widget.LibraryType;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.EnumSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import org.apache.commons.collections.Predicate;
import org.apache.commons.collections.iterators.FilterIterator;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.commons.osgi.PropertiesUtil;
import org.apache.sling.settings.SlingSettingsService;
import org.osgi.service.component.ComponentContext;

/*
 * This class specifies class file version 49.0 but uses Java 6 signatures.  Assumed Java 6.
 */
@Component(label="Adobe Granite ContextHub", metatype=1)
@Service
public class ContextHubImpl
implements ContextHub {
    @org.apache.felix.scr.annotations.Property(label="Silent Mode", description="Silent mode - turn this option on to disable ContextHub's debug output.", boolValue={0})
    private static final String PROPERTY_SILENT_MODE = "com.adobe.granite.contexthub.silent_mode";
    @org.apache.felix.scr.annotations.Property(label="Show UI", description="Indicates weather ContextHub's UI should be displayed.", boolValue={1})
    private static final String PROPERTY_SHOW_UI = "com.adobe.granite.contexthub.show_ui";
    private boolean silentMode;
    private boolean showUi;
    private Set<String> instanceRunMode;
    @Reference
    private HtmlLibraryManager htmlLibraryManager;
    @Reference
    private UserManagementService umService;
    @Reference
    private UserPropertiesService upService;
    @Reference
    private SlingSettingsService settings;

    @Activate
    private void activate(ComponentContext context, Map<String, Object> configuration) {
        this.silentMode = PropertiesUtil.toBoolean((Object)configuration.get("com.adobe.granite.contexthub.silent_mode"), (boolean)false);
        this.showUi = PropertiesUtil.toBoolean((Object)configuration.get("com.adobe.granite.contexthub.show_ui"), (boolean)true);
        this.instanceRunMode = this.settings.getRunModes();
        if (this.instanceRunMode == null) {
            this.instanceRunMode = new TreeSet<String>();
        }
    }

    private Resource findContextHub(Resource resource) {
        while (resource.getParent() != null) {
            boolean isConfigType = StringUtils.endsWith((CharSequence)resource.getResourceType(), (CharSequence)"granite/contexthub/cloudsettings/components/baseconfiguration");
            boolean isContexthubType = StringUtils.endsWith((CharSequence)resource.getResourceType(), (CharSequence)"granite/contexthub/components/contexthub");
            if (isConfigType || isContexthubType) {
                return resource;
            }
            resource = resource.getParent();
        }
        return null;
    }

    private Resource findContextHub(SlingHttpServletRequest request) {
        String path = (String)request.getAttribute("com.adobe.granite.contexthub.path");
        path = (String)StringUtils.defaultIfBlank((CharSequence)path, (CharSequence)"/etc/cloudsettings/default/contexthub");
        Resource resource = request.getResourceResolver().getResource(path);
        return this.findContextHub(resource);
    }

    @Override
    public Set<String> getRunModes() {
        return this.instanceRunMode;
    }

    @Override
    public String getPath(SlingHttpServletRequest request) {
        return this.getPath(request, true);
    }

    @Override
    public String getPath(SlingHttpServletRequest request, boolean checkRequestResource) {
        return checkRequestResource ? this.findContextHub(request.getResource()).getPath() : this.findContextHub(request).getPath();
    }

    @Override
    public List<Store> findStores(Resource contextHub) {
        Resource contexthubResource = this.findContextHub(contextHub);
        ArrayList<Store> contexthubStores = new ArrayList<Store>();
        FilterIterator it = new FilterIterator(contexthubResource.listChildren(), (Predicate)new StoreConfigFilter());
        while (it.hasNext()) {
            Resource storeResource = (Resource)it.next();
            contexthubStores.add(new Store(storeResource));
        }
        return contexthubStores;
    }

    @Override
    public List<Module> findModules(Resource contextHub) {
        Resource contexthubResource = this.findContextHub(contextHub);
        ArrayList<Module> contexthubModules = new ArrayList<Module>();
        FilterIterator modeIt = new FilterIterator(contexthubResource.listChildren(), (Predicate)new ModeConfigFilter());
        while (modeIt.hasNext()) {
            Resource modeResource = (Resource)modeIt.next();
            FilterIterator it = new FilterIterator(modeResource.listChildren(), (Predicate)new ModuleConfigFilter());
            while (it.hasNext()) {
                Resource moduleResource = (Resource)it.next();
                contexthubModules.add(new Module(moduleResource));
            }
        }
        return contexthubModules;
    }

    @Override
    public List<Mode> findModes(Resource contextHub) {
        Resource contexthubResource = this.findContextHub(contextHub);
        ArrayList<Mode> contexthubModes = new ArrayList<Mode>();
        FilterIterator modeIt = new FilterIterator(contexthubResource.listChildren(), (Predicate)new ModeConfigFilter());
        while (modeIt.hasNext()) {
            Resource modeResource = (Resource)modeIt.next();
            contexthubModes.add(new Mode(modeResource));
        }
        return contexthubModes;
    }

    @Override
    public List<Store> filterStores(Resource contextHub, EnumSet<ContextHub.StoreStatus> flags) {
        List<Store> allStores = this.findStores(contextHub);
        ArrayList<Store> result = new ArrayList<Store>();
        if (allStores != null) {
            for (Store store : allStores) {
                if (flags.contains((Object)ContextHub.StoreStatus.ENABLED) && !store.isEnabled() || flags.contains((Object)ContextHub.StoreStatus.DISABLED) && store.isEnabled()) continue;
                Set<String> storeRunModes = store.getRunModes();
                TreeSet<String> intersect = new TreeSet<String>(storeRunModes);
                intersect.retainAll(this.instanceRunMode);
                if (storeRunModes.size() > 0 && intersect.size() == 0) continue;
                result.add(store);
            }
        }
        return result;
    }

    @Override
    public List<Module> filterModules(Resource contextHub, EnumSet<ContextHub.ModuleStatus> flags) {
        List<Module> allModules = this.findModules(contextHub);
        ArrayList<Module> result = new ArrayList<Module>();
        if (allModules != null) {
            for (Module module : allModules) {
                if (flags.contains((Object)ContextHub.ModuleStatus.ENABLED) && !module.isEnabled() || flags.contains((Object)ContextHub.ModuleStatus.DISABLED) && module.isEnabled()) continue;
                Set<String> moduleRunModes = module.getRunModes();
                TreeSet<String> intersect = new TreeSet<String>(moduleRunModes);
                intersect.retainAll(this.instanceRunMode);
                if (moduleRunModes.size() > 0 && intersect.size() == 0) continue;
                result.add(module);
            }
        }
        return result;
    }

    public List<Mode> filterModes(Resource contextHub, EnumSet<ContextHub.ModeStatus> flags) {
        List<Mode> allModes = this.findModes(contextHub);
        ArrayList<Mode> result = new ArrayList<Mode>();
        if (allModes != null) {
            for (Mode mode : allModes) {
                if (flags.contains((Object)ContextHub.ModeStatus.ENABLED) && !mode.isEnabled() || flags.contains((Object)ContextHub.ModeStatus.DISABLED) && mode.isEnabled()) continue;
                Set<String> modeRunModes = mode.getRunModes();
                TreeSet<String> intersect = new TreeSet<String>(modeRunModes);
                intersect.retainAll(this.instanceRunMode);
                if (modeRunModes.size() > 0 && intersect.size() == 0) continue;
                result.add(mode);
            }
        }
        return result;
    }

    @Override
    public List<Store> getEnabledStores(Resource contextHub) {
        return this.filterStores(contextHub, EnumSet.of(ContextHub.StoreStatus.ENABLED));
    }

    @Override
    public List<Store> getDisabledStores(Resource contextHub) {
        return this.filterStores(contextHub, EnumSet.of(ContextHub.StoreStatus.DISABLED));
    }

    @Override
    public List<Module> getEnabledModules(Resource contextHub) {
        return this.filterModules(contextHub, EnumSet.of(ContextHub.ModuleStatus.ENABLED));
    }

    @Override
    public List<Module> getDisabledModules(Resource contextHub) {
        return this.filterModules(contextHub, EnumSet.of(ContextHub.ModuleStatus.DISABLED));
    }

    @Override
    public List<Mode> getEnabledModes(Resource contextHub) {
        return this.filterModes(contextHub, EnumSet.of(ContextHub.ModeStatus.ENABLED));
    }

    @Override
    public List<Mode> getDisabledModes(Resource contextHub) {
        return this.filterModes(contextHub, EnumSet.of(ContextHub.ModeStatus.DISABLED));
    }

    @Override
    public String getTheme(Resource contextHub) {
        String declaredTheme = (String)((ValueMap)this.findContextHub(contextHub).adaptTo(ValueMap.class)).get("theme", String.class);
        return (String)StringUtils.defaultIfEmpty((CharSequence)declaredTheme, (CharSequence)"default");
    }

    @Override
    public boolean isConfigured(Resource contextHub) {
        return this.findContextHub(contextHub) != null && this.findStores(contextHub).size() > 0;
    }

    @Override
    public boolean isConfigured(SlingHttpServletRequest request) {
        Resource contextHub = this.findContextHub(request);
        return contextHub != null && this.findStores(contextHub).size() > 0;
    }

    @Override
    public String getKernelCodeURL(SlingHttpServletRequest request) {
        return String.format("%s%s.%s.js", request.getContextPath(), this.findContextHub(request).getPath(), "kernel");
    }

    @Override
    public String getUICodeURL(SlingHttpServletRequest request) {
        return String.format("%s%s.%s.js", request.getContextPath(), this.findContextHub(request).getPath(), "ui");
    }

    @Override
    public String getStylesURL(SlingHttpServletRequest request) {
        return String.format("%s%s.%s.css", request.getContextPath(), this.findContextHub(request).getPath(), "styles");
    }

    @Override
    public String getUIPageURL(SlingHttpServletRequest request) {
        return request.getContextPath() + this.findContextHub(request).getPath() + ".ui.html";
    }

    @Override
    public List<String> getKernelCategories(SlingHttpServletRequest request) {
        Resource contextHub = this.findContextHub(request.getResource());
        ArrayList<String> clientLibs = new ArrayList<String>();
        List<Store> enabledStores = this.getEnabledStores(contextHub);
        clientLibs.add("sling:" + contextHub.getPath() + "." + "config" + "." + "kernel" + ".js");
        clientLibs.add(ClientLib.buildClientlibName("utils"));
        clientLibs.add(ClientLib.buildClientlibName("kernel"));
        clientLibs.add(ClientLib.buildClientlibName("generic-stores"));
        clientLibs.add(ClientLib.buildClientlibName("segment-engine"));
        clientLibs.add(ClientLib.buildClientlibName("segment-engine", "operators"));
        clientLibs.add(ClientLib.buildClientlibName("segment-engine", "operators", "custom"));
        clientLibs.add(ClientLib.buildClientlibName("segment-engine", "scripts"));
        clientLibs.add(ClientLib.buildClientlibName("segment-engine", "scripts", "custom"));
        clientLibs.add(ClientLib.buildClientlibName("segment-engine", "page-interaction"));
        for (Store store : enabledStores) {
            clientLibs.addAll(store.getCategories());
        }
        clientLibs.add(ClientLib.buildClientlibName("finalize"));
        if (this.showUi) {
            clientLibs.add(ClientLib.buildClientlibName("ui", "inject"));
        }
        return clientLibs;
    }

    @Override
    public List<String> getUICategories(SlingHttpServletRequest request) {
        Resource contextHub = this.findContextHub(request.getResource());
        ArrayList<String> clientLibs = new ArrayList<String>();
        List<Module> visibleModules = this.getEnabledModules(contextHub);
        List<Mode> visibleModes = this.getEnabledModes(contextHub);
        clientLibs.add("sling:" + contextHub.getPath() + "." + "config" + "." + "ui" + ".js");
        clientLibs.add(ClientLib.buildClientlibName("ui"));
        clientLibs.add(ClientLib.buildClientlibName("ui", "initialization"));
        clientLibs.add(ClientLib.buildClientlibName("ui", this.getTheme(contextHub)));
        LinkedList<String> includedModules = new LinkedList<String>();
        if (visibleModules.size() > 0) {
            for (Module module : visibleModules) {
                String moduleName = module.getName();
                if (includedModules.contains(moduleName)) continue;
                clientLibs.addAll(module.getCategories());
                includedModules.add(moduleName);
            }
        }
        if (visibleModes.size() > 0) {
            for (Mode mode : visibleModes) {
                clientLibs.addAll(mode.getCategories());
            }
        }
        clientLibs.add(ClientLib.buildClientlibName("ui", "finalization"));
        return clientLibs;
    }

    @Override
    public List<String> getStylesCategories(SlingHttpServletRequest request) {
        ArrayList<String> clientLibs = new ArrayList<String>();
        for (String category : this.getUICategories(request)) {
            if ((category.startsWith("file:") || category.startsWith("sling:")) && category.endsWith(".js")) continue;
            clientLibs.add(category);
        }
        return clientLibs;
    }

    private String combineCategories(SlingHttpServletRequest request, SlingHttpServletResponse response, List<String> categories) {
        StringBuilder code = new StringBuilder();
        Boolean debug = this.isDebug(request);
        ClientLib clientLib = new ClientLib(this.htmlLibraryManager, LibraryType.JS, debug);
        categories.add("sling:");
        ArrayList<String> group = new ArrayList<String>();
        for (String category : categories) {
            String path;
            block9 : {
                if (!category.startsWith("sling:") && !category.startsWith("file:")) {
                    group.add(category);
                    continue;
                }
                if (group.size() > 0) {
                    for (ClientLibrary library : clientLib.getLibraries(group)) {
                        if (library == null) continue;
                        code.append(clientLib.getContent(library));
                    }
                    group.clear();
                }
                if (category.startsWith("sling:") && (path = category.substring("sling:".length())).length() > 0) {
                    try {
                        CustomResponseWrapper responseWrapper = new CustomResponseWrapper(response);
                        RequestDispatcher rd = request.getRequestDispatcher(path);
                        rd.include((ServletRequest)request, (ServletResponse)responseWrapper);
                        String responseContent = responseWrapper.toString().trim();
                        code.append(responseContent);
                        code.append("\n");
                        responseWrapper.clearWriter();
                    }
                    catch (Exception e) {
                        if (!debug.booleanValue()) break block9;
                        code.append(String.format("\n/* Couldn't load '%s', due: %s */\n", path, e.getMessage()));
                    }
                }
            }
            if (!category.startsWith("file:") || (path = category.substring("file:".length())).length() <= 0) continue;
            try {
                Node node;
                Resource file = request.getResourceResolver().getResource(path);
                if (file == null || (node = (Node)file.adaptTo(Node.class)) == null) continue;
                Node jcrContent = node.getNode("jcr:content");
                InputStream is = jcrContent.getProperty("jcr:data").getStream();
                code.append(IOUtils.toString((InputStream)is, (String)"utf-8"));
            }
            catch (Exception e) {
                if (!debug.booleanValue()) continue;
                code.append(String.format("\n/* Couldn't load '%s', due: %s */\n", path, e.getMessage()));
            }
        }
        return code.toString();
    }

    @Override
    public String getKernelCode(SlingHttpServletRequest request, SlingHttpServletResponse response) {
        return this.combineCategories(request, response, this.getKernelCategories(request));
    }

    @Override
    public String getUICode(SlingHttpServletRequest request, SlingHttpServletResponse response) {
        return this.combineCategories(request, response, this.getUICategories(request));
    }

    @Override
    public String getStyles(SlingHttpServletRequest request) {
        StringBuilder styles = new StringBuilder();
        Boolean debug = this.isDebug(request);
        ClientLib clientLib = new ClientLib(this.htmlLibraryManager, LibraryType.CSS, debug);
        List<String> styleCategories = this.getStylesCategories(request);
        for (ClientLibrary library : clientLib.getLibraries(styleCategories)) {
            if (library == null) continue;
            styles.append(clientLib.getContent(library));
        }
        return styles.toString();
    }

    @Override
    public boolean showUi() {
        return this.showUi;
    }

    private Object getProperty(String propertyName, SlingHttpServletRequest request) {
        Resource contextHub = this.findContextHub(request);
        ValueMap properties = contextHub == null ? ValueMap.EMPTY : ResourceUtil.getValueMap((Resource)contextHub);
        return properties.get((Object)propertyName);
    }

    @Override
    public boolean isSilentMode() {
        return this.silentMode;
    }

    @Override
    public boolean isDebug(SlingHttpServletRequest request) {
        return !this.silentMode && this.getProperty("debug", request) == Boolean.TRUE;
    }

    @Override
    public boolean isDisabled(SlingHttpServletRequest request) {
        return this.getProperty("disabled", request) == Boolean.TRUE;
    }

    @Override
    public String getAnonymousPath() {
        String path = null;
        if (this.umService != null) {
            String id = this.umService.getAnonymousId();
            try {
                path = this.upService.getAuthorizablePath(id);
            }
            catch (RepositoryException var3_3) {
                // empty catch block
            }
        }
        return path;
    }

    protected void bindHtmlLibraryManager(HtmlLibraryManager htmlLibraryManager) {
        this.htmlLibraryManager = htmlLibraryManager;
    }

    protected void unbindHtmlLibraryManager(HtmlLibraryManager htmlLibraryManager) {
        if (this.htmlLibraryManager == htmlLibraryManager) {
            this.htmlLibraryManager = null;
        }
    }

    protected void bindUmService(UserManagementService userManagementService) {
        this.umService = userManagementService;
    }

    protected void unbindUmService(UserManagementService userManagementService) {
        if (this.umService == userManagementService) {
            this.umService = null;
        }
    }

    protected void bindUpService(UserPropertiesService userPropertiesService) {
        this.upService = userPropertiesService;
    }

    protected void unbindUpService(UserPropertiesService userPropertiesService) {
        if (this.upService == userPropertiesService) {
            this.upService = null;
        }
    }

    protected void bindSettings(SlingSettingsService slingSettingsService) {
        this.settings = slingSettingsService;
    }

    protected void unbindSettings(SlingSettingsService slingSettingsService) {
        if (this.settings == slingSettingsService) {
            this.settings = null;
        }
    }
}