View Javadoc
1   package org.apache.maven.wagon.providers.ssh;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * This <code>CommandFactory</code> can be used as a standalone command factory
30   * or can be used to augment another <code>CommandFactory</code> and provides
31   * <code>SCP</code> support.
32   *
33   * @see ScpCommand
34   *
35   * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
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       * Parses a command string and verifies that the basic syntax is
54       * correct. If parsing fails the responsibility is delegated to
55       * the configured {@link org.apache.sshd.server.CommandFactory} instance; if one exist.
56       *
57       * @param command command to parse
58       * @return configured {@link org.apache.sshd.server.Command} instance
59       * @throws IllegalArgumentException
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 }