001    /*
002     * Copyright (C) 2012 eXo Platform SAS.
003     *
004     * This is free software; you can redistribute it and/or modify it
005     * under the terms of the GNU Lesser General Public License as
006     * published by the Free Software Foundation; either version 2.1 of
007     * the License, or (at your option) any later version.
008     *
009     * This software is distributed in the hope that it will be useful,
010     * but WITHOUT ANY WARRANTY; without even the implied warranty of
011     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012     * Lesser General Public License for more details.
013     *
014     * You should have received a copy of the GNU Lesser General Public
015     * License along with this software; if not, write to the Free
016     * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
017     * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
018     */
019    package org.crsh.jcr;
020    
021    import org.crsh.util.Utils;
022    
023    import javax.jcr.Item;
024    import javax.jcr.Node;
025    import javax.jcr.Session;
026    import java.io.IOException;
027    import java.io.InputStream;
028    
029    public class SourceCommand extends SCPCommand implements Runnable {
030    
031      /** . */
032      private boolean recursive;
033    
034      public SourceCommand(String target, boolean recursive) {
035        super(target);
036    
037        //
038        this.recursive = recursive;
039      }
040    
041      @Override
042      protected void execute(Session session, String path) throws Exception {
043        FileSystem fs = new FileSystem() {
044          public void beginDirectory(String directoryName) throws IOException {
045            out.write("D0755 0 ".getBytes());
046            out.write(directoryName.getBytes());
047            out.write("\n".getBytes());
048            out.flush();
049            readAck();
050          }
051          public void file(String fileName, int length, InputStream data) throws IOException {
052            out.write("C0644 ".getBytes());
053            out.write(Integer.toString(length).getBytes());
054            out.write(" ".getBytes());
055            out.write(fileName.getBytes());
056            out.write("\n".getBytes());
057            out.flush();
058            readAck();
059            Utils.copy(data, out);
060            ack();
061            readAck();
062          }
063          public void endDirectory(String directoryName) throws IOException {
064            out.write("E\n".getBytes());
065            out.flush();
066            readAck();
067          }
068        };
069    
070        Item item = session.getItem(path);
071        if (item instanceof Node) {
072    
073          //
074          BytesOutputStream baos = new BytesOutputStream();
075    
076          // Perform export
077          session.exportSystemView(path, baos, false, false);
078    
079          //
080          String name = item.getName();
081          if (name.length() == 0) {
082            name = "jcr_root.xml";
083          } else {
084            name = name.replace(":", "_") + ".xml";
085          }
086    
087          //
088          baos.flush();
089          baos.close();
090    
091          //
092          fs.file(name, baos.size(), baos.getInputStream());
093        } else {
094          throw new Exception("Cannot export a property");
095        }
096      }
097    }