001: /******************************************************************************
002: * $Source: /cvsroot/sshwebproxy/src/java/com/ericdaugherty/sshwebproxy/SshSession.java,v $
003: * $Revision: 1.3 $
004: * $Author: edaugherty $
005: * $Date: 2003/12/07 20:48:18 $
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.HttpSession;
045: import javax.servlet.http.HttpServletRequest;
046: import javax.servlet.ServletContext;
047: import java.util.Map;
048: import java.util.HashMap;
049: import java.util.Collection;
050:
051: /**
052: * Wrapper class for the HTTPSession class.
053: *
054: * @author Eric Daugherty
055: */
056: public class SshSession {
057:
058: //***************************************************************
059: // Constants
060: //***************************************************************
061:
062: private static final String USER = "User";
063:
064: private static final String RESTRICTED_MODE_HOST = "RestrictedModeHost";
065:
066: private static final String ERROR_MESSAGE = "ErrorMessage";
067:
068: private static final String SSH_CONNECTIONS = "SshConnections";
069:
070: //***************************************************************
071: // Variables
072: //***************************************************************
073:
074: /** The HttpSession this class is wrapping. */
075: private HttpSession session;
076:
077: /** Logger */
078: private static final Log log = LogFactory
079: .getLog(ShellChannel.class);
080:
081: //***************************************************************
082: // Constructor
083: //***************************************************************
084:
085: /**
086: * Helper constructor. Creates a new SSHWebProxy using
087: * the current HttpServletRequest.
088: *
089: * @param request the current HttpServletRequest
090: */
091: public SshSession(HttpServletRequest request) {
092: this (request.getSession());
093: }
094:
095: /**
096: * Creates a wrapper for the current HttpSession.
097: *
098: * @param session user's HttpSession
099: */
100: public SshSession(HttpSession session) {
101: this .session = session;
102: }
103:
104: //***************************************************************
105: // Parameter Access Methods
106: //***************************************************************
107:
108: /**
109: * Checks to makes sure that a user has logged into the system.
110: *
111: * @return true if a user is logged in.
112: */
113: public boolean isValid() {
114: // If we are in restricted mode, the session is valid.
115: if (isRestrictedMode()) {
116: return true;
117: }
118:
119: // Otherwise, check for a user.
120: String username = (String) session.getAttribute(USER);
121: if (log.isDebugEnabled())
122: log.debug("Verifying user is logged in. Username: "
123: + username);
124: if (username == null) {
125: return false;
126: }
127: return true;
128: }
129:
130: /**
131: * Returns the name of the current logged-in user,
132: * or null if no user is logged in.
133: *
134: * @return Username, or null.
135: */
136: public String getUser() {
137: return (String) session.getAttribute(USER);
138: }
139:
140: /**
141: * Sets a username into the session.
142: *
143: * @param user
144: */
145: public void setUser(String user) {
146: session.setAttribute(USER, user);
147: }
148:
149: public boolean isRestrictedMode() {
150: String restrictedModeHost = getRestrictedModeHost();
151: if (restrictedModeHost != null
152: && restrictedModeHost.length() > 0) {
153: return true;
154: }
155: return false;
156: }
157:
158: public String getRestrictedModeHost() {
159: return (String) session.getAttribute(RESTRICTED_MODE_HOST);
160: }
161:
162: public void setRestrictedModeHost(String host) {
163: session.setAttribute(RESTRICTED_MODE_HOST, host);
164: }
165:
166: public String getErrorMessage() {
167: return (String) session.getAttribute(ERROR_MESSAGE);
168: }
169:
170: public void setErrorMessage(String errorMessage) {
171: session.setAttribute(ERROR_MESSAGE, errorMessage);
172: }
173:
174: /**
175: * Returns the SshConnection for the given connectionInfo.
176: * Returns null if the connection does not exist or has been closed.
177: *
178: * @param connectionInfo the connectionInfo unique to this connection.
179: * @return the SshConnection or null if it does not exist or has been closed.
180: */
181: public synchronized SshConnection getSshConnection(
182: String connectionInfo) {
183: Map sshConnections = getConnectionMap();
184: SshConnection sshConnection = (SshConnection) sshConnections
185: .get(connectionInfo);
186:
187: // If it is unknown, or open, return it.
188: if (sshConnection == null || sshConnection.isOpen()) {
189: return sshConnection;
190: }
191: // If it has been closed, remove it and return null.
192: else {
193: if (log.isDebugEnabled())
194: log
195: .debug(connectionInfo
196: + " connection is closed, removing from session.");
197: sshConnections.remove(connectionInfo);
198: return null;
199: }
200: }
201:
202: /**
203: * Stores a new SshConnection in the session.
204: *
205: * @param sshConnection the connection to store.
206: * @return returns false if this SshConnection is a duplicate.
207: */
208: public synchronized boolean addSshConnection(
209: SshConnection sshConnection) {
210: String connectionInfo = sshConnection.getConnectionInfo();
211: Map sshConnections = getConnectionMap();
212: if (sshConnections.containsKey(connectionInfo)) {
213: log
214: .warn("Error Adding new SshConnection. A connection already exists with the same connection info: "
215: + connectionInfo);
216: return false;
217: }
218:
219: sshConnections.put(connectionInfo, sshConnection);
220: if (log.isDebugEnabled())
221: log.debug(connectionInfo + " connection added to session.");
222: return true;
223: }
224:
225: /**
226: * Removes an SshConnection from the session.
227: *
228: * @param connectionInfo
229: */
230: public synchronized void removeConnection(String connectionInfo) {
231: if (log.isDebugEnabled())
232: log.debug(connectionInfo
233: + " connection removed from session.");
234: Map sshConnections = getConnectionMap();
235: sshConnections.remove(connectionInfo);
236: }
237:
238: /**
239: * Returns an Collection for the current connections.
240: *
241: * @return null if no connections exist, or an Collection for the current
242: * connections.
243: */
244: public Collection getConnections() {
245: Map connections = getConnectionMap();
246: return (connections == null) ? null : connections.values();
247: }
248:
249: //***************************************************************
250: // Private Parameter Access Methods
251: //***************************************************************
252:
253: /**
254: * Removes the ConnectionMap from the Session (actually, the ServletContext).
255: */
256: void removeConnectionMap() {
257: ServletContext context = session.getServletContext();
258: context.removeAttribute(session.getId() + SSH_CONNECTIONS);
259: }
260:
261: /**
262: * Retrieve the ConnectionMap for this session. The map is actually stored
263: * in the ServletContext so it will be accessible by the SessionCleanup class
264: * after the session is destroyed.
265: *
266: * @return A Map of SshConnections, keyed by the connectionInfo String.
267: */
268: private Map getConnectionMap() {
269: ServletContext context = session.getServletContext();
270: Map sshConnections = (Map) context.getAttribute(session.getId()
271: + SSH_CONNECTIONS);
272:
273: if (sshConnections == null) {
274: sshConnections = new HashMap();
275: context.setAttribute(session.getId() + SSH_CONNECTIONS,
276: sshConnections);
277: }
278:
279: return sshConnections;
280: }
281: }
|