Java Reference
In-Depth Information
public
public
UserMethodKey
(
String username
,
Method method
)
{
this
this
.
username
=
username
;
this
this
.
method
=
method
;
}
@Override
public
public
boolean
boolean
equals
(
Object o
)
{
iif
(
this
this
==
o
)
return
return true
true
;
iif
(
o
==
null
null
||
getClass
() !=
o
.
getClass
())
return
return false
false
;
UserMethodKey that
= (
UserMethodKey
)
o
;
iif
(!
method
.
equals
(
that
.
method
))
return
false
;
iif
(!
username
.
equals
(
that
.
username
))
return
return false
return false
false
;
return
return true
true
;
}
@Override
public
public
int
int
hashCode
()
{
int
int
result
=
username
.
hashCode
();
result
=
31
*
result
+
method
.
hashCode
();
return
return
result
;
}
}
protected
protected
Map
<
UserMethodKey
,
Integer
>
count
=
new
new
HashMap
<
UserMethodKey
,
Integer
>();
The filter instance remembers how many times in a day a particular user invoked a particular
JAX-RS method. It stores this information in the
count
variable map. This map is keyed by
a custom
UserMethodKey
class, which contains the username and JAX-RS method that is be-
ing tracked.
protected
protected
long
long
today
=
System
.
currentTimeMillis
();
protected
protected synchronized
synchronized
boolean
boolean
authorized
(
String user
,
AllowedPerDay allowed
)
{
iif
(
System
.
currentTimeMillis
() >
today
+ (
24
*
60
*
60
*
1000
))
{
today
=
System
.
currentTimeMillis
();
count
.
clear
();
}