• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

hazendaz / httpunit / 656

06 Dec 2025 09:11PM UTC coverage: 80.452% (+0.02%) from 80.435%
656

push

github

hazendaz
[maven-release-plugin] prepare for next development iteration

3213 of 4105 branches covered (78.27%)

Branch coverage included in aggregate %.

8245 of 10137 relevant lines covered (81.34%)

0.81 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

65.05
/src/main/java/com/meterware/servletunit/ServletUnitHttpSession.java
1
/*
2
 * MIT License
3
 *
4
 * Copyright 2011-2025 Russell Gold
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
7
 * documentation files (the "Software"), to deal in the Software without restriction, including without limitation
8
 * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
9
 * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
10
 *
11
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions
12
 * of the Software.
13
 *
14
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
15
 * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
17
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
18
 * DEALINGS IN THE SOFTWARE.
19
 */
20
package com.meterware.servletunit;
21

22
import java.net.URL;
23
import java.util.Date;
24
import java.util.Enumeration;
25
import java.util.Hashtable;
26

27
import javax.servlet.ServletContext;
28
import javax.servlet.http.HttpSession;
29
import javax.servlet.http.HttpSessionContext;
30

31
/**
32
 * The Class ServletUnitHttpSession.
33
 */
34
class ServletUnitHttpSession implements HttpSession {
35

36
    /** The Constant SESSION_COOKIE_NAME. */
37
    public static final String SESSION_COOKIE_NAME = "JSESSION";
38

39
    /** The servlet context. */
40
    private ServletContext _servletContext;
41

42
    /** The listener dispatcher. */
43
    private SessionListenerDispatcher _listenerDispatcher;
44

45
    /**
46
     * Instantiates a new servlet unit http session.
47
     *
48
     * @param servletContext
49
     *            the servlet context
50
     * @param listenerDispatcher
51
     *            the listener dispatcher
52
     */
53
    ServletUnitHttpSession(ServletContext servletContext, SessionListenerDispatcher listenerDispatcher) {
1✔
54
        _servletContext = servletContext;
1✔
55
        _listenerDispatcher = listenerDispatcher;
1✔
56
    }
1✔
57

58
    /**
59
     * Returns the maximum time interval, in seconds, that the servlet engine will keep this session open between client
60
     * requests. You can set the maximum time interval with the setMaxInactiveInterval method.
61
     **/
62
    @Override
63
    public int getMaxInactiveInterval() {
64
        if (_invalid) {
×
65
            throw new IllegalStateException();
×
66
        }
67
        return _maxInactiveInterval;
×
68
    }
69

70
    /**
71
     * Specifies the maximum length of time, in seconds, that the servlet engine keeps this session if no user requests
72
     * have been made of the session.
73
     **/
74
    @Override
75
    public void setMaxInactiveInterval(int interval) {
76
        if (_invalid) {
×
77
            throw new IllegalStateException();
×
78
        }
79
        _maxInactiveInterval = interval;
×
80
    }
×
81

82
    /**
83
     * Returns a string containing the unique identifier assigned to this session. The identifier is assigned by the
84
     * servlet engine and is implementation dependent.
85
     **/
86
    @Override
87
    public String getId() {
88
        if (_invalid) {
1!
89
            throw new IllegalStateException();
×
90
        }
91
        return _id;
1✔
92
    }
93

94
    /**
95
     * Returns the time when this session was created, measured in milliseconds since midnight January 1, 1970 GMT.
96
     *
97
     * @exception IllegalStateException
98
     *                if you attempt to get the session's creation time after the session has been invalidated
99
     **/
100
    @Override
101
    public long getCreationTime() {
102
        if (_invalid) {
×
103
            throw new IllegalStateException();
×
104
        }
105
        return _creationTime;
×
106
    }
107

108
    /**
109
     * Returns the last time the client sent a request associated with this session, as the number of milliseconds since
110
     * midnight January 1, 1970 GMT.
111
     **/
112
    @Override
113
    public long getLastAccessedTime() {
114
        if (_invalid) {
1!
115
            throw new IllegalStateException();
×
116
        }
117
        return _lastAccessedTime;
1✔
118
    }
119

120
    /**
121
     * Returns true if the Web server has created a session but the client has not yet joined. For example, if the
122
     * server used only cookie-based sessions, and the client had disabled the use of cookies, then a session would be
123
     * new.
124
     **/
125
    @Override
126
    public boolean isNew() {
127
        return _isNew;
1✔
128
    }
129

130
    /**
131
     * Invalidates this session and unbinds any objects bound to it.
132
     **/
133
    @Override
134
    public void invalidate() {
135
        _listenerDispatcher.sendSessionDestroyed(this);
1✔
136
        _invalid = true;
1✔
137
        _values.clear();
1✔
138
    }
1✔
139

140
    /**
141
     * @deprecated no replacement.
142
     **/
143
    @Deprecated
144
    @Override
145
    public HttpSessionContext getSessionContext() {
146
        return null;
×
147
    }
148

149
    /**
150
     * @deprecated as of JSDK 2.2, use getAttribute
151
     **/
152
    @Deprecated
153
    @Override
154
    public Object getValue(String name) {
155
        return getAttribute(name);
×
156
    }
157

158
    /**
159
     * @deprecated as of JSDK 2.2, use setAttribute
160
     **/
161
    @Deprecated
162
    @Override
163
    public void putValue(String name, Object value) {
164
        setAttribute(name, value);
×
165
    }
×
166

167
    /**
168
     * @deprecated as of JSDK 2.2, use removeAttribute
169
     **/
170
    @Deprecated
171
    @Override
172
    public void removeValue(String name) {
173
        removeAttribute(name);
×
174
    }
×
175

176
    /**
177
     * @deprecated as of JSDK 2.2, use getAttributeNames.
178
     **/
179
    @Deprecated
180
    @Override
181
    public String[] getValueNames() {
182
        if (_invalid) {
×
183
            throw new IllegalStateException();
×
184
        }
185
        return (String[]) _values.keySet().toArray(new String[_values.size()]);
×
186
    }
187

188
    /**
189
     * Returns the object bound with the specified name in this session or null if no object of that name exists.
190
     **/
191
    @Override
192
    public Object getAttribute(String name) {
193
        if (_invalid) {
1✔
194
            throw new IllegalStateException();
1✔
195
        }
196
        return _values.get(name);
1✔
197
    }
198

199
    /**
200
     * Binds an object to this session, using the name specified. If an object of the same name is already bound to the
201
     * session, the object is replaced.
202
     **/
203
    @Override
204
    public void setAttribute(String name, Object value) {
205
        if (_invalid) {
1!
206
            throw new IllegalStateException();
×
207
        }
208

209
        if (value == null) {
1✔
210
            removeAttribute(name);
1✔
211
        } else if (!_values.containsKey(name)) {
1✔
212
            _values.put(name, value);
1✔
213
            _listenerDispatcher.sendAttributeAdded(this, name, value);
1✔
214
        } else {
215
            Object oldValue = _values.get(name);
1✔
216
            _values.put(name, value);
1✔
217
            _listenerDispatcher.sendAttributeReplaced(this, name, oldValue);
1✔
218
        }
219
    }
1✔
220

221
    /**
222
     * Removes the object bound with the specified name from this session. If the session does not have an object bound
223
     * with the specified name, this method does nothing.
224
     **/
225
    @Override
226
    public void removeAttribute(String name) {
227
        if (_invalid) {
1!
228
            throw new IllegalStateException();
×
229
        }
230
        if (_values.containsKey(name)) {
1!
231
            Object oldValue = _values.get(name);
1✔
232
            _values.remove(name);
1✔
233
            _listenerDispatcher.sendAttributeRemoved(this, name, oldValue);
1✔
234
        }
235
    }
1✔
236

237
    /**
238
     * Returns an array containing the names of all the objects bound to this session. This method is useful, for
239
     * example, when you want to delete all the objects bound to this session.
240
     **/
241
    @Override
242
    public Enumeration<String> getAttributeNames() {
243
        if (_invalid) {
1✔
244
            throw new IllegalStateException();
1✔
245
        }
246
        return _values.keys();
1✔
247
    }
248

249
    // ---------------------------- methods added to HttpSession in JSDK 2.3 ----------------------------------------
250

251
    /**
252
     * Returns the ServletContext to which this session belongs.
253
     **/
254
    @Override
255
    public ServletContext getServletContext() {
256
        return _servletContext;
1✔
257
    }
258

259
    // -------------------------------------------- package members -------------------------------------------------
260

261
    /**
262
     * This method should be invoked when a servlet joins an existing session. It will update the last access time and
263
     * mark the session as no longer new.
264
     **/
265
    void access() {
266
        _lastAccessedTime = new Date().getTime();
1✔
267
        _isNew = false;
1✔
268
    }
1✔
269

270
    /**
271
     * Gets the original URL.
272
     *
273
     * @return the original URL
274
     */
275
    URL getOriginalURL() {
276
        return _originalURL;
1✔
277
    }
278

279
    /**
280
     * Sets the original URL.
281
     *
282
     * @param originalURL
283
     *            the new original URL
284
     */
285
    void setOriginalURL(URL originalURL) {
286
        _originalURL = originalURL;
1✔
287
    }
1✔
288

289
    /**
290
     * Sets the authenticated user information for a session.
291
     *
292
     * @param userName
293
     *            the name the user supplied when logging in
294
     * @param roles
295
     *            an array of role names assigned to the user
296
     **/
297
    void setUserInformation(String userName, String[] roles) {
298
        _userName = userName;
1✔
299
        _roles = roles;
1✔
300
    }
1✔
301

302
    /**
303
     * Gets the user name.
304
     *
305
     * @return the user name
306
     */
307
    String getUserName() {
308
        return _userName;
1✔
309
    }
310

311
    /**
312
     * Gets the roles.
313
     *
314
     * @return the roles
315
     */
316
    String[] getRoles() {
317
        return _roles;
1✔
318
    }
319

320
    /**
321
     * Checks if is invalid.
322
     *
323
     * @return true, if is invalid
324
     */
325
    boolean isInvalid() {
326
        return _invalid;
1✔
327
    }
328

329
    // ------------------------------------- private members ---------------------------------------
330

331
    /** The Next ID. */
332
    private static int _NextID = 1;
1✔
333

334
    /** The creation time. */
335
    private final long _creationTime = new Date().getTime();
1✔
336

337
    /** The id. */
338
    private final String _id = Integer.toString(_NextID++);
1✔
339

340
    /** The max inactive interval. */
341
    private int _maxInactiveInterval;
342

343
    /** The last accessed time. */
344
    private long _lastAccessedTime = new Date().getTime();
1✔
345

346
    /** The invalid. */
347
    private boolean _invalid;
348

349
    /** The values. */
350
    private Hashtable _values = new Hashtable<>();
1✔
351

352
    /** The is new. */
353
    private boolean _isNew = true;
1✔
354

355
    /** The user name. */
356
    private String _userName;
357

358
    /** The roles. */
359
    private String[] _roles;
360

361
    /** The original URL. */
362
    private URL _originalURL;
363

364
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc