public static List < Cookie > getCookies ( String [ ] cookiesStrings , URL targetURL ) { ArrayList < Cookie > list = new ArrayList < Cookie > ( ) ; for ( String cs : cookiesStrings ) { String name = null ; String value = null ; String expires = null ; String domain = null ; String path = null ; boolean secure = false ; String [ ] tokens = cs . split ( ";" ) ; int equals = tokens [ 0 ] . indexOf ( "=" ) ; name = tokens [ 0 ] . substring ( 0 , equals ) ; value = tokens [ 0 ] . substring ( equals + 1 ) ; for ( int i = 1 ; i < tokens . length ; i ++ ) { String ti = tokens [ i ] . trim ( ) ; if ( ti . equalsIgnoreCase ( "secure" ) ) secure = true ; if ( ti . toLowerCase ( ) . startsWith ( "path=" ) ) { path = ti . substring ( 5 ) ; } if ( ti . toLowerCase ( ) . startsWith ( "domain=" ) ) { domain = ti . substring ( 7 ) ; } if ( ti . toLowerCase ( ) . startsWith ( "expires=" ) ) { expires = ti . substring ( 8 ) ; } } BasicClientCookie cookie = new BasicClientCookie ( name , value ) ; // check domain if ( domain != null ) { cookie . setDomain ( domain ) ; if ( ! checkDomainMatchToUrl ( domain , targetURL . getHost ( ) ) ) continue ; } // check path if ( path != null ) { cookie . setPath ( path ) ; if ( ! path . equals ( "" ) && ! path . equals ( "/" ) && ! targetURL . getPath ( ) . startsWith ( path ) ) continue ; } // check secure if ( secure ) { cookie . setSecure ( secure ) ; if ( ! targetURL . getProtocol ( ) . equalsIgnoreCase ( "https" ) ) continue ; } // check expiration if ( expires != null ) { try { Date expirationDate = DATE_FORMAT . parse ( expires ) ; cookie . setExpiryDate ( expirationDate ) ; // check that it hasn't expired? if ( cookie . isExpired ( new Date ( ) ) ) continue ; cookie . setExpiryDate ( expirationDate ) ; } catch ( ParseException e ) { // ignore exceptions } } // attach additional infos to cookie list . add ( cookie ) ; } return list ; }
Get a list of cookies based on the cookies string taken from response header and the target url .