1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.store.legacy.store.internal; |
21 |
|
|
22 |
|
import java.io.File; |
23 |
|
import java.io.FileInputStream; |
24 |
|
import java.io.IOException; |
25 |
|
import java.io.InputStream; |
26 |
|
import java.util.List; |
27 |
|
import java.util.concurrent.locks.ReadWriteLock; |
28 |
|
|
29 |
|
import com.xpn.xwiki.doc.XWikiAttachment; |
30 |
|
import com.xpn.xwiki.doc.XWikiAttachmentArchive; |
31 |
|
import com.xpn.xwiki.store.AttachmentVersioningStore; |
32 |
|
import com.xpn.xwiki.XWikiContext; |
33 |
|
import com.xpn.xwiki.XWikiException; |
34 |
|
import javax.inject.Inject; |
35 |
|
import javax.inject.Named; |
36 |
|
import javax.inject.Singleton; |
37 |
|
import org.xwiki.component.annotation.Component; |
38 |
|
import org.xwiki.store.legacy.doc.internal.FilesystemAttachmentContent; |
39 |
|
import org.xwiki.store.legacy.doc.internal.ListAttachmentArchive; |
40 |
|
import org.xwiki.store.filesystem.internal.AttachmentFileProvider; |
41 |
|
import org.xwiki.store.filesystem.internal.FilesystemStoreTools; |
42 |
|
import org.xwiki.store.serialization.Serializer; |
43 |
|
import org.xwiki.store.StartableTransactionRunnable; |
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
@version |
49 |
|
@since |
50 |
|
|
51 |
|
@Component |
52 |
|
@Named("file") |
53 |
|
@Singleton |
|
|
| 49.4% |
Uncovered Elements: 43 (85) |
Complexity: 22 |
Complexity Density: 0.4 |
|
54 |
|
public class FilesystemAttachmentVersioningStore implements AttachmentVersioningStore |
55 |
|
{ |
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
private static final String UNKNOWN_NAME = "UNKNOWN"; |
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
@Inject |
65 |
|
private FilesystemStoreTools fileTools; |
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
@Inject |
71 |
|
@Named("attachment-list-meta/1.0") |
72 |
|
private Serializer<List<XWikiAttachment>, List<XWikiAttachment>> metaSerializer; |
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
@param |
78 |
|
@param |
79 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
80 |
3 |
public FilesystemAttachmentVersioningStore(final FilesystemStoreTools fileTools,... |
81 |
|
final Serializer<List<XWikiAttachment>, |
82 |
|
List<XWikiAttachment>> metaSerializer) |
83 |
|
{ |
84 |
3 |
this.fileTools = fileTools; |
85 |
3 |
this.metaSerializer = metaSerializer; |
86 |
|
} |
87 |
|
|
88 |
|
|
89 |
|
|
90 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
91 |
0 |
public FilesystemAttachmentVersioningStore()... |
92 |
|
{ |
93 |
|
} |
94 |
|
|
|
|
| 16.7% |
Uncovered Elements: 10 (12) |
Complexity: 4 |
Complexity Density: 0.5 |
|
95 |
2 |
@Override... |
96 |
|
public XWikiAttachmentArchive loadArchive(final XWikiAttachment attachment, |
97 |
|
final XWikiContext context, |
98 |
|
final boolean bTransaction) |
99 |
|
throws XWikiException |
100 |
|
{ |
101 |
|
|
102 |
2 |
try { |
103 |
2 |
return this.loadArchive(attachment, this.fileTools.getAttachmentFileProvider(attachment)); |
104 |
|
} catch (Exception e) { |
105 |
0 |
if (e instanceof XWikiException) { |
106 |
0 |
throw (XWikiException) e; |
107 |
|
} |
108 |
0 |
final Object[] args = {attachment.getFilename(), UNKNOWN_NAME}; |
109 |
0 |
if (attachment.getDoc() != null) { |
110 |
0 |
args[1] = attachment.getDoc().getFullName(); |
111 |
|
} |
112 |
0 |
throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, |
113 |
|
XWikiException.ERROR_XWIKI_UNKNOWN, |
114 |
|
"Exception while loading attachment archive {0} for document {1}", |
115 |
|
e, args); |
116 |
|
} |
117 |
|
} |
118 |
|
|
119 |
|
|
120 |
|
|
121 |
|
|
122 |
|
@param |
123 |
|
@param |
124 |
|
@return |
125 |
|
@throws |
126 |
|
|
|
|
| 90% |
Uncovered Elements: 2 (20) |
Complexity: 2 |
Complexity Density: 0.11 |
|
127 |
2 |
XWikiAttachmentArchive loadArchive(final XWikiAttachment attachment,... |
128 |
|
final AttachmentFileProvider provider) |
129 |
|
throws IOException |
130 |
|
{ |
131 |
2 |
final File metaFile = provider.getAttachmentVersioningMetaFile(); |
132 |
|
|
133 |
|
|
134 |
2 |
if (!metaFile.exists()) { |
135 |
0 |
return new ListAttachmentArchive(attachment); |
136 |
|
} |
137 |
|
|
138 |
2 |
final ReadWriteLock lock = this.fileTools.getLockForFile(metaFile); |
139 |
2 |
final List<XWikiAttachment> attachList; |
140 |
2 |
lock.readLock().lock(); |
141 |
2 |
try { |
142 |
2 |
final InputStream is = new FileInputStream(metaFile); |
143 |
2 |
attachList = this.metaSerializer.parse(is); |
144 |
2 |
is.close(); |
145 |
|
} finally { |
146 |
2 |
lock.readLock().unlock(); |
147 |
|
} |
148 |
|
|
149 |
|
|
150 |
2 |
for (XWikiAttachment attach : attachList) { |
151 |
6 |
final File contentFile = provider.getAttachmentVersionContentFile(attach.getVersion()); |
152 |
6 |
attach.setAttachment_content(new FilesystemAttachmentContent(contentFile, attach)); |
153 |
|
|
154 |
6 |
attach.setDoc(attachment.getDoc()); |
155 |
|
} |
156 |
|
|
157 |
2 |
final ListAttachmentArchive out = new ListAttachmentArchive(attachList); |
158 |
2 |
out.setAttachment(attachment); |
159 |
2 |
return out; |
160 |
|
} |
161 |
|
|
162 |
|
|
163 |
|
@inheritDoc |
164 |
|
|
165 |
|
|
166 |
|
|
167 |
|
|
168 |
|
|
169 |
|
@see |
170 |
|
|
|
|
| 12.5% |
Uncovered Elements: 14 (16) |
Complexity: 5 |
Complexity Density: 0.5 |
|
171 |
3 |
@Override... |
172 |
|
public void saveArchive(final XWikiAttachmentArchive archive, |
173 |
|
final XWikiContext context, |
174 |
|
final boolean bTransaction) |
175 |
|
throws XWikiException |
176 |
|
{ |
177 |
3 |
try { |
178 |
3 |
this.getArchiveSaveRunnable(archive, context).start(); |
179 |
|
} catch (Exception e) { |
180 |
0 |
if (e instanceof XWikiException) { |
181 |
0 |
throw (XWikiException) e; |
182 |
|
} |
183 |
0 |
final Object[] args = {UNKNOWN_NAME, UNKNOWN_NAME}; |
184 |
0 |
if (archive.getAttachment() != null) { |
185 |
0 |
args[0] = archive.getAttachment().getFilename(); |
186 |
0 |
if (archive.getAttachment().getDoc() != null) { |
187 |
0 |
args[1] = archive.getAttachment().getDoc().getFullName(); |
188 |
|
} |
189 |
|
} |
190 |
0 |
throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, |
191 |
|
XWikiException.ERROR_XWIKI_UNKNOWN, |
192 |
|
"Exception while saving attachment archive {0} of document {1}", |
193 |
|
e, args); |
194 |
|
} |
195 |
|
} |
196 |
|
|
197 |
|
|
198 |
|
|
199 |
|
|
200 |
|
|
201 |
|
@param |
202 |
|
@param |
203 |
|
|
204 |
|
@return |
205 |
|
@throws |
206 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
207 |
3 |
public StartableTransactionRunnable getArchiveSaveRunnable(final XWikiAttachmentArchive archive,... |
208 |
|
final XWikiContext context) |
209 |
|
throws XWikiException |
210 |
|
{ |
211 |
3 |
return new AttachmentArchiveSaveRunnable( |
212 |
|
archive, this.fileTools, this.fileTools.getAttachmentFileProvider(archive.getAttachment()), |
213 |
|
this.metaSerializer, context); |
214 |
|
} |
215 |
|
|
216 |
|
|
217 |
|
@inheritDoc |
218 |
|
|
219 |
|
|
220 |
|
|
221 |
|
|
222 |
|
|
223 |
|
|
224 |
|
@see |
225 |
|
|
|
|
| 29.4% |
Uncovered Elements: 12 (17) |
Complexity: 5 |
Complexity Density: 0.45 |
|
226 |
1 |
@Override... |
227 |
|
public void deleteArchive(final XWikiAttachment attachment, |
228 |
|
final XWikiContext context, |
229 |
|
final boolean bTransaction) |
230 |
|
throws XWikiException |
231 |
|
{ |
232 |
1 |
if (attachment == null) { |
233 |
0 |
throw new NullPointerException("The attachment to delete cannot be null"); |
234 |
|
} |
235 |
1 |
try { |
236 |
1 |
final XWikiAttachmentArchive archive = this.loadArchive(attachment, context, bTransaction); |
237 |
1 |
this.getArchiveDeleteRunnable(archive).start(); |
238 |
|
} catch (Exception e) { |
239 |
0 |
if (e instanceof XWikiException) { |
240 |
0 |
throw (XWikiException) e; |
241 |
|
} |
242 |
0 |
final Object[] args = {attachment.getFilename(), UNKNOWN_NAME}; |
243 |
0 |
if (attachment.getDoc() != null) { |
244 |
0 |
args[1] = attachment.getDoc().getFullName(); |
245 |
|
} |
246 |
0 |
throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, |
247 |
|
XWikiException.ERROR_XWIKI_UNKNOWN, |
248 |
|
"Exception while deleting attachment archive {0} from document {1}", |
249 |
|
e, args); |
250 |
|
} |
251 |
|
} |
252 |
|
|
253 |
|
|
254 |
|
|
255 |
|
|
256 |
|
|
257 |
|
@param |
258 |
|
@return |
259 |
|
|
|
|
| 55.6% |
Uncovered Elements: 4 (9) |
Complexity: 3 |
Complexity Density: 0.6 |
|
260 |
1 |
public StartableTransactionRunnable getArchiveDeleteRunnable(final XWikiAttachmentArchive archive)... |
261 |
|
{ |
262 |
1 |
if (archive == null) { |
263 |
0 |
throw new NullPointerException("The archive to delete cannot be null."); |
264 |
|
} |
265 |
1 |
if (archive.getAttachment() == null) { |
266 |
0 |
throw new IllegalArgumentException( |
267 |
|
"Cannot delete an archive unless it is associated with an attachment."); |
268 |
|
} |
269 |
1 |
return new AttachmentArchiveDeleteRunnable( |
270 |
|
archive, this.fileTools, this.fileTools.getAttachmentFileProvider(archive.getAttachment())); |
271 |
|
} |
272 |
|
} |