| 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.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 |
|
|
| |
|
| 86.8% |
Uncovered Elements: 5 (38) |
Complexity: 5 |
Complexity Density: 0.17 |
|
| 41 |
|
public class ObjectAddAction extends XWikiAction |
| 42 |
|
{ |
| 43 |
|
private static final String[] EMPTY_PROPERTY = new String[] { "" }; |
| 44 |
|
|
| 45 |
|
|
| 46 |
|
|
| 47 |
|
|
| 48 |
|
|
| 49 |
|
private static final Pattern XOBJECT_NUMBER_PARAMETER = Pattern.compile("(\\?|&)xobjectNumber=?(&|#|$)"); |
| 50 |
|
|
| 51 |
|
|
| 52 |
|
|
| 53 |
|
|
| 54 |
|
private EntityReferenceResolver<String> relativeResolver = Utils.getComponent(EntityReferenceResolver.TYPE_STRING, |
| 55 |
|
"relative"); |
| 56 |
|
|
| |
|
| 86.5% |
Uncovered Elements: 5 (37) |
Complexity: 5 |
Complexity Density: 0.17 |
|
| 57 |
15 |
@Override... |
| 58 |
|
public boolean action(XWikiContext context) throws XWikiException |
| 59 |
|
{ |
| 60 |
|
|
| 61 |
15 |
if (!csrfTokenCheck(context)) { |
| 62 |
0 |
return false; |
| 63 |
|
} |
| 64 |
|
|
| 65 |
15 |
XWiki xwiki = context.getWiki(); |
| 66 |
15 |
XWikiResponse response = context.getResponse(); |
| 67 |
15 |
DocumentReference userReference = context.getUserReference(); |
| 68 |
15 |
XWikiDocument doc = context.getDoc(); |
| 69 |
15 |
ObjectAddForm oform = (ObjectAddForm) context.getForm(); |
| 70 |
|
|
| 71 |
15 |
String className = oform.getClassName(); |
| 72 |
15 |
EntityReference classReference = this.relativeResolver.resolve(className, EntityType.DOCUMENT); |
| 73 |
15 |
BaseObject object = doc.newXObject(classReference, context); |
| 74 |
|
|
| 75 |
15 |
BaseClass baseclass = object.getXClass(context); |
| 76 |
|
|
| 77 |
|
|
| 78 |
|
|
| 79 |
15 |
Map<String, String[]> objmap = oform.getObject(className); |
| 80 |
|
|
| 81 |
|
|
| 82 |
15 |
@SuppressWarnings("unchecked") |
| 83 |
|
Collection<PropertyClass> fields = baseclass.getFieldList(); |
| 84 |
15 |
for (PropertyClass property : fields) { |
| 85 |
220 |
String name = property.getName(); |
| 86 |
220 |
if (objmap.get(name) == null) { |
| 87 |
186 |
objmap.put(name, EMPTY_PROPERTY); |
| 88 |
|
} |
| 89 |
|
} |
| 90 |
|
|
| 91 |
|
|
| 92 |
15 |
baseclass.fromMap(objmap, object); |
| 93 |
|
|
| 94 |
15 |
doc.setAuthorReference(userReference); |
| 95 |
15 |
if (doc.isNew()) { |
| 96 |
1 |
doc.setCreatorReference(userReference); |
| 97 |
|
} |
| 98 |
15 |
xwiki.saveDocument(doc, localizePlainOrKey("core.comment.addObject"), true, context); |
| 99 |
|
|
| 100 |
|
|
| 101 |
15 |
if (Utils.isAjaxRequest(context)) { |
| 102 |
0 |
context.getResponse().setStatus(HttpServletResponse.SC_NO_CONTENT); |
| 103 |
0 |
return false; |
| 104 |
|
} |
| 105 |
|
|
| 106 |
|
|
| 107 |
15 |
String redirect = Utils.getRedirect("edit", "editor=object", "xcontinue", "xredirect"); |
| 108 |
|
|
| 109 |
|
|
| 110 |
15 |
redirect = |
| 111 |
|
XOBJECT_NUMBER_PARAMETER.matcher(redirect).replaceFirst("$1xobjectNumber=" + object.getNumber() + "$2"); |
| 112 |
15 |
sendRedirect(response, redirect); |
| 113 |
|
|
| 114 |
15 |
return false; |
| 115 |
|
} |
| 116 |
|
} |