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;
020import javax.servlet.http.HttpServletRequest;
021import javax.servlet.http.HttpServletResponse;
022import javax.servlet.http.HttpSession;
023
024import org.springframework.web.servlet.FlashMap;
025import org.springframework.web.util.WebUtils;
026
027/**
028 * Store and retrieve {@link FlashMap} instances to and from the HTTP session.
029 *
030 * @author Rossen Stoyanchev
031 * @author Juergen Hoeller
032 * @since 3.1.1
033 */
034public class SessionFlashMapManager extends AbstractFlashMapManager {
035
036        private static final String FLASH_MAPS_SESSION_ATTRIBUTE = SessionFlashMapManager.class.getName() + ".FLASH_MAPS";
037
038
039        /**
040         * Retrieves saved FlashMap instances from the HTTP session, if any.
041         */
042        @Override
043        @SuppressWarnings("unchecked")
044        protected List<FlashMap> retrieveFlashMaps(HttpServletRequest request) {
045                HttpSession session = request.getSession(false);
046                return (session != null ? (List<FlashMap>) session.getAttribute(FLASH_MAPS_SESSION_ATTRIBUTE) : null);
047        }
048
049        /**
050         * Saves the given FlashMap instances in the HTTP session.
051         */
052        @Override
053        protected void updateFlashMaps(List<FlashMap> flashMaps, HttpServletRequest request, HttpServletResponse response) {
054                WebUtils.setSessionAttribute(request, FLASH_MAPS_SESSION_ATTRIBUTE, (!flashMaps.isEmpty() ? flashMaps : null));
055        }
056
057        /**
058         * Exposes the best available session mutex.
059         * @see org.springframework.web.util.WebUtils#getSessionMutex
060         * @see org.springframework.web.util.HttpSessionMutexListener
061         */
062        @Override
063        protected Object getFlashMapsMutex(HttpServletRequest request) {
064                return WebUtils.getSessionMutex(request.getSession());
065        }
066
067}