1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package com.xpn.xwiki.plugin.packaging

File PackageAPI.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart3.png
80% of files have more coverage

Code metrics

8
63
40
1
333
209
47
0.75
1.58
40
1.17

Classes

Class Line # Actions
PackageAPI 34 63 0% 47 82
0.2612612526.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 com.xpn.xwiki.plugin.packaging;
21   
22    import java.io.IOException;
23    import java.io.InputStream;
24    import java.util.ArrayList;
25    import java.util.List;
26   
27    import com.xpn.xwiki.XWikiContext;
28    import com.xpn.xwiki.XWikiException;
29    import com.xpn.xwiki.api.Api;
30    import com.xpn.xwiki.util.Util;
31   
32    import net.sf.json.JSONObject;
33   
 
34    public class PackageAPI extends Api
35    {
36    Package pack;
37   
 
38  224 toggle public PackageAPI(Package plugin, XWikiContext context) throws PackageException
39    {
40  224 super(context);
41   
42  224 if (!hasAdminRights()) {
43  15 throw new PackageException(XWikiException.ERROR_XWIKI_ACCESS_DENIED,
44    "Admin right is needed to use this plugin");
45    }
46   
47  209 setPlugin(plugin);
48    }
49   
 
50  209 toggle private void setPlugin(Package plugin)
51    {
52  209 this.pack = plugin;
53    }
54   
 
55  0 toggle public String getName()
56    {
57  0 return this.pack.getName();
58    }
59   
 
60  0 toggle public void setName(String name)
61    {
62  0 this.pack.setName(name);
63    }
64   
 
65  0 toggle public Package getPackage()
66    {
67  0 if (hasProgrammingRights()) {
68  0 return this.pack;
69    }
70  0 return null;
71    }
72   
 
73  0 toggle public String getDescription()
74    {
75  0 return this.pack.getDescription();
76    }
77   
 
78  0 toggle public void setDescription(String description)
79    {
80  0 this.pack.setDescription(description);
81    }
82   
 
83  0 toggle public String getVersion()
84    {
85  0 return this.pack.getVersion();
86    }
87   
 
88  0 toggle public void setVersion(String version)
89    {
90  0 this.pack.setVersion(version);
91    }
92   
 
93  0 toggle public String getLicence()
94    {
95  0 return this.pack.getLicence();
96    }
97   
 
98  0 toggle public void setLicence(String licence)
99    {
100  0 this.pack.setLicence(licence);
101    }
102   
 
103  0 toggle public String getAuthorName()
104    {
105  0 return this.pack.getAuthorName();
106    }
107   
 
108  0 toggle public void setAuthorName(String authorName)
109    {
110  0 this.pack.setAuthorName(authorName);
111    }
112   
 
113  0 toggle public boolean isBackupPack()
114    {
115  0 return this.pack.isBackupPack();
116    }
117   
 
118  2 toggle public void setBackupPack(boolean backupPack)
119    {
120  2 this.pack.setBackupPack(backupPack);
121    }
122   
123    /**
124    * Indicate if the current user has the right to import a package as a backup pack. In this implementation, to be
125    * able to import has backup pack the user must have the admin right on the XWiki.XWikiPreferences document from the
126    * main wiki (xwiki:XWiki.XWikiPreferences). The goal is to prevent local wiki administrators from importing
127    * documents saved with a global administrator as the author (rights escalation).
128    *
129    * @return true if the current user has the rights to import a package as a backup pack, false otherwise
130    */
 
131  88 toggle public boolean hasBackupPackImportRights()
132    {
133  88 return this.pack.hasBackupPackImportRights(this.context);
134    }
135   
 
136  0 toggle public boolean isVersionPreserved()
137    {
138  0 return this.pack.isVersionPreserved();
139    }
140   
141    /**
142    * Sets the flag for the packager to preserve or not existing versions of documents when installing with
143    * {@link #install()}. If set to true, the existing history revisions of documents will be preserve, if not, the
144    * history will be overridden.
145    *
146    * @param preserveVersion
147    */
 
148  1 toggle public void setPreserveVersion(boolean preserveVersion)
149    {
150  1 this.pack.setPreserveVersion(preserveVersion);
151    }
152   
 
153  0 toggle public boolean isWithVersions()
154    {
155  0 return this.pack.isWithVersions();
156    }
157   
158    /**
159    * Sets the flag for the packager to import or not history revisions included in the archive when installing with
160    * {@link #install()}. This flag will be ignored if {@link #isWithVersions()} flag is set to true. This means it's
161    * not possible to import with versions, preserving the existing document history. The behavior of the packager in
162    * this case fall backs on just adding a new version to the exsting history (ignoring the history from the package).
163    *
164    * @param withVersions should the versions contained in the archive (if any) be imported when installing.
165    */
 
166  1 toggle public void setWithVersions(boolean withVersions)
167    {
168  1 this.pack.setWithVersions(withVersions);
169    }
170   
 
171  0 toggle public void addDocumentFilter(Object filter) throws PackageException
172    {
173  0 this.pack.addDocumentFilter(filter);
174    }
175   
 
176  0 toggle public List<DocumentInfoAPI> getFiles()
177    {
178  0 List<DocumentInfo> files = this.pack.getFiles();
179  0 ArrayList<DocumentInfoAPI> apiFiles = new ArrayList<DocumentInfoAPI>(files.size());
180   
181  0 for (DocumentInfo docInfo : files) {
182  0 apiFiles.add(new DocumentInfoAPI(docInfo, getXWikiContext()));
183    }
184   
185  0 return apiFiles;
186    }
187   
 
188  0 toggle public boolean add(String docFullName, int DefaultAction) throws XWikiException
189    {
190  0 return this.pack.add(docFullName, DefaultAction, getXWikiContext());
191    }
192   
 
193  0 toggle public boolean add(String docFullName) throws XWikiException
194    {
195  0 return this.pack.add(docFullName, getXWikiContext());
196    }
197   
 
198  0 toggle public void setDocumentAction(String docFullName, int action)
199    {
200  0 for (DocumentInfo docInfo : this.pack.getFiles()) {
201  0 if (docInfo.getFullName().compareTo(docFullName) == 0) {
202  0 docInfo.setAction(action);
203    }
204    }
205    }
206   
 
207  0 toggle public void setDocumentAction(String docFullName, String language, int action)
208    {
209  0 for (DocumentInfo docInfo : this.pack.getFiles()) {
210  0 if ((docInfo.getFullName().compareTo(docFullName) == 0) && (language.equals(docInfo.getLanguage()))) {
211  0 docInfo.setAction(action);
212    }
213    }
214    }
215   
 
216  0 toggle public String export() throws IOException, XWikiException
217    {
218  0 getXWikiContext().getResponse().setContentType("application/zip");
219  0 getXWikiContext().getResponse().addHeader("Content-disposition",
220    "attachment; filename=" + Util.encodeURI(this.pack.getName(), this.context) + ".xar");
221  0 getXWikiContext().setFinished(true);
222   
223  0 return this.pack.export(getXWikiContext().getResponse().getOutputStream(), getXWikiContext());
224    }
225   
226    /**
227    * Similar to {@link #Import(byte[])}, except expected errors are catch. This version should be privileged when
228    * using the packager API from velocity scripts since it will not display stack-trace in case of error (for example
229    * if the passed file is not a valid package).
230    *
231    * @param data the file to create the package from, as a byte array.
232    * @return true if the package creation succeeded, false otherwise. If the package creation failed, the error
233    * message is placed in the velocity context under the <code>import_error</code> key,
234    * @since 2.2M1
235    */
 
236  0 toggle public boolean importPackageFromByteArray(byte data[])
237    {
238  0 try {
239  0 this.pack.Import(data, getXWikiContext());
240  0 return true;
241    } catch (XWikiException e) {
242  0 getXWikiContext().put("import_error", e.getMessage());
243  0 return false;
244    } catch (IOException e) {
245  0 getXWikiContext().put("import_error", e.getMessage());
246  0 return false;
247    }
248    }
249   
250    /**
251    * Load a package in memory from a byte array. It may be installed later using {@link #install()}. Your should
252    * prefer {@link #Import(InputStream)} which may avoid loading the package twice in memory.
253    *
254    * @param file an byte array containing a zipped package file
255    * @return an empty string, useless.
256    * @throws IOException while reading the ZipFile
257    * @throws XWikiException when package content is broken
258    */
 
259  0 toggle public String Import(byte file[]) throws IOException, XWikiException
260    {
261  0 return this.pack.Import(file, getXWikiContext());
262    }
263   
264    /**
265    * Load a package in memory from an InputStream. It may be installed later using {@link #install()}.
266    *
267    * @param file is an InputStream of a zipped package file
268    * @return an empty string, useless.
269    * @throws IOException while reading the ZipFile
270    * @throws XWikiException when package content is broken
271    * @since 2.3M2
272    */
 
273  1 toggle public String Import(InputStream file) throws IOException, XWikiException
274    {
275  1 return this.pack.Import(file, getXWikiContext());
276    }
277   
 
278  0 toggle public int testInstall()
279    {
280  0 return this.pack.testInstall(false, getXWikiContext());
281    }
282   
 
283  0 toggle public int testInstall(boolean isAdmin)
284    {
285  0 return this.pack.testInstall(isAdmin, getXWikiContext());
286    }
287   
 
288  0 toggle public void backupWiki() throws XWikiException, IOException
289    {
290  0 this.pack.addAllWikiDocuments(getXWikiContext());
291  0 this.export();
292    }
293   
 
294  0 toggle public String toXml()
295    {
296  0 return this.pack.toXml(getXWikiContext());
297    }
298   
299    /**
300    * @return a representation of this package under the JSON format
301    * @since 2.2M1
302    */
 
303  0 toggle public JSONObject toJSON()
304    {
305  0 return this.pack.toJSON(getXWikiContext());
306    }
307   
 
308  1 toggle public int install() throws XWikiException
309    {
310  1 return this.pack.install(getXWikiContext());
311    }
312   
 
313  8 toggle public List<String> getErrors()
314    {
315  8 return this.pack.getErrors(getXWikiContext());
316    }
317   
 
318  8 toggle public List<String> getSkipped()
319    {
320  8 return this.pack.getSkipped(getXWikiContext());
321    }
322   
 
323  12 toggle public List<String> getInstalled()
324    {
325  12 return this.pack.getInstalled(getXWikiContext());
326    }
327   
 
328  4 toggle public int getStatus()
329    {
330  4 return this.pack.getStatus(getXWikiContext());
331    }
332   
333    }