PageImpressionsTracker.java 4.73 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.WCMMode
 *  com.day.cq.wcm.core.stats.PageView
 *  com.day.crx.statistics.Entry
 *  javax.jcr.RepositoryException
 *  javax.servlet.Servlet
 *  javax.servlet.ServletException
 *  org.apache.felix.scr.annotations.Activate
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.ConfigurationPolicy
 *  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.api.SlingHttpServletResponse
 *  org.apache.sling.api.request.RequestParameter
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.servlets.SlingSafeMethodsServlet
 *  org.osgi.service.component.ComponentContext
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.wcm.foundation.impl;

import com.day.cq.statistics.StatisticsService;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.WCMMode;
import com.day.cq.wcm.core.stats.PageView;
import com.day.crx.statistics.Entry;
import java.io.IOException;
import java.io.PrintWriter;
import javax.jcr.RepositoryException;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.ConfigurationPolicy;
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.api.SlingHttpServletResponse;
import org.apache.sling.api.request.RequestParameter;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(metatype=1, policy=ConfigurationPolicy.OPTIONAL, label="Adobe Page Impressions Tracker", description="Adobe Page Impressions configuration component.")
@Service(value={Servlet.class})
@Properties(value={@Property(name="sling.servlet.extensions", value={"js"}, propertyPrivate=1), @Property(name="sling.servlet.paths", value={"/libs/wcm/stats/tracker"}, propertyPrivate=1), @Property(name="sling.auth.requirements", value={""}, propertyPrivate=0)})
public class PageImpressionsTracker
extends SlingSafeMethodsServlet {
    private static final long serialVersionUID = -7915330057389423541L;
    private static final Logger log = LoggerFactory.getLogger(PageImpressionsTracker.class);
    private static final String PATH_PARAMETER = "path";
    private static final String STATISTICS_PATH = "/pages";
    @Reference
    private StatisticsService statisticsService;
    private String statisticsPath;

    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
        RequestParameter[] parameter = request.getRequestParameters("path");
        response.setContentType("text/javascript");
        response.setHeader("cache-control", "no-store");
        if (parameter != null) {
            ResourceResolver resolver = request.getResourceResolver();
            for (RequestParameter path : parameter) {
                try {
                    Resource resource = resolver.getResource(path.getString());
                    if (resource == null || resource.adaptTo(Page.class) == null) continue;
                    PageView view = new PageView(this.statisticsPath, (Page)resource.adaptTo(Page.class), WCMMode.DISABLED);
                    this.statisticsService.addEntry((Entry)view);
                    continue;
                }
                catch (RepositoryException e) {
                    log.error("Failed to add entry for " + path.getString(), (Throwable)e);
                }
            }
        }
        response.getWriter().println("//impression added");
    }

    @Activate
    protected void activate(ComponentContext ctx) {
        this.statisticsPath = this.statisticsService.getPath() + "/pages";
    }

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

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