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

File PageTagsResourceImpl.java

 

Coverage histogram

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

Code metrics

10
45
3
1
145
101
10
0.22
15
3
3.33

Classes

Class Line # Actions
PageTagsResourceImpl 52 45 0% 10 9
0.844827684.5%
 

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.pages;
21   
22    import java.util.ArrayList;
23    import java.util.List;
24   
25    import javax.inject.Named;
26    import javax.ws.rs.WebApplicationException;
27    import javax.ws.rs.core.Response;
28    import javax.ws.rs.core.Response.Status;
29   
30    import org.xwiki.component.annotation.Component;
31    import org.xwiki.rest.Relations;
32    import org.xwiki.rest.XWikiRestException;
33    import org.xwiki.rest.internal.Utils;
34    import org.xwiki.rest.model.jaxb.Link;
35    import org.xwiki.rest.model.jaxb.Tag;
36    import org.xwiki.rest.model.jaxb.Tags;
37    import org.xwiki.rest.resources.pages.PageTagsResource;
38    import org.xwiki.rest.resources.tags.PagesForTagsResource;
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.BaseProperty;
45    import com.xpn.xwiki.objects.classes.BaseClass;
46   
47    /**
48    * @version $Id: d4f6a71a5af274fda8aceafbcf12df8151e4ff8e $
49    */
50    @Component
51    @Named("org.xwiki.rest.internal.resources.pages.PageTagsResourceImpl")
 
52    public class PageTagsResourceImpl extends ModifiablePageResource implements PageTagsResource
53    {
 
54  3 toggle @Override
55    public Tags getPageTags(String wikiName, String spaceName, String pageName) throws XWikiRestException
56    {
57  3 try {
58  3 String pageId = Utils.getPageId(wikiName, parseSpaceSegments(spaceName), pageName);
59  3 List<String> tagNames = getTagsFromDocument(pageId);
60   
61  3 Tags tags = objectFactory.createTags();
62  3 for (String tagName : tagNames) {
63  3 Tag tag = objectFactory.createTag();
64  3 tag.setName(tagName);
65   
66  3 String tagUri =
67    Utils.createURI(uriInfo.getBaseUri(), PagesForTagsResource.class, wikiName, tagName).toString();
68  3 Link tagLink = objectFactory.createLink();
69  3 tagLink.setHref(tagUri);
70  3 tagLink.setRel(Relations.TAG);
71  3 tag.getLinks().add(tagLink);
72   
73  3 tags.getTags().add(tag);
74    }
75   
76  3 return tags;
77    } catch (XWikiException e) {
78  0 throw new XWikiRestException(e);
79    }
80    }
81   
 
82  3 toggle @Override
83    public Response setTags(String wikiName, String spaceName, String pageName, Tags tags) throws XWikiRestException
84    {
85  3 try {
86  3 DocumentInfo documentInfo = getDocumentInfo(wikiName, spaceName, pageName, null, null, true, false);
87   
88  3 Document doc = documentInfo.getDocument();
89   
90  3 if (!doc.hasAccessLevel("edit", Utils.getXWikiUser(componentManager))) {
91  0 throw new WebApplicationException(Status.UNAUTHORIZED);
92    }
93   
94  3 List<String> tagNames = new ArrayList<String>();
95  3 for (Tag tag : tags.getTags()) {
96  3 tagNames.add(tag.getName());
97    }
98   
99  3 XWikiDocument xwikiDocument =
100    Utils.getXWiki(componentManager).getDocument(doc.getDocumentReference(),
101    Utils.getXWikiContext(componentManager));
102  3 BaseObject xwikiObject = xwikiDocument.getObject("XWiki.TagClass", 0);
103   
104  3 if (xwikiObject == null) {
105  1 int objectNumber =
106    xwikiDocument.createNewObject("XWiki.TagClass", Utils.getXWikiContext(componentManager));
107  1 xwikiObject = xwikiDocument.getObject("XWiki.TagClass", objectNumber);
108  1 if (xwikiObject == null) {
109  0 throw new WebApplicationException(Status.INTERNAL_SERVER_ERROR);
110    }
111   
112    // We must initialize all the fields to an empty value in order to correctly create the object
113  1 BaseClass xwikiClass = Utils.getXWiki(componentManager)
114    .getClass(xwikiObject.getClassName(), Utils.getXWikiContext(componentManager));
115  1 for (Object propertyNameObject : xwikiClass.getPropertyNames()) {
116  1 String propertyName = (String) propertyNameObject;
117  1 xwikiObject.set(propertyName, "", Utils.getXWikiContext(componentManager));
118    }
119    }
120   
121  3 xwikiObject.set("tags", tagNames, Utils.getXWikiContext(componentManager));
122   
123  3 doc.save();
124   
125  3 return Response.status(Status.ACCEPTED).entity(tags).build();
126    } catch (XWikiException e) {
127  0 throw new XWikiRestException(e);
128    }
129    }
130   
 
131  3 toggle private List<String> getTagsFromDocument(String documentId) throws XWikiException
132    {
133  3 XWikiDocument document =
134    Utils.getXWiki(componentManager).getDocument(documentId, Utils.getXWikiContext(componentManager));
135  3 BaseObject object = document.getObject("XWiki.TagClass");
136  3 if (object != null) {
137  3 BaseProperty prop = (BaseProperty) object.safeget("tags");
138  3 if (prop != null) {
139  3 return (List<String>) prop.getValue();
140    }
141    }
142   
143  0 return new ArrayList<String>();
144    }
145    }