]> git.argeo.org Git - gpl/argeo-slc.git/blob - legacy/org.argeo.slc.support/src/org/argeo/slc/jsch/ScpFrom.java
Adapt to changes in Argeo Commons
[gpl/argeo-slc.git] / legacy / org.argeo.slc.support / src / org / argeo / slc / jsch / ScpFrom.java
1 package org.argeo.slc.jsch;
2
3 import java.io.File;
4 import java.io.FileOutputStream;
5 import java.io.IOException;
6 import java.io.InputStream;
7 import java.io.OutputStream;
8
9 import org.apache.commons.io.IOUtils;
10 import org.apache.commons.logging.Log;
11 import org.apache.commons.logging.LogFactory;
12 import org.argeo.slc.SlcException;
13 import org.springframework.core.io.Resource;
14
15 import com.jcraft.jsch.Channel;
16 import com.jcraft.jsch.ChannelExec;
17 import com.jcraft.jsch.Session;
18
19 public class ScpFrom extends AbstractJschTask {
20 private final static Log log = LogFactory.getLog(ScpFrom.class);
21
22 private Resource localResource;
23 private String remotePath;
24 private Boolean mkdir = false;
25
26 public void run(Session session) {
27 if (localResource != null) {
28 File lFile;
29 try {
30 lFile = localResource.getFile();
31 } catch (IOException e) {
32 throw new SlcException("Cannot interpret resource "
33 + localResource + " as file.", e);
34 }
35 downloadFile(session, lFile, remotePath);
36 }
37 }
38
39 protected void downloadFile(Session session, File localFile,
40 String remoteFile) {
41 OutputStream out = null;
42 OutputStream channelOut;
43 InputStream channelIn;
44 try {
45 // exec 'scp -f rfile' remotely
46 String command = "scp -f " + remoteFile;
47 Channel channel = session.openChannel("exec");
48 ((ChannelExec) channel).setCommand(command);
49
50 // get I/O streams for remote scp
51 channelOut = channel.getOutputStream();
52 channelIn = channel.getInputStream();
53
54 channel.connect();
55
56 byte[] buf = new byte[1024];
57
58 // send '\0'
59 buf[0] = 0;
60 channelOut.write(buf, 0, 1);
61 channelOut.flush();
62
63 while (true) {
64 int c = checkAck(channelIn);
65 if (c != 'C') {
66 break;
67 }
68
69 // read '0644 '
70 channelIn.read(buf, 0, 5);
71
72 long filesize = 0L;
73 while (true) {
74 if (channelIn.read(buf, 0, 1) < 0) {
75 // error
76 break;
77 }
78 if (buf[0] == ' ')
79 break;
80 filesize = filesize * 10L + (long) (buf[0] - '0');
81 }
82
83 String remoteFileName = null;
84 for (int i = 0;; i++) {
85 channelIn.read(buf, i, 1);
86 if (buf[i] == (byte) 0x0a) {
87 remoteFileName = new String(buf, 0, i);
88 break;
89 }
90 }
91
92 // System.out.println("filesize="+filesize+", file="+file);
93
94 // send '\0'
95 buf[0] = 0;
96 channelOut.write(buf, 0, 1);
97 channelOut.flush();
98
99 // Create a s adirectory if it doesn't exists
100 if (!localFile.exists() && mkdir)
101 localFile.mkdirs();
102
103 // read a content of lfile
104 String localPath = localFile.isDirectory() ? localFile
105 .getPath()
106 + File.separator + remoteFileName : localFile.getPath();
107
108 out = new FileOutputStream(localPath);
109 int foo;
110 while (true) {
111 if (buf.length < filesize)
112 foo = buf.length;
113 else
114 foo = (int) filesize;
115 foo = channelIn.read(buf, 0, foo);
116 if (foo < 0) {
117 // error
118 break;
119 }
120 out.write(buf, 0, foo);
121 filesize -= foo;
122 if (filesize == 0L)
123 break;
124 }
125
126 checkAck(channelIn);
127
128 // send '\0'
129 buf[0] = 0;
130 channelOut.write(buf, 0, 1);
131 channelOut.flush();
132
133 if (log.isDebugEnabled())
134 log.debug("Finished downloading " + remoteFile + " on "
135 + getSshTarget() + " to " + localPath);
136 }
137
138 channel.disconnect();
139 // session.disconnect();
140 } catch (Exception e) {
141 throw new SlcException("Cannot download " + remoteFile + " to "
142 + localFile, e);
143 } finally {
144 IOUtils.closeQuietly(out);
145 }
146 }
147
148 public void setLocalResource(Resource localFile) {
149 this.localResource = localFile;
150 }
151
152 public void setRemotePath(String remoteFile) {
153 this.remotePath = remoteFile;
154 }
155
156 public void setMkdir(Boolean mkdir) {
157 this.mkdir = mkdir;
158 }
159 }