GuideNode.java 22.1 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 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.cq.sightly.WCMUse
 *  com.adobe.granite.xss.XSSAPI
 *  com.day.cq.i18n.I18n
 *  com.day.cq.wcm.api.WCMMode
 *  javax.jcr.RepositoryException
 *  javax.servlet.ServletRequest
 *  javax.servlet.http.HttpServletRequest
 *  org.apache.commons.lang3.StringEscapeUtils
 *  org.apache.commons.lang3.StringUtils
 *  org.apache.sling.api.SlingHttpServletRequest
 *  org.apache.sling.api.resource.ModifiableValueMap
 *  org.apache.sling.api.resource.PersistenceException
 *  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.json.JSONArray
 *  org.apache.sling.commons.json.JSONException
 *  org.apache.sling.commons.json.JSONObject
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.aemds.guide.common;

import com.adobe.aemds.guide.service.GuideException;
import com.adobe.aemds.guide.taglibs.GuideELUtils;
import com.adobe.aemds.guide.utils.GuideConstants;
import com.adobe.aemds.guide.utils.GuideContainerThreadLocal;
import com.adobe.aemds.guide.utils.GuideFragmentHolder;
import com.adobe.aemds.guide.utils.GuideUtils;
import com.adobe.aemds.guide.utils.NodeStructureUtils;
import com.adobe.cq.sightly.WCMUse;
import com.adobe.granite.xss.XSSAPI;
import com.day.cq.i18n.I18n;
import com.day.cq.wcm.api.WCMMode;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import javax.jcr.RepositoryException;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.ModifiableValueMap;
import org.apache.sling.api.resource.PersistenceException;
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.json.JSONArray;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/*
 * This class specifies class file version 49.0 but uses Java 6 signatures.  Assumed Java 6.
 */
public class GuideNode
extends WCMUse
implements Serializable {
    protected SlingHttpServletRequest slingRequest = null;
    protected Resource resource = null;
    protected ValueMap resourceProps = null;
    protected String id = null;
    protected String clientLibRef = null;
    protected String baseName = null;
    protected String localeName = null;
    protected I18n i18n = null;
    protected Locale locale = null;
    protected XSSAPI xssapi = null;
    protected String error = null;
    protected Map<String, String> inlineStyles = null;
    protected Logger logger = LoggerFactory.getLogger(GuideNode.class);

    public void activate() throws Exception {
        this.setResource(super.getResource());
        this.setSlingRequest(this.getRequest());
    }

    public String getAuthoringConfigJSON() {
        Map<String, Object> authoringConfig = this.getAuthoringConfig();
        JSONObject authoringConfigJson = new JSONObject(authoringConfig);
        return authoringConfigJson.toString();
    }

    public Map<String, Object> getAuthoringConfig() {
        HashMap<String, Object> authoringConfig = new HashMap<String, Object>();
        int colspan = this.getColspan();
        String title = this.getTitle();
        authoringConfig.put("colspan", colspan);
        authoringConfig.put("title", " ".equals(title) ? this.getName() : title);
        return authoringConfig;
    }

    public boolean checkIfPathIsAbsolute(String path) {
        if ((StringUtils.startsWith((CharSequence)path, (CharSequence)"/libs") || StringUtils.startsWith((CharSequence)path, (CharSequence)"/apps")) && StringUtils.endsWith((CharSequence)path, (CharSequence)".jsp")) {
            return true;
        }
        return false;
    }

    public String getAppearance() {
        return (String)this.resourceProps.get("appearance", (Object)"");
    }

    public String getStyles() {
        String style = "";
        String width = this.getWidth();
        String height = this.getHeight();
        style = style + (width.length() > 0 ? new StringBuilder().append("max-width:").append(width).append("%;").toString() : "");
        style = style + (height.length() > 0 ? new StringBuilder().append("height:").append(height).append("px;").toString() : "");
        return style;
    }

    public String getInlineStyles(String name) {
        if (this.inlineStyles == null) {
            this.inlineStyles = new HashMap<String, String>();
            String[] test = new String[]{""};
            String[] inlineStylesProperties = (String[])this.resourceProps.get("inlineStyles", (Object)test);
            if (!"".equals(inlineStylesProperties[0])) {
                for (int i = 0; i < inlineStylesProperties.length; ++i) {
                    String componentProperties = inlineStylesProperties[i].substring(inlineStylesProperties[i].indexOf("{") + 1, inlineStylesProperties[i].indexOf("}"));
                    componentProperties = componentProperties.replace(',', ';');
                    String prefix = inlineStylesProperties[i].substring(0, inlineStylesProperties[i].indexOf("{"));
                    if ("Field".equalsIgnoreCase(prefix)) {
                        this.inlineStyles.put("Field", componentProperties);
                        continue;
                    }
                    if ("Panel".equalsIgnoreCase(prefix)) {
                        this.inlineStyles.put("Panel", componentProperties);
                        continue;
                    }
                    if ("PanelDescription".equalsIgnoreCase(prefix)) {
                        this.inlineStyles.put("PanelDescription", componentProperties);
                        continue;
                    }
                    if ("Widget".equalsIgnoreCase(prefix)) {
                        this.inlineStyles.put("Widget", componentProperties);
                        continue;
                    }
                    if ("Caption".equalsIgnoreCase(prefix)) {
                        this.inlineStyles.put("Caption", componentProperties);
                        continue;
                    }
                    if ("QuestionMark".equalsIgnoreCase(prefix)) {
                        this.inlineStyles.put("QuestionMark", componentProperties);
                        continue;
                    }
                    if ("ShortDescription".equalsIgnoreCase(prefix)) {
                        this.inlineStyles.put("ShortDescription", componentProperties);
                        continue;
                    }
                    if (!"LongDescription".equalsIgnoreCase(prefix)) continue;
                    this.inlineStyles.put("LongDescription", componentProperties);
                }
            }
        }
        return this.inlineStyles.get(name);
    }

    public String getFieldInlineStyles() {
        return this.getInlineStyles("Field");
    }

    public String getQuestionMarkInlineStyles() {
        return this.getInlineStyles("QuestionMark");
    }

    public String getLongDescriptionInlineStyles() {
        return this.getInlineStyles("LongDescription");
    }

    public Integer getColspan() {
        return Integer.parseInt((String)this.resourceProps.get("colspan", (Object)"1"));
    }

    public String getHeight() {
        return (String)this.resourceProps.get("height", (Object)"");
    }

    public String getWidth() {
        return (String)this.resourceProps.get("width", (Object)"");
    }

    public void setResource(Resource resource) {
        this.resource = resource;
        this.resourceProps = ResourceUtil.getValueMap((Resource)resource);
        this.id = NodeStructureUtils.getGuideNodeHtmlId(this.resource);
    }

    public void setSlingRequest(SlingHttpServletRequest slingRequest) {
        this.slingRequest = slingRequest;
        try {
            GuideFragmentHolder guideFragmentHolder = GuideContainerThreadLocal.getGuideFragmentHolder();
            if (guideFragmentHolder != null) {
                this.i18n = guideFragmentHolder.getI18n();
                if (this.i18n == null && slingRequest != null) {
                    this.i18n = GuideELUtils.getI18n(slingRequest, this.resource);
                }
            } else if (slingRequest != null) {
                this.i18n = GuideELUtils.getI18n(slingRequest, this.resource);
            }
        }
        catch (Exception e) {
            this.logger.error("Error in setting sling request in guide node", (Throwable)e);
        }
    }

    public String getEnabledExp() {
        return (String)this.resourceProps.get("enabledExp", (Object)"");
    }

    public String getName() {
        return StringEscapeUtils.escapeHtml4((String)NodeStructureUtils.getGuideName(this.resource));
    }

    public String getDescription() {
        return this.externalize((String)this.resourceProps.get("jcr:description", (Object)""));
    }

    public String getXssDescription() {
        return this.i18n.getVar(this.getDescription());
    }

    public String getId() throws RepositoryException {
        return this.id;
    }

    public Boolean getIsEditMode() {
        return WCMMode.fromRequest((ServletRequest)this.slingRequest) == WCMMode.EDIT || WCMMode.fromRequest((ServletRequest)this.slingRequest) == WCMMode.DESIGN;
    }

    public boolean getIsRepeatable() {
        return false;
    }

    public List<GuideNode> getItems() {
        return new ArrayList<GuideNode>();
    }

    public String getPath() {
        return this.resource.getPath();
    }

    public String getResourceType() {
        return this.resource.getResourceType();
    }

    public String getResourceSuperType() {
        return this.resource.getResourceSuperType();
    }

    public String getLayoutPath() throws PersistenceException {
        return (String)this.getLayoutProperty("sling:resourceType");
    }

    public boolean isLayoutNavigable() throws PersistenceException {
        String nonNavigableProp = (String)this.getLayoutProperty("nonNavigable");
        if (nonNavigableProp == null) {
            String layoutPath = GuideUtils.getNormalizedNodeType(this.getLayoutPath(), null);
            nonNavigableProp = "fd/af/layouts/gridFluidLayout".equals(layoutPath) ? "true" : "false";
        }
        return "false".equals(nonNavigableProp);
    }

    public boolean isMenuRequired() {
        String normalizedResourceType = GuideUtils.getNormalizedNodeType(this.getResourceType(), this.getResourceSuperType());
        if (GuideConstants.MOBILE_MENU_RESOURCES.contains(normalizedResourceType)) {
            return true;
        }
        return false;
    }

    public boolean isDocumentFragmentGroup() {
        String normalizedResourceType = GuideUtils.getNormalizedNodeType(this.getResourceType(), this.getResourceSuperType());
        if ("fd/adaddon/components/guideAdModuleGroup".equals(normalizedResourceType)) {
            return true;
        }
        return false;
    }

    protected Object getLayoutProperty(String property) throws PersistenceException {
        Map<String, Object> layoutproperties = NodeStructureUtils.getLayoutProperties(this.slingRequest, this.resource, "panel");
        if (layoutproperties != null) {
            return layoutproperties.get(property);
        }
        return null;
    }

    public String getLayoutTabType() throws PersistenceException {
        String tabType = (String)this.getLayoutProperty("guideNavigatorTab");
        if (tabType == null) {
            String layoutPath = GuideUtils.getNormalizedNodeType(this.getLayoutPath(), null);
            tabType = "fd/af/layouts/panel/wizard".equals(layoutPath) ? "wizard-tab" : "tab";
        }
        return tabType;
    }

    public String getToolbarPosition() throws PersistenceException {
        String layout;
        Map<String, Object> layoutproperties = NodeStructureUtils.getLayoutProperties(this.slingRequest, this.resource, "panel");
        if (layoutproperties != null && StringUtils.isNotEmpty((CharSequence)(layout = (String)layoutproperties.get("toolbarPosition")))) {
            return layout;
        }
        return "Bottom";
    }

    public String getAddMessage() {
        String add = this.i18n.get("Add ");
        return add + this.getNavTitle();
    }

    public String getRemoveMessage() {
        String add = this.i18n.get("Remove ");
        return add + this.getNavTitle();
    }

    public String getNavTitle() {
        return this.externalize((String)this.resourceProps.get("navTitle", (Object)this.getTitle()));
    }

    public String getTitle() {
        return this.externalize((String)this.resourceProps.get("jcr:title", (Object)" "));
    }

    public SlingHttpServletRequest getSlingRequest() {
        return this.slingRequest;
    }

    public Resource getResource() {
        return this.resource;
    }

    public String getNodeClass() {
        return (String)this.resourceProps.get("guideNodeClass", (Object)"");
    }

    public String getVisibleExp() {
        return (String)this.resourceProps.get("visibleExp", (Object)"");
    }

    public XSSAPI getXssapi() {
        if (this.slingRequest != null) {
            return (XSSAPI)this.slingRequest.adaptTo(XSSAPI.class);
        }
        return null;
    }

    public String getCssClassName() {
        return (String)this.resourceProps.get("css", (Object)"");
    }

    public String externalize(String key) {
        boolean editMode;
        boolean bl = editMode = WCMMode.fromRequest((ServletRequest)this.slingRequest) == WCMMode.EDIT || WCMMode.fromRequest((ServletRequest)this.slingRequest) == WCMMode.DESIGN;
        if (editMode) {
            return key;
        }
        if (key == null || "".equals(key)) {
            return null;
        }
        String translatedStringOrOriginal = GuideUtils.translateOrReturnOriginal(key, this.i18n);
        return translatedStringOrOriginal;
    }

    public String getError() {
        return this.error;
    }

    public String getBindRef() {
        return (String)this.resourceProps.get((Object)"bindRef");
    }

    private String getXDPJSON(String bindref) {
        boolean refersXSD;
        ResourceResolver resourceResolver = this.resource.getResourceResolver();
        Resource guideContainerResource = resourceResolver.getResource(GuideELUtils.getGuideContainerPath(this.slingRequest, this.resource));
        ValueMap map = ResourceUtil.getValueMap((Resource)guideContainerResource);
        boolean refersXDP = map.get((Object)"xdpRef") != null;
        boolean bl = refersXSD = map.get((Object)"xsdRef") != null;
        if (bindref == null) {
            return null;
        }
        if (refersXSD) {
            return "xsd";
        }
        if (!refersXDP) {
            return "";
        }
        Resource assetsResource = resourceResolver.getResource(guideContainerResource, "assets/xfaObjects");
        if (assetsResource != null) {
            ValueMap xfaObjects = ResourceUtil.getValueMap((Resource)assetsResource);
            String obj = (String)xfaObjects.get((Object)bindref);
            return obj;
        }
        return null;
    }

    public boolean isValid() {
        String bindref = this.getBindRef();
        if (bindref != null) {
            String json = this.getXDPJSON(bindref);
            if ("xsd".equals(json)) {
                return true;
            }
            if (json == null) {
                I18n i18n = new I18n((HttpServletRequest)this.slingRequest);
                String e1 = i18n.get("NoSuchSOMExpression: bindRef (");
                String e2 = i18n.get(") provided doesn't point to an valid element in XDP.");
                this.error = e1 + bindref + e2;
                return false;
            }
            return true;
        }
        return true;
    }

    private boolean isPropertySame(Object oldProp, Object newProp) throws JSONException {
        if (oldProp == null && newProp == null) {
            return true;
        }
        if (oldProp == null) {
            return false;
        }
        if (newProp == null) {
            return false;
        }
        if (oldProp instanceof JSONArray && newProp instanceof String[]) {
            JSONArray sop = (JSONArray)oldProp;
            String[] snp = (String[])newProp;
            if (sop.length() != snp.length) {
                return false;
            }
            boolean flag = true;
            for (int i = 0; i < sop.length(); ++i) {
                flag = flag && sop.get(i).equals(snp[i]);
            }
            return flag;
        }
        return oldProp.equals(newProp);
    }

    private ArrayList<String> getModifiedXfaProps(JSONObject oldXfaProps) {
        ArrayList<String> props = new ArrayList<String>();
        if (oldXfaProps != null) {
            String prop = null;
            try {
                Iterator propIterator = oldXfaProps.keys();
                while (propIterator.hasNext()) {
                    prop = (String)propIterator.next();
                    if (!this.isPropertySame(oldXfaProps.get(prop), this.resourceProps.get((Object)prop))) continue;
                    this.logger.info("AEMAF unmodified property in Guide " + prop);
                    props.add(prop);
                }
            }
            catch (JSONException e) {
                this.logger.error("AEMAF error in accessing old properties " + e.getMessage() + " old property " + prop, (Throwable)e);
            }
        } else {
            this.logger.error("AEMAF xfajson attribute not present in the node " + this.resource.getPath());
        }
        return props;
    }

    private void syncProp(String prop, Object newJSONVal, ModifiableValueMap modifiableResourceProps) throws JSONException {
        Object[] newVal = newJSONVal;
        if (newJSONVal instanceof JSONArray) {
            JSONArray newJSONArr = (JSONArray)newJSONVal;
            Object[] list = new Object[newJSONArr.length()];
            for (int j = 0; j < newJSONArr.length(); ++j) {
                list[j] = newJSONArr.get(j);
            }
            newVal = list;
        }
        Object oldVal = this.resourceProps.get((Object)prop);
        if (newVal == null) {
            modifiableResourceProps.remove((Object)prop);
            this.logger.info("AEMAF Removed property [ " + prop + " : " + oldVal + " ]");
        } else {
            modifiableResourceProps.put((Object)prop, (Object)newVal);
            this.logger.info("AEMAF Modified property [ " + prop + " : " + oldVal + " => " + newVal + " ]");
        }
    }

    private void syncProps(JSONObject bindNode, ArrayList<String> unchangedProps, JSONObject oldProps) throws JSONException {
        Iterator newPropIterator = bindNode.keys();
        ModifiableValueMap modifiableResourceProps = (ModifiableValueMap)this.resource.adaptTo(ModifiableValueMap.class);
        while (newPropIterator.hasNext()) {
            String prop = (String)newPropIterator.next();
            if (!unchangedProps.contains(prop) && (oldProps.has(prop) || this.resourceProps.containsKey((Object)prop))) continue;
            this.syncProp(prop, bindNode.get(prop), modifiableResourceProps);
            if (!unchangedProps.contains(prop)) continue;
            unchangedProps.remove(prop);
        }
        for (int i = 0; i < unchangedProps.size(); ++i) {
            String prop = unchangedProps.get(i);
            this.syncProp(prop, null, modifiableResourceProps);
        }
        try {
            modifiableResourceProps.put((Object)"xfajson", (Object)bindNode.toString());
            this.resource.getResourceResolver().commit();
            this.resourceProps = (ValueMap)this.resource.adaptTo(ValueMap.class);
        }
        catch (PersistenceException e) {
            this.logger.error("AEMAF Unable to commit changes for the node " + this.resource.getPath(), (Throwable)e);
        }
    }

    public void syncNode() throws GuideException {
        String bindref = this.getBindRef();
        String jsonString = (String)this.resourceProps.get((Object)"xfajson");
        if (bindref != null && jsonString != null) {
            try {
                JSONObject oldXfaProps = new JSONObject(jsonString);
                ArrayList<String> unchangedProps = this.getModifiedXfaProps(oldXfaProps);
                if (!unchangedProps.isEmpty()) {
                    String json = this.getXDPJSON(bindref);
                    if (json != null) {
                        try {
                            JSONObject bindNode = new JSONObject(json);
                            this.syncProps(bindNode, unchangedProps, oldXfaProps);
                        }
                        catch (JSONException e) {
                            this.logger.error("AEMAF error in accessing new properties " + e.getMessage() + " properties " + json, (Throwable)e);
                        }
                    } else if (!"".equals(json)) {
                        this.logger.error("AEMAF unable to sync. Invalid SOM Reference in node " + bindref);
                    }
                }
            }
            catch (JSONException e) {
                this.logger.error("AEMAF error in accessing old properties " + e.getMessage() + " old properties " + jsonString, (Throwable)e);
                throw new GuideException((Exception)e);
            }
        }
    }

    public boolean isValidDorTemplateRef() {
        ResourceResolver resourceResolver = this.resource.getResourceResolver();
        Resource guideContainerResource = resourceResolver.getResource(GuideELUtils.getGuideContainerPath(this.slingRequest, this.resource));
        ValueMap map = ResourceUtil.getValueMap((Resource)guideContainerResource);
        String dorTemplateRef = null;
        dorTemplateRef = (String)map.get((Object)"dorTemplateRef");
        return dorTemplateRef != null && dorTemplateRef.length() > 0;
    }

    public String getClientLibRef() {
        return this.clientLibRef;
    }

    public void setClientLibRef(String clientLibRef) {
        this.clientLibRef = clientLibRef;
    }
}