PageImpl.java 14.4 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
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  aQute.bnd.annotation.ConsumerType
 *  com.adobe.cq.wcm.launches.utils.LaunchUtils
 *  com.day.cq.commons.Filter
 *  com.day.cq.commons.LabeledResource
 *  com.day.cq.commons.LanguageUtil
 *  com.day.cq.commons.SlingRepositoryException
 *  com.day.cq.tagging.Tag
 *  com.day.cq.tagging.TagManager
 *  com.day.cq.wcm.api.Page
 *  com.day.cq.wcm.api.PageManager
 *  com.day.cq.wcm.api.Template
 *  com.day.cq.wcm.api.WCMException
 *  com.day.cq.wcm.commons.DeepResourceIterator
 *  com.day.text.Text
 *  javax.jcr.AccessDeniedException
 *  javax.jcr.Node
 *  javax.jcr.Property
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  javax.jcr.lock.Lock
 *  org.apache.sling.adapter.SlingAdaptable
 *  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.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.wcm.core.impl;

import aQute.bnd.annotation.ConsumerType;
import com.adobe.cq.wcm.launches.utils.LaunchUtils;
import com.day.cq.commons.Filter;
import com.day.cq.commons.LabeledResource;
import com.day.cq.commons.LanguageUtil;
import com.day.cq.commons.SlingRepositoryException;
import com.day.cq.tagging.Tag;
import com.day.cq.tagging.TagManager;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageManager;
import com.day.cq.wcm.api.Template;
import com.day.cq.wcm.api.WCMException;
import com.day.cq.wcm.commons.DeepResourceIterator;
import com.day.cq.wcm.core.impl.PageIterator;
import com.day.text.Text;
import java.util.Calendar;
import java.util.Iterator;
import java.util.Locale;
import javax.jcr.AccessDeniedException;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.lock.Lock;
import org.apache.sling.adapter.SlingAdaptable;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;

@ConsumerType
public class PageImpl
extends SlingAdaptable
implements Page {
    private static final Logger log = LoggerFactory.getLogger(PageImpl.class);
    public static final String RESOURCE_TYPE = "cq:Page";
    private PageManager manager;
    private final Resource resource;
    protected final Node node;
    private ValueMap contentProps;
    private Tag[] tagCache;

    public PageImpl(Resource resource) {
        this.resource = resource;
        this.node = (Node)resource.adaptTo(Node.class);
        if (this.node == null) {
            throw new IllegalArgumentException("Resource argument must be adaptable to Node");
        }
    }

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

    public <AdapterType> AdapterType adaptTo(Class<AdapterType> type) {
        if (type == Resource.class) {
            return (AdapterType)this.resource;
        }
        if (type == LabeledResource.class) {
            return (AdapterType)((Object)this);
        }
        if (type == Node.class) {
            return (AdapterType)this.node;
        }
        Object ret = super.adaptTo(type);
        if (ret == null) {
            ret = this.resource.adaptTo(type);
        }
        return (AdapterType)ret;
    }

    public Resource getContentResource() {
        return this.resource.getResourceResolver().getResource(this.resource, "jcr:content");
    }

    public Resource getContentResource(String relPath) {
        if (relPath == null || relPath.length() == 0) {
            return this.getContentResource();
        }
        if (relPath.charAt(0) == '/') {
            throw new IllegalArgumentException("Relative path expected.");
        }
        return this.resource.getResourceResolver().getResource(this.resource, "jcr:content/" + relPath);
    }

    public PageManager getPageManager() {
        if (this.manager == null) {
            this.manager = (PageManager)this.resource.getResourceResolver().adaptTo(PageManager.class);
        }
        return this.manager;
    }

    public Iterator<Page> listChildren() {
        return this.listChildren(null);
    }

    public Iterator<Page> listChildren(Filter<Page> filter) {
        return new PageIterator(this.resource.getResourceResolver().listChildren(this.resource), filter);
    }

    public Iterator<Page> listChildren(Filter<Page> filter, boolean deep) {
        if (deep) {
            return new PageIterator((Iterator<Resource>)new DeepResourceIterator(this.resource), filter);
        }
        return new PageIterator(this.resource.getResourceResolver().listChildren(this.resource), filter);
    }

    public boolean hasChild(String name) {
        try {
            return this.node.hasNode(name);
        }
        catch (RepositoryException e) {
            throw new SlingRepositoryException(e);
        }
    }

    public Page getParent() {
        return this.getParent(1);
    }

    public int getDepth() {
        try {
            return this.node.getDepth();
        }
        catch (RepositoryException e) {
            throw new SlingRepositoryException(e);
        }
    }

    public Page getParent(int level) {
        if (level == 0) {
            return this;
        }
        String path = Text.getRelativeParent((String)this.resource.getPath(), (int)level);
        if (path.equals("") || path.equals("/")) {
            return null;
        }
        Resource parentRes = this.resource.getResourceResolver().getResource(path);
        return parentRes == null ? null : (Page)parentRes.adaptTo(Page.class);
    }

    public Page getAbsoluteParent(int level) {
        if (LaunchUtils.isLaunchBasedPath((String)this.resource.getPath())) {
            level += 3;
        }
        String path = Text.getAbsoluteParent((String)this.resource.getPath(), (int)level);
        Resource parentRes = this.resource.getResourceResolver().getResource(path);
        return parentRes == null ? null : (Page)parentRes.adaptTo(Page.class);
    }

    public ValueMap getProperties() {
        if (this.contentProps == null) {
            this.contentProps = ResourceUtil.getValueMap((Resource)this.getContentResource());
        }
        return this.contentProps;
    }

    public ValueMap getProperties(String relPath) {
        if (relPath == null || relPath.length() == 0) {
            return this.getProperties();
        }
        return ResourceUtil.getValueMap((Resource)this.getContentResource(relPath));
    }

    public String getName() {
        try {
            return this.node.getName();
        }
        catch (RepositoryException e) {
            return null;
        }
    }

    public String getTitle() {
        return (String)this.getProperties().get("jcr:title", String.class);
    }

    public String getDescription() {
        return (String)this.getProperties().get("jcr:description", String.class);
    }

    public String getPageTitle() {
        return (String)this.getProperties().get("pageTitle", String.class);
    }

    public String getNavigationTitle() {
        return (String)this.getProperties().get("navTitle", String.class);
    }

    public boolean isHideInNav() {
        return (Boolean)this.getProperties().get("hideInNav", (Object)Boolean.FALSE);
    }

    public boolean hasContent() {
        try {
            return this.node.hasNode("jcr:content");
        }
        catch (RepositoryException e) {
            log.warn("Repository error while accessing content of {}", (Object)this.getPath());
            return false;
        }
    }

    public boolean isValid() {
        return this.timeUntilValid() == 0;
    }

    public long timeUntilValid() {
        long timeUntilOff;
        long timeUntilOn;
        if (!this.hasContent()) {
            return Long.MIN_VALUE;
        }
        Calendar onTime = this.getOnTime();
        Calendar offTime = this.getOffTime();
        if (onTime == null && offTime == null) {
            return 0;
        }
        long now = System.currentTimeMillis();
        long l = timeUntilOn = onTime == null ? 0 : onTime.getTimeInMillis() - now;
        if (timeUntilOn > 0) {
            return timeUntilOn;
        }
        long l2 = timeUntilOff = offTime == null ? 0 : offTime.getTimeInMillis() - now;
        if (timeUntilOff < 0) {
            return timeUntilOff;
        }
        return 0;
    }

    public Calendar getOnTime() {
        return (Calendar)this.getProperties().get("onTime", Calendar.class);
    }

    public Calendar getOffTime() {
        return (Calendar)this.getProperties().get("offTime", Calendar.class);
    }

    public Calendar getDeleted() {
        return (Calendar)this.getProperties().get("deleted", Calendar.class);
    }

    public String getDeletedBy() {
        return (String)this.getProperties().get("deletedBy", String.class);
    }

    public String getLastModifiedBy() {
        String lm = (String)this.getProperties().get("cq:lastModifiedBy", String.class);
        if (lm == null) {
            lm = (String)this.getProperties().get("jcr:lastModifiedBy", String.class);
        }
        return lm;
    }

    public Calendar getLastModified() {
        Calendar lm = (Calendar)this.getProperties().get("cq:lastModified", Calendar.class);
        if (lm == null) {
            lm = (Calendar)this.getProperties().get("jcr:lastModified", Calendar.class);
        }
        return lm;
    }

    public String getVanityUrl() {
        return (String)this.getProperties().get("vanityUrl", this.getProperties().get("sling:vanityPath", String.class));
    }

    public Tag[] getTags() {
        if (this.tagCache == null) {
            Resource resource = this.getContentResource();
            TagManager tagManager = (TagManager)resource.getResourceResolver().adaptTo(TagManager.class);
            if (tagManager == null) {
                return new Tag[0];
            }
            this.tagCache = tagManager.getTags(resource);
        }
        return this.tagCache;
    }

    public void lock() throws WCMException {
        try {
            if (this.hasContent()) {
                Node content = this.node.getNode("jcr:content");
                if (content.canAddMixin("mix:lockable")) {
                    content.addMixin("mix:lockable");
                    content.save();
                }
                content.lock(true, false);
            }
        }
        catch (RepositoryException e) {
            throw new WCMException("Unable to lock page.", (Throwable)e);
        }
    }

    public boolean isLocked() {
        try {
            return this.node.getNode("jcr:content").isLocked();
        }
        catch (RepositoryException e) {
            log.warn("Repository error while checking lock status of {}", (Object)this.getPath());
            return false;
        }
    }

    public String getLockOwner() {
        try {
            Node content;
            if (this.hasContent() && (content = this.node.getNode("jcr:content")).isLocked()) {
                return content.getLock().getLockOwner();
            }
        }
        catch (RepositoryException e) {
            log.warn("Repository error while getting lock owner of {}", (Object)this.getPath());
        }
        return null;
    }

    public boolean canUnlock() {
        try {
            String lockOwner = this.getLockOwner();
            if (lockOwner != null) {
                return lockOwner.equals(this.node.getSession().getUserID());
            }
        }
        catch (RepositoryException e) {
            log.warn("Repository error while checking whether current user can unlock {}", (Object)this.getPath());
        }
        return false;
    }

    public void unlock() throws WCMException {
        try {
            if (this.hasContent()) {
                this.node.getNode("jcr:content").unlock();
            }
        }
        catch (RepositoryException e) {
            throw new WCMException("Unable to unlock page.", (Throwable)e);
        }
    }

    public Template getTemplate() {
        String tpPath = (String)this.getProperties().get("cq:template", String.class);
        if (tpPath == null) {
            return null;
        }
        Resource tpRes = this.resource.getResourceResolver().getResource(tpPath);
        if (tpRes == null) {
            return null;
        }
        return (Template)tpRes.adaptTo(Template.class);
    }

    /*
     * Unable to fully structure code
     * Enabled aggressive block sorting
     * Enabled unnecessary exception pruning
     * Enabled aggressive exception aggregation
     * Lifted jumps to return sites
     */
    public Locale getLanguage(boolean ignoreContent) {
        ISO_PROP_NAME = "jcr:content/jcr:language";
        locale = null;
        pathLocale = null;
        pageNode = this.node;
        try {
            while (pageNode != null) {
                if (ignoreContent || !pageNode.hasProperty("jcr:content/jcr:language")) ** GOTO lbl12
                iso = pageNode.getProperty("jcr:content/jcr:language").getString();
                locale = LanguageUtil.getLocale((String)iso);
                if (locale == null) {
                    PageImpl.log.warn("Invalid iso code stored in {}: {}", (Object)pageNode.getPath(), (Object)iso);
lbl12: // 2 sources:
                    if (pathLocale == null) {
                        pathLocale = LanguageUtil.getLocale((String)pageNode.getName());
                    }
                    pageNode = pageNode.getDepth() <= 1 ? null : pageNode.getParent();
                    continue;
                }
                break;
            }
        }
        catch (AccessDeniedException e) {
            try {
                PageImpl.log.info("Could not access parent of {} to look for the page language", (Object)pageNode.getPath());
            }
            catch (RepositoryException ignore) {}
        }
        catch (RepositoryException e) {
            PageImpl.log.error("Error while evaluating content language.", (Throwable)e);
        }
        if (locale == null) {
            locale = pathLocale;
        }
        if (locale != null) return locale;
        return Locale.getDefault();
    }

    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof PageImpl)) {
            return false;
        }
        PageImpl page = (PageImpl)((Object)o);
        return this.resource.getPath().equals(page.resource.getPath());
    }

    public int hashCode() {
        return this.resource.getPath().hashCode();
    }
}