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

hazendaz / httpunit / 755

14 Feb 2026 07:14PM UTC coverage: 80.526%. Remained the same
755

push

github

hazendaz
[ci] Fix badge

3213 of 4105 branches covered (78.27%)

Branch coverage included in aggregate %.

8245 of 10124 relevant lines covered (81.44%)

0.81 hits per line

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

72.83
/src/main/java/com/meterware/servletunit/ServletUnitHttpSession.java
1
/*
2
 * SPDX-License-Identifier: MIT
3
 * See LICENSE file for details.
4
 *
5
 * Copyright 2000-2026 Russell Gold
6
 * Copyright 2021-2000 hazendaz
7
 */
8
package com.meterware.servletunit;
9

10
import jakarta.servlet.ServletContext;
11
import jakarta.servlet.http.HttpSession;
12

13
import java.net.URL;
14
import java.util.Date;
15
import java.util.Enumeration;
16
import java.util.Hashtable;
17

18
/**
19
 * The Class ServletUnitHttpSession.
20
 */
21
class ServletUnitHttpSession implements HttpSession {
22

23
    /** The Constant SESSION_COOKIE_NAME. */
24
    public static final String SESSION_COOKIE_NAME = "JSESSION";
25

26
    /** The servlet context. */
27
    private ServletContext _servletContext;
28

29
    /** The listener dispatcher. */
30
    private SessionListenerDispatcher _listenerDispatcher;
31

32
    /**
33
     * Instantiates a new servlet unit http session.
34
     *
35
     * @param servletContext
36
     *            the servlet context
37
     * @param listenerDispatcher
38
     *            the listener dispatcher
39
     */
40
    ServletUnitHttpSession(ServletContext servletContext, SessionListenerDispatcher listenerDispatcher) {
1✔
41
        _servletContext = servletContext;
1✔
42
        _listenerDispatcher = listenerDispatcher;
1✔
43
    }
1✔
44

45
    /**
46
     * Returns the maximum time interval, in seconds, that the servlet engine will keep this session open between client
47
     * requests. You can set the maximum time interval with the setMaxInactiveInterval method.
48
     **/
49
    @Override
50
    public int getMaxInactiveInterval() {
51
        if (_invalid) {
×
52
            throw new IllegalStateException();
×
53
        }
54
        return _maxInactiveInterval;
×
55
    }
56

57
    /**
58
     * Specifies the maximum length of time, in seconds, that the servlet engine keeps this session if no user requests
59
     * have been made of the session.
60
     **/
61
    @Override
62
    public void setMaxInactiveInterval(int interval) {
63
        if (_invalid) {
×
64
            throw new IllegalStateException();
×
65
        }
66
        _maxInactiveInterval = interval;
×
67
    }
×
68

69
    /**
70
     * Returns a string containing the unique identifier assigned to this session. The identifier is assigned by the
71
     * servlet engine and is implementation dependent.
72
     **/
73
    @Override
74
    public String getId() {
75
        if (_invalid) {
1!
76
            throw new IllegalStateException();
×
77
        }
78
        return _id;
1✔
79
    }
80

81
    /**
82
     * Returns the time when this session was created, measured in milliseconds since midnight January 1, 1970 GMT.
83
     *
84
     * @exception IllegalStateException
85
     *                if you attempt to get the session's creation time after the session has been invalidated
86
     **/
87
    @Override
88
    public long getCreationTime() {
89
        if (_invalid) {
×
90
            throw new IllegalStateException();
×
91
        }
92
        return _creationTime;
×
93
    }
94

95
    /**
96
     * Returns the last time the client sent a request associated with this session, as the number of milliseconds since
97
     * midnight January 1, 1970 GMT.
98
     **/
99
    @Override
100
    public long getLastAccessedTime() {
101
        if (_invalid) {
1!
102
            throw new IllegalStateException();
×
103
        }
104
        return _lastAccessedTime;
1✔
105
    }
106

107
    /**
108
     * Returns true if the Web server has created a session but the client has not yet joined. For example, if the
109
     * server used only cookie-based sessions, and the client had disabled the use of cookies, then a session would be
110
     * new.
111
     **/
112
    @Override
113
    public boolean isNew() {
114
        return _isNew;
1✔
115
    }
116

117
    /**
118
     * Invalidates this session and unbinds any objects bound to it.
119
     **/
120
    @Override
121
    public void invalidate() {
122
        _listenerDispatcher.sendSessionDestroyed(this);
1✔
123
        _invalid = true;
1✔
124
        _values.clear();
1✔
125
    }
1✔
126

127
    /**
128
     * Returns the object bound with the specified name in this session or null if no object of that name exists.
129
     **/
130
    @Override
131
    public Object getAttribute(String name) {
132
        if (_invalid) {
1✔
133
            throw new IllegalStateException();
1✔
134
        }
135
        return _values.get(name);
1✔
136
    }
137

138
    /**
139
     * Binds an object to this session, using the name specified. If an object of the same name is already bound to the
140
     * session, the object is replaced.
141
     **/
142
    @Override
143
    public void setAttribute(String name, Object value) {
144
        if (_invalid) {
1!
145
            throw new IllegalStateException();
×
146
        }
147

148
        if (value == null) {
1✔
149
            removeAttribute(name);
1✔
150
        } else if (!_values.containsKey(name)) {
1✔
151
            _values.put(name, value);
1✔
152
            _listenerDispatcher.sendAttributeAdded(this, name, value);
1✔
153
        } else {
154
            Object oldValue = _values.get(name);
1✔
155
            _values.put(name, value);
1✔
156
            _listenerDispatcher.sendAttributeReplaced(this, name, oldValue);
1✔
157
        }
158
    }
1✔
159

160
    /**
161
     * Removes the object bound with the specified name from this session. If the session does not have an object bound
162
     * with the specified name, this method does nothing.
163
     **/
164
    @Override
165
    public void removeAttribute(String name) {
166
        if (_invalid) {
1!
167
            throw new IllegalStateException();
×
168
        }
169
        if (_values.containsKey(name)) {
1!
170
            Object oldValue = _values.get(name);
1✔
171
            _values.remove(name);
1✔
172
            _listenerDispatcher.sendAttributeRemoved(this, name, oldValue);
1✔
173
        }
174
    }
1✔
175

176
    /**
177
     * Returns an array containing the names of all the objects bound to this session. This method is useful, for
178
     * example, when you want to delete all the objects bound to this session.
179
     **/
180
    @Override
181
    public Enumeration<String> getAttributeNames() {
182
        if (_invalid) {
1✔
183
            throw new IllegalStateException();
1✔
184
        }
185
        return _values.keys();
1✔
186
    }
187

188
    // ---------------------------- methods added to HttpSession in JSDK 2.3 ----------------------------------------
189

190
    /**
191
     * Returns the ServletContext to which this session belongs.
192
     **/
193
    @Override
194
    public ServletContext getServletContext() {
195
        return _servletContext;
1✔
196
    }
197

198
    // -------------------------------------------- package members -------------------------------------------------
199

200
    /**
201
     * This method should be invoked when a servlet joins an existing session. It will update the last access time and
202
     * mark the session as no longer new.
203
     **/
204
    void access() {
205
        _lastAccessedTime = new Date().getTime();
1✔
206
        _isNew = false;
1✔
207
    }
1✔
208

209
    /**
210
     * Gets the original URL.
211
     *
212
     * @return the original URL
213
     */
214
    URL getOriginalURL() {
215
        return _originalURL;
1✔
216
    }
217

218
    /**
219
     * Sets the original URL.
220
     *
221
     * @param originalURL
222
     *            the new original URL
223
     */
224
    void setOriginalURL(URL originalURL) {
225
        _originalURL = originalURL;
1✔
226
    }
1✔
227

228
    /**
229
     * Sets the authenticated user information for a session.
230
     *
231
     * @param userName
232
     *            the name the user supplied when logging in
233
     * @param roles
234
     *            an array of role names assigned to the user
235
     **/
236
    void setUserInformation(String userName, String[] roles) {
237
        _userName = userName;
1✔
238
        _roles = roles;
1✔
239
    }
1✔
240

241
    /**
242
     * Gets the user name.
243
     *
244
     * @return the user name
245
     */
246
    String getUserName() {
247
        return _userName;
1✔
248
    }
249

250
    /**
251
     * Gets the roles.
252
     *
253
     * @return the roles
254
     */
255
    String[] getRoles() {
256
        return _roles;
1✔
257
    }
258

259
    /**
260
     * Checks if is invalid.
261
     *
262
     * @return true, if is invalid
263
     */
264
    boolean isInvalid() {
265
        return _invalid;
1✔
266
    }
267

268
    // ------------------------------------- private members ---------------------------------------
269

270
    /** The Next ID. */
271
    private static int _NextID = 1;
1✔
272

273
    /** The creation time. */
274
    private final long _creationTime = new Date().getTime();
1✔
275

276
    /** The id. */
277
    private final String _id = Integer.toString(_NextID++);
1✔
278

279
    /** The max inactive interval. */
280
    private int _maxInactiveInterval;
281

282
    /** The last accessed time. */
283
    private long _lastAccessedTime = new Date().getTime();
1✔
284

285
    /** The invalid. */
286
    private boolean _invalid;
287

288
    /** The values. */
289
    private Hashtable _values = new Hashtable<>();
1✔
290

291
    /** The is new. */
292
    private boolean _isNew = true;
1✔
293

294
    /** The user name. */
295
    private String _userName;
296

297
    /** The roles. */
298
    private String[] _roles;
299

300
    /** The original URL. */
301
    private URL _originalURL;
302

303
}
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