001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing,
013     * software distributed under the License is distributed on an
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     * KIND, either express or implied.  See the License for the
016     * specific language governing permissions and limitations
017     * under the License.
018     */
019    package org.apache.shiro.web.subject.support;
020    
021    import org.apache.shiro.mgt.SecurityManager;
022    import org.apache.shiro.session.Session;
023    import org.apache.shiro.session.mgt.SessionContext;
024    import org.apache.shiro.subject.PrincipalCollection;
025    import org.apache.shiro.subject.support.DelegatingSubject;
026    import org.apache.shiro.util.StringUtils;
027    import org.apache.shiro.web.session.mgt.DefaultWebSessionContext;
028    import org.apache.shiro.web.session.mgt.WebSessionContext;
029    import org.apache.shiro.web.subject.WebSubject;
030    
031    import javax.servlet.ServletRequest;
032    import javax.servlet.ServletResponse;
033    
034    /**
035     * Default {@link WebSubject WebSubject} implementation that additional ensures the ability to retain a
036     * servlet request/response pair to be used by internal shiro components as necessary during the request execution.
037     *
038     * @since 1.0
039     */
040    public class WebDelegatingSubject extends DelegatingSubject implements WebSubject {
041    
042        private static final long serialVersionUID = -1655724323350159250L;
043    
044        private final ServletRequest servletRequest;
045        private final ServletResponse servletResponse;
046    
047        public WebDelegatingSubject(PrincipalCollection principals, boolean authenticated,
048                                    String host, Session session,
049                                    ServletRequest request, ServletResponse response,
050                                    SecurityManager securityManager) {
051            super(principals, authenticated, host, session, securityManager);
052            this.servletRequest = request;
053            this.servletResponse = response;
054        }
055    
056        public ServletRequest getServletRequest() {
057            return servletRequest;
058        }
059    
060        public ServletResponse getServletResponse() {
061            return servletResponse;
062        }
063    
064        @Override
065        protected SessionContext createSessionContext() {
066            WebSessionContext wsc = new DefaultWebSessionContext();
067            String host = getHost();
068            if (StringUtils.hasText(host)) {
069                wsc.setHost(host);
070            }
071            wsc.setServletRequest(this.servletRequest);
072            wsc.setServletResponse(this.servletResponse);
073            return wsc;
074        }
075    }