1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springbyexample.jcr;
18
19 import java.io.File;
20 import java.io.FileOutputStream;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.OutputStream;
24 import java.util.ArrayList;
25 import java.util.List;
26
27 import javax.jcr.Node;
28 import javax.jcr.Property;
29 import javax.jcr.RepositoryException;
30 import javax.jcr.Session;
31
32 import org.apache.commons.io.FileUtils;
33 import org.apache.commons.io.IOUtils;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.springmodules.jcr.JcrConstants;
37
38
39
40
41
42
43
44
45 public class JcrContentExporter {
46
47 final Logger logger = LoggerFactory.getLogger(JcrContentExporter.class);
48
49 protected JcrContentRecurser contentRecurser = null;
50 protected String rootFolder = null;
51
52
53
54
55 public void setContentRecurser(JcrContentRecurser contentRecurser) {
56 this.contentRecurser = contentRecurser;
57 }
58
59
60
61
62 public void setRootFolder(String rootFolder) {
63 this.rootFolder = rootFolder;
64 }
65
66
67
68
69
70
71 public List<String> process() {
72 final List<String> lResults = new ArrayList<String>();
73
74 contentRecurser.recurse(new JcrNodeCallback() {
75 public void doInJcrNode(Session session, Node node)
76 throws IOException, RepositoryException {
77 JcrConstants jcrConstants = new JcrConstants(session);
78
79 String fileName = node.getParent().getName();
80
81 String path = node.getParent().getParent().getPath();
82
83 logger.debug("In content recurser callback." +
84 " fileName='{}' path='{}'", fileName, path);
85
86 File fileDir = new File(rootFolder + path);
87 File file = new File(fileDir, fileName);
88
89 Property dataProperty = node.getProperty(jcrConstants.getJCR_DATA());
90
91 InputStream in = null;
92 OutputStream out = null;
93
94 try {
95 FileUtils.forceMkdir(fileDir);
96
97 in = dataProperty.getStream();
98 out = new FileOutputStream(file);
99
100 IOUtils.copy(in, out);
101
102 lResults.add(path + File.pathSeparator + fileName);
103 } finally {
104 IOUtils.closeQuietly(in);
105 IOUtils.closeQuietly(out);
106 }
107 }
108 });
109
110 return lResults;
111 }
112
113 }