|
1 | 1 | /*
|
2 |
| - * Copyright 2012-2019 the original author or authors. |
| 2 | + * Copyright 2012-2022 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
16 | 16 |
|
17 | 17 | package org.springframework.boot.actuate.session;
|
18 | 18 |
|
19 |
| -import java.time.Instant; |
20 | 19 | import java.util.List;
|
21 | 20 | import java.util.Map;
|
22 |
| -import java.util.Set; |
23 | 21 | import java.util.stream.Collectors;
|
24 | 22 |
|
25 | 23 | import org.springframework.boot.actuate.endpoint.annotation.DeleteOperation;
|
|
28 | 26 | import org.springframework.boot.actuate.endpoint.annotation.Selector;
|
29 | 27 | import org.springframework.session.FindByIndexNameSessionRepository;
|
30 | 28 | import org.springframework.session.Session;
|
| 29 | +import org.springframework.session.SessionRepository; |
31 | 30 |
|
32 | 31 | /**
|
33 | 32 | * {@link Endpoint @Endpoint} to expose a user's {@link Session}s.
|
|
38 | 37 | @Endpoint(id = "sessions")
|
39 | 38 | public class SessionsEndpoint {
|
40 | 39 |
|
41 |
| - private final FindByIndexNameSessionRepository<? extends Session> sessionRepository; |
| 40 | + private final SessionRepository<? extends Session> sessionRepository; |
| 41 | + |
| 42 | + private final FindByIndexNameSessionRepository<? extends Session> indexedSessionRepository; |
42 | 43 |
|
43 | 44 | /**
|
44 | 45 | * Create a new {@link SessionsEndpoint} instance.
|
45 | 46 | * @param sessionRepository the session repository
|
| 47 | + * @param indexedSessionRepository the indexed session repository |
46 | 48 | */
|
47 |
| - public SessionsEndpoint(FindByIndexNameSessionRepository<? extends Session> sessionRepository) { |
| 49 | + public SessionsEndpoint(SessionRepository<? extends Session> sessionRepository, |
| 50 | + FindByIndexNameSessionRepository<? extends Session> indexedSessionRepository) { |
48 | 51 | this.sessionRepository = sessionRepository;
|
| 52 | + this.indexedSessionRepository = indexedSessionRepository; |
49 | 53 | }
|
50 | 54 |
|
51 | 55 | @ReadOperation
|
52 | 56 | public SessionsReport sessionsForUsername(String username) {
|
53 |
| - Map<String, ? extends Session> sessions = this.sessionRepository.findByPrincipalName(username); |
| 57 | + if (this.indexedSessionRepository == null) { |
| 58 | + return null; |
| 59 | + } |
| 60 | + Map<String, ? extends Session> sessions = this.indexedSessionRepository.findByPrincipalName(username); |
54 | 61 | return new SessionsReport(sessions);
|
55 | 62 | }
|
56 | 63 |
|
@@ -86,57 +93,4 @@ public List<SessionDescriptor> getSessions() {
|
86 | 93 |
|
87 | 94 | }
|
88 | 95 |
|
89 |
| - /** |
90 |
| - * A description of user's {@link Session session}. Primarily intended for |
91 |
| - * serialization to JSON. |
92 |
| - */ |
93 |
| - public static final class SessionDescriptor { |
94 |
| - |
95 |
| - private final String id; |
96 |
| - |
97 |
| - private final Set<String> attributeNames; |
98 |
| - |
99 |
| - private final Instant creationTime; |
100 |
| - |
101 |
| - private final Instant lastAccessedTime; |
102 |
| - |
103 |
| - private final long maxInactiveInterval; |
104 |
| - |
105 |
| - private final boolean expired; |
106 |
| - |
107 |
| - public SessionDescriptor(Session session) { |
108 |
| - this.id = session.getId(); |
109 |
| - this.attributeNames = session.getAttributeNames(); |
110 |
| - this.creationTime = session.getCreationTime(); |
111 |
| - this.lastAccessedTime = session.getLastAccessedTime(); |
112 |
| - this.maxInactiveInterval = session.getMaxInactiveInterval().getSeconds(); |
113 |
| - this.expired = session.isExpired(); |
114 |
| - } |
115 |
| - |
116 |
| - public String getId() { |
117 |
| - return this.id; |
118 |
| - } |
119 |
| - |
120 |
| - public Set<String> getAttributeNames() { |
121 |
| - return this.attributeNames; |
122 |
| - } |
123 |
| - |
124 |
| - public Instant getCreationTime() { |
125 |
| - return this.creationTime; |
126 |
| - } |
127 |
| - |
128 |
| - public Instant getLastAccessedTime() { |
129 |
| - return this.lastAccessedTime; |
130 |
| - } |
131 |
| - |
132 |
| - public long getMaxInactiveInterval() { |
133 |
| - return this.maxInactiveInterval; |
134 |
| - } |
135 |
| - |
136 |
| - public boolean isExpired() { |
137 |
| - return this.expired; |
138 |
| - } |
139 |
| - |
140 |
| - } |
141 |
| - |
142 | 96 | }
|
0 commit comments