PageViewStatisticsImpl.java 5.24 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.statistics.StatisticsService
 *  com.day.cq.wcm.api.Page
 *  com.day.cq.wcm.api.WCMException
 *  com.day.cq.wcm.api.WCMMode
 *  com.day.crx.statistics.Report
 *  javax.jcr.Node
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Property
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.ReferencePolicy
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.osgi.service.component.ComponentContext
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.wcm.core.stats;

import com.day.cq.statistics.StatisticsService;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.WCMException;
import com.day.cq.wcm.api.WCMMode;
import com.day.cq.wcm.core.stats.PageViewReport;
import com.day.cq.wcm.core.stats.PageViewStatistics;
import com.day.crx.statistics.Report;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Dictionary;
import java.util.Iterator;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferencePolicy;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(metatype=1)
@Service
public class PageViewStatisticsImpl
implements PageViewStatistics {
    private static final Logger log = LoggerFactory.getLogger(PageViewStatisticsImpl.class);
    @Property
    private static final String TRACKING_URL_PROPERTY = "pageviewstatistics.trackingurl";
    @Property(label="Tracking script enabled", description="Enable or disable the inclusion of the tracking script on the pages. No page tracking is available when disabled ")
    private static final String TRACKING_SCRIPT_ENABLED = "pageviewstatistics.trackingscript.enabled";
    @Reference(policy=ReferencePolicy.STATIC)
    private StatisticsService statistics;
    private boolean isLocal = true;
    private String trackingUrl;
    private boolean trackingScriptEnabled = false;
    private String dataPath;

    @Override
    public URL getTrackingURL() {
        if (!this.trackingScriptEnabled) {
            return null;
        }
        return this.getURL(this.trackingUrl);
    }

    @Override
    public URI getTrackingURI() {
        if (!this.trackingScriptEnabled) {
            return null;
        }
        return this.getURI(this.trackingUrl);
    }

    @Override
    public Object[] report(Page page) throws WCMException {
        if (this.isLocal) {
            return this.reportLocal(page);
        }
        log.debug("Remote reports not implemented");
        return null;
    }

    protected void activate(ComponentContext context) {
        Dictionary props = context.getProperties();
        this.trackingUrl = (String)props.get("pageviewstatistics.trackingurl");
        this.trackingScriptEnabled = Boolean.parseBoolean((String)props.get("pageviewstatistics.trackingscript.enabled"));
        this.dataPath = this.statistics.getPath() + "/pages";
    }

    private Object[] reportLocal(Page page) throws WCMException {
        try {
            PageViewReport report = new PageViewReport(this.dataPath, page, WCMMode.PREVIEW);
            report.setPeriod(1);
            Node node = (Node)page.adaptTo(Node.class);
            Session s = node != null ? node.getSession() : (Session)page.getContentResource().getResourceResolver().adaptTo(Session.class);
            Iterator result = this.statistics.runReport(s, (Report)report);
            if (result.hasNext()) {
                return (Object[])result.next();
            }
            return null;
        }
        catch (RepositoryException e) {
            throw new WCMException((Throwable)e);
        }
    }

    private URL getURL(String urlProperty) {
        if (urlProperty != null) {
            try {
                return new URL(urlProperty);
            }
            catch (MalformedURLException e) {
                log.error("Configuration contained URL that is not vald{}: {}", (Object)urlProperty, (Object)e);
            }
        }
        return null;
    }

    private URI getURI(String urlProperty) {
        if (urlProperty != null) {
            try {
                return new URI(urlProperty);
            }
            catch (URISyntaxException e) {
                log.error("Configuration contained URI that is not valid{}: {}", (Object)urlProperty, (Object)e);
            }
        }
        return null;
    }

    protected void bindStatistics(StatisticsService statisticsService) {
        this.statistics = statisticsService;
    }

    protected void unbindStatistics(StatisticsService statisticsService) {
        if (this.statistics == statisticsService) {
            this.statistics = null;
        }
    }
}