1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package com.xpn.xwiki.web

File ObjectRemoveAction.java

 

Coverage histogram

../../../../img/srcFileCovDistChart6.png
72% of files have more coverage

Code metrics

14
39
3
1
111
78
11
0.28
13
3
3.67

Classes

Class Line # Actions
ObjectRemoveAction 35 39 0% 11 25
0.553571455.4%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package com.xpn.xwiki.web;
21   
22    import java.io.IOException;
23   
24    import javax.script.ScriptContext;
25    import javax.servlet.http.HttpServletResponse;
26   
27    import org.apache.commons.lang3.StringUtils;
28   
29    import com.xpn.xwiki.XWiki;
30    import com.xpn.xwiki.XWikiContext;
31    import com.xpn.xwiki.XWikiException;
32    import com.xpn.xwiki.doc.XWikiDocument;
33    import com.xpn.xwiki.objects.BaseObject;
34   
 
35    public class ObjectRemoveAction extends XWikiAction
36    {
 
37  3 toggle protected BaseObject getObject(XWikiDocument doc, XWikiContext context)
38    {
39  3 ObjectRemoveForm form = (ObjectRemoveForm) context.getForm();
40  3 BaseObject obj = null;
41   
42  3 String className = form.getClassName();
43  3 int classId = form.getClassId();
44  3 if (StringUtils.isBlank(className)) {
45  0 getCurrentScriptContext().setAttribute("message",
46    localizePlainOrKey("platform.core.action.objectRemove.noClassnameSpecified"),
47    ScriptContext.ENGINE_SCOPE);
48  3 } else if (classId < 0) {
49  0 getCurrentScriptContext().setAttribute("message",
50    localizePlainOrKey("platform.core.action.objectRemove.noObjectSpecified"), ScriptContext.ENGINE_SCOPE);
51    } else {
52  3 obj = doc.getObject(className, classId);
53  3 if (obj == null) {
54  0 getCurrentScriptContext().setAttribute("message",
55    localizePlainOrKey("platform.core.action.objectRemove.invalidObject"), ScriptContext.ENGINE_SCOPE);
56    }
57    }
58   
59  3 return obj;
60    }
61   
 
62  3 toggle @Override
63    public boolean action(XWikiContext context) throws XWikiException
64    {
65    // CSRF prevention
66  3 if (!csrfTokenCheck(context)) {
67  0 return false;
68    }
69   
70  3 XWiki xwiki = context.getWiki();
71  3 XWikiResponse response = context.getResponse();
72  3 String username = context.getUser();
73  3 XWikiDocument doc = context.getDoc();
74  3 BaseObject obj = getObject(doc, context);
75  3 if (obj == null) {
76  0 return true;
77    }
78   
79  3 doc.removeObject(obj);
80  3 doc.setAuthor(username);
81  3 xwiki.saveDocument(doc, localizePlainOrKey("core.comment.deleteObject"), true, context);
82   
83  3 if (Utils.isAjaxRequest(context)) {
84  0 response.setStatus(HttpServletResponse.SC_NO_CONTENT);
85  0 response.setContentLength(0);
86    } else {
87    // forward to edit
88  3 String redirect = Utils.getRedirect("edit", context);
89  3 sendRedirect(response, redirect);
90    }
91  3 return false;
92    }
93   
 
94  0 toggle @Override
95    public String render(XWikiContext context) throws XWikiException
96    {
97  0 if (Utils.isAjaxRequest(context)) {
98  0 XWikiResponse response = context.getResponse();
99  0 response.setStatus(HttpServletResponse.SC_CONFLICT);
100  0 response.setContentType("text/plain");
101  0 try {
102  0 response.getWriter().write("failed");
103  0 response.setContentLength(6);
104    } catch (IOException e) {
105    }
106  0 return null;
107    } else {
108  0 return "error";
109    }
110    }
111    }