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