LinkCheckerSettingsProviderImpl.java 6.72 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.i18n.I18n
 *  com.day.cq.rewriter.linkchecker.LinkCheckerSettings
 *  com.day.cq.rewriter.linkchecker.LinkCheckerSettingsProvider
 *  com.day.cq.rewriter.linkchecker.LinkRewriteConfig
 *  com.day.cq.wcm.api.WCMMode
 *  javax.servlet.ServletRequest
 *  javax.servlet.http.HttpServletRequest
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Properties
 *  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.commons.osgi.PropertiesUtil
 *  org.apache.sling.jcr.api.SlingRepository
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.wcm.core.impl;

import com.day.cq.i18n.I18n;
import com.day.cq.rewriter.linkchecker.LinkCheckerSettings;
import com.day.cq.rewriter.linkchecker.LinkCheckerSettingsProvider;
import com.day.cq.rewriter.linkchecker.LinkRewriteConfig;
import com.day.cq.wcm.api.WCMMode;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.commons.osgi.PropertiesUtil;
import org.apache.sling.jcr.api.SlingRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component
@Service(value={LinkCheckerSettingsProvider.class})
@Properties(value={@Property(name="service.description", value={"LinkChecker Service"})})
public class LinkCheckerSettingsProviderImpl
implements LinkCheckerSettingsProvider {
    private final Logger log;
    @Reference
    protected SlingRepository repository;
    private ReentrantReadWriteLock readWriteLock;
    private Map<String, RewriteConfigs> configs;

    public LinkCheckerSettingsProviderImpl() {
        this.log = LoggerFactory.getLogger(this.getClass());
        this.readWriteLock = new ReentrantReadWriteLock(true);
        this.configs = new HashMap<String, RewriteConfigs>();
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    public LinkCheckerSettings createSettings(SlingHttpServletRequest request) {
        this.readWriteLock.readLock().lock();
        try {
            LinkCheckerSettings settings = null;
            WCMMode mode = WCMMode.fromRequest((ServletRequest)request);
            I18n i18n = new I18n((HttpServletRequest)request);
            for (Map.Entry<String, RewriteConfigs> configsEntry : this.configs.entrySet()) {
                if (!configsEntry.getValue().matches(mode)) continue;
                settings = configsEntry.getValue().createSettings(i18n);
                break;
            }
            Iterator<Map.Entry<String, RewriteConfigs>> i$ = settings;
            return i$;
        }
        finally {
            this.readWriteLock.readLock().unlock();
        }
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    public void updateConfigs(Map<String, Object> params) {
        this.readWriteLock.writeLock().lock();
        try {
            this.configs.put(PropertiesUtil.toString((Object)params.get("service.pid"), (String)null), new RewriteConfigs(params));
        }
        finally {
            this.readWriteLock.writeLock().unlock();
        }
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    public void deleteConfig(Map<String, Object> params) {
        this.readWriteLock.writeLock().lock();
        try {
            this.configs.remove(PropertiesUtil.toString((Object)params.get("service.pid"), (String)null));
        }
        finally {
            this.readWriteLock.writeLock().unlock();
        }
    }

    protected void bindRepository(SlingRepository slingRepository) {
        this.repository = slingRepository;
    }

    protected void unbindRepository(SlingRepository slingRepository) {
        if (this.repository == slingRepository) {
            this.repository = null;
        }
    }

    private static class RewriteConfigs {
        private final EnumSet<WCMMode> modes = EnumSet.noneOf(WCMMode.class);
        private final LinkRewriteConfig invalid;
        private final LinkRewriteConfig predated;
        private final LinkRewriteConfig expired;

        private RewriteConfigs(Map<String, Object> params) {
            for (String v : PropertiesUtil.toStringArray((Object)params.get("link.wcmmodes"), (String[])null)) {
                this.modes.add(WCMMode.valueOf((String)v.toUpperCase()));
            }
            this.invalid = this.readRewriteConfig(PropertiesUtil.toBoolean((Object)params.get("link.invalid.remove"), (boolean)false), PropertiesUtil.toString((Object)params.get("link.invalid.prefix"), (String)null), PropertiesUtil.toString((Object)params.get("link.invalid.suffix"), (String)null));
            this.predated = this.readRewriteConfig(PropertiesUtil.toBoolean((Object)params.get("link.predated.remove"), (boolean)false), PropertiesUtil.toString((Object)params.get("link.predated.prefix"), (String)null), PropertiesUtil.toString((Object)params.get("link.predated.suffix"), (String)null));
            this.expired = this.readRewriteConfig(PropertiesUtil.toBoolean((Object)params.get("link.expired.remove"), (boolean)false), PropertiesUtil.toString((Object)params.get("link.expired.prefix"), (String)null), PropertiesUtil.toString((Object)params.get("link.expired.suffix"), (String)null));
        }

        private LinkRewriteConfig readRewriteConfig(boolean remove, String prefix, String suffix) {
            return new LinkRewriteConfig(remove, prefix, suffix);
        }

        public boolean matches(WCMMode mode) {
            return this.modes.contains((Object)mode);
        }

        private LinkRewriteConfig localize(LinkRewriteConfig config, I18n i18n) {
            return new LinkRewriteConfig(config.remove(), i18n.getVar(config.getPrefix()), i18n.getVar(config.getSuffix()));
        }

        public LinkCheckerSettings createSettings(I18n i18n) {
            LinkCheckerSettings settings = new LinkCheckerSettings();
            settings.setInvalidConfig(this.localize(this.invalid, i18n));
            settings.setExpiredConfig(this.localize(this.expired, i18n));
            settings.setPredatedConfig(this.localize(this.predated, i18n));
            return settings;
        }
    }

}