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 */
019package org.apache.shiro.web.subject.support;
020
021import org.apache.shiro.subject.Subject;
022import org.apache.shiro.subject.support.DefaultSubjectContext;
023import org.apache.shiro.web.subject.WebSubject;
024import org.apache.shiro.web.subject.WebSubjectContext;
025
026import javax.servlet.ServletRequest;
027import javax.servlet.ServletResponse;
028
029/**
030 * Default {@code WebSubjectContext} implementation that provides for additional storage and retrieval of
031 * a {@link ServletRequest} and {@link ServletResponse}.
032 *
033 * @since 1.0
034 */
035public class DefaultWebSubjectContext extends DefaultSubjectContext implements WebSubjectContext {
036
037    private static final long serialVersionUID = 8188555355305827739L;
038
039    private static final String SERVLET_REQUEST = DefaultWebSubjectContext.class.getName() + ".SERVLET_REQUEST";
040    private static final String SERVLET_RESPONSE = DefaultWebSubjectContext.class.getName() + ".SERVLET_RESPONSE";
041
042    public DefaultWebSubjectContext() {
043    }
044
045    public DefaultWebSubjectContext(WebSubjectContext context) {
046        super(context);
047    }
048
049    @Override
050    public String resolveHost() {
051        String host = super.resolveHost();
052        if (host == null) {
053            ServletRequest request = resolveServletRequest();
054            if (request != null) {
055                host = request.getRemoteHost();
056            }
057        }
058        return host;
059    }
060
061    public ServletRequest getServletRequest() {
062        return getTypedValue(SERVLET_REQUEST, ServletRequest.class);
063    }
064
065    public void setServletRequest(ServletRequest request) {
066        if (request != null) {
067            put(SERVLET_REQUEST, request);
068        }
069    }
070
071    public ServletRequest resolveServletRequest() {
072
073        ServletRequest request = getServletRequest();
074
075        //fall back on existing subject instance if it exists:
076        if (request == null) {
077            Subject existing = getSubject();
078            if (existing instanceof WebSubject) {
079                request = ((WebSubject) existing).getServletRequest();
080            }
081        }
082
083        return request;
084    }
085
086    public ServletResponse getServletResponse() {
087        return getTypedValue(SERVLET_RESPONSE, ServletResponse.class);
088    }
089
090    public void setServletResponse(ServletResponse response) {
091        if (response != null) {
092            put(SERVLET_RESPONSE, response);
093        }
094    }
095
096    public ServletResponse resolveServletResponse() {
097
098        ServletResponse response = getServletResponse();
099
100        //fall back on existing subject instance if it exists:
101        if (response == null) {
102            Subject existing = getSubject();
103            if (existing instanceof WebSubject) {
104                response = ((WebSubject) existing).getServletResponse();
105            }
106        }
107
108        return response;
109    }
110}