View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.shiro.web.subject.support;
20  
21  import org.apache.shiro.subject.Subject;
22  import org.apache.shiro.subject.support.DefaultSubjectContext;
23  import org.apache.shiro.web.subject.WebSubject;
24  import org.apache.shiro.web.subject.WebSubjectContext;
25  
26  import javax.servlet.ServletRequest;
27  import javax.servlet.ServletResponse;
28  
29  /**
30   * Default {@code WebSubjectContext} implementation that provides for additional storage and retrieval of
31   * a {@link ServletRequest} and {@link ServletResponse}.
32   *
33   * @since 1.0
34   */
35  public class DefaultWebSubjectContext extends DefaultSubjectContext implements WebSubjectContext {
36  
37      private static final long serialVersionUID = 8188555355305827739L;
38  
39      private static final String SERVLET_REQUEST = DefaultWebSubjectContext.class.getName() + ".SERVLET_REQUEST";
40      private static final String SERVLET_RESPONSE = DefaultWebSubjectContext.class.getName() + ".SERVLET_RESPONSE";
41  
42      public DefaultWebSubjectContext() {
43      }
44  
45      public DefaultWebSubjectContext(WebSubjectContext context) {
46          super(context);
47      }
48  
49      @Override
50      public String resolveHost() {
51          String host = super.resolveHost();
52          if (host == null) {
53              ServletRequest request = resolveServletRequest();
54              if (request != null) {
55                  host = request.getRemoteHost();
56              }
57          }
58          return host;
59      }
60  
61      public ServletRequest getServletRequest() {
62          return getTypedValue(SERVLET_REQUEST, ServletRequest.class);
63      }
64  
65      public void setServletRequest(ServletRequest request) {
66          if (request != null) {
67              put(SERVLET_REQUEST, request);
68          }
69      }
70  
71      public ServletRequest resolveServletRequest() {
72  
73          ServletRequest request = getServletRequest();
74  
75          //fall back on existing subject instance if it exists:
76          if (request == null) {
77              Subject existing = getSubject();
78              if (existing instanceof WebSubject) {
79                  request = ((WebSubject) existing).getServletRequest();
80              }
81          }
82  
83          return request;
84      }
85  
86      public ServletResponse getServletResponse() {
87          return getTypedValue(SERVLET_RESPONSE, ServletResponse.class);
88      }
89  
90      public void setServletResponse(ServletResponse response) {
91          if (response != null) {
92              put(SERVLET_RESPONSE, response);
93          }
94      }
95  
96      public ServletResponse resolveServletResponse() {
97  
98          ServletResponse response = getServletResponse();
99  
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 }