org.apache.sshd.client.scp.DefaultScpClient Maven / Gradle / Ivy
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.sshd.client.scp;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.FileSystem;
import java.nio.file.Path;
import java.nio.file.attribute.PosixFilePermission;
import java.util.Collection;
import java.util.Collections;
import java.util.EnumSet;
import java.util.Objects;
import org.apache.sshd.client.channel.ChannelExec;
import org.apache.sshd.client.session.ClientSession;
import org.apache.sshd.common.FactoryManager;
import org.apache.sshd.common.file.FileSystemFactory;
import org.apache.sshd.common.file.util.MockFileSystem;
import org.apache.sshd.common.file.util.MockPath;
import org.apache.sshd.common.scp.ScpFileOpener;
import org.apache.sshd.common.scp.ScpHelper;
import org.apache.sshd.common.scp.ScpTimestamp;
import org.apache.sshd.common.scp.ScpTransferEventListener;
import org.apache.sshd.common.scp.helpers.DefaultScpFileOpener;
import org.apache.sshd.common.util.ValidateUtils;
/**
* @author Apache MINA SSHD Project
*/
public class DefaultScpClient extends AbstractScpClient {
protected final ScpFileOpener opener;
protected final ScpTransferEventListener listener;
private final ClientSession clientSession;
public DefaultScpClient(
ClientSession clientSession, ScpFileOpener fileOpener, ScpTransferEventListener eventListener) {
this.clientSession = Objects.requireNonNull(clientSession, "No client session");
this.opener = (fileOpener == null) ? DefaultScpFileOpener.INSTANCE : fileOpener;
this.listener = (eventListener == null) ? ScpTransferEventListener.EMPTY : eventListener;
}
@Override
public ClientSession getClientSession() {
return clientSession;
}
@Override
public void download(String remote, OutputStream local) throws IOException {
String cmd = ScpClient.createReceiveCommand(remote, Collections.emptyList());
ClientSession session = getClientSession();
ChannelExec channel = openCommandChannel(session, cmd);
try (InputStream invOut = channel.getInvertedOut();
OutputStream invIn = channel.getInvertedIn()) {
// NOTE: we use a mock file system since we expect no invocations for it
ScpHelper helper = new ScpHelper(session, invOut, invIn, new MockFileSystem(remote), opener, listener);
helper.receiveFileStream(local, ScpHelper.DEFAULT_RECEIVE_BUFFER_SIZE);
handleCommandExitStatus(cmd, channel);
} finally {
channel.close(false);
}
}
@Override
protected void download(String remote, FileSystem fs, Path local, Collection
© 2015 - 2024 Weber Informatics LLC | Privacy Policy