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.wikis; |
21 |
|
|
22 |
|
import java.io.IOException; |
23 |
|
import java.io.InputStream; |
24 |
|
import java.util.List; |
25 |
|
|
26 |
|
import javax.inject.Named; |
27 |
|
import javax.ws.rs.WebApplicationException; |
28 |
|
import javax.ws.rs.core.Response; |
29 |
|
|
30 |
|
import org.xwiki.component.annotation.Component; |
31 |
|
import org.xwiki.rest.XWikiResource; |
32 |
|
import org.xwiki.rest.XWikiRestException; |
33 |
|
import org.xwiki.rest.internal.DomainObjectFactory; |
34 |
|
import org.xwiki.rest.internal.Utils; |
35 |
|
import org.xwiki.rest.model.jaxb.Wiki; |
36 |
|
import org.xwiki.rest.resources.wikis.WikiResource; |
37 |
|
|
38 |
|
import com.xpn.xwiki.XWikiContext; |
39 |
|
import com.xpn.xwiki.XWikiException; |
40 |
|
import com.xpn.xwiki.plugin.packaging.PackageAPI; |
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
@version |
46 |
|
|
47 |
|
@Component |
48 |
|
@Named("org.xwiki.rest.internal.resources.wikis.WikiResourceImpl") |
|
|
| 54.4% |
Uncovered Elements: 31 (68) |
Complexity: 18 |
Complexity Density: 0.36 |
|
49 |
|
public class WikiResourceImpl extends XWikiResource implements WikiResource |
50 |
|
{ |
51 |
|
|
52 |
|
|
53 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
54 |
|
private enum HistoryOptions |
55 |
|
{ |
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
ADD, |
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
RESET, |
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
REPLACE |
68 |
|
} |
69 |
|
|
|
|
| 0% |
Uncovered Elements: 7 (7) |
Complexity: 3 |
Complexity Density: 0.6 |
|
70 |
0 |
@Override... |
71 |
|
public Wiki get(String wikiName) throws XWikiRestException |
72 |
|
{ |
73 |
0 |
try { |
74 |
0 |
if (wikiExists(wikiName)) { |
75 |
0 |
return DomainObjectFactory.createWiki(objectFactory, uriInfo.getBaseUri(), wikiName); |
76 |
|
} |
77 |
|
|
78 |
0 |
throw new WebApplicationException(Response.Status.NOT_FOUND); |
79 |
|
} catch (XWikiException e) { |
80 |
0 |
throw new XWikiRestException(e); |
81 |
|
} |
82 |
|
} |
83 |
|
|
|
|
| 57.5% |
Uncovered Elements: 17 (40) |
Complexity: 9 |
Complexity Density: 0.26 |
|
84 |
1 |
@Override... |
85 |
|
public Wiki importXAR(String wikiName, Boolean backup, String history, InputStream is) throws XWikiRestException |
86 |
|
{ |
87 |
1 |
try { |
88 |
1 |
if (!wikiExists(wikiName)) { |
89 |
0 |
throw new WebApplicationException(Response.Status.NOT_FOUND); |
90 |
|
} |
91 |
|
|
92 |
|
|
93 |
1 |
XWikiContext xwikiContext = getXWikiContext(); |
94 |
1 |
PackageAPI importer = ((PackageAPI) xwikiContext.getWiki().getPluginApi("package", xwikiContext)); |
95 |
1 |
if (importer == null) { |
96 |
0 |
throw new XWikiException(XWikiException.MODULE_XWIKI_ACCESS, XWikiException.ERROR_XWIKI_ACCESS_DENIED, |
97 |
|
"Can't access Package plugin API. Generally mean you don't have enough rights."); |
98 |
|
} |
99 |
|
|
100 |
1 |
String database = xwikiContext.getWikiId(); |
101 |
|
|
102 |
1 |
try { |
103 |
1 |
xwikiContext.setWikiId(wikiName); |
104 |
1 |
importer.setBackupPack(backup); |
105 |
|
|
106 |
1 |
importer.Import(is); |
107 |
|
|
108 |
1 |
HistoryOptions historyOption = parseHistoryOption(history, HistoryOptions.ADD); |
109 |
|
|
110 |
1 |
switch (historyOption) { |
111 |
0 |
case RESET: |
112 |
0 |
importer.setPreserveVersion(false); |
113 |
0 |
importer.setWithVersions(false); |
114 |
0 |
break; |
115 |
0 |
case REPLACE: |
116 |
0 |
importer.setPreserveVersion(false); |
117 |
0 |
importer.setWithVersions(true); |
118 |
0 |
break; |
119 |
0 |
default: |
120 |
1 |
case ADD: |
121 |
1 |
importer.setPreserveVersion(true); |
122 |
1 |
importer.setWithVersions(false); |
123 |
1 |
break; |
124 |
|
} |
125 |
|
|
126 |
|
|
127 |
1 |
importer.setBackupPack(backup); |
128 |
|
|
129 |
1 |
if (importer.install() == com.xpn.xwiki.plugin.packaging.DocumentInfo.INSTALL_IMPOSSIBLE) { |
130 |
0 |
throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR); |
131 |
|
} |
132 |
|
} catch (IOException e) { |
133 |
0 |
throw new WebApplicationException(e); |
134 |
|
} finally { |
135 |
1 |
xwikiContext.setWikiId(database); |
136 |
|
} |
137 |
|
|
138 |
1 |
return DomainObjectFactory.createWiki(objectFactory, uriInfo.getBaseUri(), wikiName); |
139 |
|
} catch (XWikiException e) { |
140 |
0 |
throw new XWikiRestException(e); |
141 |
|
} |
142 |
|
} |
143 |
|
|
144 |
|
|
145 |
|
|
146 |
|
|
147 |
|
@param |
148 |
|
@return |
149 |
|
@throws |
150 |
|
|
|
|
| 63.6% |
Uncovered Elements: 4 (11) |
Complexity: 3 |
Complexity Density: 0.43 |
|
151 |
1 |
protected boolean wikiExists(String wikiName) throws XWikiException... |
152 |
|
{ |
153 |
1 |
List<String> databaseNames = |
154 |
|
Utils.getXWiki(componentManager).getVirtualWikisDatabaseNames(Utils.getXWikiContext(componentManager)); |
155 |
|
|
156 |
1 |
if (databaseNames.isEmpty()) { |
157 |
0 |
databaseNames.add("xwiki"); |
158 |
|
} |
159 |
|
|
160 |
1 |
for (String databaseName : databaseNames) { |
161 |
1 |
if (databaseName.equals(wikiName)) { |
162 |
1 |
return true; |
163 |
|
} |
164 |
|
} |
165 |
|
|
166 |
0 |
return false; |
167 |
|
} |
168 |
|
|
169 |
|
|
170 |
|
|
171 |
|
|
172 |
|
@param |
173 |
|
@param |
174 |
|
@return |
175 |
|
|
|
|
| 66.7% |
Uncovered Elements: 2 (6) |
Complexity: 3 |
Complexity Density: 0.75 |
|
176 |
1 |
protected HistoryOptions parseHistoryOption(String value, HistoryOptions defaultValue)... |
177 |
|
{ |
178 |
1 |
try { |
179 |
1 |
if (value != null) { |
180 |
1 |
return HistoryOptions.valueOf(value.toUpperCase()); |
181 |
|
} |
182 |
|
} catch (IllegalArgumentException e) { |
183 |
|
|
184 |
|
} |
185 |
|
|
186 |
0 |
return defaultValue; |
187 |
|
} |
188 |
|
} |