Class ServerCookieEncoder

java.lang.Object
io.netty.handler.codec.http.cookie.CookieEncoder
io.netty.handler.codec.http.cookie.ServerCookieEncoder

public final class ServerCookieEncoder extends CookieEncoder
A RFC6265 compliant cookie encoder to be used server side, so some fields are sent (Version is typically ignored). As Netty's Cookie merges Expires and MaxAge into one single field, only Max-Age field is sent. Note that multiple cookies must be sent as separate "Set-Cookie" headers.
// Example
HttpResponse res = ...;
res.setHeader("Set-Cookie", ServerCookieEncoder.encode("JSESSIONID", "1234"));
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final ServerCookieEncoder
    Lax instance that doesn't validate name and value, and that allows multiple cookies with the same name.
    static final ServerCookieEncoder
    Strict encoder that validates that name and value chars are in the valid scope defined in RFC6265, and (for methods that accept multiple cookies) that only one cookie is encoded with any given name.

    Fields inherited from class CookieEncoder

    strict
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    private
    ServerCookieEncoder(boolean strict)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    private static java.util.List<java.lang.String>
    dedup(java.util.List<java.lang.String> encoded, java.util.Map<java.lang.String, java.lang.Integer> nameToLastIndex)
    Deduplicate a list of encoded cookies by keeping only the last instance with a given name.
    java.lang.String
    encode(Cookie cookie)
    Encodes the specified cookie into a Set-Cookie header value.
    java.util.List<java.lang.String>
    encode(Cookie... cookies)
    Batch encodes cookies into Set-Cookie header values.
    java.util.List<java.lang.String>
    encode(java.lang.Iterable<? extends Cookie> cookies)
    Batch encodes cookies into Set-Cookie header values.
    java.lang.String
    encode(java.lang.String name, java.lang.String value)
    Encodes the specified cookie name-value pair into a Set-Cookie header value.
    java.util.List<java.lang.String>
    encode(java.util.Collection<? extends Cookie> cookies)
    Batch encodes cookies into Set-Cookie header values.

    Methods inherited from class CookieEncoder

    validateCookie

    Methods inherited from class java.lang.Object

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

    • STRICT

      public static final ServerCookieEncoder STRICT
      Strict encoder that validates that name and value chars are in the valid scope defined in RFC6265, and (for methods that accept multiple cookies) that only one cookie is encoded with any given name. (If multiple cookies have the same name, the last one is the one that is encoded.)
    • LAX

      public static final ServerCookieEncoder LAX
      Lax instance that doesn't validate name and value, and that allows multiple cookies with the same name.
  • Constructor Details

    • ServerCookieEncoder

      private ServerCookieEncoder(boolean strict)
  • Method Details

    • encode

      public java.lang.String encode(java.lang.String name, java.lang.String value)
      Encodes the specified cookie name-value pair into a Set-Cookie header value.
      Parameters:
      name - the cookie name
      value - the cookie value
      Returns:
      a single Set-Cookie header value
    • encode

      public java.lang.String encode(Cookie cookie)
      Encodes the specified cookie into a Set-Cookie header value.
      Parameters:
      cookie - the cookie
      Returns:
      a single Set-Cookie header value
    • dedup

      private static java.util.List<java.lang.String> dedup(java.util.List<java.lang.String> encoded, java.util.Map<java.lang.String, java.lang.Integer> nameToLastIndex)
      Deduplicate a list of encoded cookies by keeping only the last instance with a given name.
      Parameters:
      encoded - The list of encoded cookies.
      nameToLastIndex - A map from cookie name to index of last cookie instance.
      Returns:
      The encoded list with all but the last instance of a named cookie.
    • encode

      public java.util.List<java.lang.String> encode(Cookie... cookies)
      Batch encodes cookies into Set-Cookie header values.
      Parameters:
      cookies - a bunch of cookies
      Returns:
      the corresponding bunch of Set-Cookie headers
    • encode

      public java.util.List<java.lang.String> encode(java.util.Collection<? extends Cookie> cookies)
      Batch encodes cookies into Set-Cookie header values.
      Parameters:
      cookies - a bunch of cookies
      Returns:
      the corresponding bunch of Set-Cookie headers
    • encode

      public java.util.List<java.lang.String> encode(java.lang.Iterable<? extends Cookie> cookies)
      Batch encodes cookies into Set-Cookie header values.
      Parameters:
      cookies - a bunch of cookies
      Returns:
      the corresponding bunch of Set-Cookie headers