View Javadoc

1   /*
2    * Copyright 2007-2012 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Processes JCR content repository and 
40   * writes out all files into a root directory in the 
41   * same hierarchical structure they were found in.
42   * 
43   * @author David Winterfeldt
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       * Sets JCR content recurser.
54       */
55      public void setContentRecurser(JcrContentRecurser contentRecurser) {
56          this.contentRecurser = contentRecurser;
57      }
58  
59      /**
60       * Sets root folder where repository content will be copied.
61       */
62      public void setRootFolder(String rootFolder) {
63          this.rootFolder = rootFolder;
64      }
65      
66      /**
67       * Process content nodes.
68       * 
69       *  @return     List     List of content node paths processed.     
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                  // file name node is above content
79                  String fileName = node.getParent().getName();
80                  // file path is two above content
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 }