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

File WikiResourceImpl.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart6.png
69% of files have more coverage

Code metrics

14
50
4
2
188
112
18
0.36
12.5
2
4.5

Classes

Class Line # Actions
WikiResourceImpl 49 50 0% 18 31
0.544117654.4%
WikiResourceImpl.HistoryOptions 54 0 - 0 0
-1.0 -
 

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.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    * Resource for interacting with a specific wiki.
44    *
45    * @version $Id: 6ce1ba28904c1572ab627710f2996094103d17e7 $
46    */
47    @Component
48    @Named("org.xwiki.rest.internal.resources.wikis.WikiResourceImpl")
 
49    public class WikiResourceImpl extends XWikiResource implements WikiResource
50    {
51    /**
52    * The possible option for managing history when importing wiki documents.
53    */
 
54    private enum HistoryOptions
55    {
56    /**
57    * Add a new version.
58    */
59    ADD,
60    /**
61    * Reset the version to 1.1.
62    */
63    RESET,
64    /**
65    * Replace the current version.
66    */
67    REPLACE
68    }
69   
 
70  0 toggle @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   
 
84  1 toggle @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    /* Use the package plugin for importing pages */
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    // Set the backup pack option
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    * Check if a wiki exists.
146    *
147    * @param wikiName the wiki name.
148    * @return true if the wiki exists.
149    * @throws XWikiException if something goes wrong.
150    */
 
151  1 toggle 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    * Return the HistoryOptions enum object corresponding to a string.
171    *
172    * @param value a string representing a history option.
173    * @param defaultValue the value to be returned in the case no corresponding history option is found.
174    * @return the history option enum object,
175    */
 
176  1 toggle 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    // Invalid query type string.
184    }
185   
186  0 return defaultValue;
187    }
188    }