001    /*
002     * Copyright 2008 Les Hazlewood
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 org.apache.shiro.web.session.mgt;
017    
018    import org.apache.shiro.session.mgt.DefaultSessionKey;
019    import org.apache.shiro.web.util.RequestPairSource;
020    
021    import javax.servlet.ServletRequest;
022    import javax.servlet.ServletResponse;
023    import java.io.Serializable;
024    
025    /**
026     * A {@link org.apache.shiro.session.mgt.SessionKey SessionKey} implementation that also retains the
027     * {@code ServletRequest} and {@code ServletResponse} associated with the web request that is performing the
028     * session lookup.
029     *
030     * @since 1.0
031     */
032    public class WebSessionKey extends DefaultSessionKey implements RequestPairSource {
033    
034        private final ServletRequest servletRequest;
035        private final ServletResponse servletResponse;
036    
037        public WebSessionKey(ServletRequest request, ServletResponse response) {
038            if (request == null) {
039                throw new NullPointerException("request argument cannot be null.");
040            }
041            if (response == null) {
042                throw new NullPointerException("response argument cannot be null.");
043            }
044            this.servletRequest = request;
045            this.servletResponse = response;
046        }
047    
048        public WebSessionKey(Serializable sessionId, ServletRequest request, ServletResponse response) {
049            this(request, response);
050            setSessionId(sessionId);
051        }
052    
053        public ServletRequest getServletRequest() {
054            return servletRequest;
055        }
056    
057        public ServletResponse getServletResponse() {
058            return servletResponse;
059        }
060    }