001/*
002 * Copyright 2002-2014 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.web.servlet.support;
018
019import java.util.List;
020
021import javax.servlet.http.HttpServletRequest;
022import javax.servlet.http.HttpServletResponse;
023import javax.servlet.http.HttpSession;
024
025import org.springframework.lang.Nullable;
026import org.springframework.web.servlet.FlashMap;
027import org.springframework.web.util.WebUtils;
028
029/**
030 * Store and retrieve {@link FlashMap} instances to and from the HTTP session.
031 *
032 * @author Rossen Stoyanchev
033 * @author Juergen Hoeller
034 * @since 3.1.1
035 */
036public class SessionFlashMapManager extends AbstractFlashMapManager {
037
038        private static final String FLASH_MAPS_SESSION_ATTRIBUTE = SessionFlashMapManager.class.getName() + ".FLASH_MAPS";
039
040
041        /**
042         * Retrieves saved FlashMap instances from the HTTP session, if any.
043         */
044        @Override
045        @SuppressWarnings("unchecked")
046        @Nullable
047        protected List<FlashMap> retrieveFlashMaps(HttpServletRequest request) {
048                HttpSession session = request.getSession(false);
049                return (session != null ? (List<FlashMap>) session.getAttribute(FLASH_MAPS_SESSION_ATTRIBUTE) : null);
050        }
051
052        /**
053         * Saves the given FlashMap instances in the HTTP session.
054         */
055        @Override
056        protected void updateFlashMaps(List<FlashMap> flashMaps, HttpServletRequest request, HttpServletResponse response) {
057                WebUtils.setSessionAttribute(request, FLASH_MAPS_SESSION_ATTRIBUTE, (!flashMaps.isEmpty() ? flashMaps : null));
058        }
059
060        /**
061         * Exposes the best available session mutex.
062         * @see org.springframework.web.util.WebUtils#getSessionMutex
063         * @see org.springframework.web.util.HttpSessionMutexListener
064         */
065        @Override
066        protected Object getFlashMapsMutex(HttpServletRequest request) {
067                return WebUtils.getSessionMutex(request.getSession());
068        }
069
070}