1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.wiki.workspacesmigrator.internal

File DefaultDocumentRestorerFromAttachedXAR.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

12
35
2
1
135
82
9
0.26
17.5
2
4.5

Classes

Class Line # Actions
DefaultDocumentRestorerFromAttachedXAR 52 35 0% 9 1
0.9795918598%
 

Contributing tests

This file is covered by 4 tests. .

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.wiki.workspacesmigrator.internal;
21   
22    import java.io.File;
23    import java.io.IOException;
24    import java.util.Iterator;
25    import java.util.List;
26   
27    import javax.inject.Inject;
28    import javax.inject.Provider;
29    import javax.inject.Singleton;
30   
31    import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
32    import org.apache.commons.compress.archivers.zip.ZipFile;
33    import org.apache.commons.io.FileUtils;
34    import org.slf4j.Logger;
35    import org.xwiki.component.annotation.Component;
36    import org.xwiki.model.reference.DocumentReference;
37   
38    import com.xpn.xwiki.XWiki;
39    import com.xpn.xwiki.XWikiContext;
40    import com.xpn.xwiki.XWikiException;
41    import com.xpn.xwiki.doc.XWikiAttachment;
42    import com.xpn.xwiki.doc.XWikiDocument;
43   
44    /**
45    * Default implementation for {@link DocumentRestorerFromAttachedXAR}.
46    *
47    * @version $Id: 91b65d9f15523fb99bb71dd7264b436430401d40 $
48    * @since 5.3RC1
49    */
50    @Component
51    @Singleton
 
52    public class DefaultDocumentRestorerFromAttachedXAR implements DocumentRestorerFromAttachedXAR
53    {
54    @Inject
55    private Logger logger;
56   
57    @Inject
58    private Provider<XWikiContext> xcontextProvider;
59   
 
60  4 toggle private File getTemporaryZipFile(DocumentReference docReference, String attachmentName)
61    throws XWikiException, IOException
62    {
63  4 XWikiContext xcontext = xcontextProvider.get();
64  4 XWiki xwiki = xcontext.getWiki();
65   
66    // Get the document
67  4 XWikiDocument document = xwiki.getDocument(docReference, xcontext);
68  4 if (document.isNew()) {
69  1 logger.warn("[{}] does not exist", docReference);
70  1 return null;
71    }
72   
73    // Get the attachment
74  3 XWikiAttachment xar = document.getAttachment(attachmentName);
75  3 if (xar == null) {
76  1 logger.warn("[{}] has no attachment named [{}].", docReference, attachmentName);
77  1 return null;
78    }
79   
80    // We need to copy the attachment to a temporary file because we want ti use ZipFile
81    // instead of ZipArchiveInputStream (see: http://commons.apache.org/proper/commons-compress/zip.html)
82  2 File tempFile = File.createTempFile(attachmentName, ".tmp");
83    // We copy the content of the attachment
84  2 FileUtils.copyInputStreamToFile(xar.getContentInputStream(xcontext), tempFile);
85   
86    // Return the temp file
87  2 return tempFile;
88    }
89   
 
90  4 toggle @Override
91    public void restoreDocumentFromAttachedXAR(DocumentReference docReference, String attachmentName,
92    List<DocumentReference> documentsToRestore) throws XWikiException
93    {
94  4 XWikiContext xcontext = xcontextProvider.get();
95  4 XWiki xwiki = xcontext.getWiki();
96  4 File tempZipFile = null;
97  4 try {
98  4 tempZipFile = getTemporaryZipFile(docReference, attachmentName);
99  4 if (tempZipFile == null) {
100  2 return;
101    }
102  2 ZipFile zipFile = new ZipFile(tempZipFile);
103    // We look for each document to restore if there is a corresponding zipEntry.
104  1 Iterator<DocumentReference> itDocumentsToRestore = documentsToRestore.iterator();
105  3 while (itDocumentsToRestore.hasNext()) {
106  2 DocumentReference docRef = itDocumentsToRestore.next();
107   
108    // Compute what should be the filename of the document to restore
109  2 String fileNameToRestore = String.format("%s/%s.xml", docRef.getLastSpaceReference().getName(),
110    docRef.getName());
111   
112    // Get the corresponding zip Entry
113  2 ZipArchiveEntry zipEntry = zipFile.getEntry(fileNameToRestore);
114  2 if (zipEntry != null) {
115    // Restore the document
116  2 XWikiDocument docToRestore = xwiki.getDocument(docRef, xcontext);
117  2 docToRestore.fromXML(zipFile.getInputStream(zipEntry));
118  2 xwiki.saveDocument(docToRestore, xcontext);
119    // We have restored this document
120  2 itDocumentsToRestore.remove();
121    }
122    }
123  1 zipFile.close();
124    } catch (IOException e) {
125  1 logger.error("Error during the decompression of [{}].", attachmentName, e);
126    } finally {
127    // Delete the temporary zip file
128  4 if (tempZipFile != null) {
129  2 tempZipFile.delete();
130    }
131    }
132    }
133   
134   
135    }