@ -15,32 +15,52 @@
@@ -15,32 +15,52 @@
* /
package org.springframework.http ;
import org.springframework.util.Assert ;
import org.springframework.util.ObjectUtils ;
/ * *
* Representation for an HTTP Cookie .
*
* < p > Use the { @link # clientCookie } factory method to create a client - to - server ,
* name - value pair cookie and the { @link # serverCookie } factory method to build
* a server - to - client cookie with additional attributes .
*
* @author Rossen Stoyanchev
* @see < a href = "https://tools.ietf.org/html/rfc6265" > RFC 6265 < / a >
* /
public class HttpCookie {
public final class HttpCookie {
private final String name ;
private final String value ;
private String domain ;
private final int maxAge ;
private String path ;
private final String domain ;
private long maxAge = Long . MIN_VALUE ;
private final String path ;
private boolean secure ;
private final boolean secure ;
private boolean httpOnly ;
private final boolean httpOnly ;
public HttpCookie ( String name , String value ) {
private HttpCookie ( String name , String value ) {
this ( name , value , - 1 , null , null , false , false ) ;
}
private HttpCookie ( String name , String value , int maxAge , String domain , String path ,
boolean secure , boolean httpOnly ) {
Assert . hasLength ( name , "'name' is required and must not be empty." ) ;
Assert . hasLength ( value , "'value' is required and must not be empty." ) ;
this . name = name ;
this . value = value ;
this . maxAge = ( maxAge > - 1 ? maxAge : - 1 ) ;
this . domain = domain ;
this . path = path ;
this . secure = secure ;
this . httpOnly = httpOnly ;
}
/ * *
@ -57,66 +77,180 @@ public class HttpCookie {
@@ -57,66 +77,180 @@ public class HttpCookie {
return this . value ;
}
public HttpCookie setPath ( String path ) {
this . path = path ;
return this ;
/ * *
* Return the cookie "Max-Age" attribute in seconds .
*
* < p > A positive value indicates when the cookie expires relative to the
* current time . A value of 0 means the cookie should expire immediately .
* A negative value means no "Max-Age" attribute in which case the cookie
* is removed when the browser is closed .
* /
public int getMaxAge ( ) {
return this . maxAge ;
}
/ * *
* Return the domain attribute of the cookie .
* Return the cookie "Domain" attribut e.
* /
public String getDomain ( ) {
return this . domain ;
}
public HttpCookie setDomain ( String domain ) {
this . domain = domain ;
return this ;
}
/ * *
* Return the path attribute of the cooki e.
* Return the cookie "Path" attribute .
* /
public String getPath ( ) {
return this . path ;
}
public HttpCookie setMaxAge ( long maxAge ) {
this . maxAge = maxAge ;
return this ;
/ * *
* Return { @code true } if the cookie has the "Secure" attribute .
* /
public boolean isSecure ( ) {
return this . secure ;
}
/ * *
* Return the maximum age attribute of the cookie in seconds or
* { @link Long # MIN_VALUE } if not set .
* Return { @code true } if the cookie has the "HttpOnly" attribute .
* @see < a href = "http://www.owasp.org/index.php/HTTPOnly" > http : //www.owasp.org/index.php/HTTPOnly</a>
* /
public long getMaxAge ( ) {
return this . maxAge ;
public boolean isHttpOnly ( ) {
return this . httpOnly ;
}
public HttpCookie setSecure ( boolean secure ) {
this . secure = secure ;
return this ;
@Override
public int hashCode ( ) {
int result = this . name . hashCode ( ) ;
result = 31 * result + ObjectUtils . nullSafeHashCode ( this . domain ) ;
result = 31 * result + ObjectUtils . nullSafeHashCode ( this . path ) ;
return result ;
}
@Override
public boolean equals ( Object other ) {
if ( this = = other ) {
return true ;
}
if ( ! ( other instanceof HttpCookie ) ) {
return false ;
}
HttpCookie otherCookie = ( HttpCookie ) other ;
return ( this . name . equalsIgnoreCase ( otherCookie . getName ( ) ) & &
ObjectUtils . nullSafeEquals ( this . path , otherCookie . getPath ( ) ) & &
ObjectUtils . nullSafeEquals ( this . domain , otherCookie . getDomain ( ) ) ) ;
}
/ * *
* Return true if the "Secure" attribute of the cookie is present .
* Factory method to create a cookie sent from a client to a server .
* Client cookies are name - value pairs only without attributes .
* @param name the cookie name
* @param value the cookie value
* @return the created cookie instance
* /
public boolean isSecure ( ) {
return this . secure ;
public static HttpCookie clientCookie ( String name , String value ) {
return new HttpCookie ( name , value ) ;
}
public HttpCookie setHttpOnly ( boolean httpOnly ) {
this . httpOnly = httpOnly ;
return this ;
/ * *
* Factory method to obtain a builder for a server - defined cookie that starts
* with a name - value pair and may also include attributes .
* @param name the cookie name
* @param value the cookie value
* @return the created cookie instance
* /
public static HttpCookieBuilder serverCookie ( final String name , final String value ) {
return new HttpCookieBuilder ( ) {
private int maxAge = - 1 ;
private String domain ;
private String path ;
private boolean secure ;
private boolean httpOnly ;
@Override
public HttpCookieBuilder maxAge ( int maxAge ) {
this . maxAge = maxAge ;
return this ;
}
@Override
public HttpCookieBuilder domain ( String domain ) {
this . domain = domain ;
return this ;
}
@Override
public HttpCookieBuilder path ( String path ) {
this . path = path ;
return this ;
}
@Override
public HttpCookieBuilder secure ( ) {
this . secure = true ;
return this ;
}
@Override
public HttpCookieBuilder httpOnly ( ) {
this . httpOnly = true ;
return this ;
}
@Override
public HttpCookie build ( ) {
return new HttpCookie ( name , value , this . maxAge , this . domain , this . path ,
this . secure , this . httpOnly ) ;
}
} ;
}
/ * *
* Return true if the "HttpOnly" attribute of the cookie is present .
* @see < a href = "http://www.owasp.org/index.php/HTTPOnly" > http : //www.owasp.org/index.php/HTTPOnly</a>
* A builder for a server - defined HttpCookie with attributes .
* /
public boolean isHttpOnly ( ) {
return this . httpOnly ;
public interface HttpCookieBuilder {
/ * *
* Set the cookie "Max-Age" attribute in seconds .
*
* < p > A positive value indicates when the cookie should expire relative
* to the current time . A value of 0 means the cookie should expire
* immediately . A negative value results in no "Max-Age" attribute in
* which case the cookie is removed when the browser is closed .
* /
HttpCookieBuilder maxAge ( int maxAge ) ;
/ * *
* Set the cookie "Path" attribute .
* /
HttpCookieBuilder path ( String path ) ;
/ * *
* Set the cookie "Domain" attribute .
* /
HttpCookieBuilder domain ( String domain ) ;
/ * *
* Add the "Secure" attribute to the cookie .
* /
HttpCookieBuilder secure ( ) ;
/ * *
* Add the "HttpOnly" attribute to the cookie .
* @see < a href = "http://www.owasp.org/index.php/HTTPOnly" > http : //www.owasp.org/index.php/HTTPOnly</a>
* /
HttpCookieBuilder httpOnly ( ) ;
/ * *
* Create the HttpCookie .
* /
HttpCookie build ( ) ;
}
}