|
| 1 | +/* |
| 2 | + * Copyright 2014-2019 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package docs; |
| 18 | + |
| 19 | +import java.time.Duration; |
| 20 | +import java.util.concurrent.ConcurrentHashMap; |
| 21 | + |
| 22 | +import com.hazelcast.config.Config; |
| 23 | +import com.hazelcast.core.Hazelcast; |
| 24 | +import com.hazelcast.core.HazelcastInstance; |
| 25 | +import org.junit.jupiter.api.Test; |
| 26 | + |
| 27 | +import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; |
| 28 | +import org.springframework.data.redis.core.ReactiveRedisTemplate; |
| 29 | +import org.springframework.data.redis.core.RedisTemplate; |
| 30 | +import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer; |
| 31 | +import org.springframework.data.redis.serializer.RedisSerializationContext; |
| 32 | +import org.springframework.jdbc.core.JdbcTemplate; |
| 33 | +import org.springframework.mock.web.MockServletContext; |
| 34 | +import org.springframework.session.MapSession; |
| 35 | +import org.springframework.session.MapSessionRepository; |
| 36 | +import org.springframework.session.ReactiveSessionRepository; |
| 37 | +import org.springframework.session.Session; |
| 38 | +import org.springframework.session.SessionRepository; |
| 39 | +import org.springframework.session.data.redis.ReactiveRedisSessionRepository; |
| 40 | +import org.springframework.session.data.redis.RedisIndexedSessionRepository; |
| 41 | +import org.springframework.session.hazelcast.HazelcastIndexedSessionRepository; |
| 42 | +import org.springframework.session.jdbc.JdbcIndexedSessionRepository; |
| 43 | +import org.springframework.session.web.http.SessionRepositoryFilter; |
| 44 | +import org.springframework.transaction.support.TransactionTemplate; |
| 45 | +import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; |
| 46 | + |
| 47 | +import static org.assertj.core.api.Assertions.assertThat; |
| 48 | + |
| 49 | +/** |
| 50 | + * @author Rob Winch |
| 51 | + * @author Vedran Pavic |
| 52 | + */ |
| 53 | +class IndexDocTests { |
| 54 | + |
| 55 | + private static final String ATTR_USER = "user"; |
| 56 | + |
| 57 | + @Test |
| 58 | + void repositoryDemo() { |
| 59 | + RepositoryDemo<MapSession> demo = new RepositoryDemo<>(); |
| 60 | + demo.repository = new MapSessionRepository(new ConcurrentHashMap<>()); |
| 61 | + |
| 62 | + demo.demo(); |
| 63 | + } |
| 64 | + |
| 65 | + // tag::repository-demo[] |
| 66 | + public class RepositoryDemo<S extends Session> { |
| 67 | + |
| 68 | + private SessionRepository<S> repository; // <1> |
| 69 | + |
| 70 | + public void demo() { |
| 71 | + S toSave = this.repository.createSession(); // <2> |
| 72 | + |
| 73 | + // <3> |
| 74 | + User rwinch = new User("rwinch"); |
| 75 | + toSave.setAttribute(ATTR_USER, rwinch); |
| 76 | + |
| 77 | + this.repository.save(toSave); // <4> |
| 78 | + |
| 79 | + S session = this.repository.findById(toSave.getId()); // <5> |
| 80 | + |
| 81 | + // <6> |
| 82 | + User user = session.getAttribute(ATTR_USER); |
| 83 | + assertThat(user).isEqualTo(rwinch); |
| 84 | + } |
| 85 | + |
| 86 | + // ... setter methods ... |
| 87 | + |
| 88 | + } |
| 89 | + // end::repository-demo[] |
| 90 | + |
| 91 | + @Test |
| 92 | + void expireRepositoryDemo() { |
| 93 | + ExpiringRepositoryDemo<MapSession> demo = new ExpiringRepositoryDemo<>(); |
| 94 | + demo.repository = new MapSessionRepository(new ConcurrentHashMap<>()); |
| 95 | + |
| 96 | + demo.demo(); |
| 97 | + } |
| 98 | + |
| 99 | + // tag::expire-repository-demo[] |
| 100 | + public class ExpiringRepositoryDemo<S extends Session> { |
| 101 | + |
| 102 | + private SessionRepository<S> repository; // <1> |
| 103 | + |
| 104 | + public void demo() { |
| 105 | + S toSave = this.repository.createSession(); // <2> |
| 106 | + // ... |
| 107 | + toSave.setMaxInactiveInterval(Duration.ofSeconds(30)); // <3> |
| 108 | + |
| 109 | + this.repository.save(toSave); // <4> |
| 110 | + |
| 111 | + S session = this.repository.findById(toSave.getId()); // <5> |
| 112 | + // ... |
| 113 | + } |
| 114 | + |
| 115 | + // ... setter methods ... |
| 116 | + |
| 117 | + } |
| 118 | + // end::expire-repository-demo[] |
| 119 | + |
| 120 | + @Test |
| 121 | + @SuppressWarnings("unused") |
| 122 | + void newRedisIndexedSessionRepository() { |
| 123 | + // tag::new-redisindexedsessionrepository[] |
| 124 | + RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>(); |
| 125 | + |
| 126 | + // ... configure redisTemplate ... |
| 127 | + |
| 128 | + SessionRepository<? extends Session> repository = new RedisIndexedSessionRepository(redisTemplate); |
| 129 | + // end::new-redisindexedsessionrepository[] |
| 130 | + } |
| 131 | + |
| 132 | + @Test |
| 133 | + @SuppressWarnings("unused") |
| 134 | + void newReactiveRedisSessionRepository() { |
| 135 | + LettuceConnectionFactory connectionFactory = new LettuceConnectionFactory(); |
| 136 | + RedisSerializationContext<String, Object> serializationContext = RedisSerializationContext |
| 137 | + .<String, Object>newSerializationContext(new JdkSerializationRedisSerializer()).build(); |
| 138 | + |
| 139 | + // tag::new-reactiveredissessionrepository[] |
| 140 | + // ... create and configure connectionFactory and serializationContext ... |
| 141 | + |
| 142 | + ReactiveRedisTemplate<String, Object> redisTemplate = new ReactiveRedisTemplate<>(connectionFactory, |
| 143 | + serializationContext); |
| 144 | + |
| 145 | + ReactiveSessionRepository<? extends Session> repository = new ReactiveRedisSessionRepository(redisTemplate); |
| 146 | + // end::new-reactiveredissessionrepository[] |
| 147 | + } |
| 148 | + |
| 149 | + @Test |
| 150 | + @SuppressWarnings("unused") |
| 151 | + void mapRepository() { |
| 152 | + // tag::new-mapsessionrepository[] |
| 153 | + SessionRepository<? extends Session> repository = new MapSessionRepository(new ConcurrentHashMap<>()); |
| 154 | + // end::new-mapsessionrepository[] |
| 155 | + } |
| 156 | + |
| 157 | + @Test |
| 158 | + @SuppressWarnings("unused") |
| 159 | + void newJdbcIndexedSessionRepository() { |
| 160 | + // tag::new-jdbcindexedsessionrepository[] |
| 161 | + JdbcTemplate jdbcTemplate = new JdbcTemplate(); |
| 162 | + |
| 163 | + // ... configure jdbcTemplate ... |
| 164 | + |
| 165 | + TransactionTemplate transactionTemplate = new TransactionTemplate(); |
| 166 | + |
| 167 | + // ... configure transactionTemplate ... |
| 168 | + |
| 169 | + SessionRepository<? extends Session> repository = new JdbcIndexedSessionRepository(jdbcTemplate, |
| 170 | + transactionTemplate); |
| 171 | + // end::new-jdbcindexedsessionrepository[] |
| 172 | + } |
| 173 | + |
| 174 | + @Test |
| 175 | + @SuppressWarnings("unused") |
| 176 | + void newHazelcastIndexedSessionRepository() { |
| 177 | + // tag::new-hazelcastindexedsessionrepository[] |
| 178 | + |
| 179 | + Config config = new Config(); |
| 180 | + |
| 181 | + // ... configure Hazelcast ... |
| 182 | + |
| 183 | + HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance(config); |
| 184 | + |
| 185 | + HazelcastIndexedSessionRepository repository = new HazelcastIndexedSessionRepository(hazelcastInstance); |
| 186 | + // end::new-hazelcastindexedsessionrepository[] |
| 187 | + } |
| 188 | + |
| 189 | + @Test |
| 190 | + void runSpringHttpSessionConfig() { |
| 191 | + AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); |
| 192 | + context.register(SpringHttpSessionConfig.class); |
| 193 | + context.setServletContext(new MockServletContext()); |
| 194 | + context.refresh(); |
| 195 | + |
| 196 | + try { |
| 197 | + context.getBean(SessionRepositoryFilter.class); |
| 198 | + } |
| 199 | + finally { |
| 200 | + context.close(); |
| 201 | + } |
| 202 | + } |
| 203 | + |
| 204 | + private static final class User { |
| 205 | + |
| 206 | + private User(String username) { |
| 207 | + } |
| 208 | + |
| 209 | + } |
| 210 | + |
| 211 | +} |
0 commit comments