Skip to content

Package: ScpTo

ScpTo

nameinstructionbranchcomplexitylinemethod
checkAck(InputStream)
M: 40 C: 0
0%
M: 10 C: 0
0%
M: 6 C: 0
0%
M: 11 C: 0
0%
M: 1 C: 0
0%
cleanup(FileInputStream, byte[], OutputStream, Channel)
M: 18 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
createScpStatement(String, long, String)
M: 45 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
handleException(Exception, FileInputStream)
M: 13 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
scpTo(String, SshSession, String)
M: 120 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 32 C: 0
0%
M: 1 C: 0
0%

Coverage

1: package update.remote.ssh;
2:
3: import java.io.File;
4: import java.io.FileInputStream;
5: import java.io.IOException;
6: import java.io.InputStream;
7: import java.io.OutputStream;
8: import java.nio.charset.Charset;
9:
10: import com.jcraft.jsch.Channel;
11: import com.jcraft.jsch.ChannelExec;
12: import com.jcraft.jsch.Session;
13:
14: /**
15: * send files with this marvellous class. This is a modification of the ScpTo example on the jsch
16: * homepage.
17: *
18: * @author Muri
19: *
20: */
21: public final class ScpTo {
22:
23:         /**
24:          * UTF-8.
25:          */
26:         private static final String UTF_8 = "UTF-8";
27:
28:         /**
29:          *
30:          */
31:         private ScpTo() {
32:                 super();
33:                 // TODO Auto-generated constructor stub
34:         }
35:
36:         /**
37:          * This is 2^10 (aka. 1024).
38:          */
39:         private static final int NUMBER_1024 = 1024;
40:         /**
41:          * Yes, it is 1000.
42:          */
43:         private static final int NUMBER_1000 = 1000;
44:
45:         /**
46:          * use this method to push something via ssh.
47:          *
48:          * @param path
49:          * path
50:          * @param sshSession
51:          * sshSession
52:          * @param destination
53:          * destination
54:          */
55:         public static void scpTo(final String path, final SshSession sshSession,
56:                         final String destination) {
57:
58:                 FileInputStream fis = null;
59:                 try {
60:
61:                         final Session session = sshSession.getSession();
62:
63:                         // exec 'scp -t rfile' remotely
64:                         String command = "scp -p -t " + destination;
65:                         final Channel channel = session.openChannel("exec");
66:                         ((ChannelExec) channel).setCommand(command);
67:
68:                         // get I/O streams for remote scp
69:                         final OutputStream out = channel.getOutputStream();
70:                         final InputStream inst = channel.getInputStream();
71:
72:                         channel.connect();
73:
74:•                        if (checkAck(inst) != 0) {
75:                                 System.exit(0); // NOPMD because it's not an EE app.
76:                         }
77:
78:                         final File file = new File(path);
79:
80:                         // The access time should be sent at second,
81:                         // but it is not accessible with JavaAPI ;-<
82:                         command =
83:                                         "T " + (file.lastModified() / NUMBER_1000) + " 0 "
84:                                                         + (file.lastModified() / NUMBER_1000) + " 0\n";
85:                         out.write(command.getBytes(Charset.forName(UTF_8)));
86:                         out.flush();
87:
88:                         // send "C0644 filesize filename", where filename should not include '/'
89:                         final long filesize = file.length();
90:                         command = createScpStatement(command, filesize, path);
91:                         out.write(command.getBytes(Charset.forName(UTF_8)));
92:                         out.flush();
93:
94:                         // send a content of path
95:                         fis = new FileInputStream(path);
96:                         final byte[] buf = new byte[NUMBER_1024];
97:                         while (true) {
98:                                 final int len = fis.read(buf, 0, buf.length);
99:•                                if (len <= 0) {
100:                                         break;
101:                                 }
102:                                 out.write(buf, 0, len); // out.flush();
103:                         }
104:                         cleanup(fis, buf, out, channel);
105:
106:                 } catch (final Exception e) {
107:                         handleException(e, fis);
108:                 }
109:         }
110:
111:         /**
112:          * extracted exception handling to shorten scpTo.
113:          *
114:          * @param exception
115:          * exception
116:          * @param fis
117:          * fis
118:          */
119:         private static void handleException(final Exception exception, final FileInputStream fis) {
120:                 System.out.println(exception); // NOPMD This is okay, because it should be a console output.
121:                 try {
122:•                        if (fis != null) {
123:                                 fis.close();
124:                         }
125:                 } catch (final Exception ee) {
126:                         System.out.println(ee); // NOPMD This is okay, because it should be a console output.
127:                 }
128:         }
129:
130:         /**
131:          * extracted cleanup to shorten scpTo.
132:          *
133:          * @param fis
134:          * fis
135:          * @param buf
136:          * buf
137:          * @param out
138:          * out
139:          * @param channel
140:          * channel
141:          * @throws IOException
142:          * IOException
143:          */
144:         private static void cleanup(final FileInputStream fis, final byte[] buf,
145:                         final OutputStream out, final Channel channel) throws IOException {
146:                 fis.close();
147:                 // send '\0'
148:                 buf[0] = 0;
149:                 out.write(buf, 0, 1);
150:                 out.flush();
151:                 out.close();
152:
153:                 channel.disconnect();
154:         }
155:
156:         /**
157:          * extracted statement creation to make scpTo shorter.
158:          *
159:          * @param command
160:          * command
161:          * @param filesize
162:          * filesize
163:          * @param path
164:          * path
165:          * @return the new command
166:          */
167:         private static String createScpStatement(final String command, final long filesize,
168:                         final String path) {
169:                 final StringBuffer newCommand = new StringBuffer(command);
170:                 newCommand.append("C0644 " + filesize + " ");
171:•                if (path.lastIndexOf('/') > 0) {
172:                         newCommand.append(path.substring(path.lastIndexOf('/') + 1));
173:                 } else {
174:                         newCommand.append(path);
175:                 }
176:                 newCommand.append(Character.valueOf('\n'));
177:                 return newCommand.toString();
178:         }
179:
180:         /**
181:          * method to check connection.
182:          *
183:          * @param inst
184:          * inst
185:          * @return int
186:          * @throws IOException
187:          * IOException
188:          */
189:         private static int checkAck(final InputStream inst) throws IOException {
190:                 final int errorcode = inst.read();
191:                 // b may be 0 for success,
192:                 // 1 for error,
193:                 // 2 for fatal error,
194:                 // -1
195:•                if (errorcode == 1 || errorcode == 2) {
196:                         final StringBuffer stb = new StringBuffer();
197:                         int code;
198:                         do {
199:                                 code = inst.read();
200:                                 stb.append((char) code);
201:•                        } while (code != '\n');
202:•                        if (errorcode == 1) { // error
203:                                 System.out.print(stb.toString()); // NOPMD This is okay, because it should be a
204:                                                                                                         // console output.
205:                         }
206:•                        if (errorcode == 2) { // fatal error
207:                                 System.out.print(stb.toString()); // NOPMD This is okay, because it should be a
208:                                                                                                         // console output.
209:                         }
210:                 }
211:                 return errorcode;
212:         }
213: }