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

File ObjectAddAction.java

 

Coverage histogram

../../../../img/srcFileCovDistChart9.png
41% of files have more coverage

Code metrics

8
29
1
1
116
63
5
0.17
29
1
5

Classes

Class Line # Actions
ObjectAddAction 41 29 0% 5 7
0.8157894681.6%
 

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.util.Collection;
23    import java.util.Map;
24    import java.util.regex.Pattern;
25   
26    import javax.servlet.http.HttpServletResponse;
27   
28    import org.xwiki.model.EntityType;
29    import org.xwiki.model.reference.DocumentReference;
30    import org.xwiki.model.reference.EntityReference;
31    import org.xwiki.model.reference.EntityReferenceResolver;
32   
33    import com.xpn.xwiki.XWiki;
34    import com.xpn.xwiki.XWikiContext;
35    import com.xpn.xwiki.XWikiException;
36    import com.xpn.xwiki.doc.XWikiDocument;
37    import com.xpn.xwiki.objects.BaseObject;
38    import com.xpn.xwiki.objects.classes.BaseClass;
39    import com.xpn.xwiki.objects.classes.PropertyClass;
40   
 
41    public class ObjectAddAction extends XWikiAction
42    {
43    private static final String[] EMPTY_PROPERTY = new String[] { "" };
44   
45    /**
46    * A pattern that matches the {@code xobjectNumber} request parameter which is used to pass the number of the added
47    * object to the redirect URL.
48    */
49    private static final Pattern XOBJECT_NUMBER_PARAMETER = Pattern.compile("(\\?|&)xobjectNumber=?(&|#|$)");
50   
51    /**
52    * Used to resolve XClass references.
53    */
54    private EntityReferenceResolver<String> relativeResolver = Utils.getComponent(EntityReferenceResolver.TYPE_STRING,
55    "relative");
56   
 
57  13 toggle @Override
58    public boolean action(XWikiContext context) throws XWikiException
59    {
60    // CSRF prevention
61  13 if (!csrfTokenCheck(context)) {
62  0 return false;
63    }
64   
65  13 XWiki xwiki = context.getWiki();
66  13 XWikiResponse response = context.getResponse();
67  13 DocumentReference userReference = context.getUserReference();
68  13 XWikiDocument doc = context.getDoc();
69  13 ObjectAddForm oform = (ObjectAddForm) context.getForm();
70   
71  13 String className = oform.getClassName();
72  13 EntityReference classReference = this.relativeResolver.resolve(className, EntityType.DOCUMENT);
73  13 BaseObject object = doc.newXObject(classReference, context);
74   
75  13 BaseClass baseclass = object.getXClass(context);
76    // The request parameter names that correspond to object fields must NOT specify the object number because the
77    // object number is not known before the object is added. The following is a good parameter name:
78    // Space.Class_property. As a consequence we use only the class name to extract the object from the request.
79  13 Map<String, String[]> objmap = oform.getObject(className);
80    // We need to have a string in the map for each field for the object to be correctly created.
81    // Otherwise, queries using the missing properties will fail to return this object.
82  13 @SuppressWarnings("unchecked")
83    Collection<PropertyClass> fields = baseclass.getFieldList();
84  13 for (PropertyClass property : fields) {
85  212 String name = property.getName();
86  212 if (objmap.get(name) == null) {
87  184 objmap.put(name, EMPTY_PROPERTY);
88    }
89    }
90   
91    // Load the object properties that are defined in the request.
92  13 baseclass.fromMap(objmap, object);
93   
94  13 doc.setAuthorReference(userReference);
95  13 if (doc.isNew()) {
96  0 doc.setCreatorReference(userReference);
97    }
98  13 xwiki.saveDocument(doc, localizePlainOrKey("core.comment.addObject"), true, context);
99   
100    // If this is an ajax request, no need to redirect.
101  13 if (Utils.isAjaxRequest(context)) {
102  0 context.getResponse().setStatus(HttpServletResponse.SC_NO_CONTENT);
103  0 return false;
104    }
105   
106    // forward to edit
107  13 String redirect = Utils.getRedirect("edit", "editor=object", "xcontinue", "xredirect");
108    // If the redirect URL contains the xobjectNumber parameter then inject the number of the added object as its
109    // value so that the target page knows which object was added.
110  13 redirect =
111    XOBJECT_NUMBER_PARAMETER.matcher(redirect).replaceFirst("$1xobjectNumber=" + object.getNumber() + "$2");
112  13 sendRedirect(response, redirect);
113   
114  13 return false;
115    }
116    }