| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
package org.xwiki.search.solr.script; |
| 21 |
|
|
| 22 |
|
import java.util.HashSet; |
| 23 |
|
import java.util.List; |
| 24 |
|
import java.util.Set; |
| 25 |
|
|
| 26 |
|
import javax.inject.Inject; |
| 27 |
|
import javax.inject.Named; |
| 28 |
|
import javax.inject.Provider; |
| 29 |
|
import javax.inject.Singleton; |
| 30 |
|
|
| 31 |
|
import org.apache.solr.common.SolrDocument; |
| 32 |
|
import org.slf4j.Logger; |
| 33 |
|
import org.xwiki.component.annotation.Component; |
| 34 |
|
import org.xwiki.model.EntityType; |
| 35 |
|
import org.xwiki.model.reference.DocumentReference; |
| 36 |
|
import org.xwiki.model.reference.DocumentReferenceResolver; |
| 37 |
|
import org.xwiki.model.reference.EntityReference; |
| 38 |
|
import org.xwiki.model.reference.EntityReferenceResolver; |
| 39 |
|
import org.xwiki.script.service.ScriptService; |
| 40 |
|
import org.xwiki.search.solr.internal.api.FieldUtils; |
| 41 |
|
import org.xwiki.search.solr.internal.api.SolrIndexer; |
| 42 |
|
import org.xwiki.security.authorization.AuthorizationManager; |
| 43 |
|
import org.xwiki.security.authorization.Right; |
| 44 |
|
|
| 45 |
|
import com.xpn.xwiki.XWikiContext; |
| 46 |
|
|
| 47 |
|
|
| 48 |
|
@link |
| 49 |
|
|
| 50 |
|
|
| 51 |
|
@version |
| 52 |
|
@since |
| 53 |
|
|
| 54 |
|
@Component |
| 55 |
|
@Named("solr") |
| 56 |
|
@Singleton |
| |
|
| 91.3% |
Uncovered Elements: 6 (69) |
Complexity: 23 |
Complexity Density: 0.46 |
|
| 57 |
|
public class SolrIndexScriptService implements ScriptService |
| 58 |
|
{ |
| 59 |
|
|
| 60 |
|
|
| 61 |
|
|
| 62 |
|
public static final String CONTEXT_LASTEXCEPTION = "lastexception"; |
| 63 |
|
|
| 64 |
|
|
| 65 |
|
|
| 66 |
|
|
| 67 |
|
@Inject |
| 68 |
|
private Logger logger; |
| 69 |
|
|
| 70 |
|
|
| 71 |
|
@link |
| 72 |
|
|
| 73 |
|
@Inject |
| 74 |
|
private SolrIndexer solrIndexer; |
| 75 |
|
|
| 76 |
|
|
| 77 |
|
|
| 78 |
|
|
| 79 |
|
@Inject |
| 80 |
|
private AuthorizationManager authorization; |
| 81 |
|
|
| 82 |
|
|
| 83 |
|
@link |
| 84 |
|
|
| 85 |
|
@Inject |
| 86 |
|
private Provider<XWikiContext> xcontextProvider; |
| 87 |
|
|
| 88 |
|
|
| 89 |
|
@link@link |
| 90 |
|
|
| 91 |
|
@Inject |
| 92 |
|
private DocumentReferenceResolver<SolrDocument> solrDocumentReferenceResolver; |
| 93 |
|
|
| 94 |
|
|
| 95 |
|
@link@link |
| 96 |
|
|
| 97 |
|
@Inject |
| 98 |
|
private EntityReferenceResolver<SolrDocument> solrEntityReferenceResolver; |
| 99 |
|
|
| 100 |
|
|
| 101 |
|
|
| 102 |
|
|
| 103 |
|
|
| 104 |
|
|
| 105 |
|
@param |
| 106 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 2 |
Complexity Density: 0.4 |
|
| 107 |
4 |
public void index(EntityReference reference)... |
| 108 |
|
{ |
| 109 |
4 |
clearException(); |
| 110 |
|
|
| 111 |
4 |
try { |
| 112 |
4 |
checkAccessToWikiIndex(reference); |
| 113 |
|
|
| 114 |
2 |
this.solrIndexer.index(reference, true); |
| 115 |
|
} catch (Exception e) { |
| 116 |
2 |
error(e); |
| 117 |
|
} |
| 118 |
|
} |
| 119 |
|
|
| 120 |
|
|
| 121 |
|
|
| 122 |
|
|
| 123 |
|
|
| 124 |
|
|
| 125 |
|
@param |
| 126 |
|
|
| |
|
| 83.3% |
Uncovered Elements: 1 (6) |
Complexity: 2 |
Complexity Density: 0.33 |
|
| 127 |
3 |
public void index(List<EntityReference> references)... |
| 128 |
|
{ |
| 129 |
3 |
clearException(); |
| 130 |
|
|
| 131 |
3 |
try { |
| 132 |
3 |
checkAccessToWikiIndex(references); |
| 133 |
|
|
| 134 |
3 |
for (EntityReference reference : references) { |
| 135 |
10 |
this.solrIndexer.index(reference, true); |
| 136 |
|
} |
| 137 |
|
} catch (Exception e) { |
| 138 |
0 |
error(e); |
| 139 |
|
} |
| 140 |
|
} |
| 141 |
|
|
| 142 |
|
|
| 143 |
|
|
| 144 |
|
|
| 145 |
|
|
| 146 |
|
|
| 147 |
|
@param |
| 148 |
|
|
| |
|
| 80% |
Uncovered Elements: 1 (5) |
Complexity: 2 |
Complexity Density: 0.4 |
|
| 149 |
1 |
public void delete(EntityReference reference)... |
| 150 |
|
{ |
| 151 |
1 |
clearException(); |
| 152 |
|
|
| 153 |
1 |
try { |
| 154 |
1 |
checkAccessToWikiIndex(reference); |
| 155 |
|
|
| 156 |
1 |
this.solrIndexer.delete(reference, true); |
| 157 |
|
} catch (Exception e) { |
| 158 |
0 |
error(e); |
| 159 |
|
} |
| 160 |
|
} |
| 161 |
|
|
| 162 |
|
|
| 163 |
|
|
| 164 |
|
|
| 165 |
|
|
| 166 |
|
|
| 167 |
|
@param |
| 168 |
|
|
| |
|
| 83.3% |
Uncovered Elements: 1 (6) |
Complexity: 2 |
Complexity Density: 0.33 |
|
| 169 |
1 |
public void delete(List<EntityReference> references)... |
| 170 |
|
{ |
| 171 |
1 |
clearException(); |
| 172 |
|
|
| 173 |
1 |
try { |
| 174 |
1 |
checkAccessToWikiIndex(references); |
| 175 |
|
|
| 176 |
1 |
for (EntityReference reference : references) { |
| 177 |
1 |
this.solrIndexer.delete(reference, true); |
| 178 |
|
} |
| 179 |
|
} catch (Exception e) { |
| 180 |
0 |
error(e); |
| 181 |
|
} |
| 182 |
|
} |
| 183 |
|
|
| 184 |
|
|
| 185 |
|
@return |
| 186 |
|
@since |
| 187 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 188 |
4821 |
public int getQueueSize()... |
| 189 |
|
{ |
| 190 |
4821 |
return this.solrIndexer.getQueueSize(); |
| 191 |
|
} |
| 192 |
|
|
| 193 |
|
|
| 194 |
|
@link@link |
| 195 |
|
|
| 196 |
|
@param@link@link |
| 197 |
|
@param |
| 198 |
|
|
| 199 |
|
@return@link |
| 200 |
|
@since |
| 201 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 202 |
0 |
public DocumentReference resolveDocument(SolrDocument document, Object... parameters)... |
| 203 |
|
{ |
| 204 |
0 |
return this.solrDocumentReferenceResolver.resolve(document, parameters); |
| 205 |
|
} |
| 206 |
|
|
| 207 |
|
|
| 208 |
|
@link@link |
| 209 |
|
|
| 210 |
|
@link |
| 211 |
|
|
| 212 |
|
@param@link@link |
| 213 |
|
|
| 214 |
|
@param |
| 215 |
|
|
| 216 |
|
@return@link |
| 217 |
|
@since |
| 218 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 3 |
Complexity Density: 0.5 |
|
| 219 |
3 |
public EntityReference resolve(SolrDocument document, Object... parameters)... |
| 220 |
|
{ |
| 221 |
3 |
EntityType type; |
| 222 |
3 |
try { |
| 223 |
3 |
type = EntityType.valueOf((String) document.get(FieldUtils.TYPE)); |
| 224 |
|
} catch (IllegalArgumentException e) { |
| 225 |
1 |
return null; |
| 226 |
|
} catch (NullPointerException e) { |
| 227 |
1 |
return null; |
| 228 |
|
} |
| 229 |
|
|
| 230 |
1 |
return resolve(document, type, parameters); |
| 231 |
|
} |
| 232 |
|
|
| 233 |
|
|
| 234 |
|
@link@link |
| 235 |
|
|
| 236 |
|
|
| 237 |
|
@param@link@link |
| 238 |
|
@param |
| 239 |
|
@param |
| 240 |
|
|
| 241 |
|
@return@link |
| 242 |
|
@since |
| 243 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 244 |
1 |
public EntityReference resolve(SolrDocument document, EntityType type, Object... parameters)... |
| 245 |
|
{ |
| 246 |
1 |
return this.solrEntityReferenceResolver.resolve(document, type, parameters); |
| 247 |
|
} |
| 248 |
|
|
| 249 |
|
|
| 250 |
|
|
| 251 |
|
|
| 252 |
|
@param |
| 253 |
|
@param |
| 254 |
|
@see |
| 255 |
|
|
| |
|
| 85.7% |
Uncovered Elements: 1 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
| 256 |
2 |
private void error(String errorMessage, Exception e)... |
| 257 |
|
{ |
| 258 |
2 |
String errorMessageToLog = errorMessage; |
| 259 |
2 |
if (errorMessageToLog == null) { |
| 260 |
2 |
errorMessageToLog = e.getMessage(); |
| 261 |
|
} |
| 262 |
|
|
| 263 |
2 |
this.logger.error(errorMessageToLog, e); |
| 264 |
|
|
| 265 |
2 |
this.xcontextProvider.get().put(CONTEXT_LASTEXCEPTION, e); |
| 266 |
|
} |
| 267 |
|
|
| 268 |
|
|
| 269 |
|
|
| 270 |
|
|
| 271 |
|
|
| 272 |
|
@param |
| 273 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 274 |
2 |
private void error(Exception e)... |
| 275 |
|
{ |
| 276 |
2 |
error(null, e); |
| 277 |
|
} |
| 278 |
|
|
| 279 |
|
|
| 280 |
|
|
| 281 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 282 |
9 |
private void clearException()... |
| 283 |
|
{ |
| 284 |
9 |
this.xcontextProvider.get().remove(CONTEXT_LASTEXCEPTION); |
| 285 |
|
} |
| 286 |
|
|
| 287 |
|
|
| 288 |
|
|
| 289 |
|
|
| 290 |
|
@param |
| 291 |
|
@throws |
| 292 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 3 |
Complexity Density: 0.5 |
|
| 293 |
11 |
private void checkAccessToWikiIndex(EntityReference reference) throws IllegalAccessException... |
| 294 |
|
{ |
| 295 |
11 |
EntityReference wikiReference = reference.extractReference(EntityType.WIKI); |
| 296 |
|
|
| 297 |
11 |
XWikiContext xcontext = this.xcontextProvider.get(); |
| 298 |
|
|
| 299 |
11 |
DocumentReference userReference = xcontext.getUserReference(); |
| 300 |
11 |
DocumentReference programmingUserReference = xcontext.getDoc().getContentAuthorReference(); |
| 301 |
|
|
| 302 |
11 |
if (!this.authorization.hasAccess(Right.ADMIN, userReference, wikiReference) |
| 303 |
|
|| !this.authorization.hasAccess(Right.PROGRAM, programmingUserReference, wikiReference)) { |
| 304 |
2 |
throw new IllegalAccessException(String.format( |
| 305 |
|
"The user '%s' is not allowed to alter the index for the entity '%s'", userReference, reference)); |
| 306 |
|
} |
| 307 |
|
} |
| 308 |
|
|
| 309 |
|
|
| 310 |
|
|
| 311 |
|
|
| 312 |
|
@param |
| 313 |
|
@throws |
| 314 |
|
|
| 315 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 2 |
Complexity Density: 0.33 |
|
| 316 |
4 |
private void checkAccessToWikiIndex(List<EntityReference> references) throws IllegalAccessException... |
| 317 |
|
{ |
| 318 |
4 |
Set<EntityReference> representatives = new HashSet<EntityReference>(); |
| 319 |
4 |
for (EntityReference reference : references) { |
| 320 |
11 |
EntityReference wikiReference = reference.extractReference(EntityType.WIKI); |
| 321 |
11 |
if (!representatives.contains(wikiReference)) { |
| 322 |
6 |
checkAccessToWikiIndex(wikiReference); |
| 323 |
6 |
representatives.add(wikiReference); |
| 324 |
|
} |
| 325 |
|
} |
| 326 |
|
} |
| 327 |
|
} |