LinkCheckerTask.java
13.1 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.jcr.RepositoryException
* org.apache.felix.scr.annotations.Activate
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Deactivate
* 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.http.StatusLine
* org.apache.http.client.config.RequestConfig
* org.apache.http.client.config.RequestConfig$Builder
* org.apache.http.client.methods.CloseableHttpResponse
* org.apache.http.client.methods.HttpGet
* org.apache.http.client.methods.HttpHead
* org.apache.http.client.methods.HttpUriRequest
* org.apache.http.conn.HttpClientConnectionManager
* org.apache.http.impl.client.CloseableHttpClient
* org.apache.http.impl.client.HttpClientBuilder
* org.apache.http.impl.conn.PoolingHttpClientConnectionManager
* org.apache.http.osgi.services.HttpClientBuilderFactory
* org.apache.sling.commons.osgi.OsgiUtil
* org.apache.sling.discovery.InstanceDescription
* org.apache.sling.discovery.TopologyEvent
* org.apache.sling.discovery.TopologyEvent$Type
* org.apache.sling.discovery.TopologyEventListener
* org.apache.sling.discovery.TopologyView
* org.osgi.service.component.ComponentContext
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.rewriter.linkchecker.impl;
import com.day.cq.rewriter.linkchecker.ExternalLinkChecker;
import com.day.cq.rewriter.linkchecker.LinkCheckerConfigProvider;
import com.day.cq.rewriter.linkchecker.LinkInfo;
import com.day.cq.rewriter.linkchecker.LinkInfoStorage;
import com.day.cq.rewriter.linkchecker.impl.LinkCheckerTransformerConfig;
import java.io.IOException;
import java.net.ConnectException;
import java.net.NoRouteToHostException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.UnknownHostException;
import java.net.UnknownServiceException;
import java.util.Calendar;
import java.util.Dictionary;
import javax.jcr.RepositoryException;
import javax.net.ssl.SSLException;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
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.http.StatusLine;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpHead;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.conn.HttpClientConnectionManager;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.osgi.services.HttpClientBuilderFactory;
import org.apache.sling.commons.osgi.OsgiUtil;
import org.apache.sling.discovery.InstanceDescription;
import org.apache.sling.discovery.TopologyEvent;
import org.apache.sling.discovery.TopologyEventListener;
import org.apache.sling.discovery.TopologyView;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(label="%taskservice.name", description="%taskservice.description", metatype=1)
@Service
@Properties(value={@Property(name="scheduler.period", longValue={3600}), @Property(name="scheduler.concurrent", boolValue={0})})
public class LinkCheckerTask
implements Runnable,
ExternalLinkChecker,
TopologyEventListener {
private final Logger log;
private Boolean isMasterInstance;
private static final int DEFAULT_GOOD_LINK_TEST_INTERVAL = 24;
@Property(intValue={24})
private static final String GOOD_LINK_TEST_INTERVAL = "good_link_test_interval";
private static final int DEFAULT_BAD_LINK_TEST_INTERVAL = 1;
@Property(intValue={1})
private static final String BAD_LINK_TEST_INTERVAL = "bad_link_test_interval";
private static final int DEFAULT_LINK_UNUSED_INTERVAL = 168;
@Property(intValue={168})
private static final String LINK_UNUSED_INTERVAL = "link_unused_interval";
private static final int DEFAULT_CONNECTION_TIMEOUT = 10000;
@Reference
private HttpClientBuilderFactory httpClientBuilderFactory;
@Property(intValue={10000})
private static final String CONNECTION_TIMEOUT = "connection.timeout";
@Reference
protected LinkInfoStorage storage;
@Reference
protected LinkCheckerConfigProvider configProvider;
protected int goodLinkTestInterval;
protected int badLinkTestInterval;
protected int linkUnusedInterval;
private CloseableHttpClient httpClient;
public LinkCheckerTask() {
this.log = LoggerFactory.getLogger(this.getClass());
this.isMasterInstance = Boolean.FALSE;
}
@Activate
protected void activate(ComponentContext context) throws RepositoryException {
Dictionary props = context.getProperties();
this.goodLinkTestInterval = OsgiUtil.toInteger(props.get("good_link_test_interval"), (int)24);
this.badLinkTestInterval = OsgiUtil.toInteger(props.get("bad_link_test_interval"), (int)1);
this.linkUnusedInterval = OsgiUtil.toInteger(props.get("link_unused_interval"), (int)168);
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
int connectionTimeout = OsgiUtil.toInteger(props.get("connection.timeout"), (int)10000);
if (connectionTimeout >= 5000) {
RequestConfig config = RequestConfig.custom().setConnectTimeout(connectionTimeout).build();
this.httpClient = this.httpClientBuilderFactory.newBuilder().setDefaultRequestConfig(config).setConnectionManager((HttpClientConnectionManager)cm).build();
} else {
this.httpClient = this.httpClientBuilderFactory.newBuilder().setConnectionManager((HttpClientConnectionManager)cm).build();
}
this.log.info("LinkChecker service activated");
}
@Deactivate
protected void deactivate(ComponentContext componentContext) {
if (this.httpClient != null) {
try {
this.httpClient.close();
}
catch (IOException e) {
this.log.error("Unable to close the HTTP client", (Object)e.getMessage());
}
this.httpClient = null;
}
this.log.info("LinkChecker service shut down");
}
@Override
public void run() {
if (!this.isMasterInstance.booleanValue() || this.configProvider.getConfig().getDisableChecking()) {
return;
}
this.checkLinks();
}
@Override
public void checkLinks() {
String[] links;
if (!this.isMasterInstance.booleanValue()) {
return;
}
for (String link : links = this.storage.getLinks()) {
this.checkLink(link, false);
}
}
@Override
public LinkInfo checkLink(String link, boolean force) {
boolean check;
LinkInfo info = this.storage.getLinkInfo(link);
if (info == null) {
return null;
}
Calendar now = Calendar.getInstance();
if (this.hoursPassed(info.getLastAccessed(), this.linkUnusedInterval)) {
this.storage.deleteLinkInfo(info);
return null;
}
boolean bl = check = info.getLastChecked() == null || info.isValid() && this.hoursPassed(info.getLastChecked(), this.goodLinkTestInterval) || !info.isValid() && this.hoursPassed(info.getLastChecked(), this.badLinkTestInterval);
if (check || force) {
int status;
try {
this.log.debug("Checking URL {}", (Object)link);
status = this.check(link);
if (status == 404 || status == 301) {
this.log.info("Checked URL {}: {} (invalid)", (Object)link, (Object)status);
info.setValid(false);
} else if (status == 200 || status == 302) {
this.log.info("Checked URL {}: {} (valid)", (Object)link, (Object)status);
info.setValid(true);
info.setLastAvailable(now);
}
}
catch (SSLException e) {
status = -7;
this.log.error("Failed to validate URL {}: {}", (Object)link, (Object)e.toString());
}
catch (UnknownServiceException e) {
status = -1;
this.log.error("Failed to validate URL {}: {}", (Object)link, (Object)e.toString());
}
catch (NoRouteToHostException e) {
status = -4;
info.setValid(false);
this.log.error("Failed to validate URL {}: {}", (Object)link, (Object)e.toString());
}
catch (UnknownHostException e) {
status = -5;
info.setValid(false);
this.log.error("Failed to validate URL {}: {}", (Object)link, (Object)e.toString());
}
catch (ConnectException e) {
status = -3;
this.log.error("Failed to validate URL {}: {}", (Object)link, (Object)e.toString());
}
catch (URISyntaxException e) {
status = -2;
info.setValid(false);
this.log.error("Failed to validate URL {}: {}", (Object)link, (Object)e.toString());
}
catch (IOException e) {
status = -6;
this.log.error("Failed to validate URL {}: {}", (Object)link, (Object)e.toString());
}
catch (Exception e) {
status = -6;
this.log.error("Failed to validate URL {}: {}", (Object)link, (Object)e.toString());
}
info.setLastStatus(status);
info.setLastChecked(now);
this.storage.putLinkInfo(info);
}
return info;
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
@Override
public int check(String url) throws UnknownServiceException, URISyntaxException, ConnectException, NoRouteToHostException, UnknownHostException, IOException {
URI uri;
HttpGet getMethod;
HttpHead headMethod;
uri = new URI(url);
try {
headMethod = new HttpHead(uri.toString());
}
catch (Exception e) {
throw new UnknownServiceException(e.getMessage());
}
try {
int status;
CloseableHttpResponse httpResp = this.httpClient.execute((HttpUriRequest)headMethod);
if (httpResp != null && ((status = httpResp.getStatusLine().getStatusCode()) == 200 || status == 302)) {
int n = status;
return n;
}
}
finally {
headMethod.releaseConnection();
}
try {
getMethod = new HttpGet(uri.toString());
}
catch (Exception e) {
throw new UnknownServiceException(e.getMessage());
}
try {
int e = this.httpClient.execute((HttpUriRequest)getMethod).getStatusLine().getStatusCode();
return e;
}
finally {
getMethod.releaseConnection();
}
}
private boolean hoursPassed(Calendar calendar, int hours) {
if (calendar == null) {
return true;
}
Calendar now = Calendar.getInstance();
Calendar check = (Calendar)calendar.clone();
check.add(10, hours);
return now.after(check);
}
public void handleTopologyEvent(TopologyEvent event) {
if (event.getType() == TopologyEvent.Type.TOPOLOGY_CHANGED || event.getType() == TopologyEvent.Type.TOPOLOGY_INIT) {
this.isMasterInstance = event.getNewView().getLocalInstance().isLeader();
} else if (event.getType() == TopologyEvent.Type.TOPOLOGY_CHANGING) {
this.isMasterInstance = false;
}
}
protected void bindHttpClientBuilderFactory(HttpClientBuilderFactory httpClientBuilderFactory) {
this.httpClientBuilderFactory = httpClientBuilderFactory;
}
protected void unbindHttpClientBuilderFactory(HttpClientBuilderFactory httpClientBuilderFactory) {
if (this.httpClientBuilderFactory == httpClientBuilderFactory) {
this.httpClientBuilderFactory = null;
}
}
protected void bindStorage(LinkInfoStorage linkInfoStorage) {
this.storage = linkInfoStorage;
}
protected void unbindStorage(LinkInfoStorage linkInfoStorage) {
if (this.storage == linkInfoStorage) {
this.storage = null;
}
}
protected void bindConfigProvider(LinkCheckerConfigProvider linkCheckerConfigProvider) {
this.configProvider = linkCheckerConfigProvider;
}
protected void unbindConfigProvider(LinkCheckerConfigProvider linkCheckerConfigProvider) {
if (this.configProvider == linkCheckerConfigProvider) {
this.configProvider = null;
}
}
}