001: /******************************************************************************
002: * $Source: /cvsroot/sshwebproxy/src/java/com/ericdaugherty/sshwebproxy/FileChannel.java,v $
003: * $Revision: 1.2 $
004: * $Author: edaugherty $
005: * $Date: 2003/11/23 00:18:10 $
006: ******************************************************************************
007: * Copyright (c) 2003, Eric Daugherty (http://www.ericdaugherty.com)
008: * All rights reserved.
009: *
010: * Redistribution and use in source and binary forms, with or without
011: * modification, are permitted provided that the following conditions are met:
012: *
013: * * Redistributions of source code must retain the above copyright notice,
014: * this list of conditions and the following disclaimer.
015: * * Redistributions in binary form must reproduce the above copyright
016: * notice, this list of conditions and the following disclaimer in the
017: * documentation and/or other materials provided with the distribution.
018: * * Neither the name of the Eric Daugherty nor the names of its
019: * contributors may be used to endorse or promote products derived
020: * from this software without specific prior written permission.
021: *
022: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
023: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
024: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
025: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
026: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
027: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
028: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
029: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
030: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
031: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
032: * THE POSSIBILITY OF SUCH DAMAGE.
033: * *****************************************************************************
034: * For current versions and more information, please visit:
035: * http://www.ericdaugherty.com/dev/sshwebproxy
036: *
037: * or contact the author at:
038: * web@ericdaugherty.com
039: *****************************************************************************/package com.ericdaugherty.sshwebproxy;
040:
041: import org.apache.commons.logging.LogFactory;
042: import org.apache.commons.logging.Log;
043:
044: import java.io.*;
045: import java.util.List;
046:
047: import com.sshtools.j2ssh.SftpClient;
048: import com.sshtools.j2ssh.SshClient;
049:
050: /**
051: * Provide an implementation of the SshChannel for transfering
052: * files over an SshConnection.
053: *
054: * @author Eric Daugherty
055: */
056: public class FileChannel extends SshChannel implements SshConstants {
057:
058: //***************************************************************
059: // Variables
060: //***************************************************************
061:
062: /** The SftpClient */
063: private SftpClient sftpClient;
064:
065: /** Logger */
066: private static final Log log = LogFactory.getLog(FileChannel.class);
067:
068: //***************************************************************
069: // Constructor
070: //***************************************************************
071:
072: /**
073: * Opens a file transfer session with the server.
074: *
075: * @param sshConnection the connection to use.
076: * @param sshClient the SSH API client.
077: * @throws SshConnectException thrown if there is any error opening
078: * the connection.
079: */
080: public FileChannel(SshConnection sshConnection, SshClient sshClient)
081: throws SshConnectException {
082: super (CHANNEL_TYPE_FILE, sshConnection);
083:
084: try {
085: sftpClient = sshClient.openSftpClient();
086: } catch (IOException ioException) {
087: log
088: .warn(
089: "FileChannel constructor failed, IOException occured while setting up channel to : "
090: + sshConnection.getConnectionInfo()
091: + ". Exception: " + ioException,
092: ioException);
093: throw new SshConnectException(
094: "Unable to establish File Connection. IOExeption occured: "
095: + ioException);
096: }
097:
098: if (log.isInfoEnabled())
099: log.debug("New FileChannel opened to: "
100: + sshConnection.getConnectionInfo());
101: }
102:
103: //***************************************************************
104: // SshChannel Methods
105: //***************************************************************
106:
107: /**
108: * Closes the Reader and Writer after the Channel has been closed.
109: * This should only be called by the SshConnection
110: * class and never directly called from this class.
111: */
112: public void close() {
113: // Close Readers and Writers.
114: if (log.isInfoEnabled())
115: log.debug("Closing FileChannel connected to: "
116: + sshConnection.getConnectionInfo());
117:
118: try {
119: sftpClient.quit();
120: } catch (IOException ioException) {
121: log.warn("IOException while closing FileChannel: "
122: + ioException);
123: }
124: }
125:
126: /**
127: * Indicates whether this connection is still active.
128: *
129: * @return true if this connection is still active.
130: */
131: public boolean isConnected() {
132: return !sftpClient.isClosed();
133: }
134:
135: /**
136: * Returns the page that should be used to display this Channel.
137: *
138: * @return
139: */
140: public String getPage() {
141: return PAGE_FILE_HOME + "?connection="
142: + sshConnection.getConnectionInfo() + "&channel="
143: + getChannelId();
144: }
145:
146: //***************************************************************
147: // Public Methods
148: //***************************************************************
149:
150: /**
151: * Returns the present working directory for this channel.
152: *
153: * @return result of pwd command.
154: */
155: public String getCurrentDirectory() {
156: return sftpClient.pwd();
157: }
158:
159: /**
160: * Returns a listing of the current directory.
161: *
162: * @return a valid list, or null if an error occured.
163: */
164: public List getCurrentDirectoryListing() {
165: try {
166: return sftpClient.ls();
167: } catch (IOException ioException) {
168: log.info("Error occured while getting directory listing: "
169: + ioException);
170: return null;
171: }
172: }
173:
174: /**
175: * Change to a different directory.
176: *
177: * @param directory
178: * @return true if successful.
179: */
180: public boolean changeDirectory(String directory) {
181: try {
182: sftpClient.cd(directory);
183: return true;
184: } catch (IOException ioException) {
185: log.info("Error occured while changing directory listing: "
186: + ioException);
187: return false;
188: }
189: }
190:
191: /**
192: * Downloads a file from the remote server.
193: *
194: * @param fileName the name of the file to download.
195: * @param outputStream the stream to write the downloaded file to.
196: * @throws IOException thrown if there is an error reading the file.
197: */
198: public void downloadFile(String fileName, OutputStream outputStream)
199: throws IOException {
200: sftpClient.get(fileName, outputStream);
201: }
202:
203: /**
204: * Uploads a file to the remote server.
205: *
206: * @param fileName the file to upload.
207: * @param inputStream the file as an InputStream.
208: * @throws IOException thrown if there is an error writing the file.
209: */
210: public void uploadFile(String fileName, InputStream inputStream)
211: throws IOException {
212: sftpClient.put(inputStream, fileName);
213: }
214: }
|