AbstractMockServerSpec() { // Default instance to be re-used across requests, unless one is configured explicitly this.sessionManager = new DefaultWebSessionManager(); }
private Mono<Void> save(ServerWebExchange exchange, WebSession session) { List<String> ids = getSessionIdResolver().resolveSessionIds(exchange); if (!session.isStarted() || session.isExpired()) { if (!ids.isEmpty()) { // Expired on retrieve or while processing request, or invalidated.. this.sessionIdResolver.expireSession(exchange); } return Mono.empty(); } if (ids.isEmpty() || !session.getId().equals(ids.get(0))) { this.sessionIdResolver.setSessionId(exchange, session.getId()); } return session.save(); }
@Override public Mono<WebSession> getSession(ServerWebExchange exchange) { return Mono.defer(() -> retrieveSession(exchange) .switchIfEmpty(this.sessionStore.createWebSession()) .doOnNext(session -> exchange.getResponse().beforeCommit(() -> save(exchange, session)))); }
/** * Configure a {@link WebSessionManager} using a provided {@link ReactiveSessionRepository}. * * @param repository a bean that implements {@link ReactiveSessionRepository}. * @return a configured {@link WebSessionManager} registered with a preconfigured name. */ @Bean(WebHttpHandlerBuilder.WEB_SESSION_MANAGER_BEAN_NAME) public WebSessionManager webSessionManager(ReactiveSessionRepository<? extends Session> repository) { SpringSessionWebSessionStore<? extends Session> sessionStore = new SpringSessionWebSessionStore<>(repository); DefaultWebSessionManager manager = new DefaultWebSessionManager(); manager.setSessionStore(sessionStore); if (this.webSessionIdResolver != null) { manager.setSessionIdResolver(this.webSessionIdResolver); } return manager; } }
@Before public void setUp() throws Exception { when(this.createSession.save()).thenReturn(Mono.empty()); when(this.createSession.getId()).thenReturn("create-session-id"); when(this.updateSession.getId()).thenReturn("update-session-id"); when(this.sessionStore.createWebSession()).thenReturn(Mono.just(this.createSession)); when(this.sessionStore.retrieveSession(this.updateSession.getId())).thenReturn(Mono.just(this.updateSession)); this.sessionManager = new DefaultWebSessionManager(); this.sessionManager.setSessionIdResolver(this.sessionIdResolver); this.sessionManager.setSessionStore(this.sessionStore); MockServerHttpRequest request = MockServerHttpRequest.get("/path").build(); MockServerHttpResponse response = new MockServerHttpResponse(); this.exchange = new DefaultServerWebExchange(request, response, this.sessionManager, ServerCodecConfigurer.create(), new AcceptHeaderLocaleContextResolver()); }
/** * Build the {@code MockServerWebExchange} instance. */ public MockServerWebExchange build() { return new MockServerWebExchange(this.request, this.sessionManager != null ? this.sessionManager : new DefaultWebSessionManager()); } }
private Mono<WebSession> retrieveSession(ServerWebExchange exchange) { return Flux.fromIterable(getSessionIdResolver().resolveSessionIds(exchange)) .concatMap(this.sessionStore::retrieveSession) .next(); }
@Override public Mono<WebSession> getSession(ServerWebExchange exchange) { return Mono.defer(() -> retrieveSession(exchange) .switchIfEmpty(this.sessionStore.createWebSession()) .doOnNext(session -> exchange.getResponse().beforeCommit(() -> save(exchange, session)))); }
/** * Build the {@code MockServerWebExchange} instance. */ public MockServerWebExchange build() { return new MockServerWebExchange(this.request, this.sessionManager != null ? this.sessionManager : new DefaultWebSessionManager()); } }
private Mono<Void> save(ServerWebExchange exchange, WebSession session) { List<String> ids = getSessionIdResolver().resolveSessionIds(exchange); if (!session.isStarted() || session.isExpired()) { if (!ids.isEmpty()) { // Expired on retrieve or while processing request, or invalidated.. this.sessionIdResolver.expireSession(exchange); } return Mono.empty(); } if (ids.isEmpty() || !session.getId().equals(ids.get(0))) { this.sessionIdResolver.setSessionId(exchange, session.getId()); } return session.save(); }
@Override public Mono<WebSession> getSession(ServerWebExchange exchange) { return Mono.defer(() -> retrieveSession(exchange) .switchIfEmpty(this.sessionStore.createWebSession()) .doOnNext(session -> exchange.getResponse().beforeCommit(() -> save(exchange, session)))); }
private DefaultServerWebExchange createExchange(MockServerHttpRequest request) { return new DefaultServerWebExchange(request, new MockServerHttpResponse(), new DefaultWebSessionManager(), ServerCodecConfigurer.create(), new AcceptHeaderLocaleContextResolver()); }
private Mono<WebSession> retrieveSession(ServerWebExchange exchange) { return Flux.fromIterable(getSessionIdResolver().resolveSessionIds(exchange)) .concatMap(this.sessionStore::retrieveSession) .next(); }
@Override public Mono<WebSession> getSession(ServerWebExchange exchange) { return Mono.defer(() -> retrieveSession(exchange) .switchIfEmpty(this.sessionStore.createWebSession()) .doOnNext(session -> exchange.getResponse().beforeCommit(() -> save(exchange, session)))); }
@Override protected HttpHandler createHttpHandler() { this.sessionManager = new DefaultWebSessionManager(); this.handler = new TestWebHandler(); return WebHttpHandlerBuilder.webHandler(this.handler).sessionManager(this.sessionManager).build(); }
@Test public void defaultSessionIdResolverShouldBeCookieBased() { this.context = new AnnotationConfigApplicationContext(); this.context.register(GoodConfig.class); this.context.refresh(); DefaultWebSessionManager manager = this.context.getBean(DefaultWebSessionManager.class); assertThat(manager.getSessionIdResolver().getClass()).isAssignableFrom(CookieWebSessionIdResolver.class); }
AbstractMockServerSpec() { // Default instance to be re-used across requests, unless one is configured explicitly this.sessionManager = new DefaultWebSessionManager(); }
@Test public void providedSessionIdResolverShouldBePickedUpAutomatically() { this.context = new AnnotationConfigApplicationContext(); this.context.register(OverrideSessionIdResolver.class); this.context.refresh(); DefaultWebSessionManager manager = this.context.getBean(DefaultWebSessionManager.class); assertThat(manager.getSessionIdResolver().getClass()).isAssignableFrom(HeaderWebSessionIdResolver.class); }
AbstractMockServerSpec() { // Default instance to be re-used across requests, unless one is configured explicitly this.sessionManager = new DefaultWebSessionManager(); }
private Mono<Void> save(ServerWebExchange exchange, WebSession session) { List<String> ids = getSessionIdResolver().resolveSessionIds(exchange); if (!session.isStarted() || session.isExpired()) { if (!ids.isEmpty()) { // Expired on retrieve or while processing request, or invalidated.. this.sessionIdResolver.expireSession(exchange); } return Mono.empty(); } if (ids.isEmpty() || !session.getId().equals(ids.get(0))) { this.sessionIdResolver.setSessionId(exchange, session.getId()); } return session.save(); }