001: /******************************************************************************
002: * $Source: /cvsroot/sshwebproxy/src/java/com/ericdaugherty/sshwebproxy/SshShellServlet.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:
044: import javax.servlet.http.HttpServletRequest;
045: import javax.servlet.http.HttpServletResponse;
046: import javax.servlet.http.HttpServlet;
047: import javax.servlet.ServletException;
048: import java.io.IOException;
049:
050: /**
051: * Handles interaction between the ShellChannel and the UI.
052: *
053: * @author Eric Daugherty
054: */
055: public class SshShellServlet extends HttpServlet implements
056: SshConstants {
057:
058: //***************************************************************
059: // Variables
060: //***************************************************************
061:
062: /** Logger */
063: private static final Log log = LogFactory
064: .getLog(SshShellServlet.class);
065:
066: //***************************************************************
067: // HTTPServlet Methods
068: //***************************************************************
069:
070: protected void doGet(HttpServletRequest request,
071: HttpServletResponse response) throws ServletException,
072: IOException {
073: log.warn("doGet called, but is not implemented.");
074: response.sendRedirect(PAGE_HOME);
075: }
076:
077: /**
078: * Handles requests from the SHH client JSP page. All requests from
079: * that page should be via POST.
080: *
081: * @param request
082: * @param response
083: * @throws ServletException
084: * @throws IOException
085: */
086: protected void doPost(HttpServletRequest request,
087: HttpServletResponse response) throws ServletException,
088: IOException {
089:
090: // Validate login.
091: SshSession sshSession = new SshSession(request);
092: if (!sshSession.isValid()) {
093: response.sendRedirect(SshConstants.PAGE_LOGIN);
094: return;
095: }
096:
097: String action = request.getParameter(PARAMETER_ACTION);
098:
099: // Verify we received an action to perform.
100: if (action == null || action.trim().length() == 0) {
101: log
102: .warn("POST Request received without an action parameter.");
103: response.sendRedirect(PAGE_HOME);
104: }
105:
106: action = action.trim();
107: if (ACTION_WRITE.equals(action)) {
108: if (request.getParameter("write") != null) {
109: write(request, response, false);
110: } else if (request.getParameter("writeline") != null) {
111: write(request, response, true);
112: } else {
113: log
114: .warn("Invalid write request recieved. Request does not contain write or writeline parameter.");
115: response.sendRedirect(PAGE_HOME);
116: }
117: } else {
118: log
119: .warn("POST Request received with an invalid action parameter: "
120: + action);
121: response.sendRedirect(PAGE_HOME);
122: }
123: }
124:
125: //***************************************************************
126: // Private Action Handlers
127: //***************************************************************
128:
129: /**
130: * Write data to a channel.
131: * @param request
132: * @param response
133: * @param sendNewLine
134: * @throws IOException
135: */
136: private void write(HttpServletRequest request,
137: HttpServletResponse response, boolean sendNewLine)
138: throws IOException {
139: log.debug("Write request received.");
140:
141: SshSession session = new SshSession(request);
142: String connectionInfo = request.getParameter("connection");
143: String channelId = request.getParameter("channel");
144: boolean valid = false;
145:
146: // Get the Channel and write to it.
147: SshConnection sshConnection = session
148: .getSshConnection(connectionInfo);
149: ShellChannel shellChannel = null;
150: if (sshConnection != null) {
151: shellChannel = sshConnection.getShellChannel(channelId);
152: if (shellChannel != null) {
153: String data = request.getParameter("data");
154: shellChannel.write(data, sendNewLine);
155: valid = true;
156: }
157: }
158:
159: // Redirect to the result page.
160: if (valid) {
161: response.sendRedirect(shellChannel.getPage());
162: if (log.isDebugEnabled())
163: log.debug("Successful Write to " + connectionInfo + " "
164: + channelId);
165: } else {
166: log.info("Write request to invalid channel.");
167: session.setErrorMessage("Invalid connection or channel.");
168: response.sendRedirect(PAGE_HOME);
169: }
170: }
171: }
|