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

hazendaz / httpunit / 313

26 Apr 2025 12:51AM UTC coverage: 80.503% (-0.03%) from 80.53%
313

push

github

hazendaz
Merge branch 'master' into javax

3231 of 4121 branches covered (78.4%)

Branch coverage included in aggregate %.

0 of 1 new or added line in 1 file covered. (0.0%)

3 existing lines in 2 files now uncovered.

8285 of 10184 relevant lines covered (81.35%)

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
class ServletUnitHttpSession implements HttpSession {
32

33
    public static final String SESSION_COOKIE_NAME = "JSESSION";
34

35
    private ServletContext _servletContext;
36
    private SessionListenerDispatcher _listenerDispatcher;
37

38
    ServletUnitHttpSession(ServletContext servletContext, SessionListenerDispatcher listenerDispatcher) {
1✔
39
        _servletContext = servletContext;
1✔
40
        _listenerDispatcher = listenerDispatcher;
1✔
41
    }
1✔
42

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

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

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

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

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

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

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

125
    /**
126
     * @deprecated no replacement.
127
     **/
128
    @Deprecated
129
    @Override
130
    public HttpSessionContext getSessionContext() {
131
        return null;
×
132
    }
133

134
    /**
135
     * @deprecated as of JSDK 2.2, use getAttribute
136
     **/
137
    @Deprecated
138
    @Override
139
    public Object getValue(String name) {
140
        return getAttribute(name);
×
141
    }
142

143
    /**
144
     * @deprecated as of JSDK 2.2, use setAttribute
145
     **/
146
    @Deprecated
147
    @Override
148
    public void putValue(String name, Object value) {
149
        setAttribute(name, value);
×
150
    }
×
151

152
    /**
153
     * @deprecated as of JSDK 2.2, use removeAttribute
154
     **/
155
    @Deprecated
156
    @Override
157
    public void removeValue(String name) {
158
        removeAttribute(name);
×
159
    }
×
160

161
    /**
162
     * @deprecated as of JSDK 2.2, use getAttributeNames.
163
     **/
164
    @Deprecated
165
    @Override
166
    public String[] getValueNames() {
UNCOV
167
        if (_invalid) {
×
168
            throw new IllegalStateException();
×
169
        }
UNCOV
170
        return (String[]) _values.keySet().toArray(new String[_values.size()]);
×
171
    }
172

173
    /**
174
     * Returns the object bound with the specified name in this session or null if no object of that name exists.
175
     **/
176
    @Override
177
    public Object getAttribute(String name) {
178
        if (_invalid) {
1✔
179
            throw new IllegalStateException();
1✔
180
        }
181
        return _values.get(name);
1✔
182
    }
183

184
    /**
185
     * Binds an object to this session, using the name specified. If an object of the same name is already bound to the
186
     * session, the object is replaced.
187
     **/
188
    @Override
189
    public void setAttribute(String name, Object value) {
190
        if (_invalid) {
1!
191
            throw new IllegalStateException();
×
192
        }
193

194
        if (value == null) {
1✔
195
            removeAttribute(name);
1✔
196
        } else if (!_values.containsKey(name)) {
1✔
197
            _values.put(name, value);
1✔
198
            _listenerDispatcher.sendAttributeAdded(this, name, value);
1✔
199
        } else {
200
            Object oldValue = _values.get(name);
1✔
201
            _values.put(name, value);
1✔
202
            _listenerDispatcher.sendAttributeReplaced(this, name, oldValue);
1✔
203
        }
204
    }
1✔
205

206
    /**
207
     * Removes the object bound with the specified name from this session. If the session does not have an object bound
208
     * with the specified name, this method does nothing.
209
     **/
210
    @Override
211
    public void removeAttribute(String name) {
212
        if (_invalid) {
1!
213
            throw new IllegalStateException();
×
214
        }
215
        if (_values.containsKey(name)) {
1!
216
            Object oldValue = _values.get(name);
1✔
217
            _values.remove(name);
1✔
218
            _listenerDispatcher.sendAttributeRemoved(this, name, oldValue);
1✔
219
        }
220
    }
1✔
221

222
    /**
223
     * Returns an array containing the names of all the objects bound to this session. This method is useful, for
224
     * example, when you want to delete all the objects bound to this session.
225
     **/
226
    @Override
227
    public Enumeration<String> getAttributeNames() {
228
        if (_invalid) {
1✔
229
            throw new IllegalStateException();
1✔
230
        }
231
        return _values.keys();
1✔
232
    }
233

234
    // ---------------------------- methods added to HttpSession in JSDK 2.3 ----------------------------------------
235

236
    /**
237
     * Returns the ServletContext to which this session belongs.
238
     *
239
     * @since 1.3
240
     **/
241
    @Override
242
    public ServletContext getServletContext() {
243
        return _servletContext;
1✔
244
    }
245

246
    // -------------------------------------------- package members -------------------------------------------------
247

248
    /**
249
     * This method should be invoked when a servlet joins an existing session. It will update the last access time and
250
     * mark the session as no longer new.
251
     **/
252
    void access() {
253
        _lastAccessedTime = new Date().getTime();
1✔
254
        _isNew = false;
1✔
255
    }
1✔
256

257
    URL getOriginalURL() {
258
        return _originalURL;
1✔
259
    }
260

261
    void setOriginalURL(URL originalURL) {
262
        _originalURL = originalURL;
1✔
263
    }
1✔
264

265
    /**
266
     * Sets the authenticated user information for a session.
267
     *
268
     * @param userName
269
     *            the name the user supplied when logging in
270
     * @param roles
271
     *            an array of role names assigned to the user
272
     **/
273
    void setUserInformation(String userName, String[] roles) {
274
        _userName = userName;
1✔
275
        _roles = roles;
1✔
276
    }
1✔
277

278
    String getUserName() {
279
        return _userName;
1✔
280
    }
281

282
    String[] getRoles() {
283
        return _roles;
1✔
284
    }
285

286
    boolean isInvalid() {
287
        return _invalid;
1✔
288
    }
289

290
    // ------------------------------------- private members ---------------------------------------
291

292
    private static int _NextID = 1;
1✔
293

294
    private final long _creationTime = new Date().getTime();
1✔
295

296
    private final String _id = Integer.toString(_NextID++);
1✔
297

298
    private int _maxInactiveInterval;
299

300
    private long _lastAccessedTime = new Date().getTime();
1✔
301

302
    private boolean _invalid;
303

304
    private Hashtable _values = new Hashtable();
1✔
305

306
    private boolean _isNew = true;
1✔
307

308
    private String _userName;
309

310
    private String[] _roles;
311

312
    private URL _originalURL;
313

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