| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
package org.xwiki.rendering.internal.transformation.linkchecker; |
| 21 |
|
|
| 22 |
|
import java.util.HashMap; |
| 23 |
|
import java.util.List; |
| 24 |
|
import java.util.Map; |
| 25 |
|
import java.util.Queue; |
| 26 |
|
import java.util.concurrent.ConcurrentHashMap; |
| 27 |
|
import java.util.regex.Matcher; |
| 28 |
|
import java.util.regex.Pattern; |
| 29 |
|
|
| 30 |
|
import javax.inject.Inject; |
| 31 |
|
import javax.inject.Provider; |
| 32 |
|
|
| 33 |
|
import org.slf4j.Logger; |
| 34 |
|
import org.xwiki.component.annotation.Component; |
| 35 |
|
import org.xwiki.component.annotation.InstantiationStrategy; |
| 36 |
|
import org.xwiki.component.descriptor.ComponentInstantiationStrategy; |
| 37 |
|
import org.xwiki.observation.ObservationManager; |
| 38 |
|
import org.xwiki.rendering.transformation.linkchecker.InvalidURLEvent; |
| 39 |
|
import org.xwiki.rendering.transformation.linkchecker.LinkCheckerThreadInitializer; |
| 40 |
|
import org.xwiki.rendering.transformation.linkchecker.LinkCheckerTransformationConfiguration; |
| 41 |
|
import org.xwiki.rendering.transformation.linkchecker.LinkState; |
| 42 |
|
import org.xwiki.rendering.transformation.linkchecker.LinkStateManager; |
| 43 |
|
|
| 44 |
|
|
| 45 |
|
|
| 46 |
|
@link |
| 47 |
|
|
| 48 |
|
|
| 49 |
|
@version |
| 50 |
|
@since |
| 51 |
|
|
| 52 |
|
|
| 53 |
|
@Component |
| 54 |
|
@InstantiationStrategy(ComponentInstantiationStrategy.PER_LOOKUP) |
| |
|
| 98.8% |
Uncovered Elements: 1 (82) |
Complexity: 23 |
Complexity Density: 0.43 |
|
| 55 |
|
public class DefaultLinkCheckerThread extends java.lang.Thread implements LinkCheckerThread |
| 56 |
|
{ |
| 57 |
|
private static final String EVENT_DATA_SOURCE = "source"; |
| 58 |
|
private static final String EVENT_DATA_URL = "url"; |
| 59 |
|
private static final String EVENT_DATA_STATE = "state"; |
| 60 |
|
private static final String EVENT_DATA_CONTEXTDATA = "contextData"; |
| 61 |
|
|
| 62 |
|
@Inject |
| 63 |
|
private Logger logger; |
| 64 |
|
|
| 65 |
|
@Inject |
| 66 |
|
private Provider<List<LinkCheckerThreadInitializer>> linkCheckerThreadInitializersProvider; |
| 67 |
|
|
| 68 |
|
|
| 69 |
|
|
| 70 |
|
|
| 71 |
|
@Inject |
| 72 |
|
private LinkStateManager linkStateManager; |
| 73 |
|
|
| 74 |
|
@Inject |
| 75 |
|
private Provider<ObservationManager> observationManagerProvider; |
| 76 |
|
|
| 77 |
|
|
| 78 |
|
|
| 79 |
|
|
| 80 |
|
@Inject |
| 81 |
|
private HTTPChecker httpChecker; |
| 82 |
|
|
| 83 |
|
@Inject |
| 84 |
|
private LinkCheckerTransformationConfiguration configuration; |
| 85 |
|
|
| 86 |
|
|
| 87 |
|
|
| 88 |
|
|
| 89 |
|
private Queue<LinkQueueItem> linkQueue; |
| 90 |
|
|
| 91 |
|
|
| 92 |
|
@link |
| 93 |
|
|
| 94 |
|
private volatile boolean shouldStop; |
| 95 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 96 |
7 |
@Override... |
| 97 |
|
public void startProcessing(Queue<LinkQueueItem> linkQueue) |
| 98 |
|
{ |
| 99 |
7 |
this.linkQueue = linkQueue; |
| 100 |
7 |
start(); |
| 101 |
|
} |
| 102 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 103 |
1 |
@Override... |
| 104 |
|
public void run(Queue<LinkQueueItem> linkQueue) |
| 105 |
|
{ |
| 106 |
1 |
this.linkQueue = linkQueue; |
| 107 |
1 |
run(); |
| 108 |
|
} |
| 109 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 3 |
Complexity Density: 0.38 |
|
| 110 |
8 |
@Override... |
| 111 |
|
public void run() |
| 112 |
|
{ |
| 113 |
|
|
| 114 |
|
|
| 115 |
8 |
for (LinkCheckerThreadInitializer initializer : this.linkCheckerThreadInitializersProvider.get()) { |
| 116 |
2 |
initializer.initialize(); |
| 117 |
|
} |
| 118 |
|
|
| 119 |
65 |
while (!this.shouldStop) { |
| 120 |
64 |
try { |
| 121 |
64 |
processLinkQueue(); |
| 122 |
63 |
Thread.sleep(300L); |
| 123 |
|
} catch (Exception e) { |
| 124 |
|
|
| 125 |
7 |
this.logger.error("Link checker Thread was stopped due to some problem", e); |
| 126 |
7 |
break; |
| 127 |
|
} |
| 128 |
|
} |
| 129 |
|
} |
| 130 |
|
|
| 131 |
|
|
| 132 |
|
|
| 133 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 134 |
6 |
@Override... |
| 135 |
|
public void stopProcessing() |
| 136 |
|
{ |
| 137 |
6 |
this.shouldStop = true; |
| 138 |
|
|
| 139 |
6 |
interrupt(); |
| 140 |
|
} |
| 141 |
|
|
| 142 |
|
|
| 143 |
|
|
| 144 |
|
|
| |
|
| 96.6% |
Uncovered Elements: 1 (29) |
Complexity: 9 |
Complexity Density: 0.53 |
|
| 145 |
67 |
protected void processLinkQueue()... |
| 146 |
|
{ |
| 147 |
67 |
long timeout = this.configuration.getCheckTimeout(); |
| 148 |
66 |
List<Pattern> excludedReferencePatterns = this.configuration.getExcludedReferencePatterns(); |
| 149 |
|
|
| 150 |
|
|
| 151 |
|
|
| 152 |
|
|
| 153 |
66 |
LinkQueueItem queueItem = null; |
| 154 |
66 |
boolean shouldBeChecked = false; |
| 155 |
|
|
| 156 |
78 |
while (!this.linkQueue.isEmpty() && !shouldBeChecked) { |
| 157 |
15 |
queueItem = this.linkQueue.poll(); |
| 158 |
|
|
| 159 |
|
|
| 160 |
15 |
shouldBeChecked = isExcluded(queueItem.getContentReference(), excludedReferencePatterns); |
| 161 |
15 |
if (!shouldBeChecked) { |
| 162 |
3 |
break; |
| 163 |
|
} |
| 164 |
|
|
| 165 |
12 |
Map<String, LinkState> contentReferences = |
| 166 |
|
this.linkStateManager.getLinkStates().get(queueItem.getLinkReference()); |
| 167 |
12 |
if (contentReferences != null) { |
| 168 |
2 |
LinkState state = contentReferences.get(queueItem.getContentReference()); |
| 169 |
2 |
if (state != null) { |
| 170 |
2 |
if (System.currentTimeMillis() - state.getLastCheckedTime() <= timeout) { |
| 171 |
1 |
shouldBeChecked = false; |
| 172 |
|
} |
| 173 |
|
} |
| 174 |
|
} |
| 175 |
|
} |
| 176 |
|
|
| 177 |
66 |
if (shouldBeChecked && queueItem != null) { |
| 178 |
11 |
checkLink(queueItem); |
| 179 |
|
} |
| 180 |
|
} |
| 181 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
| 182 |
15 |
private boolean isExcluded(String contentReference, List<Pattern> excludedReferencePatterns)... |
| 183 |
|
{ |
| 184 |
15 |
for (Pattern pattern : excludedReferencePatterns) { |
| 185 |
5 |
Matcher matcher = pattern.matcher(contentReference); |
| 186 |
5 |
if (matcher.matches()) { |
| 187 |
3 |
return false; |
| 188 |
|
} |
| 189 |
|
} |
| 190 |
|
|
| 191 |
12 |
return true; |
| 192 |
|
} |
| 193 |
|
|
| 194 |
|
|
| 195 |
|
@link |
| 196 |
|
|
| 197 |
|
@param |
| 198 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (18) |
Complexity: 4 |
Complexity Density: 0.29 |
|
| 199 |
11 |
private void checkLink(LinkQueueItem queueItem)... |
| 200 |
|
{ |
| 201 |
11 |
int responseCode = this.httpChecker.check(queueItem.getLinkReference()); |
| 202 |
|
|
| 203 |
11 |
Map<String, LinkState> contentReferences = |
| 204 |
|
this.linkStateManager.getLinkStates().get(queueItem.getLinkReference()); |
| 205 |
11 |
if (contentReferences == null) { |
| 206 |
10 |
contentReferences = new ConcurrentHashMap<>(); |
| 207 |
|
} |
| 208 |
11 |
LinkState state = new LinkState(responseCode, System.currentTimeMillis(), queueItem.getContextData()); |
| 209 |
11 |
contentReferences.put(queueItem.getContentReference(), state); |
| 210 |
11 |
this.linkStateManager.getLinkStates().put(queueItem.getLinkReference(), contentReferences); |
| 211 |
|
|
| 212 |
|
|
| 213 |
11 |
if (responseCode < 200 || responseCode > 299) { |
| 214 |
4 |
Map<String, Object> eventData = new HashMap<>(); |
| 215 |
4 |
eventData.put(EVENT_DATA_URL, queueItem.getLinkReference()); |
| 216 |
4 |
eventData.put(EVENT_DATA_SOURCE, queueItem.getContentReference()); |
| 217 |
4 |
eventData.put(EVENT_DATA_STATE, state); |
| 218 |
4 |
eventData.put(EVENT_DATA_CONTEXTDATA, queueItem.getContextData()); |
| 219 |
4 |
sendEvent(queueItem.getLinkReference(), eventData); |
| 220 |
|
} |
| 221 |
|
} |
| 222 |
|
|
| 223 |
|
|
| 224 |
|
@link |
| 225 |
|
|
| 226 |
|
@param |
| 227 |
|
@param |
| 228 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 2 |
Complexity Density: 0.5 |
|
| 229 |
4 |
private void sendEvent(String url, Map<String, Object> data)... |
| 230 |
|
{ |
| 231 |
|
|
| 232 |
4 |
try { |
| 233 |
4 |
ObservationManager observationManager = this.observationManagerProvider.get(); |
| 234 |
2 |
observationManager.notify(new InvalidURLEvent(url), data); |
| 235 |
|
} catch (Exception e) { |
| 236 |
|
|
| 237 |
2 |
this.logger.warn("The Invalid URL Event for URL [{}] (source [{}]) wasn't sent as no Observation Manager " |
| 238 |
|
+ "Component was found", url, data.get(EVENT_DATA_SOURCE)); |
| 239 |
|
} |
| 240 |
|
} |
| 241 |
|
} |