Java Code Examples for org.springframework.session.MapSession#setAttribute()
The following examples show how to use
org.springframework.session.MapSession#setAttribute() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RedissonSessionRepository.java From redisson with Apache License 2.0 | 6 votes |
private MapSession loadSession(String sessionId) { RMap<String, Object> map = redisson.getMap(keyPrefix + sessionId, new CompositeCodec(StringCodec.INSTANCE, redisson.getConfig().getCodec())); Set<Entry<String, Object>> entrySet = map.readAllEntrySet(); if (entrySet.isEmpty()) { return null; } MapSession delegate = new MapSession(sessionId); for (Entry<String, Object> entry : entrySet) { if ("session:creationTime".equals(entry.getKey())) { delegate.setCreationTime(Instant.ofEpochMilli((Long) entry.getValue())); } else if ("session:lastAccessedTime".equals(entry.getKey())) { delegate.setLastAccessedTime(Instant.ofEpochMilli((Long) entry.getValue())); } else if ("session:maxInactiveInterval".equals(entry.getKey())) { delegate.setMaxInactiveInterval(Duration.ofSeconds((Long) entry.getValue())); } else if (entry.getKey().startsWith(SESSION_ATTR_PREFIX)) { delegate.setAttribute(entry.getKey().substring(SESSION_ATTR_PREFIX.length()), entry.getValue()); } } return delegate; }
Example 2
Source File: SessionUpdateEntryProcessor.java From spring-session with Apache License 2.0 | 6 votes |
@Override public Object process(Map.Entry<String, MapSession> entry) { MapSession value = entry.getValue(); if (value == null) { return Boolean.FALSE; } if (this.lastAccessedTime != null) { value.setLastAccessedTime(this.lastAccessedTime); } if (this.maxInactiveInterval != null) { value.setMaxInactiveInterval(this.maxInactiveInterval); } if (this.delta != null) { for (final Map.Entry<String, Object> attribute : this.delta.entrySet()) { if (attribute.getValue() != null) { value.setAttribute(attribute.getKey(), attribute.getValue()); } else { value.removeAttribute(attribute.getKey()); } } } entry.setValue(value); return Boolean.TRUE; }
Example 3
Source File: ReactiveRedisSessionRepositoryTests.java From spring-session with Apache License 2.0 | 6 votes |
@Test void saveWithSaveModeOnGetAttribute() { given(this.redisOperations.hasKey(anyString())).willReturn(Mono.just(true)); given(this.redisOperations.opsForHash()).willReturn(this.hashOperations); given(this.hashOperations.putAll(anyString(), any())).willReturn(Mono.just(true)); given(this.redisOperations.expire(anyString(), any())).willReturn(Mono.just(true)); this.repository.setSaveMode(SaveMode.ON_GET_ATTRIBUTE); MapSession delegate = new MapSession(); delegate.setAttribute("attribute1", "value1"); delegate.setAttribute("attribute2", "value2"); delegate.setAttribute("attribute3", "value3"); RedisSession session = this.repository.new RedisSession(delegate, false); session.getAttribute("attribute2"); session.setAttribute("attribute3", "value4"); StepVerifier.create(this.repository.save(session)).verifyComplete(); verify(this.redisOperations).hasKey(anyString()); verify(this.redisOperations).opsForHash(); verify(this.hashOperations).putAll(anyString(), this.delta.capture()); assertThat(this.delta.getValue()).hasSize(2); verify(this.redisOperations).expire(anyString(), any()); verifyZeroInteractions(this.redisOperations); verifyZeroInteractions(this.hashOperations); }
Example 4
Source File: HazelcastIndexedSessionRepositoryTests.java From spring-session with Apache License 2.0 | 6 votes |
@Test @SuppressWarnings("unchecked") void saveWithSaveModeOnSetAttribute() { verify(this.sessions).addEntryListener(any(MapListener.class), anyBoolean()); this.repository.setSaveMode(SaveMode.ON_SET_ATTRIBUTE); MapSession delegate = new MapSession(); delegate.setAttribute("attribute1", "value1"); delegate.setAttribute("attribute2", "value2"); delegate.setAttribute("attribute3", "value3"); HazelcastSession session = this.repository.new HazelcastSession(delegate, false); session.getAttribute("attribute2"); session.setAttribute("attribute3", "value4"); this.repository.save(session); ArgumentCaptor<SessionUpdateEntryProcessor> captor = ArgumentCaptor.forClass(SessionUpdateEntryProcessor.class); verify(this.sessions).executeOnKey(eq(session.getId()), captor.capture()); assertThat((Map<String, Object>) ReflectionTestUtils.getField(captor.getValue(), "delta")).hasSize(1); verifyZeroInteractions(this.sessions); }
Example 5
Source File: HazelcastIndexedSessionRepositoryTests.java From spring-session with Apache License 2.0 | 6 votes |
@Test @SuppressWarnings("unchecked") void saveWithSaveModeOnGetAttribute() { verify(this.sessions).addEntryListener(any(MapListener.class), anyBoolean()); this.repository.setSaveMode(SaveMode.ON_GET_ATTRIBUTE); MapSession delegate = new MapSession(); delegate.setAttribute("attribute1", "value1"); delegate.setAttribute("attribute2", "value2"); delegate.setAttribute("attribute3", "value3"); HazelcastSession session = this.repository.new HazelcastSession(delegate, false); session.getAttribute("attribute2"); session.setAttribute("attribute3", "value4"); this.repository.save(session); ArgumentCaptor<SessionUpdateEntryProcessor> captor = ArgumentCaptor.forClass(SessionUpdateEntryProcessor.class); verify(this.sessions).executeOnKey(eq(session.getId()), captor.capture()); assertThat((Map<String, Object>) ReflectionTestUtils.getField(captor.getValue(), "delta")).hasSize(2); verifyZeroInteractions(this.sessions); }
Example 6
Source File: HazelcastIndexedSessionRepositoryTests.java From spring-session with Apache License 2.0 | 6 votes |
@Test @SuppressWarnings("unchecked") void saveWithSaveModeAlways() { verify(this.sessions).addEntryListener(any(MapListener.class), anyBoolean()); this.repository.setSaveMode(SaveMode.ALWAYS); MapSession delegate = new MapSession(); delegate.setAttribute("attribute1", "value1"); delegate.setAttribute("attribute2", "value2"); delegate.setAttribute("attribute3", "value3"); HazelcastSession session = this.repository.new HazelcastSession(delegate, false); session.getAttribute("attribute2"); session.setAttribute("attribute3", "value4"); this.repository.save(session); ArgumentCaptor<SessionUpdateEntryProcessor> captor = ArgumentCaptor.forClass(SessionUpdateEntryProcessor.class); verify(this.sessions).executeOnKey(eq(session.getId()), captor.capture()); assertThat((Map<String, Object>) ReflectionTestUtils.getField(captor.getValue(), "delta")).hasSize(3); verifyZeroInteractions(this.sessions); }
Example 7
Source File: ReactiveRedisSessionRepositoryTests.java From spring-session with Apache License 2.0 | 6 votes |
@Test void saveWithSaveModeOnSetAttribute() { given(this.redisOperations.hasKey(anyString())).willReturn(Mono.just(true)); given(this.redisOperations.opsForHash()).willReturn(this.hashOperations); given(this.hashOperations.putAll(anyString(), any())).willReturn(Mono.just(true)); given(this.redisOperations.expire(anyString(), any())).willReturn(Mono.just(true)); this.repository.setSaveMode(SaveMode.ON_SET_ATTRIBUTE); MapSession delegate = new MapSession(); delegate.setAttribute("attribute1", "value1"); delegate.setAttribute("attribute2", "value2"); delegate.setAttribute("attribute3", "value3"); RedisSession session = this.repository.new RedisSession(delegate, false); session.getAttribute("attribute2"); session.setAttribute("attribute3", "value4"); StepVerifier.create(this.repository.save(session)).verifyComplete(); verify(this.redisOperations).hasKey(anyString()); verify(this.redisOperations).opsForHash(); verify(this.hashOperations).putAll(anyString(), this.delta.capture()); assertThat(this.delta.getValue()).hasSize(1); verify(this.redisOperations).expire(anyString(), any()); verifyZeroInteractions(this.redisOperations); verifyZeroInteractions(this.hashOperations); }
Example 8
Source File: JdbcIndexedSessionRepositoryTests.java From spring-session with Apache License 2.0 | 6 votes |
@Test void saveWithSaveModeOnGetAttribute() { this.repository.setSaveMode(SaveMode.ON_GET_ATTRIBUTE); MapSession delegate = new MapSession(); delegate.setAttribute("attribute1", (Supplier<String>) () -> "value1"); delegate.setAttribute("attribute2", (Supplier<String>) () -> "value2"); delegate.setAttribute("attribute3", (Supplier<String>) () -> "value3"); JdbcSession session = this.repository.new JdbcSession(delegate, UUID.randomUUID().toString(), false); session.getAttribute("attribute2"); session.setAttribute("attribute3", "value4"); this.repository.save(session); ArgumentCaptor<BatchPreparedStatementSetter> captor = ArgumentCaptor .forClass(BatchPreparedStatementSetter.class); verify(this.jdbcOperations).batchUpdate(startsWith("UPDATE SPRING_SESSION_ATTRIBUTES SET"), captor.capture()); assertThat(captor.getValue().getBatchSize()).isEqualTo(2); verifyNoMoreInteractions(this.jdbcOperations); }
Example 9
Source File: JdbcIndexedSessionRepositoryTests.java From spring-session with Apache License 2.0 | 6 votes |
@Test void saveWithSaveModeAlways() { this.repository.setSaveMode(SaveMode.ALWAYS); MapSession delegate = new MapSession(); delegate.setAttribute("attribute1", (Supplier<String>) () -> "value1"); delegate.setAttribute("attribute2", (Supplier<String>) () -> "value2"); delegate.setAttribute("attribute3", (Supplier<String>) () -> "value3"); JdbcSession session = this.repository.new JdbcSession(delegate, UUID.randomUUID().toString(), false); session.getAttribute("attribute2"); session.setAttribute("attribute3", "value4"); this.repository.save(session); ArgumentCaptor<BatchPreparedStatementSetter> captor = ArgumentCaptor .forClass(BatchPreparedStatementSetter.class); verify(this.jdbcOperations).batchUpdate(startsWith("UPDATE SPRING_SESSION_ATTRIBUTES SET"), captor.capture()); assertThat(captor.getValue().getBatchSize()).isEqualTo(3); verifyNoMoreInteractions(this.jdbcOperations); }
Example 10
Source File: ReactiveRedisSessionRepositoryTests.java From spring-session with Apache License 2.0 | 6 votes |
@Test void saveWithSaveModeAlways() { given(this.redisOperations.hasKey(anyString())).willReturn(Mono.just(true)); given(this.redisOperations.opsForHash()).willReturn(this.hashOperations); given(this.hashOperations.putAll(anyString(), any())).willReturn(Mono.just(true)); given(this.redisOperations.expire(anyString(), any())).willReturn(Mono.just(true)); this.repository.setSaveMode(SaveMode.ALWAYS); MapSession delegate = new MapSession(); delegate.setAttribute("attribute1", "value1"); delegate.setAttribute("attribute2", "value2"); delegate.setAttribute("attribute3", "value3"); RedisSession session = this.repository.new RedisSession(delegate, false); session.getAttribute("attribute2"); session.setAttribute("attribute3", "value4"); StepVerifier.create(this.repository.save(session)).verifyComplete(); verify(this.redisOperations).hasKey(anyString()); verify(this.redisOperations).opsForHash(); verify(this.hashOperations).putAll(anyString(), this.delta.capture()); assertThat(this.delta.getValue()).hasSize(3); verify(this.redisOperations).expire(anyString(), any()); verifyZeroInteractions(this.redisOperations); verifyZeroInteractions(this.hashOperations); }
Example 11
Source File: RedisIndexedSessionRepository.java From spring-session with Apache License 2.0 | 6 votes |
private MapSession loadSession(String id, Map<Object, Object> entries) { MapSession loaded = new MapSession(id); for (Map.Entry<Object, Object> entry : entries.entrySet()) { String key = (String) entry.getKey(); if (RedisSessionMapper.CREATION_TIME_KEY.equals(key)) { loaded.setCreationTime(Instant.ofEpochMilli((long) entry.getValue())); } else if (RedisSessionMapper.MAX_INACTIVE_INTERVAL_KEY.equals(key)) { loaded.setMaxInactiveInterval(Duration.ofSeconds((int) entry.getValue())); } else if (RedisSessionMapper.LAST_ACCESSED_TIME_KEY.equals(key)) { loaded.setLastAccessedTime(Instant.ofEpochMilli((long) entry.getValue())); } else if (key.startsWith(RedisSessionMapper.ATTRIBUTE_PREFIX)) { loaded.setAttribute(key.substring(RedisSessionMapper.ATTRIBUTE_PREFIX.length()), entry.getValue()); } } return loaded; }
Example 12
Source File: RedisIndexedSessionRepositoryTests.java From spring-session with Apache License 2.0 | 6 votes |
@Test @SuppressWarnings("unchecked") void delete() { String attrName = "attrName"; MapSession expected = new MapSession(); expected.setLastAccessedTime(Instant.now().minusSeconds(60)); expected.setAttribute(attrName, "attrValue"); given(this.redisOperations.boundHashOps(anyString())).willReturn(this.boundHashOperations); given(this.redisOperations.boundSetOps(anyString())).willReturn(this.boundSetOperations); Map map = map(RedisIndexedSessionRepository.getSessionAttrNameKey(attrName), expected.getAttribute(attrName), RedisSessionMapper.CREATION_TIME_KEY, expected.getCreationTime().toEpochMilli(), RedisSessionMapper.MAX_INACTIVE_INTERVAL_KEY, (int) expected.getMaxInactiveInterval().getSeconds(), RedisSessionMapper.LAST_ACCESSED_TIME_KEY, expected.getLastAccessedTime().toEpochMilli()); given(this.boundHashOperations.entries()).willReturn(map); given(this.redisOperations.boundSetOps(anyString())).willReturn(this.boundSetOperations); String id = expected.getId(); this.redisRepository.deleteById(id); assertThat(getDelta().get(RedisSessionMapper.MAX_INACTIVE_INTERVAL_KEY)).isEqualTo(0); verify(this.redisOperations, atLeastOnce()).delete(getKey("expires:" + id)); verify(this.redisOperations, never()).boundValueOps(getKey("expires:" + id)); }
Example 13
Source File: RedisIndexedSessionRepositoryTests.java From spring-session with Apache License 2.0 | 6 votes |
@Test void saveWithSaveModeAlways() { given(this.redisOperations.boundHashOps(anyString())).willReturn(this.boundHashOperations); given(this.redisOperations.boundSetOps(anyString())).willReturn(this.boundSetOperations); given(this.redisOperations.boundValueOps(anyString())).willReturn(this.boundValueOperations); this.redisRepository.setSaveMode(SaveMode.ALWAYS); MapSession delegate = new MapSession(); delegate.setAttribute("attribute1", "value1"); delegate.setAttribute("attribute2", "value2"); delegate.setAttribute("attribute3", "value3"); RedisSession session = this.redisRepository.new RedisSession(delegate, false); session.getAttribute("attribute2"); session.setAttribute("attribute3", "value4"); this.redisRepository.save(session); assertThat(getDelta()).hasSize(3); }
Example 14
Source File: RedisIndexedSessionRepositoryTests.java From spring-session with Apache License 2.0 | 6 votes |
@Test void saveWithSaveModeOnSetAttribute() { given(this.redisOperations.boundHashOps(anyString())).willReturn(this.boundHashOperations); given(this.redisOperations.boundSetOps(anyString())).willReturn(this.boundSetOperations); given(this.redisOperations.boundValueOps(anyString())).willReturn(this.boundValueOperations); this.redisRepository.setSaveMode(SaveMode.ON_SET_ATTRIBUTE); MapSession delegate = new MapSession(); delegate.setAttribute("attribute1", "value1"); delegate.setAttribute("attribute2", "value2"); delegate.setAttribute("attribute3", "value3"); RedisSession session = this.redisRepository.new RedisSession(delegate, false); session.getAttribute("attribute2"); session.setAttribute("attribute3", "value4"); this.redisRepository.save(session); assertThat(getDelta()).hasSize(1); }
Example 15
Source File: RedisIndexedSessionRepositoryTests.java From spring-session with Apache License 2.0 | 6 votes |
@Test void saveWithSaveModeOnGetAttribute() { given(this.redisOperations.boundHashOps(anyString())).willReturn(this.boundHashOperations); given(this.redisOperations.boundSetOps(anyString())).willReturn(this.boundSetOperations); given(this.redisOperations.boundValueOps(anyString())).willReturn(this.boundValueOperations); this.redisRepository.setSaveMode(SaveMode.ON_GET_ATTRIBUTE); MapSession delegate = new MapSession(); delegate.setAttribute("attribute1", "value1"); delegate.setAttribute("attribute2", "value2"); delegate.setAttribute("attribute3", "value3"); RedisSession session = this.redisRepository.new RedisSession(delegate, false); session.getAttribute("attribute2"); session.setAttribute("attribute3", "value4"); this.redisRepository.save(session); assertThat(getDelta()).hasSize(2); }
Example 16
Source File: SpringSessionBackedSessionRegistryTest.java From spring-session with Apache License 2.0 | 5 votes |
private Session createSession(String sessionId, String userName, Instant lastAccessed) { MapSession session = new MapSession(sessionId); session.setLastAccessedTime(lastAccessed); Authentication authentication = mock(Authentication.class); when(authentication.getName()).thenReturn(userName); SecurityContextImpl securityContext = new SecurityContextImpl(); securityContext.setAuthentication(authentication); session.setAttribute("SPRING_SECURITY_CONTEXT", securityContext); return session; }
Example 17
Source File: RedisIndexedSessionRepositoryTests.java From spring-session with Apache License 2.0 | 5 votes |
@Test @SuppressWarnings("unchecked") void getSessionFound() { String attribute1 = "attribute1"; String attribute2 = "attribute2"; MapSession expected = new MapSession(); expected.setLastAccessedTime(Instant.now().minusSeconds(60)); expected.setAttribute(attribute1, "test"); expected.setAttribute(attribute2, null); given(this.redisOperations.boundHashOps(getKey(expected.getId()))).willReturn(this.boundHashOperations); Map map = map(RedisIndexedSessionRepository.getSessionAttrNameKey(attribute1), expected.getAttribute(attribute1), RedisIndexedSessionRepository.getSessionAttrNameKey(attribute2), expected.getAttribute(attribute2), RedisSessionMapper.CREATION_TIME_KEY, expected.getCreationTime().toEpochMilli(), RedisSessionMapper.MAX_INACTIVE_INTERVAL_KEY, (int) expected.getMaxInactiveInterval().getSeconds(), RedisSessionMapper.LAST_ACCESSED_TIME_KEY, expected.getLastAccessedTime().toEpochMilli()); given(this.boundHashOperations.entries()).willReturn(map); RedisSession session = this.redisRepository.findById(expected.getId()); assertThat(session.getId()).isEqualTo(expected.getId()); assertThat(session.getAttributeNames()).isEqualTo(expected.getAttributeNames()); assertThat(session.<String>getAttribute(attribute1)).isEqualTo(expected.getAttribute(attribute1)); assertThat(session.<String>getAttribute(attribute2)).isEqualTo(expected.getAttribute(attribute2)); assertThat(session.getCreationTime().truncatedTo(ChronoUnit.MILLIS)) .isEqualTo(expected.getCreationTime().truncatedTo(ChronoUnit.MILLIS)); assertThat(session.getMaxInactiveInterval()).isEqualTo(expected.getMaxInactiveInterval()); assertThat(session.getLastAccessedTime().truncatedTo(ChronoUnit.MILLIS)) .isEqualTo(expected.getLastAccessedTime().truncatedTo(ChronoUnit.MILLIS)); }
Example 18
Source File: JdbcIndexedSessionRepositoryTests.java From spring-session with Apache License 2.0 | 5 votes |
@Test void saveWithSaveModeOnSetAttribute() { this.repository.setSaveMode(SaveMode.ON_SET_ATTRIBUTE); MapSession delegate = new MapSession(); delegate.setAttribute("attribute1", (Supplier<String>) () -> "value1"); delegate.setAttribute("attribute2", (Supplier<String>) () -> "value2"); delegate.setAttribute("attribute3", (Supplier<String>) () -> "value3"); JdbcSession session = this.repository.new JdbcSession(delegate, UUID.randomUUID().toString(), false); session.getAttribute("attribute2"); session.setAttribute("attribute3", "value4"); this.repository.save(session); verify(this.jdbcOperations).update(startsWith("UPDATE SPRING_SESSION_ATTRIBUTES SET"), isA(PreparedStatementSetter.class)); verifyNoMoreInteractions(this.jdbcOperations); }
Example 19
Source File: HazelcastIndexedSessionRepositoryTests.java From spring-session with Apache License 2.0 | 5 votes |
@Test void getSessionFound() { verify(this.sessions, times(1)).addEntryListener(any(MapListener.class), anyBoolean()); MapSession saved = new MapSession(); saved.setAttribute("savedName", "savedValue"); given(this.sessions.get(eq(saved.getId()))).willReturn(saved); HazelcastSession session = this.repository.findById(saved.getId()); assertThat(session.getId()).isEqualTo(saved.getId()); assertThat(session.<String>getAttribute("savedName")).isEqualTo("savedValue"); verify(this.sessions, times(1)).get(eq(saved.getId())); verifyZeroInteractions(this.sessions); }
Example 20
Source File: RouteProviderTest.java From syncope with Apache License 2.0 | 4 votes |
@Test public void principalToRequestHeader() throws IllegalArgumentException, IllegalAccessException { // first mock... OidcIdToken oidcIdToken = mock(OidcIdToken.class); when(oidcIdToken.getTokenValue()).thenReturn("john.doe"); OidcUser user = mock(OidcUser.class); when(user.getIdToken()).thenReturn(oidcIdToken); Authentication authentication = mock(Authentication.class); when(authentication.getPrincipal()).thenReturn(user); MapSession session = new MapSession(); session.setAttribute( WebSessionServerSecurityContextRepository.DEFAULT_SPRING_SECURITY_CONTEXT_ATTR_NAME, new SecurityContextImpl(authentication)); Cache cache = mock(Cache.class); when(cache.get(anyString(), eq(Session.class))).thenReturn(session); CacheManager cacheManager = mock(CacheManager.class); when(cacheManager.getCache(eq(SessionConfig.DEFAULT_CACHE))).thenReturn(cache); PrincipalToRequestHeaderFilterFactory factory = new PrincipalToRequestHeaderFilterFactory(); ReflectionTestUtils.setField(factory, "cacheManager", cacheManager); ctx.getBeanFactory().registerSingleton(PrincipalToRequestHeaderFilterFactory.class.getName(), factory); // ...then test stubFor(get(urlEqualTo("/principalToRequestHeader")).willReturn(aResponse())); SRARouteTO route = new SRARouteTO(); route.setKey("principalToRequestHeader"); route.setTarget(URI.create("http://localhost:" + wiremockPort)); route.setType(SRARouteType.PROTECTED); route.getFilters().add(new SRARouteFilter.Builder(). factory(SRARouteFilterFactory.PRINCIPAL_TO_REQUEST_HEADER).args("HTTP_REMOTE_USER").build()); SyncopeCoreTestingServer.ROUTES.put(route.getKey(), route); routeRefresher.refresh(); webClient.get().uri("/principalToRequestHeader").exchange(). expectStatus().isOk(); verify(getRequestedFor(urlEqualTo("/principalToRequestHeader")). withHeader("HTTP_REMOTE_USER", equalTo("john.doe"))); }