001/*
002 * Copyright 2002-2017 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.bind.support;
018
019import org.springframework.lang.Nullable;
020import org.springframework.util.Assert;
021import org.springframework.web.context.request.WebRequest;
022
023/**
024 * Default implementation of the {@link SessionAttributeStore} interface,
025 * storing the attributes in the WebRequest session (i.e. HttpSession).
026 *
027 * @author Juergen Hoeller
028 * @since 2.5
029 * @see #setAttributeNamePrefix
030 * @see org.springframework.web.context.request.WebRequest#setAttribute
031 * @see org.springframework.web.context.request.WebRequest#getAttribute
032 * @see org.springframework.web.context.request.WebRequest#removeAttribute
033 */
034public class DefaultSessionAttributeStore implements SessionAttributeStore {
035
036        private String attributeNamePrefix = "";
037
038
039        /**
040         * Specify a prefix to use for the attribute names in the backend session.
041         * <p>Default is to use no prefix, storing the session attributes with the
042         * same name as in the model.
043         */
044        public void setAttributeNamePrefix(@Nullable String attributeNamePrefix) {
045                this.attributeNamePrefix = (attributeNamePrefix != null ? attributeNamePrefix : "");
046        }
047
048
049        @Override
050        public void storeAttribute(WebRequest request, String attributeName, Object attributeValue) {
051                Assert.notNull(request, "WebRequest must not be null");
052                Assert.notNull(attributeName, "Attribute name must not be null");
053                Assert.notNull(attributeValue, "Attribute value must not be null");
054                String storeAttributeName = getAttributeNameInSession(request, attributeName);
055                request.setAttribute(storeAttributeName, attributeValue, WebRequest.SCOPE_SESSION);
056        }
057
058        @Override
059        @Nullable
060        public Object retrieveAttribute(WebRequest request, String attributeName) {
061                Assert.notNull(request, "WebRequest must not be null");
062                Assert.notNull(attributeName, "Attribute name must not be null");
063                String storeAttributeName = getAttributeNameInSession(request, attributeName);
064                return request.getAttribute(storeAttributeName, WebRequest.SCOPE_SESSION);
065        }
066
067        @Override
068        public void cleanupAttribute(WebRequest request, String attributeName) {
069                Assert.notNull(request, "WebRequest must not be null");
070                Assert.notNull(attributeName, "Attribute name must not be null");
071                String storeAttributeName = getAttributeNameInSession(request, attributeName);
072                request.removeAttribute(storeAttributeName, WebRequest.SCOPE_SESSION);
073        }
074
075
076        /**
077         * Calculate the attribute name in the backend session.
078         * <p>The default implementation simply prepends the configured
079         * {@link #setAttributeNamePrefix "attributeNamePrefix"}, if any.
080         * @param request the current request
081         * @param attributeName the name of the attribute
082         * @return the attribute name in the backend session
083         */
084        protected String getAttributeNameInSession(WebRequest request, String attributeName) {
085                return this.attributeNamePrefix + attributeName;
086        }
087
088}