Source Code Cross Referenced for SshFileServlet.java in  » Net » ssh-web-proxy » com » ericdaugherty » sshwebproxy » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Net » ssh web proxy » com.ericdaugherty.sshwebproxy 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.