View Javadoc

1   package org.apache.maven.wagon.providers.ssh;
2   /*
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   */
20  
21  import org.apache.sshd.server.Command;
22  import org.apache.sshd.server.CommandFactory;
23  
24  import java.util.ArrayList;
25  import java.util.List;
26  
27  /**
28   * This <code>CommandFactory</code> can be used as a standalone command factory
29   * or can be used to augment another <code>CommandFactory</code> and provides
30   * <code>SCP</code> support.
31   *
32   * @see ScpCommand
33   *
34   * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
35   */
36  public class ScpCommandFactory
37      implements CommandFactory {
38  
39      private CommandFactory delegate;
40  
41      public ScpCommandFactory() {
42      }
43  
44      public ScpCommandFactory( CommandFactory delegate ) {
45          this.delegate = delegate;
46      }
47  
48      /**
49       * Parses a command string and verifies that the basic syntax is
50       * correct. If parsing fails the responsibility is delegated to
51       * the configured {@link org.apache.sshd.server.CommandFactory} instance; if one exist.
52       *
53       * @param command command to parse
54       * @return configured {@link org.apache.sshd.server.Command} instance
55       * @throws IllegalArgumentException
56       */
57      public Command createCommand(String command) {
58          try {
59              return new ScpCommand(splitCommandString(command));
60          } catch (IllegalArgumentException iae) {
61              if (delegate != null) {
62                  return delegate.createCommand(command);
63              }
64              throw iae;
65          }
66      }
67  
68      private String[] splitCommandString(String command) {
69          if (!command.trim().startsWith("scp")) {
70              throw new IllegalArgumentException("Unknown command, does not begin with 'scp'");
71          }
72  
73          String[] args = command.split(" ");
74          List<String> parts = new ArrayList<String>();
75          parts.add(args[0]);
76          for (int i = 1; i < args.length; i++) {
77              if (!args[i].trim().startsWith("-")) {
78                  parts.add(concatenateWithSpace(args, i));
79                  break;
80              } else {
81                  parts.add(args[i]);
82              }
83          }
84          return parts.toArray(new String[parts.size()]);
85      }
86  
87      private String concatenateWithSpace(String[] args, int from) {
88          StringBuilder sb = new StringBuilder();
89  
90          for (int i = from; i < args.length; i++) {
91              sb.append(args[i] + " ");
92          }
93          return sb.toString().trim();
94      }
95  }