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