001package org.apache.maven.wagon.providers.ssh; 002/* 003 * Licensed to the Apache Software Foundation (ASF) under one 004 * or more contributor license agreements. See the NOTICE file 005 * distributed with this work for additional information 006 * regarding copyright ownership. The ASF licenses this file 007 * to you under the Apache License, Version 2.0 (the 008 * "License"); you may not use this file except in compliance 009 * with the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, 014 * software distributed under the License is distributed on an 015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 016 * KIND, either express or implied. See the License for the 017 * specific language governing permissions and limitations 018 * under the License. 019 */ 020 021import org.apache.sshd.server.Command; 022import org.apache.sshd.server.CommandFactory; 023 024import java.util.ArrayList; 025import java.util.List; 026 027/** 028 * This <code>CommandFactory</code> can be used as a standalone command factory 029 * or can be used to augment another <code>CommandFactory</code> and provides 030 * <code>SCP</code> support. 031 * 032 * @see ScpCommand 033 * 034 * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a> 035 */ 036public class ScpCommandFactory 037 implements CommandFactory { 038 039 private CommandFactory delegate; 040 041 public ScpCommandFactory() { 042 } 043 044 public ScpCommandFactory( CommandFactory delegate ) { 045 this.delegate = delegate; 046 } 047 048 /** 049 * Parses a command string and verifies that the basic syntax is 050 * correct. If parsing fails the responsibility is delegated to 051 * the configured {@link org.apache.sshd.server.CommandFactory} instance; if one exist. 052 * 053 * @param command command to parse 054 * @return configured {@link org.apache.sshd.server.Command} instance 055 * @throws IllegalArgumentException 056 */ 057 public Command createCommand(String command) { 058 try { 059 return new ScpCommand(splitCommandString(command)); 060 } catch (IllegalArgumentException iae) { 061 if (delegate != null) { 062 return delegate.createCommand(command); 063 } 064 throw iae; 065 } 066 } 067 068 private String[] splitCommandString(String command) { 069 if (!command.trim().startsWith("scp")) { 070 throw new IllegalArgumentException("Unknown command, does not begin with 'scp'"); 071 } 072 073 String[] args = command.split(" "); 074 List<String> parts = new ArrayList<String>(); 075 parts.add(args[0]); 076 for (int i = 1; i < args.length; i++) { 077 if (!args[i].trim().startsWith("-")) { 078 parts.add(concatenateWithSpace(args, i)); 079 break; 080 } else { 081 parts.add(args[i]); 082 } 083 } 084 return parts.toArray(new String[parts.size()]); 085 } 086 087 private String concatenateWithSpace(String[] args, int from) { 088 StringBuilder sb = new StringBuilder(); 089 090 for (int i = from; i < args.length; i++) { 091 sb.append(args[i] + " "); 092 } 093 return sb.toString().trim(); 094 } 095}