1
2
3
4
5
6
7
8
9
10
11
12package security.user;
13
14import java.security.Principal;
15
16import javax.servlet.http.HttpServletRequest;
17
18public class ServletUser implements User
19{
20 private final HttpServletRequest _request;
21
22 public ServletUser(HttpServletRequest request)
23 {
24 _request = request;
25 }
26
27 public String getName()
28 {
29 Principal principal = getPrincipal();
30 if (principal == null)
31 {
32 return null;
33 }
34 return principal.getName();
35 }
36
37 public boolean isInRole(String role)
38 {
39 return _request.isUserInRole(role);
40 }
41
42 public Principal getPrincipal()
43 {
44 return _request.getUserPrincipal();
45 }
46
47 public String toString()
48 {
49 return getClass().getSimpleName() + "{" + getName() + "}";
50 }
51
52 public boolean equals(Object obj)
53 {
54 if (obj == null)
55 {
56 return false;
57 }
58 if (obj == this)
59 {
60 return true;
61 }
62 if (obj instanceof ServletUser)
63 {
64 return equals((ServletUser) obj);
65 }
66 return false;
67 }
68
69 public boolean equals(ServletUser other)
70 {
71 Principal p1 = this.getPrincipal();
72 Principal p2 = other.getPrincipal();
73 if (p1 == p2)
74 {
75 return true;
76 }
77 if (p1 == null || p2 == null)
78 {
79 return false;
80 }
81 return p1.equals(p2);
82 }
83
84 public int hashCode()
85 {
86 Principal principal = getPrincipal();
87 if (principal == null)
88 {
89 return ServletUser.class.hashCode();
90 }
91 return principal.hashCode();
92 }
93}
// Copyright 2007, Tim Vernum
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
// modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package security.user;
import java.security.Principal;
import javax.servlet.http.HttpServletRequest;
public class ServletUser implements User
{
private final HttpServletRequest _request;
public ServletUser(HttpServletRequest request)
{
_request = request;
}
public String getName()
{
Principal principal = getPrincipal();
if (principal == null)
{
return null;
}
return principal.getName();
}
public boolean isInRole(String role)
{
return _request.isUserInRole(role);
}
public Principal getPrincipal()
{
return _request.getUserPrincipal();
}
public String toString()
{
return getClass().getSimpleName() + "{" + getName() + "}";
}
public boolean equals(Object obj)
{
if (obj == null)
{
return false;
}
if (obj == this)
{
return true;
}
if (obj instanceof ServletUser)
{
return equals((ServletUser) obj);
}
return false;
}
public boolean equals(ServletUser other)
{
Principal p1 = this.getPrincipal();
Principal p2 = other.getPrincipal();
if (p1 == p2)
{
return true;
}
if (p1 == null || p2 == null)
{
return false;
}
return p1.equals(p2);
}
public int hashCode()
{
Principal principal = getPrincipal();
if (principal == null)
{
return ServletUser.class.hashCode();
}
return principal.hashCode();
}
}