PageImpressionsTracker.java
4.73 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
/*
* 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;
}
}
}