View Javadoc
1   /*
2    * Copyright 2014 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty.handler.codec.http;
17  
18  import static io.netty.handler.codec.http.CookieEncoderUtil.*;
19  
20  /**
21   * A <a href="http://tools.ietf.org/html/rfc6265">RFC6265</a> compliant cookie encoder to be used client side,
22   * so only name=value pairs are sent.
23   *
24   * User-Agents are not supposed to interpret cookies, so, if present, {@link Cookie#rawValue()} will be used.
25   * Otherwise, {@link Cookie#value()} will be used unquoted.
26   *
27   * Note that multiple cookies are supposed to be sent at once in a single "Cookie" header.
28   *
29   * <pre>
30   * // Example
31   * {@link HttpRequest} req = ...;
32   * res.setHeader("Cookie", {@link ClientCookieEncoder}.encode("JSESSIONID", "1234"));
33   * </pre>
34   *
35   * @see ClientCookieDecoder
36   */
37  public final class ClientCookieEncoder {
38  
39      /**
40       * Encodes the specified cookie into a Cookie header value.
41       *
42       * @param name the cookie name
43       * @param value the cookie value
44       * @return a Rfc6265 style Cookie header value
45       */
46      public static String encode(String name, String value) {
47          return encode(new DefaultCookie(name, value));
48      }
49  
50      /**
51       * Encodes the specified cookie into a Cookie header value.
52       *
53       * @param specified the cookie
54       * @return a Rfc6265 style Cookie header value
55       */
56      public static String encode(Cookie cookie) {
57          if (cookie == null) {
58              throw new NullPointerException("cookie");
59          }
60  
61          StringBuilder buf = stringBuilder();
62          encode(buf, cookie);
63          return stripTrailingSeparator(buf);
64      }
65  
66      /**
67       * Encodes the specified cookies into a single Cookie header value.
68       *
69       * @param cookies some cookies
70       * @return a Rfc6265 style Cookie header value, null if no cookies are passed.
71       */
72      public static String encode(Cookie... cookies) {
73          if (cookies == null) {
74              throw new NullPointerException("cookies");
75          }
76  
77          if (cookies.length == 0) {
78              return null;
79          }
80  
81          StringBuilder buf = stringBuilder();
82          for (Cookie c : cookies) {
83              if (c == null) {
84                  break;
85              }
86  
87              encode(buf, c);
88          }
89          return stripTrailingSeparatorOrNull(buf);
90      }
91  
92      /**
93       * Encodes the specified cookies into a single Cookie header value.
94       *
95       * @param cookies some cookies
96       * @return a Rfc6265 style Cookie header value, null if no cookies are passed.
97       */
98      public static String encode(Iterable<Cookie> cookies) {
99          if (cookies == null) {
100             throw new NullPointerException("cookies");
101         }
102 
103         if (!cookies.iterator().hasNext()) {
104             return null;
105         }
106 
107         StringBuilder buf = stringBuilder();
108         for (Cookie c : cookies) {
109             if (c == null) {
110                 break;
111             }
112 
113             encode(buf, c);
114         }
115         return stripTrailingSeparatorOrNull(buf);
116     }
117 
118     private static void encode(StringBuilder buf, Cookie c) {
119         // rawValue > value > ""
120         String value = c.rawValue() != null ? c.rawValue()
121                 : c.value() != null ? c.value() : "";
122         addUnquoted(buf, c.name(), value);
123     }
124 
125     private ClientCookieEncoder() {
126         // unused
127     }
128 }