]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/jsch/ScpFrom.java
Add mail support
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.simple / src / main / java / 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 if (log.isDebugEnabled())
108 log.debug("Download " + remoteFile + " to " + localPath);
109
110 out = new FileOutputStream(localPath);
111 int foo;
112 while (true) {
113 if (buf.length < filesize)
114 foo = buf.length;
115 else
116 foo = (int) filesize;
117 foo = channelIn.read(buf, 0, foo);
118 if (foo < 0) {
119 // error
120 break;
121 }
122 out.write(buf, 0, foo);
123 filesize -= foo;
124 if (filesize == 0L)
125 break;
126 }
127
128 checkAck(channelIn);
129
130 // send '\0'
131 buf[0] = 0;
132 channelOut.write(buf, 0, 1);
133 channelOut.flush();
134 }
135
136 channel.disconnect();
137 // session.disconnect();
138 } catch (Exception e) {
139 throw new SlcException("Cannot download " + remoteFile + " to "
140 + localFile, e);
141 } finally {
142 IOUtils.closeQuietly(out);
143 }
144 }
145
146 public void setLocalResource(Resource localFile) {
147 this.localResource = localFile;
148 }
149
150 public void setRemotePath(String remoteFile) {
151 this.remotePath = remoteFile;
152 }
153
154 public void setMkdir(Boolean mkdir) {
155 this.mkdir = mkdir;
156 }
157 }