001/* 002 * Copyright 2002-2012 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.util.Assert; 020import org.springframework.web.context.request.WebRequest; 021 022/** 023 * Default implementation of the {@link SessionAttributeStore} interface, 024 * storing the attributes in the WebRequest session (i.e. HttpSession 025 * or PortletSession). 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(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 public Object retrieveAttribute(WebRequest request, String attributeName) { 060 Assert.notNull(request, "WebRequest must not be null"); 061 Assert.notNull(attributeName, "Attribute name must not be null"); 062 String storeAttributeName = getAttributeNameInSession(request, attributeName); 063 return request.getAttribute(storeAttributeName, WebRequest.SCOPE_SESSION); 064 } 065 066 @Override 067 public void cleanupAttribute(WebRequest request, String attributeName) { 068 Assert.notNull(request, "WebRequest must not be null"); 069 Assert.notNull(attributeName, "Attribute name must not be null"); 070 String storeAttributeName = getAttributeNameInSession(request, attributeName); 071 request.removeAttribute(storeAttributeName, WebRequest.SCOPE_SESSION); 072 } 073 074 075 /** 076 * Calculate the attribute name in the backend session. 077 * <p>The default implementation simply prepends the configured 078 * {@link #setAttributeNamePrefix "attributeNamePrefix"}, if any. 079 * @param request the current request 080 * @param attributeName the name of the attribute 081 * @return the attribute name in the backend session 082 */ 083 protected String getAttributeNameInSession(WebRequest request, String attributeName) { 084 return this.attributeNamePrefix + attributeName; 085 } 086 087}