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 | 1 | private static final String SERVLET_REQUEST = DefaultWebSubjectContext.class.getName() + ".SERVLET_REQUEST"; |
40 | 1 | private static final String SERVLET_RESPONSE = DefaultWebSubjectContext.class.getName() + ".SERVLET_RESPONSE"; |
41 | |
|
42 | 9 | public DefaultWebSubjectContext() { |
43 | 9 | } |
44 | |
|
45 | |
public DefaultWebSubjectContext(WebSubjectContext context) { |
46 | 6 | super(context); |
47 | 6 | } |
48 | |
|
49 | |
@Override |
50 | |
public String resolveHost() { |
51 | 6 | String host = super.resolveHost(); |
52 | 6 | if (host == null) { |
53 | 6 | ServletRequest request = resolveServletRequest(); |
54 | 6 | if (request != null) { |
55 | 6 | host = request.getRemoteHost(); |
56 | |
} |
57 | |
} |
58 | 6 | return host; |
59 | |
} |
60 | |
|
61 | |
public ServletRequest getServletRequest() { |
62 | 49 | return getTypedValue(SERVLET_REQUEST, ServletRequest.class); |
63 | |
} |
64 | |
|
65 | |
public void setServletRequest(ServletRequest request) { |
66 | 8 | if (request != null) { |
67 | 8 | put(SERVLET_REQUEST, request); |
68 | |
} |
69 | 8 | } |
70 | |
|
71 | |
public ServletRequest resolveServletRequest() { |
72 | |
|
73 | 20 | ServletRequest request = getServletRequest(); |
74 | |
|
75 | |
|
76 | 20 | if (request == null) { |
77 | 2 | Subject existing = getSubject(); |
78 | 2 | if (existing instanceof WebSubject) { |
79 | 2 | request = ((WebSubject) existing).getServletRequest(); |
80 | |
} |
81 | |
} |
82 | |
|
83 | 20 | return request; |
84 | |
} |
85 | |
|
86 | |
public ServletResponse getServletResponse() { |
87 | 35 | return getTypedValue(SERVLET_RESPONSE, ServletResponse.class); |
88 | |
} |
89 | |
|
90 | |
public void setServletResponse(ServletResponse response) { |
91 | 8 | if (response != null) { |
92 | 8 | put(SERVLET_RESPONSE, response); |
93 | |
} |
94 | 8 | } |
95 | |
|
96 | |
public ServletResponse resolveServletResponse() { |
97 | |
|
98 | 6 | ServletResponse response = getServletResponse(); |
99 | |
|
100 | |
|
101 | 6 | if (response == null) { |
102 | 1 | Subject existing = getSubject(); |
103 | 1 | if (existing instanceof WebSubject) { |
104 | 1 | response = ((WebSubject) existing).getServletResponse(); |
105 | |
} |
106 | |
} |
107 | |
|
108 | 6 | return response; |
109 | |
} |
110 | |
} |