1 package org.apache.maven.wagon.providers.ssh;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.sshd.server.Command;
23 import org.apache.sshd.server.CommandFactory;
24
25 import java.util.ArrayList;
26 import java.util.List;
27
28
29
30
31
32
33
34
35
36
37 public class ScpCommandFactory
38 implements CommandFactory
39 {
40
41 private CommandFactory delegate;
42
43 public ScpCommandFactory()
44 {
45 }
46
47 public ScpCommandFactory( CommandFactory delegate )
48 {
49 this.delegate = delegate;
50 }
51
52
53
54
55
56
57
58
59
60
61 public Command createCommand( String command )
62 {
63 try
64 {
65 return new ScpCommand( splitCommandString( command ) );
66 }
67 catch ( IllegalArgumentException iae )
68 {
69 if ( delegate != null )
70 {
71 return delegate.createCommand( command );
72 }
73 throw iae;
74 }
75 }
76
77 private String[] splitCommandString( String command )
78 {
79 if ( !command.trim().startsWith( "scp" ) )
80 {
81 throw new IllegalArgumentException( "Unknown command, does not begin with 'scp'" );
82 }
83
84 String[] args = command.split( " " );
85 List<String> parts = new ArrayList<String>();
86 parts.add( args[0] );
87 for ( int i = 1; i < args.length; i++ )
88 {
89 if ( !args[i].trim().startsWith( "-" ) )
90 {
91 parts.add( concatenateWithSpace( args, i ) );
92 break;
93 }
94 else
95 {
96 parts.add( args[i] );
97 }
98 }
99 return parts.toArray( new String[parts.size()] );
100 }
101
102 private String concatenateWithSpace( String[] args, int from )
103 {
104 StringBuilder sb = new StringBuilder();
105
106 for ( int i = from; i < args.length; i++ )
107 {
108 sb.append( args[i] + " " );
109 }
110 return sb.toString().trim();
111 }
112 }