| 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.LinkedHashMap; |
| 23 |
|
import java.util.List; |
| 24 |
|
import java.util.Map; |
| 25 |
|
import java.util.Queue; |
| 26 |
|
import java.util.concurrent.ConcurrentLinkedQueue; |
| 27 |
|
|
| 28 |
|
import javax.inject.Inject; |
| 29 |
|
import javax.inject.Named; |
| 30 |
|
import javax.inject.Provider; |
| 31 |
|
import javax.inject.Singleton; |
| 32 |
|
|
| 33 |
|
import org.xwiki.component.annotation.Component; |
| 34 |
|
import org.xwiki.component.phase.Initializable; |
| 35 |
|
import org.xwiki.component.phase.InitializationException; |
| 36 |
|
import org.xwiki.rendering.block.Block; |
| 37 |
|
import org.xwiki.rendering.block.LinkBlock; |
| 38 |
|
import org.xwiki.rendering.block.MetaDataBlock; |
| 39 |
|
import org.xwiki.rendering.block.match.ClassBlockMatcher; |
| 40 |
|
import org.xwiki.rendering.block.match.MetadataBlockMatcher; |
| 41 |
|
import org.xwiki.rendering.listener.MetaData; |
| 42 |
|
import org.xwiki.rendering.listener.reference.ResourceType; |
| 43 |
|
import org.xwiki.rendering.transformation.AbstractTransformation; |
| 44 |
|
import org.xwiki.rendering.transformation.TransformationContext; |
| 45 |
|
import org.xwiki.rendering.transformation.TransformationException; |
| 46 |
|
import org.xwiki.rendering.transformation.linkchecker.LinkContextDataProvider; |
| 47 |
|
|
| 48 |
|
|
| 49 |
|
|
| 50 |
|
|
| 51 |
|
|
| 52 |
|
@version |
| 53 |
|
@since |
| 54 |
|
|
| 55 |
|
@Component |
| 56 |
|
@Named("linkchecker") |
| 57 |
|
@Singleton |
| |
|
| 95.2% |
Uncovered Elements: 2 (42) |
Complexity: 11 |
Complexity Density: 0.42 |
|
| 58 |
|
public class LinkCheckerTransformation extends AbstractTransformation implements Initializable |
| 59 |
|
{ |
| 60 |
|
|
| 61 |
|
|
| 62 |
|
|
| 63 |
|
|
| 64 |
|
|
| 65 |
|
|
| 66 |
|
|
| 67 |
|
|
| 68 |
|
|
| 69 |
|
static final int MAX_LINKS_IN_QUEUE = 10; |
| 70 |
|
|
| 71 |
|
@Inject |
| 72 |
|
private LinkCheckerThread checkerThread; |
| 73 |
|
|
| 74 |
|
@Inject |
| 75 |
|
private Provider<List<LinkContextDataProvider>> linkContextDataProvidersProvider; |
| 76 |
|
|
| 77 |
|
|
| 78 |
|
|
| 79 |
|
|
| 80 |
|
|
| 81 |
|
private Queue<LinkQueueItem> linkQueue = new ConcurrentLinkedQueue<>(); |
| 82 |
|
|
| 83 |
|
|
| 84 |
|
|
| 85 |
|
|
| 86 |
|
@throws |
| 87 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 88 |
8 |
@Override... |
| 89 |
|
public void initialize() throws InitializationException |
| 90 |
|
{ |
| 91 |
8 |
this.checkerThread.setName("Link Checker Thread"); |
| 92 |
8 |
this.checkerThread.startProcessing(getLinkQueue()); |
| 93 |
|
} |
| 94 |
|
|
| |
|
| 93.3% |
Uncovered Elements: 1 (15) |
Complexity: 4 |
Complexity Density: 0.44 |
|
| 95 |
14 |
@Override... |
| 96 |
|
public void transform(Block source, TransformationContext context) throws TransformationException |
| 97 |
|
{ |
| 98 |
|
|
| 99 |
|
|
| 100 |
|
|
| 101 |
|
|
| 102 |
|
|
| 103 |
|
|
| 104 |
|
|
| 105 |
14 |
if (getLinkQueue().size() < MAX_LINKS_IN_QUEUE) { |
| 106 |
13 |
for (LinkBlock linkBlock : source.<LinkBlock>getBlocks( |
| 107 |
|
new ClassBlockMatcher(LinkBlock.class), Block.Axes.DESCENDANT)) |
| 108 |
|
{ |
| 109 |
24 |
if (linkBlock.getReference().getType().equals(ResourceType.URL)) { |
| 110 |
|
|
| 111 |
24 |
String linkReference = linkBlock.getReference().getReference(); |
| 112 |
24 |
String contentReference = extractSourceContentReference(linkBlock); |
| 113 |
|
|
| 114 |
24 |
if (contentReference == null) { |
| 115 |
20 |
contentReference = "default"; |
| 116 |
|
} |
| 117 |
|
|
| 118 |
24 |
Map<String, Object> linkContextData = createLinkContextData(linkReference, contentReference); |
| 119 |
24 |
this.linkQueue.add(new LinkQueueItem(linkReference, contentReference, linkContextData)); |
| 120 |
|
} |
| 121 |
|
} |
| 122 |
|
} |
| 123 |
|
} |
| 124 |
|
|
| 125 |
|
|
| 126 |
|
|
| 127 |
|
|
| 128 |
|
@throws |
| 129 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 130 |
7 |
public void stopLinkCheckerThread() throws InterruptedException... |
| 131 |
|
{ |
| 132 |
7 |
this.checkerThread.stopProcessing(); |
| 133 |
|
|
| 134 |
7 |
this.checkerThread.join(); |
| 135 |
|
} |
| 136 |
|
|
| 137 |
|
|
| 138 |
|
@return |
| 139 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 140 |
24 |
public Queue<LinkQueueItem> getLinkQueue()... |
| 141 |
|
{ |
| 142 |
24 |
return this.linkQueue; |
| 143 |
|
} |
| 144 |
|
|
| 145 |
|
|
| 146 |
|
@param |
| 147 |
|
@param |
| 148 |
|
@return |
| 149 |
|
|
| 150 |
|
|
| 151 |
|
|
| |
|
| 88.9% |
Uncovered Elements: 1 (9) |
Complexity: 2 |
Complexity Density: 0.29 |
|
| 152 |
24 |
private Map<String, Object> createLinkContextData(String linkReference, String contentReference)... |
| 153 |
|
{ |
| 154 |
|
|
| 155 |
|
|
| 156 |
24 |
Map<String, Object> linkContextData = null; |
| 157 |
24 |
for (LinkContextDataProvider linkContextDataProvider : this.linkContextDataProvidersProvider.get()) { |
| 158 |
4 |
Map<String, Object> contextData = |
| 159 |
|
linkContextDataProvider.getContextData(linkReference, contentReference); |
| 160 |
4 |
if (linkContextData == null) { |
| 161 |
4 |
linkContextData = new LinkedHashMap<>(contextData.size()); |
| 162 |
|
} |
| 163 |
4 |
linkContextData.putAll(contextData); |
| 164 |
|
} |
| 165 |
24 |
return linkContextData; |
| 166 |
|
} |
| 167 |
|
|
| 168 |
|
|
| 169 |
|
@param |
| 170 |
|
@return |
| 171 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
| 172 |
24 |
private String extractSourceContentReference(Block source)... |
| 173 |
|
{ |
| 174 |
24 |
String contentSource = null; |
| 175 |
24 |
MetaDataBlock metaDataBlock = |
| 176 |
|
source.getFirstBlock(new MetadataBlockMatcher(MetaData.SOURCE), Block.Axes.ANCESTOR); |
| 177 |
24 |
if (metaDataBlock != null) { |
| 178 |
4 |
contentSource = (String) metaDataBlock.getMetaData().getMetaData(MetaData.SOURCE); |
| 179 |
|
} |
| 180 |
24 |
return contentSource; |
| 181 |
|
} |
| 182 |
|
} |