1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package com.xpn.xwiki.web; |
21 |
|
|
22 |
|
import java.util.List; |
23 |
|
|
24 |
|
import org.apache.commons.lang3.StringUtils; |
25 |
|
import org.xwiki.job.Job; |
26 |
|
import org.xwiki.model.reference.DocumentReference; |
27 |
|
import org.xwiki.model.reference.EntityReference; |
28 |
|
import org.xwiki.refactoring.script.RefactoringScriptService; |
29 |
|
import org.xwiki.script.service.ScriptService; |
30 |
|
|
31 |
|
import com.xpn.xwiki.XWiki; |
32 |
|
import com.xpn.xwiki.XWikiContext; |
33 |
|
import com.xpn.xwiki.XWikiException; |
34 |
|
import com.xpn.xwiki.api.DeletedDocument; |
35 |
|
import com.xpn.xwiki.doc.XWikiDeletedDocument; |
36 |
|
import com.xpn.xwiki.doc.XWikiDocument; |
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
@version |
42 |
|
|
|
|
| 83.9% |
Uncovered Elements: 18 (112) |
Complexity: 29 |
Complexity Density: 0.41 |
|
43 |
|
public class DeleteAction extends XWikiAction |
44 |
|
{ |
45 |
|
|
46 |
|
protected static final String CONFIRM_PARAM = "confirm"; |
47 |
|
|
48 |
|
protected static final String ACTION_NAME = "delete"; |
49 |
|
|
50 |
|
protected static final String ASYNC_PARAM = "async"; |
51 |
|
|
52 |
|
protected static final String RECYCLED_DOCUMENT_ID_PARAM = "id"; |
53 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
54 |
20 |
private boolean isAsync(XWikiRequest request)... |
55 |
|
{ |
56 |
20 |
return "true".equals(request.get(ASYNC_PARAM)); |
57 |
|
} |
58 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
59 |
13 |
private boolean doesAffectChildren(XWikiRequest request, DocumentReference documentReference)... |
60 |
|
{ |
61 |
|
|
62 |
13 |
return StringUtils.isNotEmpty(request.getParameter("affectChildren")) |
63 |
|
&& "WebHome".equals(documentReference.getName()); |
64 |
|
} |
65 |
|
|
|
|
| 90% |
Uncovered Elements: 2 (20) |
Complexity: 5 |
Complexity Density: 0.42 |
|
66 |
63 |
@Override... |
67 |
|
public boolean action(XWikiContext context) throws XWikiException |
68 |
|
{ |
69 |
63 |
XWikiRequest request = context.getRequest(); |
70 |
|
|
71 |
|
|
72 |
|
|
73 |
63 |
if (!"1".equals(request.getParameter(CONFIRM_PARAM))) { |
74 |
11 |
return true; |
75 |
|
} |
76 |
|
|
77 |
|
|
78 |
52 |
if (!csrfTokenCheck(context)) { |
79 |
0 |
return false; |
80 |
|
} |
81 |
|
|
82 |
52 |
boolean redirected = delete(context); |
83 |
|
|
84 |
52 |
if (!redirected) { |
85 |
|
|
86 |
|
|
87 |
11 |
String redirect = Utils.getRedirect(request, null); |
88 |
11 |
if (redirect != null) { |
89 |
1 |
sendRedirect(context.getResponse(), redirect); |
90 |
1 |
redirected = true; |
91 |
|
} |
92 |
|
} |
93 |
|
|
94 |
52 |
return !redirected; |
95 |
|
} |
96 |
|
|
|
|
| 61.9% |
Uncovered Elements: 8 (21) |
Complexity: 6 |
Complexity Density: 0.46 |
|
97 |
14 |
@Override... |
98 |
|
public String render(XWikiContext context) throws XWikiException |
99 |
|
{ |
100 |
14 |
XWikiRequest request = context.getRequest(); |
101 |
14 |
XWikiDocument doc = context.getDoc(); |
102 |
14 |
String sindex = request.getParameter(RECYCLED_DOCUMENT_ID_PARAM); |
103 |
14 |
boolean recycleIdIsValid = false; |
104 |
14 |
if (sindex != null) { |
105 |
0 |
long index = Long.parseLong(sindex); |
106 |
0 |
if (context.getWiki().getRecycleBinStore().getDeletedDocument(doc, index, context, true) != null) { |
107 |
0 |
recycleIdIsValid = true; |
108 |
|
} |
109 |
|
} |
110 |
|
|
111 |
14 |
if ("1".equals(request.getParameter(CONFIRM_PARAM))) { |
112 |
3 |
return "deleted"; |
113 |
|
} |
114 |
11 |
if (doc.isNew() && !recycleIdIsValid) { |
115 |
0 |
return Utils.getPage(request, "docdoesnotexist"); |
116 |
|
} |
117 |
|
|
118 |
11 |
return ACTION_NAME; |
119 |
|
} |
120 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (16) |
Complexity: 4 |
Complexity Density: 0.33 |
|
121 |
45 |
protected boolean delete(XWikiContext context) throws XWikiException... |
122 |
|
{ |
123 |
45 |
XWiki xwiki = context.getWiki(); |
124 |
45 |
XWikiRequest request = context.getRequest(); |
125 |
45 |
XWikiResponse response = context.getResponse(); |
126 |
45 |
XWikiDocument doc = context.getDoc(); |
127 |
|
|
128 |
45 |
String sindex = request.getParameter(RECYCLED_DOCUMENT_ID_PARAM); |
129 |
45 |
if (sindex != null && xwiki.hasRecycleBin(context)) { |
130 |
2 |
deleteFromRecycleBin(Long.parseLong(sindex), context); |
131 |
2 |
return true; |
132 |
43 |
} else if (doc.isNew()) { |
133 |
|
|
134 |
30 |
sendRedirect(response, Utils.getRedirect("view", context)); |
135 |
30 |
return true; |
136 |
|
} else { |
137 |
|
|
138 |
13 |
return deleteToRecycleBin(context); |
139 |
|
} |
140 |
|
} |
141 |
|
|
|
|
| 72.2% |
Uncovered Elements: 5 (18) |
Complexity: 4 |
Complexity Density: 0.33 |
|
142 |
2 |
private void deleteFromRecycleBin(long index, XWikiContext context) throws XWikiException... |
143 |
|
{ |
144 |
2 |
XWiki xwiki = context.getWiki(); |
145 |
2 |
XWikiResponse response = context.getResponse(); |
146 |
2 |
XWikiDocument doc = context.getDoc(); |
147 |
|
|
148 |
2 |
XWikiDeletedDocument dd = xwiki.getRecycleBinStore().getDeletedDocument(doc, index, context, true); |
149 |
|
|
150 |
|
|
151 |
2 |
if (dd != null) { |
152 |
2 |
DeletedDocument ddapi = new DeletedDocument(dd, context); |
153 |
2 |
if (!ddapi.canDelete()) { |
154 |
0 |
throw new XWikiException(XWikiException.MODULE_XWIKI_ACCESS, XWikiException.ERROR_XWIKI_ACCESS_DENIED, |
155 |
|
"You are not allowed to delete a document from the trash " |
156 |
|
+ "immediately after it has been deleted from the wiki"); |
157 |
|
} |
158 |
2 |
if (!dd.getFullName().equals(doc.getFullName())) { |
159 |
0 |
throw new XWikiException(XWikiException.MODULE_XWIKI_APP, XWikiException.ERROR_XWIKI_APP_URL_EXCEPTION, |
160 |
|
"The specified trash entry does not match the current document"); |
161 |
|
} |
162 |
2 |
xwiki.getRecycleBinStore().deleteFromRecycleBin(doc, index, context, true); |
163 |
|
} |
164 |
2 |
sendRedirect(response, Utils.getRedirect("view", context)); |
165 |
|
} |
166 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
167 |
13 |
private boolean deleteToRecycleBin(XWikiContext context) throws XWikiException... |
168 |
|
{ |
169 |
13 |
XWikiRequest request = context.getRequest(); |
170 |
13 |
XWikiDocument doc = context.getDoc(); |
171 |
|
|
172 |
13 |
EntityReference documentReference = |
173 |
13 |
doesAffectChildren(request, doc.getDocumentReference()) ? doc.getDocumentReference() |
174 |
|
.getLastSpaceReference() : doc.getTranslatedDocument(context).getDocumentReferenceWithLocale(); |
175 |
|
|
176 |
13 |
return deleteToRecycleBin(documentReference, context); |
177 |
|
} |
178 |
|
|
|
|
| 90.9% |
Uncovered Elements: 1 (11) |
Complexity: 3 |
Complexity Density: 0.33 |
|
179 |
20 |
protected boolean deleteToRecycleBin(EntityReference entityReference, XWikiContext context) throws XWikiException... |
180 |
|
{ |
181 |
20 |
Job deleteJob = startDeleteJob(entityReference); |
182 |
|
|
183 |
|
|
184 |
20 |
if (isAsync(context.getRequest())) { |
185 |
9 |
List<String> jobId = deleteJob.getRequest().getId(); |
186 |
|
|
187 |
|
|
188 |
|
|
189 |
9 |
sendRedirect(context.getResponse(), |
190 |
|
Utils.getRedirect("view", "xpage=delete&jobId=" + serializeJobId(jobId), context)); |
191 |
|
|
192 |
|
|
193 |
9 |
return true; |
194 |
|
} |
195 |
|
|
196 |
|
|
197 |
11 |
try { |
198 |
11 |
deleteJob.join(); |
199 |
|
} catch (InterruptedException e) { |
200 |
0 |
throw new XWikiException(String.format("Failed to delete [%s]", entityReference), e); |
201 |
|
} |
202 |
|
|
203 |
|
|
204 |
11 |
return false; |
205 |
|
} |
206 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
207 |
9 |
private String serializeJobId(List<String> jobId)... |
208 |
|
{ |
209 |
9 |
return StringUtils.join(jobId, "/"); |
210 |
|
} |
211 |
|
|
|
|
| 71.4% |
Uncovered Elements: 2 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
212 |
20 |
private Job startDeleteJob(EntityReference entityReference) throws XWikiException... |
213 |
|
{ |
214 |
20 |
RefactoringScriptService refactoring = |
215 |
|
(RefactoringScriptService) Utils.getComponent(ScriptService.class, "refactoring"); |
216 |
20 |
Job deleteJob = refactoring.delete(entityReference); |
217 |
20 |
if (deleteJob != null) { |
218 |
20 |
return deleteJob; |
219 |
|
} else { |
220 |
0 |
throw new XWikiException(String.format("Failed to schedule the delete job for [%s]", entityReference), |
221 |
|
refactoring.getLastError()); |
222 |
|
} |
223 |
|
} |
224 |
|
} |