1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
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 |
|
@link |
46 |
|
|
47 |
|
@version |
48 |
|
@since |
49 |
|
|
50 |
|
@Component |
51 |
|
@Singleton |
|
|
| 98% |
Uncovered Elements: 1 (49) |
Complexity: 9 |
Complexity Density: 0.26 |
|
52 |
|
public class DefaultDocumentRestorerFromAttachedXAR implements DocumentRestorerFromAttachedXAR |
53 |
|
{ |
54 |
|
@Inject |
55 |
|
private Logger logger; |
56 |
|
|
57 |
|
@Inject |
58 |
|
private Provider<XWikiContext> xcontextProvider; |
59 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (17) |
Complexity: 3 |
Complexity Density: 0.23 |
|
60 |
4 |
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 |
|
|
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 |
|
|
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 |
|
|
81 |
|
|
82 |
2 |
File tempFile = File.createTempFile(attachmentName, ".tmp"); |
83 |
|
|
84 |
2 |
FileUtils.copyInputStreamToFile(xar.getContentInputStream(xcontext), tempFile); |
85 |
|
|
86 |
|
|
87 |
2 |
return tempFile; |
88 |
|
} |
89 |
|
|
|
|
| 96.7% |
Uncovered Elements: 1 (30) |
Complexity: 6 |
Complexity Density: 0.27 |
|
90 |
4 |
@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 |
|
|
104 |
1 |
Iterator<DocumentReference> itDocumentsToRestore = documentsToRestore.iterator(); |
105 |
3 |
while (itDocumentsToRestore.hasNext()) { |
106 |
2 |
DocumentReference docRef = itDocumentsToRestore.next(); |
107 |
|
|
108 |
|
|
109 |
2 |
String fileNameToRestore = String.format("%s/%s.xml", docRef.getLastSpaceReference().getName(), |
110 |
|
docRef.getName()); |
111 |
|
|
112 |
|
|
113 |
2 |
ZipArchiveEntry zipEntry = zipFile.getEntry(fileNameToRestore); |
114 |
2 |
if (zipEntry != null) { |
115 |
|
|
116 |
2 |
XWikiDocument docToRestore = xwiki.getDocument(docRef, xcontext); |
117 |
2 |
docToRestore.fromXML(zipFile.getInputStream(zipEntry)); |
118 |
2 |
xwiki.saveDocument(docToRestore, xcontext); |
119 |
|
|
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 |
|
|
128 |
4 |
if (tempZipFile != null) { |
129 |
2 |
tempZipFile.delete(); |
130 |
|
} |
131 |
|
} |
132 |
|
} |
133 |
|
|
134 |
|
|
135 |
|
} |