public void addSetCookie ( Cookie cookie ) { String name = cookie . getName ( ) ; String value = cookie . getValue ( ) ; int version = cookie . getVersion ( ) ; // Check arguments if ( name == null || name . length ( ) == 0 ) throw new IllegalArgumentException ( "Bad cookie name" ) ; // Format value and params StringBuffer buf = new StringBuffer ( 128 ) ; String name_value_params = null ; synchronized ( buf ) { buf . append ( name ) ; buf . append ( ' ' ) ; if ( value != null && value . length ( ) > 0 ) { if ( version == 0 ) URI . encodeString ( buf , value , "\";, '" ) ; else buf . append ( QuotedStringTokenizer . quote ( value , "\";, '" ) ) ; } if ( version > 0 ) { buf . append ( ";Version=" ) ; buf . append ( version ) ; String comment = cookie . getComment ( ) ; if ( comment != null && comment . length ( ) > 0 ) { buf . append ( ";Comment=" ) ; QuotedStringTokenizer . quote ( buf , comment ) ; } } String path = cookie . getPath ( ) ; if ( path != null && path . length ( ) > 0 ) { buf . append ( ";Path=" ) ; buf . append ( path ) ; } String domain = cookie . getDomain ( ) ; if ( domain != null && domain . length ( ) > 0 ) { buf . append ( ";Domain=" ) ; buf . append ( domain . toLowerCase ( ) ) ; // lowercase for IE } long maxAge = cookie . getMaxAge ( ) ; if ( maxAge >= 0 ) { if ( version == 0 ) { buf . append ( ";Expires=" ) ; if ( maxAge == 0 ) buf . append ( __01Jan1970 ) ; else formatDate ( buf , System . currentTimeMillis ( ) + 1000L * maxAge , true ) ; } else { buf . append ( ";Max-Age=" ) ; buf . append ( cookie . getMaxAge ( ) ) ; } } else if ( version > 0 ) { buf . append ( ";Discard" ) ; } if ( cookie . getSecure ( ) ) { buf . append ( ";Secure" ) ; } if ( cookie instanceof HttpOnlyCookie ) buf . append ( ";HttpOnly" ) ; name_value_params = buf . toString ( ) ; } put ( __Expires , __01Jan1970 ) ; add ( __SetCookie , name_value_params ) ; }
Format a set cookie value