001    /*
002     * Copyright 2010 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    package com.hs.mail.web;
017    
018    import java.io.Serializable;
019    import java.util.Enumeration;
020    
021    import javax.servlet.http.HttpServletRequest;
022    import javax.servlet.http.HttpServletResponse;
023    import javax.servlet.http.HttpSession;
024    
025    import com.hs.mail.web.util.RequestUtils;
026    
027    /**
028     * 
029     * @author Won Chul Doh
030     * @since Sep 1, 2010
031     *
032     */
033    public class WebSession implements Serializable {
034    
035            private static final long serialVersionUID = 1L;
036    
037            public static final String LOGIN_CONTEXT = "lc";
038    
039            public static final String ACCOUNT_COUNT = "/account.count";
040            
041            public static final String ALIAS_COUNT = "/alias.count";
042            
043            // http session state related
044            transient private HttpServletRequest request;
045            
046            transient private HttpServletResponse response;
047            
048            transient private HttpSession websession;
049            
050            public WebSession(HttpServletRequest request, HttpServletResponse response) {
051                    this.request = request;
052                    this.response = response;
053                    this.websession = request.getSession(true);
054            }
055    
056            public HttpServletRequest getRequest() {
057                    return request;
058            }
059    
060            public HttpServletResponse getResponse() {
061                    return response;
062            }
063    
064            public String getReturnUrl() {
065                    return RequestUtils.getReturnUrl(request, response);
066            }
067            
068            public boolean isValid() {
069                    return retrieveBean(LOGIN_CONTEXT) != null;
070            }
071            
072            public void storeBean(String name, Object bean) {
073                    websession.setAttribute(name, bean);
074            }
075            
076            public Object retrieveBean(String name) {
077                    return websession.getAttribute(name);
078            }
079            
080            public void removeBean(String name) {
081                    websession.removeAttribute(name);
082            }
083            
084            public void removeBeans(String postfix) {
085                    Enumeration enums = websession.getAttributeNames();
086                    while (enums.hasMoreElements()) {
087                            String name = (String) enums.nextElement();
088                            if (name.endsWith(postfix)) {
089                                    websession.removeAttribute(name);
090                            }
091                    }
092            }
093    
094    }