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