Skip to content

Commit fdb4cba

Browse files
committed
first spike of CookieJar
1 parent 7c91e4d commit fdb4cba

File tree

5 files changed

+271
-0
lines changed

5 files changed

+271
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.ning.http.client.cookiejar;
2+
3+
import java.util.Collection;
4+
5+
import com.ning.http.client.Cookie;
6+
import com.ning.http.client.filter.RequestFilter;
7+
import com.ning.http.client.filter.ResponseFilter;
8+
9+
public abstract class AbstractCookieJar
10+
{
11+
12+
private final RequestFilter requestFilter = new CookieJarRequestFilter( this );
13+
14+
private final ResponseFilter responseFilter = new CookieJarResponseFilter( this );
15+
16+
public final RequestFilter getRequestFilter()
17+
{
18+
return requestFilter;
19+
}
20+
21+
public final ResponseFilter getResponseFilter()
22+
{
23+
return responseFilter;
24+
}
25+
26+
protected final void store( String host, Cookie cookie )
27+
throws Exception
28+
{
29+
persist( host, cookie );
30+
31+
String domain = cookie.getDomain();
32+
if ( !host.equals( domain ) )
33+
{
34+
persist( domain, cookie );
35+
}
36+
}
37+
38+
protected abstract void persist( String host, Cookie cookie )
39+
throws Exception;
40+
41+
protected abstract Collection<Cookie> retrieve( String host )
42+
throws Exception;
43+
44+
protected final void delete( String host, Cookie cookie )
45+
throws Exception
46+
{
47+
remove( host, cookie );
48+
49+
String domain = cookie.getDomain();
50+
if ( !host.equals( domain ) )
51+
{
52+
remove( domain, cookie );
53+
}
54+
}
55+
56+
protected abstract void remove( String host, Cookie cookie )
57+
throws Exception;
58+
59+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.ning.http.client.cookiejar;
2+
3+
abstract class AbstractCookieJarFilter
4+
{
5+
6+
protected static final String HOST = "Host";
7+
8+
protected final AbstractCookieJar cookieJar;
9+
10+
public AbstractCookieJarFilter(AbstractCookieJar cookieJar)
11+
{
12+
this.cookieJar = cookieJar;
13+
}
14+
15+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.ning.http.client.cookiejar;
2+
3+
import static java.lang.String.format;
4+
5+
import java.util.Collection;
6+
7+
import com.ning.http.client.Cookie;
8+
import com.ning.http.client.RequestBuilder;
9+
import com.ning.http.client.filter.FilterContext;
10+
import com.ning.http.client.filter.FilterException;
11+
import com.ning.http.client.filter.RequestFilter;
12+
13+
final class CookieJarRequestFilter
14+
extends AbstractCookieJarFilter
15+
implements RequestFilter
16+
{
17+
18+
public CookieJarRequestFilter(AbstractCookieJar cookieJar)
19+
{
20+
super( cookieJar );
21+
}
22+
23+
@SuppressWarnings( "rawtypes" )
24+
public final FilterContext filter( FilterContext ctx )
25+
throws FilterException
26+
{
27+
String domain = "";
28+
29+
Collection<Cookie> cookies = null;
30+
try
31+
{
32+
cookies = cookieJar.retrieve( domain );
33+
}
34+
catch ( Exception e )
35+
{
36+
throw new FilterException( format( "Impossible to retrieve cookies from domain %s: %s",
37+
domain, e.getMessage() ),
38+
e );
39+
}
40+
41+
if ( cookies == null || cookies.isEmpty() )
42+
{
43+
return ctx;
44+
}
45+
46+
final RequestBuilder requestBuilder = new RequestBuilder( ctx.getRequest() );
47+
48+
for ( Cookie cookie : cookies )
49+
{
50+
requestBuilder.addCookie( cookie );
51+
}
52+
53+
return new FilterContext.FilterContextBuilder( ctx )
54+
.request( requestBuilder.build() )
55+
.build();
56+
}
57+
58+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.ning.http.client.cookiejar;
2+
3+
import static java.lang.String.format;
4+
import static com.ning.http.util.AsyncHttpProviderUtils.parseCookie;
5+
6+
import java.util.List;
7+
8+
import com.ning.http.client.Cookie;
9+
import com.ning.http.client.filter.FilterContext;
10+
import com.ning.http.client.filter.FilterException;
11+
import com.ning.http.client.filter.ResponseFilter;
12+
13+
final class CookieJarResponseFilter
14+
extends AbstractCookieJarFilter
15+
implements ResponseFilter
16+
{
17+
18+
private static final String SET_COOKIE = "Set-Cookie";
19+
20+
public CookieJarResponseFilter(AbstractCookieJar cookieJar)
21+
{
22+
super( cookieJar );
23+
}
24+
25+
@SuppressWarnings( "rawtypes" )
26+
public FilterContext filter( FilterContext ctx )
27+
throws FilterException
28+
{
29+
final List<String> cookiesString = ctx.getResponseHeaders().getHeaders().get( SET_COOKIE );
30+
31+
if ( cookiesString != null && !cookiesString.isEmpty() )
32+
{
33+
String host = ctx.getRequest().getHeaders().getFirstValue( HOST );
34+
35+
for ( String cookieValue : cookiesString )
36+
{
37+
Cookie currentCookie = parseCookie( cookieValue );
38+
39+
try
40+
{
41+
cookieJar.store( host, currentCookie );
42+
}
43+
catch ( Exception e )
44+
{
45+
throw new FilterException( format( "Impossible to store cookie %s: %s", cookieValue, e.getMessage() ),
46+
e );
47+
}
48+
}
49+
}
50+
51+
return ctx;
52+
}
53+
54+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package com.ning.http.client.cookiejar;
2+
3+
import java.util.Collection;
4+
import java.util.concurrent.ConcurrentHashMap;
5+
import java.util.concurrent.ConcurrentMap;
6+
import java.util.concurrent.locks.Lock;
7+
import java.util.concurrent.locks.ReentrantLock;
8+
9+
import com.ning.http.client.Cookie;
10+
11+
public final class InMemoryCookieJar
12+
extends AbstractCookieJar
13+
{
14+
15+
private final ConcurrentMap<String, ConcurrentMap<String, Cookie>> cookiesRegistry =
16+
new ConcurrentHashMap<String, ConcurrentMap<String, Cookie>>();
17+
18+
private final Lock lock = new ReentrantLock();
19+
20+
@Override
21+
protected void persist( String host, Cookie cookie )
22+
throws Exception
23+
{
24+
lock.lock();
25+
26+
try
27+
{
28+
ConcurrentMap<String, Cookie> domainCookies = cookiesRegistry.get( host );
29+
if ( domainCookies == null )
30+
{
31+
// create the index and store it
32+
domainCookies = new ConcurrentHashMap<String, Cookie>();
33+
cookiesRegistry.put( host, domainCookies );
34+
}
35+
36+
domainCookies.put( cookie.getName(), cookie );
37+
}
38+
finally
39+
{
40+
lock.unlock();
41+
}
42+
}
43+
44+
@Override
45+
protected Collection<Cookie> retrieve( String host )
46+
throws Exception
47+
{
48+
lock.lock();
49+
50+
try
51+
{
52+
ConcurrentMap<String, Cookie> hostCookies = cookiesRegistry.get( host );
53+
if ( hostCookies != null )
54+
{
55+
return hostCookies.values();
56+
}
57+
return null;
58+
}
59+
finally
60+
{
61+
lock.unlock();
62+
}
63+
}
64+
65+
@Override
66+
protected void remove( String host, Cookie cookie )
67+
throws Exception
68+
{
69+
lock.lock();
70+
71+
try
72+
{
73+
ConcurrentMap<String, Cookie> hostCookies = cookiesRegistry.get( host );
74+
if ( hostCookies != null )
75+
{
76+
hostCookies.remove( cookie.getName() );
77+
}
78+
}
79+
finally
80+
{
81+
lock.unlock();
82+
}
83+
}
84+
85+
}

0 commit comments

Comments
 (0)