Project

General

Profile

« Previous | Next » 

Revision 4780

Added by daigle about 15 years ago

Handle sessions with null ids gracefully.

View differences:

src/edu/ucsb/nceas/metacat/MetaCatServlet.java
1117 1117
        try {
1118 1118
            sess = new AuthSession();
1119 1119
        } catch (Exception e) {
1120
            System.out.println("error in MetacatServlet.handleLoginAction: "
1121
                    + e.getMessage());
1122
            out.println(e.getMessage());
1120
        	String errorMsg = "Problem in MetacatServlet.handleLoginAction() authenicating session: "
1121
                + e.getMessage();
1122
            logMetacat.error(errorMsg);
1123
            out.println(errorMsg);
1123 1124
            return;
1124 1125
        }
1125 1126
        boolean isValid = sess.authenticate(request, un, pw);
......
1128 1129
        if (isValid) {
1129 1130
            HttpSession session = sess.getSessions();
1130 1131
            String id = session.getId();
1131
            logMetacat.info("Store session id " + id
1132
            logMetacat.debug("Store session id " + id
1132 1133
                    + " which has username" + session.getAttribute("username")
1133 1134
                    + " into hash in login method");
1134
            SessionService.registerSession(id, 
1135
					(String) session.getAttribute("username"), 
1136
					(String[]) session.getAttribute("groupnames"),
1137
					(String) session.getAttribute("password"));
1135
            try {
1136
				SessionService.registerSession(id, (String) session
1137
						.getAttribute("username"), (String[]) session
1138
						.getAttribute("groupnames"), (String) session
1139
						.getAttribute("password"));
1140
			} catch (ServiceException se) {
1141
				String errorMsg = "Problem in MetacatServlet.handleLoginAction() registering session: "
1142
						+ se.getMessage();
1143
				logMetacat.error(errorMsg);
1144
				out.println(errorMsg);
1145
				return;
1146
			}
1138 1147
        }
1139 1148
        
1140 1149
        // format and transform the output
......
1148 1157
                trans.transformXMLDocument(sess.getMessage(),
1149 1158
                        "-//NCEAS//login//EN", "-//W3C//HTML//EN", qformat,
1150 1159
                        out, null);
1151
            } catch (Exception e) {
1152
                
1153
                logMetacat.error(
1154
                        "Error in MetaCatServlet.handleLoginAction: "
1160
            } catch (Exception e) {               
1161
                logMetacat.error("Error in MetaCatServlet.handleLoginAction: "
1155 1162
                        + e.getMessage());
1156 1163
            }
1157 1164
        }
src/edu/ucsb/nceas/metacat/service/SessionService.java
44 44
	/**
45 45
	 * private constructor since this is a singleton
46 46
	 */
47
	private SessionService() {
47
	private SessionService() throws ServiceException {
48 48
		sessionHash = new Hashtable<String, SessionData>();
49
		logMetacat.debug("Registering public session id: " + PUBLIC_SESSION_ID);
49 50
		registerSession(PUBLIC_SESSION_ID, "public", null, null);
50 51
	}
51 52
	
......
54 55
	 * 
55 56
	 * @return the single instance of SessionService
56 57
	 */
57
	public static SessionService getInstance() {
58
	public static SessionService getInstance() throws ServiceException {
58 59
		if (sessionService == null) {
59 60
			sessionService = new SessionService();
60 61
		}
......
83 84
	 *            the password for the session
84 85
	 */
85 86
	public static void registerSession(String sessionId, String userName,
86
			String[] groupNames, String password) {
87
			String[] groupNames, String password) throws ServiceException {
88
		if (sessionId == null) {
89
			throw new ServiceException("Cannot register a null session id");
90
		}
91
		logMetacat.debug("Registering session id: " + sessionId);
87 92
		SessionData sessionData = new SessionData(sessionId, userName, groupNames,
88 93
				password);
89 94
		sessionHash.put(sessionId, sessionData);
......
95 100
	 * @param sessionData
96 101
	 *            the session data object to add to the session hash
97 102
	 */
98
	public static void registerSession(SessionData sessionData) {
103
	public static void registerSession(SessionData sessionData) throws ServiceException {
104
		if (sessionData == null) {
105
			throw new ServiceException("Cannot register null session data");
106
		}
107
		logMetacat.debug("Registering session date with id: " + sessionData.getId());
99 108
		sessionHash.put(sessionData.getId(), sessionData);
100 109
	}
101 110
	
......
106 115
	 *            the id of the session to remove.
107 116
	 */
108 117
	public static void unRegisterSession(String sessionId) {
109
		sessionHash.remove(sessionId);
118
		if (sessionId != null) {
119
			logMetacat.error("trying to unregister a session with null id");
120
			sessionHash.remove(sessionId);
121
		}
110 122
	}
111 123
	
112 124
	/**
......
117 129
	 *            the id of the session to look for.
118 130
	 */
119 131
	public static boolean isSessionRegistered(String sessionId) {
132
		if (sessionId == null) {
133
			logMetacat.error("trying to check if a session with null id is registered");
134
			return false;
135
		}
120 136
		return sessionHash.containsKey(sessionId);
121 137
	}
122 138
	
......
128 144
	 *            the id of the session to retrieve.
129 145
	 */
130 146
	public static SessionData getRegisteredSession(String sessionId) {
147
		if (sessionId == null) {
148
			logMetacat.error("trying to get a session with null id");
149
			return null;
150
		}
131 151
		return sessionHash.get(sessionId);
132 152
	}
133 153
	
......
145 165
	 *            the id of the session to update.
146 166
	 */
147 167
	public static synchronized void touchSession(String sessionId) {
148
		if (sessionId != null && isSessionRegistered(sessionId)) {
168
		if (sessionId == null) {
169
			logMetacat.error("trying to touch a session with null id");
170
		} else if (isSessionRegistered(sessionId)) {
149 171
			SessionData sessionData = getRegisteredSession(sessionId);
150 172
			sessionData.setLastAccessedTime();
151 173
		}
src/edu/ucsb/nceas/metacat/util/AuthUtil.java
34 34

  
35 35
import edu.ucsb.nceas.metacat.AuthSession;
36 36
import edu.ucsb.nceas.metacat.service.PropertyService;
37
import edu.ucsb.nceas.metacat.service.ServiceException;
37 38
import edu.ucsb.nceas.metacat.service.SessionService;
38 39
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
39 40
import edu.ucsb.nceas.utilities.StringUtil;
......
194 195
		// global session list.
195 196
		HttpSession session = authSession.getSessions();
196 197
		String sessionId = session.getId();
198
		
199
		try {
197 200
		SessionService.registerSession(sessionId, 
198 201
				(String) session.getAttribute("username"), 
199 202
				(String[]) session.getAttribute("groupnames"),
200 203
				(String) session.getAttribute("password"));
204
		} catch (ServiceException se) {
205
			throw new UtilException("Problem registering session: " + se.getMessage());
206
		}
201 207
		
202 208
		return true;
203 209
	}
src/edu/ucsb/nceas/metacat/util/MetacatUtil.java
823 823
                logMetacat.error("Uploaded file '" + fileName + "'is empty!");
824 824
            }
825 825
        } catch (IOException e) {
826
            logMetacat.error("IO exception which writing temporary file: " +
826
            logMetacat.error("MetacatUtil.writeTempFile() - IO exception when writing temporary file: " +
827 827
                             tempFilePath + " " + e.getMessage());
828 828
        }
829 829

  
830
        logMetacat.info("Temporary file is: " + tempFilePath);
830
        logMetacat.debug("Temporary file is: " + tempFilePath);
831 831

  
832 832
        return tempFilePath;
833 833
    }

Also available in: Unified diff