1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
31
32
33
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
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
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 }