001: /******************************************************************************
002: * $Source: /cvsroot/sshwebproxy/src/java/com/ericdaugherty/sshwebproxy/SshFileServlet.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.Log;
042: import org.apache.commons.logging.LogFactory;
043: import org.apache.commons.fileupload.FileUpload;
044: import org.apache.commons.fileupload.DiskFileUpload;
045: import org.apache.commons.fileupload.FileItem;
046: import org.apache.commons.fileupload.FileUploadException;
047:
048: import javax.servlet.http.HttpServletRequest;
049: import javax.servlet.http.HttpServletResponse;
050: import javax.servlet.http.HttpServlet;
051: import javax.servlet.ServletException;
052: import javax.servlet.ServletOutputStream;
053: import java.io.IOException;
054: import java.util.List;
055: import java.util.Iterator;
056:
057: /**
058: * Handles interaction between the FileChannel and the UI.
059: *
060: * @author Eric Daugherty
061: */
062: public class SshFileServlet extends HttpServlet implements SshConstants {
063:
064: //***************************************************************
065: // Variables
066: //***************************************************************
067:
068: /** Logger */
069: private static final Log log = LogFactory
070: .getLog(SshFileServlet.class);
071:
072: //***************************************************************
073: // HTTPServlet Methods
074: //***************************************************************
075:
076: protected void doGet(HttpServletRequest request,
077: HttpServletResponse response) throws ServletException,
078: IOException {
079: doPost(request, response);
080: }
081:
082: /**
083: * Handles requests from the SHH client JSP page. All requests from
084: * that page should be via POST.
085: *
086: * @param request
087: * @param response
088: * @throws ServletException
089: * @throws IOException
090: */
091: protected void doPost(HttpServletRequest request,
092: HttpServletResponse response) throws ServletException,
093: IOException {
094:
095: // Validate login.
096: SshSession sshSession = new SshSession(request);
097: if (!sshSession.isValid()) {
098: response.sendRedirect(SshConstants.PAGE_LOGIN);
099: return;
100: }
101:
102: if (FileUpload.isMultipartContent(request)) {
103: upload(request, response);
104: } else {
105: String action = request.getParameter(PARAMETER_ACTION);
106:
107: // Verify we received an action to perform.
108: if (action == null || action.trim().length() == 0) {
109: log
110: .warn("POST Request received without an action parameter.");
111: response.sendRedirect(PAGE_HOME);
112: }
113:
114: action = action.trim();
115: if (ACTION_DOWNLOAD.equals(action)) {
116: download(request, response);
117: } else if (ACTION_CHANGE_DIRECTORY.equals(action)) {
118: changeDirectory(request, response);
119: } else {
120: log
121: .warn("POST Request received with an invalid action parameter: "
122: + action);
123: response.sendRedirect(PAGE_HOME);
124: }
125: }
126: }
127:
128: //***************************************************************
129: // Private Action Handlers
130: //***************************************************************
131:
132: /**
133: * Download a file to the client.
134: *
135: * @param request
136: * @param response
137: * @throws IOException
138: */
139: private void download(HttpServletRequest request,
140: HttpServletResponse response) throws IOException {
141: log.debug("Download request received.");
142:
143: SshSession session = new SshSession(request);
144: String connectionInfo = request
145: .getParameter(PARAMETER_CONNECTION);
146: String channelId = request.getParameter(PARAMETER_CHANNEL);
147: String filename = request.getParameter(PARAMETER_FILENAME);
148:
149: // Get the Channel
150: SshConnection sshConnection = session
151: .getSshConnection(connectionInfo);
152: FileChannel fileChannel = null;
153: if (sshConnection != null) {
154: fileChannel = sshConnection.getFileChannel(channelId);
155: if (fileChannel != null) {
156: // Setup the headers information
157: response.setContentType("application/x-download");
158: response.setHeader("Content-Disposition",
159: "attachment; filename=" + filename);
160:
161: // Start writing the output.
162: ServletOutputStream outputStream = response
163: .getOutputStream();
164: fileChannel.downloadFile(filename, outputStream);
165: }
166: }
167: }
168:
169: /**
170: * Upload a file from the client.
171: *
172: * @param request
173: * @param response
174: * @throws IOException
175: */
176: private void upload(HttpServletRequest request,
177: HttpServletResponse response) throws IOException {
178: log.debug("Upload request received.");
179:
180: DiskFileUpload upload = new DiskFileUpload();
181:
182: SshSession session = new SshSession(request);
183: String connectionInfo = "";
184: String channelId = "";
185: String fileName = "";
186: FileItem file = null;
187: String redirectPage = PAGE_HOME;
188:
189: try {
190: // Parse the parts into Parameters and the file.
191: List files = upload.parseRequest(request);
192: Iterator iter = files.iterator();
193: while (iter.hasNext()) {
194: FileItem fileItem = (FileItem) iter.next();
195: String fieldName;
196: if (fileItem.isFormField()) {
197: fieldName = fileItem.getFieldName();
198: if (PARAMETER_CONNECTION.equals(fieldName)) {
199: connectionInfo = fileItem.getString();
200: } else if (PARAMETER_CHANNEL.equals(fieldName)) {
201: channelId = fileItem.getString();
202:
203: } else if (PARAMETER_FILENAME.equals(fieldName)) {
204: fileName = fileItem.getString();
205: }
206: } else {
207: file = fileItem;
208: }
209: }
210:
211: // Get the Channel
212: SshConnection sshConnection = session
213: .getSshConnection(connectionInfo);
214: FileChannel fileChannel = null;
215: if (sshConnection != null) {
216: fileChannel = sshConnection.getFileChannel(channelId);
217: if (fileChannel != null) {
218: redirectPage = fileChannel.getPage();
219:
220: // Verify the input parameters.
221: if (fileName == null || fileName.length() == 0) {
222: session
223: .setErrorMessage("The Remote Filename must be specified.");
224: }
225: // Upload the file.
226: else {
227: fileChannel.uploadFile(fileName, file
228: .getInputStream());
229: redirectPage = fileChannel.getPage();
230: }
231: }
232: }
233: } catch (FileUploadException fileUploadException) {
234: session.setErrorMessage("Unable to upload file: "
235: + fileUploadException.getMessage());
236: log.warn("Error uploading file from client: "
237: + fileUploadException, fileUploadException);
238: }
239:
240: response.sendRedirect(redirectPage);
241: }
242:
243: /**
244: * Change the current directory.
245: *
246: * @param request
247: * @param response
248: * @throws IOException
249: */
250: private void changeDirectory(HttpServletRequest request,
251: HttpServletResponse response) throws IOException {
252: log.debug("Download request received.");
253: SshSession session = new SshSession(request);
254: String connectionInfo = request
255: .getParameter(PARAMETER_CONNECTION);
256: String channelId = request.getParameter(PARAMETER_CHANNEL);
257: String directory = request.getParameter(PARAMETER_DIRECTORY);
258: String redirectPage = PAGE_HOME;
259:
260: // Get the Channel
261: SshConnection sshConnection = session
262: .getSshConnection(connectionInfo);
263: FileChannel fileChannel = null;
264: if (sshConnection != null) {
265: fileChannel = sshConnection.getFileChannel(channelId);
266: if (fileChannel != null) {
267: if (!fileChannel.changeDirectory(directory)) {
268: session.setErrorMessage("Directory Change failed.");
269: }
270: redirectPage = fileChannel.getPage();
271: }
272: }
273:
274: // Redirect to the result page.
275: response.sendRedirect(redirectPage);
276: }
277: }
|