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 javax.script.ScriptContext; |
23 |
|
import javax.servlet.http.HttpServletResponse; |
24 |
|
|
25 |
|
import org.apache.commons.lang3.exception.ExceptionUtils; |
26 |
|
import org.apache.commons.lang3.math.NumberUtils; |
27 |
|
import org.xwiki.model.EntityType; |
28 |
|
import org.xwiki.resource.ResourceReference; |
29 |
|
import org.xwiki.resource.ResourceReferenceManager; |
30 |
|
import org.xwiki.resource.entity.EntityResourceReference; |
31 |
|
|
32 |
|
import com.xpn.xwiki.XWiki; |
33 |
|
import com.xpn.xwiki.XWikiContext; |
34 |
|
import com.xpn.xwiki.XWikiException; |
35 |
|
import com.xpn.xwiki.doc.DeletedAttachment; |
36 |
|
import com.xpn.xwiki.doc.XWikiAttachment; |
37 |
|
import com.xpn.xwiki.doc.XWikiDocument; |
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
@version |
43 |
|
|
|
|
| 43.2% |
Uncovered Elements: 50 (88) |
Complexity: 17 |
Complexity Density: 0.29 |
|
44 |
|
public class DeleteAttachmentAction extends XWikiAction |
45 |
|
{ |
|
|
| 40.7% |
Uncovered Elements: 48 (81) |
Complexity: 15 |
Complexity Density: 0.27 |
|
46 |
3 |
@Override... |
47 |
|
public boolean action(XWikiContext context) throws XWikiException |
48 |
|
{ |
49 |
|
|
50 |
3 |
if (!csrfTokenCheck(context)) { |
51 |
0 |
return false; |
52 |
|
} |
53 |
|
|
54 |
3 |
XWikiRequest request = context.getRequest(); |
55 |
3 |
XWikiResponse response = context.getResponse(); |
56 |
3 |
XWikiDocument doc = context.getDoc(); |
57 |
3 |
XWikiAttachment attachment = null; |
58 |
3 |
XWiki xwiki = context.getWiki(); |
59 |
3 |
String filename; |
60 |
|
|
61 |
|
|
62 |
3 |
if (request.getParameter("trashId") != null) { |
63 |
0 |
long trashId = NumberUtils.toLong(request.getParameter("trashId")); |
64 |
0 |
DeletedAttachment da = xwiki.getAttachmentRecycleBinStore().getDeletedAttachment(trashId, context, true); |
65 |
|
|
66 |
|
|
67 |
0 |
if (da != null) { |
68 |
0 |
com.xpn.xwiki.api.DeletedAttachment daapi = new com.xpn.xwiki.api.DeletedAttachment(da, context); |
69 |
0 |
if (!daapi.canDelete()) { |
70 |
0 |
throw new XWikiException(XWikiException.MODULE_XWIKI_ACCESS, |
71 |
|
XWikiException.ERROR_XWIKI_ACCESS_DENIED, |
72 |
|
"You are not allowed to delete an attachment from the trash " |
73 |
|
+ "immediately after it has been deleted from the wiki"); |
74 |
|
} |
75 |
0 |
if (!da.getDocName().equals(doc.getFullName())) { |
76 |
0 |
throw new XWikiException(XWikiException.MODULE_XWIKI_APP, |
77 |
|
XWikiException.ERROR_XWIKI_APP_URL_EXCEPTION, |
78 |
|
"The specified trash entry does not match the current document"); |
79 |
|
} |
80 |
|
|
81 |
0 |
xwiki.getAttachmentRecycleBinStore().deleteFromRecycleBin(trashId, context, true); |
82 |
|
} |
83 |
0 |
sendRedirect(response, Utils.getRedirect("attach", context)); |
84 |
0 |
return false; |
85 |
|
} |
86 |
|
|
87 |
3 |
if (context.getMode() == XWikiContext.MODE_PORTLET) { |
88 |
0 |
filename = request.getParameter("filename"); |
89 |
|
} else { |
90 |
|
|
91 |
|
|
92 |
3 |
String requestUri = request.getRequestURI(); |
93 |
3 |
filename = getFileName(); |
94 |
|
} |
95 |
|
|
96 |
3 |
XWikiDocument newdoc = doc.clone(); |
97 |
|
|
98 |
|
|
99 |
3 |
if (request.getParameter("id") != null) { |
100 |
0 |
int id = NumberUtils.toInt(request.getParameter("id")); |
101 |
0 |
if (newdoc.getAttachmentList().size() > id) { |
102 |
0 |
attachment = newdoc.getAttachmentList().get(id); |
103 |
|
} |
104 |
|
} else { |
105 |
3 |
attachment = newdoc.getAttachment(filename); |
106 |
|
} |
107 |
|
|
108 |
|
|
109 |
3 |
if (attachment == null) { |
110 |
0 |
response.setStatus(HttpServletResponse.SC_NOT_FOUND); |
111 |
|
|
112 |
0 |
ScriptContext scriptContext = getCurrentScriptContext(); |
113 |
0 |
if (scriptContext != null) { |
114 |
0 |
scriptContext.setAttribute("message", |
115 |
|
localizePlainOrKey("core.action.deleteAttachment.failed", filename), ScriptContext.ENGINE_SCOPE); |
116 |
0 |
scriptContext.setAttribute("details", |
117 |
|
localizePlainOrKey("platform.core.action.deleteAttachment.noAttachment"), |
118 |
|
ScriptContext.ENGINE_SCOPE); |
119 |
|
} |
120 |
|
|
121 |
0 |
return true; |
122 |
|
} |
123 |
|
|
124 |
3 |
newdoc.setAuthorReference(context.getUserReference()); |
125 |
|
|
126 |
|
|
127 |
3 |
String comment; |
128 |
3 |
if (attachment.isImage(context)) { |
129 |
0 |
comment = localizePlainOrKey("core.comment.deleteImageComment", filename); |
130 |
|
} else { |
131 |
3 |
comment = localizePlainOrKey("core.comment.deleteAttachmentComment", filename); |
132 |
|
} |
133 |
|
|
134 |
3 |
try { |
135 |
3 |
newdoc.removeAttachment(attachment); |
136 |
3 |
xwiki.saveDocument(newdoc, comment, context); |
137 |
|
} catch (Exception ex) { |
138 |
0 |
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); |
139 |
|
|
140 |
0 |
ScriptContext scriptContext = getCurrentScriptContext(); |
141 |
0 |
if (scriptContext != null) { |
142 |
0 |
scriptContext.setAttribute("message", |
143 |
|
localizePlainOrKey("core.action.deleteAttachment.failed", filename), ScriptContext.ENGINE_SCOPE); |
144 |
0 |
scriptContext.setAttribute("details", ExceptionUtils.getRootCauseMessage(ex), |
145 |
|
ScriptContext.ENGINE_SCOPE); |
146 |
|
} |
147 |
|
|
148 |
0 |
return true; |
149 |
|
} |
150 |
|
|
151 |
|
|
152 |
3 |
if (!((Boolean) context.get("ajax")).booleanValue()) { |
153 |
3 |
String redirect = Utils.getRedirect("attach", context); |
154 |
3 |
sendRedirect(response, redirect); |
155 |
|
} |
156 |
|
|
157 |
3 |
return false; |
158 |
|
} |
159 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
160 |
0 |
@Override... |
161 |
|
public String render(XWikiContext context) |
162 |
|
{ |
163 |
0 |
return "error"; |
164 |
|
} |
165 |
|
|
166 |
|
|
167 |
|
@return |
168 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
169 |
3 |
private String getFileName()... |
170 |
|
{ |
171 |
|
|
172 |
3 |
ResourceReference resourceReference = Utils.getComponent(ResourceReferenceManager.class).getResourceReference(); |
173 |
3 |
EntityResourceReference entityResource = (EntityResourceReference) resourceReference; |
174 |
3 |
return entityResource.getEntityReference().extractReference(EntityType.ATTACHMENT).getName(); |
175 |
|
} |
176 |
|
} |