PageViewStatisticsImpl.java
5.24 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
/*
* 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;
}
}
}