Skip to content

Commit 43ca9e6

Browse files
committed
added the scheduler for deletion
1 parent c34e1a2 commit 43ca9e6

File tree

2 files changed

+59
-4
lines changed

2 files changed

+59
-4
lines changed

src/main/java/com/ning/http/client/cookiejar/AbstractCookieJar.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.ning.http.client.cookiejar;
22

33
import java.util.Collection;
4+
import java.util.Timer;
45

56
import com.ning.http.client.Cookie;
67
import com.ning.http.client.filter.RequestFilter;
@@ -10,10 +11,14 @@ public abstract class AbstractCookieJar
1011
implements CookieJar
1112
{
1213

14+
private static final int MILLISENCONDS_IN_SECOND = 1000;
15+
1316
private final RequestFilter requestFilter = new CookieJarRequestFilter( this );
1417

1518
private final ResponseFilter responseFilter = new CookieJarResponseFilter( this );
1619

20+
private final Timer timer = new Timer( true );
21+
1722
public final RequestFilter getRequestFilter()
1823
{
1924
return requestFilter;
@@ -24,15 +29,27 @@ public final ResponseFilter getResponseFilter()
2429
return responseFilter;
2530
}
2631

27-
protected final void store( String host, Cookie cookie )
32+
final void store( String host, Cookie cookie )
2833
throws Exception
2934
{
30-
persist( host, cookie );
35+
storeAndSchedule( host, cookie );
3136

3237
String domain = cookie.getDomain();
3338
if ( !host.equals( domain ) )
3439
{
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 );
3653
}
3754
}
3855

@@ -42,7 +59,7 @@ protected abstract void persist( String host, Cookie cookie )
4259
protected abstract Collection<Cookie> retrieve( String host )
4360
throws Exception;
4461

45-
protected final void delete( String host, Cookie cookie )
62+
final void delete( String host, Cookie cookie )
4663
throws Exception
4764
{
4865
remove( host, cookie );
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.ning.http.client.cookiejar;
2+
3+
import java.util.TimerTask;
4+
5+
import com.ning.http.client.Cookie;
6+
7+
final class DeleteCookieTimerTask
8+
extends TimerTask
9+
{
10+
11+
private final AbstractCookieJar cookieJar;
12+
13+
private final String host;
14+
15+
private final Cookie cookie;
16+
17+
public DeleteCookieTimerTask( AbstractCookieJar cookieJar, String host, Cookie cookie )
18+
{
19+
this.cookieJar = cookieJar;
20+
this.host = host;
21+
this.cookie = cookie;
22+
}
23+
24+
@Override
25+
public void run()
26+
{
27+
try
28+
{
29+
cookieJar.remove( host, cookie );
30+
}
31+
catch ( Exception e )
32+
{
33+
// TODO Auto-generated catch block
34+
e.printStackTrace();
35+
}
36+
}
37+
38+
}

0 commit comments

Comments
 (0)