Class CookieEncoder


  • public class CookieEncoder
    extends java.lang.Object
    Encodes Cookies into an HTTP header value. This encoder can encode the HTTP cookie version 0, 1, and 2.

    This encoder is stateful. It maintains an internal data structure that holds the Cookies added by the addCookie(String, String) method. Once encode() is called, all added Cookies are encoded into an HTTP header value and all Cookies in the internal data structure are removed so that the encoder can start over.

     // Client-side example
     HttpRequest req = ...;
     CookieEncoder encoder = new CookieEncoder(false);
     encoder.addCookie("JSESSIONID", "1234");
     res.setHeader("Cookie", encoder.encode());
    
     // Server-side example
     HttpResponse res = ...;
     CookieEncoder encoder = new CookieEncoder(true);
     encoder.addCookie("JSESSIONID", "1234");
     res.setHeader("Set-Cookie", encoder.encode());
     
    See Also:
    CookieDecoder
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.util.Set<Cookie> cookies  
      private boolean server  
      private boolean strict  
    • Constructor Summary

      Constructors 
      Constructor Description
      CookieEncoder​(boolean server)
      Creates a new encoder.
      CookieEncoder​(boolean server, boolean strict)
      Creates a new encoder.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addCookie​(java.lang.String name, java.lang.String value)
      Adds a new Cookie created with the specified name and value to this encoder.
      void addCookie​(Cookie cookie)
      Adds the specified Cookie to this encoder.
      java.lang.String encode()
      Encodes the Cookies which were added by addCookie(Cookie) so far into an HTTP header value.
      private java.lang.String encodeClientSide()  
      private java.lang.String encodeServerSide()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • cookies

        private final java.util.Set<Cookie> cookies
      • server

        private final boolean server
      • strict

        private final boolean strict
    • Constructor Detail

      • CookieEncoder

        public CookieEncoder​(boolean server)
        Creates a new encoder.
        Parameters:
        server - true if and only if this encoder is supposed to encode server-side cookies. false if and only if this encoder is supposed to encode client-side cookies.
      • CookieEncoder

        public CookieEncoder​(boolean server,
                             boolean strict)
        Creates a new encoder.
        Parameters:
        server - true if and only if this encoder is supposed to encode server-side cookies. false if and only if this encoder is supposed to encode client-side cookies.
        strict - true if and only if this encoder is supposed to validate characters according to RFC6265.
    • Method Detail

      • addCookie

        public void addCookie​(java.lang.String name,
                              java.lang.String value)
        Adds a new Cookie created with the specified name and value to this encoder.
      • addCookie

        public void addCookie​(Cookie cookie)
        Adds the specified Cookie to this encoder.
      • encode

        public java.lang.String encode()
        Encodes the Cookies which were added by addCookie(Cookie) so far into an HTTP header value. If no Cookies were added, an empty string is returned. Be aware that calling this method will clear the content of the CookieEncoder
      • encodeServerSide

        private java.lang.String encodeServerSide()
      • encodeClientSide

        private java.lang.String encodeClientSide()