Skip to content

Commit 313f67f

Browse files
committed
Proper global attribute names
1 parent 870ed97 commit 313f67f

File tree

5 files changed

+51
-10
lines changed

5 files changed

+51
-10
lines changed

src/main/java/com/github/mkopylec/sessioncouchbase/persistent/CouchbaseSession.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public class CouchbaseSession implements ExpiringSession, Serializable {
2828
public static final String CREATION_TIME_ATTRIBUTE = "$creationTime";
2929
public static final String LAST_ACCESSED_TIME_ATTRIBUTE = "$lastAccessedTime";
3030
public static final String MAX_INACTIVE_INTERVAL_ATTRIBUTE = "$maxInactiveInterval";
31-
protected static final String GLOBAL_ATTRIBUTE_NAME_PREFIX = CouchbaseSession.class.getName() + ".global";
31+
protected static final String GLOBAL_ATTRIBUTE_NAME_PREFIX = CouchbaseSession.class.getName() + ".global.";
3232

3333
private static final Logger log = getLogger(CouchbaseSession.class);
3434

@@ -111,7 +111,9 @@ public <T> T getAttribute(String attributeName) {
111111
@Override
112112
public Set<String> getAttributeNames() {
113113
Set<String> attributesNames = new HashSet<>();
114-
attributesNames.addAll(globalAttributes.keySet());
114+
for (String attributeName : globalAttributes.keySet()) {
115+
attributesNames.add(globalAttributeName(attributeName));
116+
}
115117
attributesNames.addAll(namespaceAttributes.keySet());
116118
return unmodifiableSet(attributesNames);
117119
}
@@ -180,7 +182,7 @@ protected void setCreationTime(long creationTime) {
180182

181183
protected void checkAttributeName(String attributeName) {
182184
hasText(attributeName, "Empty HTTP session attribute name");
183-
isTrue(!attributeName.equals(GLOBAL_ATTRIBUTE_NAME_PREFIX), "Empty HTTP session global attribute name");
185+
isTrue(!attributeName.trim().equals(GLOBAL_ATTRIBUTE_NAME_PREFIX), "Empty HTTP session global attribute name");
184186
}
185187

186188
protected boolean isGlobal(String attributeName) {

src/test/groovy/com/github/mkopylec/sessioncouchbase/BasicSpec.groovy

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,12 @@ abstract class BasicSpec extends Specification {
135135
return post('session/principal', null, extraInstancePort, String).body
136136
}
137137

138-
protected ResponseEntity<Set<String>> getPrincipalSessions() {
139-
return get('session/principal', Set, getPort())
138+
protected ResponseEntity<List<String>> getPrincipalSessions() {
139+
return get('session/principal', List, getPort())
140+
}
141+
142+
protected ResponseEntity<List<String>> getSessionAttributeNames() {
143+
return get('session/attribute/names', List, getPort())
140144
}
141145

142146
protected void clearSessionCookie() {

src/test/groovy/com/github/mkopylec/sessioncouchbase/assertions/ResponseAssert.groovy

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ class ResponseAssert {
1212
}
1313

1414
ResponseAssert hasSessionIds(String... sessionIds) {
15-
assert actual.body != null
16-
def actualSessionIds = actual.body as Set
17-
assert actualSessionIds.size() == sessionIds.size()
18-
assert actualSessionIds.containsAll(sessionIds)
19-
return this
15+
hasElements(sessionIds)
16+
}
17+
18+
ResponseAssert hasSessionAttributeNames(String... attributeNames) {
19+
return hasElements(attributeNames)
2020
}
2121

2222
ResponseAssert hasNoSessionIds() {
@@ -35,4 +35,12 @@ class ResponseAssert {
3535
assert actual.body == null
3636
return this
3737
}
38+
39+
private ResponseAssert hasElements(String... elements) {
40+
assert actual.body != null
41+
def actualSessionIds = actual.body as List
42+
assert actualSessionIds.size() == elements.size()
43+
assert actualSessionIds.containsAll(elements)
44+
return this
45+
}
3846
}

src/test/groovy/com/github/mkopylec/sessioncouchbase/specification/PersistentSessionSpec.groovy

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,24 @@ class PersistentSessionSpec extends SessionSpec {
3939
then:
4040
thrown HttpServerErrorException
4141
}
42+
43+
def "Should get global and application namespace HTTP session attribute names"() {
44+
given:
45+
def message = new Message(text: 'how do you do', number: 10)
46+
setGlobalSessionAttribute message
47+
setSessionAttribute message
48+
49+
when:
50+
def response = getSessionAttributeNames()
51+
52+
then:
53+
assertThat(response)
54+
.hasSessionAttributeNames(
55+
'attribute',
56+
'com.github.mkopylec.sessioncouchbase.persistent.CouchbaseSession.global.attribute',
57+
'com.github.mkopylec.sessioncouchbase.persistent.CouchbaseSession.global.$lastAccessedTime',
58+
'com.github.mkopylec.sessioncouchbase.persistent.CouchbaseSession.global.$creationTime',
59+
'com.github.mkopylec.sessioncouchbase.persistent.CouchbaseSession.global.$maxInactiveInterval'
60+
)
61+
}
4262
}

src/test/java/com/github/mkopylec/sessioncouchbase/SessionController.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@
99

1010
import javax.servlet.http.HttpServletRequest;
1111
import javax.servlet.http.HttpSession;
12+
import java.util.List;
1213
import java.util.Map;
1314
import java.util.Set;
1415

1516
import static com.github.mkopylec.sessioncouchbase.persistent.CouchbaseSession.globalAttributeName;
17+
import static java.util.Collections.list;
1618
import static org.springframework.session.FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME;
1719
import static org.springframework.web.bind.annotation.RequestMethod.DELETE;
1820
import static org.springframework.web.bind.annotation.RequestMethod.POST;
@@ -95,4 +97,9 @@ public Set<String> getPrincipalSessions() {
9597
Map<String, CouchbaseSession> sessionsById = sessionRepository.findByIndexNameAndIndexValue(PRINCIPAL_NAME_INDEX_NAME, PRINCIPAL_NAME);
9698
return sessionsById.keySet();
9799
}
100+
101+
@RequestMapping("attribute/names")
102+
public List<String> getAttributeNames(HttpSession session) {
103+
return list(session.getAttributeNames());
104+
}
98105
}

0 commit comments

Comments
 (0)