001    /*
002     * Copyright (C) 2003-2009 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.BytesOutputStream;
022    import org.crsh.util.IO;
023    
024    import javax.jcr.Item;
025    import javax.jcr.Node;
026    import javax.jcr.Session;
027    import java.io.IOException;
028    import java.io.InputStream;
029    
030    /**
031     * @author <a href="mailto:julien.viet@exoplatform.com">Julien Viet</a>
032     * @version $Revision$
033     */
034    public class SourceCommand extends SCPCommand implements Runnable {
035    
036      /** . */
037      private boolean recursive;
038    
039      public SourceCommand(String target, boolean recursive) {
040        super(target);
041    
042        //
043        this.recursive = recursive;
044      }
045    
046      @Override
047      protected void execute(Session session, String path) throws Exception {
048        FileSystem fs = new FileSystem() {
049          public void startDirectory(String directoryName) throws IOException {
050            out.write("D0755 0 ".getBytes());
051            out.write(directoryName.getBytes());
052            out.write("\n".getBytes());
053            out.flush();
054            readAck();
055          }
056          public void file(String fileName, int length, InputStream data) throws IOException {
057            out.write("C0644 ".getBytes());
058            out.write(Integer.toString(length).getBytes());
059            out.write(" ".getBytes());
060            out.write(fileName.getBytes());
061            out.write("\n".getBytes());
062            out.flush();
063            readAck();
064            IO.copy(data, out);
065            ack();
066            readAck();
067          }
068          public void endDirectory(String directoryName) throws IOException {
069            out.write("E\n".getBytes());
070            out.flush();
071            readAck();
072          }
073        };
074    
075        Item item = session.getItem(path);
076        if (item instanceof Node) {
077    
078          //
079          BytesOutputStream baos = new BytesOutputStream();
080    
081          // Perform export
082          session.exportSystemView(path, baos, false, false);
083    
084          //
085          String name = item.getName();
086          if (name.length() == 0) {
087            name = "jcr_root.xml";
088          } else {
089            name = name.replace(":", "_") + ".xml";
090          }
091    
092          //
093          baos.flush();
094          baos.close();
095    
096          //
097          fs.file(name, baos.size(), baos.getInputStream());
098        } else {
099          throw new Exception("Cannot export a property");
100        }
101      }
102    }