1
1
package com .ning .http .client .cookiejar ;
2
2
3
3
import java .util .Collection ;
4
+ import java .util .Timer ;
4
5
5
6
import com .ning .http .client .Cookie ;
6
7
import com .ning .http .client .filter .RequestFilter ;
@@ -10,10 +11,14 @@ public abstract class AbstractCookieJar
10
11
implements CookieJar
11
12
{
12
13
14
+ private static final int MILLISENCONDS_IN_SECOND = 1000 ;
15
+
13
16
private final RequestFilter requestFilter = new CookieJarRequestFilter ( this );
14
17
15
18
private final ResponseFilter responseFilter = new CookieJarResponseFilter ( this );
16
19
20
+ private final Timer timer = new Timer ( true );
21
+
17
22
public final RequestFilter getRequestFilter ()
18
23
{
19
24
return requestFilter ;
@@ -24,15 +29,27 @@ public final ResponseFilter getResponseFilter()
24
29
return responseFilter ;
25
30
}
26
31
27
- protected final void store ( String host , Cookie cookie )
32
+ final void store ( String host , Cookie cookie )
28
33
throws Exception
29
34
{
30
- persist ( host , cookie );
35
+ storeAndSchedule ( host , cookie );
31
36
32
37
String domain = cookie .getDomain ();
33
38
if ( !host .equals ( domain ) )
34
39
{
35
- persist ( domain , cookie );
40
+ storeAndSchedule ( domain , cookie );
41
+ }
42
+ }
43
+
44
+ private void storeAndSchedule ( String host , Cookie cookie )
45
+ throws Exception
46
+ {
47
+ persist ( host , cookie );
48
+
49
+ if ( cookie .getMaxAge () > 0 ) // otherwise will be just deleted
50
+ {
51
+ long delay = cookie .getMaxAge () * MILLISENCONDS_IN_SECOND ;
52
+ timer .schedule ( new DeleteCookieTimerTask ( this , host , cookie ), delay );
36
53
}
37
54
}
38
55
@@ -42,7 +59,7 @@ protected abstract void persist( String host, Cookie cookie )
42
59
protected abstract Collection <Cookie > retrieve ( String host )
43
60
throws Exception ;
44
61
45
- protected final void delete ( String host , Cookie cookie )
62
+ final void delete ( String host , Cookie cookie )
46
63
throws Exception
47
64
{
48
65
remove ( host , cookie );
0 commit comments