View Javadoc

1   /* Copyright 2009-2012 Capgemini 
2    * 
3    * Licensed under the Apache License, Version 2.0
4    * (the "License"); you may not use this file except in compliance with the
5    * License. You may obtain a copy of the License at
6    * 
7    * http://www.apache.org/licenses/LICENSE-2.0
8    * 
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12   * License for the specific language governing permissions and limitations under
13   * the License.
14   */
15  package net.sourceforge.statelessfilter.backend.session;
16  
17  import java.io.IOException;
18  import java.util.List;
19  import java.util.Map;
20  
21  import javax.servlet.http.HttpServletRequest;
22  import javax.servlet.http.HttpServletResponse;
23  import javax.servlet.http.HttpSession;
24  
25  import net.sourceforge.statelessfilter.backend.ISessionBackend;
26  import net.sourceforge.statelessfilter.backend.ISessionData;
27  import net.sourceforge.statelessfilter.backend.support.CookieDataSupport;
28  import net.sourceforge.statelessfilter.filter.Configuration;
29  
30  import org.slf4j.Logger;
31  import org.slf4j.LoggerFactory;
32  
33  /**
34   * Backend based on an unencrypted cookie. Can be used for non sensitive data.
35   * Cookie can be compressed to save bandwidth.
36   * 
37   * <p>
38   * Parameters :
39   * </p>
40   * <ul>
41   * <li>cookiename : name of the cookie</li>
42   * <li>compress : enable cookie compression (gzip) when value is "true"</li>
43   * </ul>
44   * 
45   * @author Nicolas Richeton - Capgemini
46   * 
47   */
48  public class SessionBackend implements ISessionBackend {
49  	private static final String ID = "session"; //$NON-NLS-1$
50  	private static final String SESSION_SESSION = "stateless.session";
51  	Logger logger = LoggerFactory.getLogger(SessionBackend.class);
52  
53  	public SessionBackend() {
54  		// empty constructor
55  	}
56  
57  	/**
58  	 * @see net.sourceforge.statelessfilter.backend.session.ISessionBackend#destroy()
59  	 */
60  
61  	public void destroy() {
62  		// Nothing to clean
63  	}
64  
65  	/**
66  	 * @see net.sourceforge.statelessfilter.backend.session.ISessionBackend#getId()
67  	 */
68  
69  	public String getId() {
70  		return ID;
71  	}
72  
73  	/**
74  	 * @see net.sourceforge.statelessfilter.backend.session.ISessionBackend#init(java.util.Map)
75  	 */
76  
77  	public void init(Map<String, String> config) throws Exception {
78  		// Nothing to init
79  	}
80  
81  	public String isBufferingRequired() {
82  		return Configuration.BUFFERING_HEADERS;
83  	}
84  
85  	/**
86  	 * @see net.sourceforge.statelessfilter.backend.session.ISessionBackend#restore(javax.servlet.http.HttpServletRequest)
87  	 */
88  
89  	public ISessionData restore(HttpServletRequest request) {
90  		HttpSession session = request.getSession(false);
91  		if (session != null) {
92  			CookieDataSupport s = (CookieDataSupport) session
93  					.getAttribute(SESSION_SESSION);
94  			if (s != null && s.isValid()) {
95  				return s;
96  			}
97  		}
98  
99  		return null;
100 	}
101 
102 	/**
103 	 * @see net.sourceforge.statelessfilter.backend.ISessionBackend#save(net.sourceforge.statelessfilter.backend.ISessionData,
104 	 *      java.util.List, javax.servlet.http.HttpServletRequest,
105 	 *      javax.servlet.http.HttpServletResponse)
106 	 */
107 	public void save(ISessionData session, List<String> dirtyAttributes,
108 			HttpServletRequest request, HttpServletResponse response)
109 			throws IOException {
110 		if (session != null) {
111 			HttpSession httpSession = request.getSession(true);
112 
113 			if (!session.isValid()) {
114 				httpSession.invalidate();
115 			} else {
116 				CookieDataSupport cookieData = new CookieDataSupport(session);
117 				httpSession.setAttribute(SESSION_SESSION, cookieData);
118 			}
119 		} else {
120 			// Session is null
121 			HttpSession httpSession = request.getSession(false);
122 			if (httpSession != null) {
123 				httpSession.setAttribute(SESSION_SESSION, null);
124 			}
125 		}
126 
127 	}
128 }