1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.rest.internal.resources.objects

File ObjectsResourceImpl.java

 

Coverage histogram

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

Code metrics

8
32
2
1
140
94
8
0.25
16
2
4

Classes

Class Line # Actions
ObjectsResourceImpl 51 32 0% 8 5
0.8809523688.1%
 

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 org.xwiki.rest.internal.resources.objects;
21   
22    import java.util.List;
23   
24    import javax.inject.Named;
25    import javax.ws.rs.WebApplicationException;
26    import javax.ws.rs.core.Response;
27    import javax.ws.rs.core.Response.Status;
28   
29    import org.xwiki.component.annotation.Component;
30    import org.xwiki.rest.XWikiRestException;
31    import org.xwiki.rest.internal.DomainObjectFactory;
32    import org.xwiki.rest.internal.RangeIterable;
33    import org.xwiki.rest.internal.Utils;
34    import org.xwiki.rest.model.jaxb.Object;
35    import org.xwiki.rest.model.jaxb.Objects;
36    import org.xwiki.rest.model.jaxb.Property;
37    import org.xwiki.rest.resources.objects.ObjectResource;
38    import org.xwiki.rest.resources.objects.ObjectsResource;
39   
40    import com.xpn.xwiki.XWikiException;
41    import com.xpn.xwiki.api.Document;
42    import com.xpn.xwiki.doc.XWikiDocument;
43    import com.xpn.xwiki.objects.BaseObject;
44    import com.xpn.xwiki.objects.classes.BaseClass;
45   
46    /**
47    * @version $Id: 0eef85f856be150de973e85a3a76ef66f97d1629 $
48    */
49    @Component
50    @Named("org.xwiki.rest.internal.resources.objects.ObjectsResourceImpl")
 
51    public class ObjectsResourceImpl extends BaseObjectsResource implements ObjectsResource
52    {
 
53  13 toggle @Override
54    public Objects getObjects(String wikiName, String spaceName, String pageName, Integer start, Integer number,
55    Boolean withPrettyNames) throws XWikiRestException
56    {
57  13 try {
58  13 DocumentInfo documentInfo = getDocumentInfo(wikiName, spaceName, pageName, null, null, true, false);
59   
60  13 Document doc = documentInfo.getDocument();
61   
62  13 Objects objects = objectFactory.createObjects();
63   
64  13 List<BaseObject> objectList = getBaseObjects(doc);
65   
66  13 RangeIterable<BaseObject> ri = new RangeIterable<BaseObject>(objectList, start, number);
67   
68  13 for (BaseObject object : ri) {
69    /* By deleting objects, some of them might become null, so we must check for this */
70  13 if (object != null) {
71  13 objects.getObjectSummaries().add(
72    DomainObjectFactory.createObjectSummary(objectFactory, uriInfo.getBaseUri(),
73    Utils.getXWikiContext(componentManager), doc, object, false,
74    Utils.getXWikiApi(componentManager), withPrettyNames));
75    }
76    }
77   
78  13 return objects;
79    } catch (XWikiException e) {
80  0 throw new XWikiRestException(e);
81    }
82    }
83   
 
84  19 toggle @Override
85    public Response addObject(String wikiName, String spaceName, String pageName, Object object)
86    throws XWikiRestException
87    {
88  19 if (object.getClassName() == null) {
89  1 throw new WebApplicationException(Status.BAD_REQUEST);
90    }
91   
92  18 try {
93  18 List<String> spaces = parseSpaceSegments(spaceName);
94  18 DocumentInfo documentInfo = getDocumentInfo(wikiName, spaces, pageName, null, null, true, false);
95   
96  18 Document doc = documentInfo.getDocument();
97   
98  18 if (!doc.hasAccessLevel("edit", Utils.getXWikiUser(componentManager))) {
99  1 throw new WebApplicationException(Status.UNAUTHORIZED);
100    }
101   
102  17 XWikiDocument xwikiDocument =
103    Utils.getXWiki(componentManager).getDocument(doc.getDocumentReference(),
104    Utils.getXWikiContext(componentManager));
105   
106  17 BaseObject xwikiObject =
107    xwikiDocument.newObject(object.getClassName(), Utils.getXWikiContext(componentManager));
108   
109  17 if (xwikiObject == null) {
110  0 throw new WebApplicationException(Status.INTERNAL_SERVER_ERROR);
111    }
112   
113    // We must initialize all the fields to an empty value in order to correctly create the object
114  17 BaseClass xwikiClass =
115    Utils.getXWiki(componentManager).getClass(xwikiObject.getClassName(),
116    Utils.getXWikiContext(componentManager));
117  17 for (java.lang.Object propertyNameObject : xwikiClass.getPropertyNames()) {
118  17 String propertyName = (String) propertyNameObject;
119  17 xwikiObject.set(propertyName, "", Utils.getXWikiContext(componentManager));
120    }
121   
122  17 for (Property property : object.getProperties()) {
123  2 xwikiObject.set(property.getName(), property.getValue(), Utils.getXWikiContext(componentManager));
124    }
125   
126  17 doc.save();
127   
128  17 return Response
129    .created(
130    Utils.createURI(uriInfo.getBaseUri(), ObjectResource.class, wikiName, spaces, pageName,
131    object.getClassName(), xwikiObject.getNumber()))
132    .entity(
133    DomainObjectFactory.createObject(objectFactory, uriInfo.getBaseUri(),
134    Utils.getXWikiContext(componentManager), doc, xwikiObject, false,
135    Utils.getXWikiApi(componentManager), false)).build();
136    } catch (XWikiException e) {
137  0 throw new XWikiRestException(e);
138    }
139    }
140    }