1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
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 |
49 |
|
|
50 |
|
@Component |
51 |
|
@Named("org.xwiki.rest.internal.resources.pages.PageTagsResourceImpl") |
|
|
| 84.5% |
Uncovered Elements: 9 (58) |
Complexity: 10 |
Complexity Density: 0.22 |
|
52 |
|
public class PageTagsResourceImpl extends ModifiablePageResource implements PageTagsResource |
53 |
|
{ |
|
|
| 93.3% |
Uncovered Elements: 1 (15) |
Complexity: 2 |
Complexity Density: 0.13 |
|
54 |
3 |
@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.8% |
Uncovered Elements: 5 (29) |
Complexity: 5 |
Complexity Density: 0.22 |
|
82 |
3 |
@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 |
|
|
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 |
|
|
|
|
| 72.7% |
Uncovered Elements: 3 (11) |
Complexity: 3 |
Complexity Density: 0.43 |
|
131 |
3 |
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 |
|
} |