1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.extension.script; |
21 |
|
|
22 |
|
import java.io.InputStream; |
23 |
|
import java.io.InputStreamReader; |
24 |
|
import java.util.ArrayList; |
25 |
|
import java.util.Arrays; |
26 |
|
import java.util.Collection; |
27 |
|
import java.util.Collections; |
28 |
|
import java.util.Date; |
29 |
|
import java.util.List; |
30 |
|
import java.util.concurrent.ThreadLocalRandom; |
31 |
|
|
32 |
|
import javax.inject.Inject; |
33 |
|
import javax.inject.Named; |
34 |
|
import javax.inject.Singleton; |
35 |
|
|
36 |
|
import org.apache.commons.collections4.Predicate; |
37 |
|
import org.apache.commons.collections4.PredicateUtils; |
38 |
|
import org.xwiki.component.annotation.Component; |
39 |
|
import org.xwiki.extension.job.AbstractExtensionRequest; |
40 |
|
import org.xwiki.extension.job.history.ExtensionJobHistory; |
41 |
|
import org.xwiki.extension.job.history.ExtensionJobHistoryRecord; |
42 |
|
import org.xwiki.extension.job.history.ExtensionJobHistorySerializer; |
43 |
|
import org.xwiki.extension.job.history.ReplayJobStatus; |
44 |
|
import org.xwiki.extension.job.history.ReplayRequest; |
45 |
|
import org.xwiki.extension.job.history.internal.ReplayJob; |
46 |
|
import org.xwiki.job.AbstractRequest; |
47 |
|
import org.xwiki.job.Job; |
48 |
|
import org.xwiki.job.JobException; |
49 |
|
import org.xwiki.model.reference.WikiReference; |
50 |
|
import org.xwiki.security.authorization.Right; |
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
@version |
56 |
|
@since |
57 |
|
|
58 |
|
@Component |
59 |
|
@Named(ExtensionManagerScriptService.ROLEHINT + '.' + ExtensionHistoryScriptService.ID) |
60 |
|
@Singleton |
|
|
| 65.1% |
Uncovered Elements: 22 (63) |
Complexity: 20 |
Complexity Density: 0.44 |
|
61 |
|
public class ExtensionHistoryScriptService extends AbstractExtensionScriptService |
62 |
|
{ |
63 |
|
|
64 |
|
|
65 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (13) |
Complexity: 5 |
Complexity Density: 0.62 |
|
66 |
|
public class ExtensionHistoryFilter |
67 |
|
{ |
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
private final List<Predicate<ExtensionJobHistoryRecord>> constraints = new ArrayList<>(); |
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
@param |
77 |
|
@return |
78 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
79 |
1 |
public ExtensionHistoryFilter ofType(final List<String> jobTypes)... |
80 |
|
{ |
81 |
1 |
this.constraints.add(new Predicate<ExtensionJobHistoryRecord>() |
82 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
83 |
4 |
@Override... |
84 |
|
public boolean evaluate(ExtensionJobHistoryRecord record) |
85 |
|
{ |
86 |
4 |
return jobTypes.contains(record.getJobType()); |
87 |
|
} |
88 |
|
}); |
89 |
1 |
return this; |
90 |
|
} |
91 |
|
|
92 |
|
|
93 |
|
|
94 |
|
|
95 |
|
@return |
96 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
97 |
1 |
public ExtensionHistoryFilter fromThisWiki()... |
98 |
|
{ |
99 |
1 |
final String currentWikiNamespace = WIKI_NAMESPACE_PREFIX + xcontextProvider.get().getWikiId(); |
100 |
1 |
this.constraints.add(new Predicate<ExtensionJobHistoryRecord>() |
101 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
102 |
5 |
@Override... |
103 |
|
public boolean evaluate(ExtensionJobHistoryRecord record) |
104 |
|
{ |
105 |
5 |
return !record.getRequest().hasNamespaces() |
106 |
|
|| record.getRequest().getNamespaces().contains(currentWikiNamespace); |
107 |
|
} |
108 |
|
}); |
109 |
1 |
return this; |
110 |
|
} |
111 |
|
|
112 |
|
|
113 |
|
|
114 |
|
|
115 |
|
@param |
116 |
|
|
117 |
|
@param |
118 |
|
@return |
119 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
120 |
1 |
public List<ExtensionJobHistoryRecord> list(String offsetRecordId, int limit)... |
121 |
|
{ |
122 |
1 |
return history.getRecords(PredicateUtils.allPredicate(this.constraints), offsetRecordId, limit); |
123 |
|
} |
124 |
|
} |
125 |
|
|
126 |
|
|
127 |
|
@link |
128 |
|
|
129 |
|
public static final String ID = "history"; |
130 |
|
|
131 |
|
@Inject |
132 |
|
private ExtensionJobHistory history; |
133 |
|
|
134 |
|
@Inject |
135 |
|
private ExtensionJobHistorySerializer serializer; |
136 |
|
|
137 |
|
|
138 |
|
@return |
139 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
140 |
1 |
public ExtensionHistoryFilter getRecords()... |
141 |
|
{ |
142 |
1 |
return new ExtensionHistoryFilter(); |
143 |
|
} |
144 |
|
|
145 |
|
|
146 |
|
|
147 |
|
|
148 |
|
@param |
149 |
|
@return |
150 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 2 |
Complexity Density: 0.4 |
|
151 |
0 |
public String serialize(ExtensionJobHistoryRecord record)... |
152 |
|
{ |
153 |
0 |
setError(null); |
154 |
|
|
155 |
0 |
try { |
156 |
0 |
return this.serializer.serialize(record); |
157 |
|
} catch (Exception e) { |
158 |
0 |
setError(e); |
159 |
0 |
return null; |
160 |
|
} |
161 |
|
} |
162 |
|
|
163 |
|
|
164 |
|
|
165 |
|
|
166 |
|
@param |
167 |
|
@return |
168 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 2 |
Complexity Density: 0.4 |
|
169 |
0 |
public List<ExtensionJobHistoryRecord> deserialize(String serializedHistoryRecords)... |
170 |
|
{ |
171 |
0 |
setError(null); |
172 |
|
|
173 |
0 |
try { |
174 |
0 |
return this.serializer.deserialize(serializedHistoryRecords); |
175 |
|
} catch (Exception e) { |
176 |
0 |
setError(e); |
177 |
0 |
return null; |
178 |
|
} |
179 |
|
} |
180 |
|
|
181 |
|
|
182 |
|
|
183 |
|
|
184 |
|
@param |
185 |
|
@return |
186 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 2 |
Complexity Density: 0.4 |
|
187 |
0 |
public List<ExtensionJobHistoryRecord> read(InputStream inputStream)... |
188 |
|
{ |
189 |
0 |
setError(null); |
190 |
|
|
191 |
0 |
try { |
192 |
0 |
return this.serializer.read(new InputStreamReader(inputStream)); |
193 |
|
} catch (Exception e) { |
194 |
0 |
setError(e); |
195 |
0 |
return null; |
196 |
|
} |
197 |
|
} |
198 |
|
|
199 |
|
|
200 |
|
|
201 |
|
|
202 |
|
@param |
203 |
|
@return@link |
204 |
|
|
205 |
|
|
|
|
| 66.7% |
Uncovered Elements: 2 (6) |
Complexity: 2 |
Complexity Density: 0.33 |
|
206 |
3 |
public Job replay(List<ExtensionJobHistoryRecord> records)... |
207 |
|
{ |
208 |
3 |
setError(null); |
209 |
|
|
210 |
3 |
ReplayRequest request = createReplayRequest(createReplayPlan(records, true, null)); |
211 |
|
|
212 |
3 |
try { |
213 |
3 |
return this.jobExecutor.execute(ReplayJob.JOB_TYPE, request); |
214 |
|
} catch (JobException e) { |
215 |
0 |
setError(e); |
216 |
0 |
return null; |
217 |
|
} |
218 |
|
} |
219 |
|
|
220 |
|
|
221 |
|
|
222 |
|
|
223 |
|
@param |
224 |
|
@param |
225 |
|
|
226 |
|
@param |
227 |
|
|
228 |
|
@return |
229 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 3 |
Complexity Density: 0.5 |
|
230 |
3 |
public List<ExtensionJobHistoryRecord> createReplayPlan(List<ExtensionJobHistoryRecord> records,... |
231 |
|
boolean preserveUsers, Collection<String> namespaces) |
232 |
|
{ |
233 |
3 |
String currentWiki = this.xcontextProvider.get().getWikiId(); |
234 |
3 |
if (!this.authorization.hasAccess(Right.ADMIN, new WikiReference(currentWiki))) { |
235 |
1 |
return Collections.emptyList(); |
236 |
2 |
} else if (!this.authorization.hasAccess(Right.PROGRAM)) { |
237 |
|
|
238 |
1 |
return createReplayPlanInternal(records, false, Arrays.asList(WIKI_NAMESPACE_PREFIX + currentWiki)); |
239 |
|
} else { |
240 |
|
|
241 |
1 |
return createReplayPlanInternal(records, preserveUsers, namespaces); |
242 |
|
} |
243 |
|
} |
244 |
|
|
245 |
|
|
246 |
|
@param |
247 |
|
@return |
248 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
249 |
0 |
public ReplayJobStatus getReplayJobStatus(String id)... |
250 |
|
{ |
251 |
0 |
return (ReplayJobStatus) getJobStatus(getReplayJobId(id)); |
252 |
|
} |
253 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 5 |
Complexity Density: 0.83 |
|
254 |
2 |
private List<ExtensionJobHistoryRecord> createReplayPlanInternal(List<ExtensionJobHistoryRecord> records,... |
255 |
|
boolean preserveUsers, Collection<String> namespaces) |
256 |
|
{ |
257 |
2 |
for (ExtensionJobHistoryRecord record : records) { |
258 |
2 |
if (!preserveUsers) { |
259 |
1 |
setRightsProperties((AbstractRequest) record.getRequest()); |
260 |
|
} |
261 |
2 |
if (record.getRequest().hasNamespaces() && namespaces != null && namespaces.size() > 0) { |
262 |
1 |
((AbstractRequest) record.getRequest()).setProperty("namespaces", namespaces); |
263 |
|
} |
264 |
|
} |
265 |
2 |
return records; |
266 |
|
} |
267 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
|
268 |
3 |
private ReplayRequest createReplayRequest(List<ExtensionJobHistoryRecord> records)... |
269 |
|
{ |
270 |
3 |
ReplayRequest request = new ReplayRequest(); |
271 |
3 |
String suffix = new Date().getTime() + "-" + ThreadLocalRandom.current().nextInt(100, 1000); |
272 |
3 |
request.setId(getReplayJobId(suffix)); |
273 |
|
|
274 |
3 |
request.setInteractive(true); |
275 |
3 |
request.setRecords(records); |
276 |
|
|
277 |
|
|
278 |
3 |
request.setProperty(PROPERTY_CONTEXT_WIKI, this.xcontextProvider.get().getWikiId()); |
279 |
3 |
request.setProperty(PROPERTY_CONTEXT_ACTION, this.xcontextProvider.get().getAction()); |
280 |
|
|
281 |
3 |
setRightsProperties(request); |
282 |
|
|
283 |
3 |
return request; |
284 |
|
} |
285 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
286 |
3 |
private List<String> getReplayJobId(String suffix)... |
287 |
|
{ |
288 |
3 |
return Arrays.asList(AbstractExtensionRequest.JOBID_PREFIX, ID, suffix); |
289 |
|
} |
290 |
|
} |