001/*
002 * Copyright 2012-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 *      http://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.boot.autoconfigure.session;
018
019import org.springframework.beans.factory.ObjectProvider;
020import org.springframework.boot.autoconfigure.web.ServerProperties;
021import org.springframework.boot.context.properties.ConfigurationProperties;
022import org.springframework.session.data.redis.RedisFlushMode;
023import org.springframework.session.hazelcast.HazelcastFlushMode;
024
025/**
026 * Configuration properties for Spring Session.
027 *
028 * @author Tommy Ludwig
029 * @author Stephane Nicoll
030 * @author Vedran Pavic
031 * @since 1.4.0
032 */
033@ConfigurationProperties(prefix = "spring.session")
034public class SessionProperties {
035
036        /**
037         * Session store type.
038         */
039        private StoreType storeType;
040
041        private final Integer timeout;
042
043        private final Hazelcast hazelcast = new Hazelcast();
044
045        private final Jdbc jdbc = new Jdbc();
046
047        private final Mongo mongo = new Mongo();
048
049        private final Redis redis = new Redis();
050
051        public SessionProperties(ObjectProvider<ServerProperties> serverProperties) {
052                ServerProperties properties = serverProperties.getIfUnique();
053                this.timeout = (properties != null ? properties.getSession().getTimeout() : null);
054        }
055
056        public StoreType getStoreType() {
057                return this.storeType;
058        }
059
060        public void setStoreType(StoreType storeType) {
061                this.storeType = storeType;
062        }
063
064        /**
065         * Return the session timeout in seconds.
066         * @return the session timeout in seconds
067         * @see ServerProperties#getSession()
068         */
069        public Integer getTimeout() {
070                return this.timeout;
071        }
072
073        public Hazelcast getHazelcast() {
074                return this.hazelcast;
075        }
076
077        public Jdbc getJdbc() {
078                return this.jdbc;
079        }
080
081        public Mongo getMongo() {
082                return this.mongo;
083        }
084
085        public Redis getRedis() {
086                return this.redis;
087        }
088
089        public static class Hazelcast {
090
091                /**
092                 * Name of the map used to store sessions.
093                 */
094                private String mapName = "spring:session:sessions";
095
096                /**
097                 * Sessions flush mode.
098                 */
099                private HazelcastFlushMode flushMode = HazelcastFlushMode.ON_SAVE;
100
101                public String getMapName() {
102                        return this.mapName;
103                }
104
105                public void setMapName(String mapName) {
106                        this.mapName = mapName;
107                }
108
109                public HazelcastFlushMode getFlushMode() {
110                        return this.flushMode;
111                }
112
113                public void setFlushMode(HazelcastFlushMode flushMode) {
114                        this.flushMode = flushMode;
115                }
116
117        }
118
119        public static class Jdbc {
120
121                private static final String DEFAULT_SCHEMA_LOCATION = "classpath:org/springframework/"
122                                + "session/jdbc/schema-@@platform@@.sql";
123
124                private static final String DEFAULT_TABLE_NAME = "SPRING_SESSION";
125
126                /**
127                 * Path to the SQL file to use to initialize the database schema.
128                 */
129                private String schema = DEFAULT_SCHEMA_LOCATION;
130
131                /**
132                 * Name of database table used to store sessions.
133                 */
134                private String tableName = DEFAULT_TABLE_NAME;
135
136                private final Initializer initializer = new Initializer();
137
138                public String getSchema() {
139                        return this.schema;
140                }
141
142                public void setSchema(String schema) {
143                        this.schema = schema;
144                }
145
146                public String getTableName() {
147                        return this.tableName;
148                }
149
150                public void setTableName(String tableName) {
151                        this.tableName = tableName;
152                }
153
154                public Initializer getInitializer() {
155                        return this.initializer;
156                }
157
158                public class Initializer {
159
160                        /**
161                         * Create the required session tables on startup if necessary. Enabled
162                         * automatically if the default table name is set or a custom schema is
163                         * configured.
164                         */
165                        private Boolean enabled;
166
167                        public boolean isEnabled() {
168                                if (this.enabled != null) {
169                                        return this.enabled;
170                                }
171                                boolean defaultTableName = DEFAULT_TABLE_NAME
172                                                .equals(Jdbc.this.getTableName());
173                                boolean customSchema = !DEFAULT_SCHEMA_LOCATION
174                                                .equals(Jdbc.this.getSchema());
175                                return (defaultTableName || customSchema);
176                        }
177
178                        public void setEnabled(boolean enabled) {
179                                this.enabled = enabled;
180                        }
181
182                }
183
184        }
185
186        public static class Mongo {
187
188                /**
189                 * Collection name used to store sessions.
190                 */
191                private String collectionName = "sessions";
192
193                public String getCollectionName() {
194                        return this.collectionName;
195                }
196
197                public void setCollectionName(String collectionName) {
198                        this.collectionName = collectionName;
199                }
200
201        }
202
203        public static class Redis {
204
205                /**
206                 * Namespace for keys used to store sessions.
207                 */
208                private String namespace = "";
209
210                /**
211                 * Sessions flush mode.
212                 */
213                private RedisFlushMode flushMode = RedisFlushMode.ON_SAVE;
214
215                public String getNamespace() {
216                        return this.namespace;
217                }
218
219                public void setNamespace(String namespace) {
220                        this.namespace = namespace;
221                }
222
223                public RedisFlushMode getFlushMode() {
224                        return this.flushMode;
225                }
226
227                public void setFlushMode(RedisFlushMode flushMode) {
228                        this.flushMode = flushMode;
229                }
230
231        }
232
233}