1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.jboss.netty.handler.codec.http.cookie;
17
18 import static org.jboss.netty.handler.codec.http.cookie.CookieUtil.*;
19
20 import java.util.Iterator;
21
22 import org.jboss.netty.handler.codec.http.HttpRequest;
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41 public final class ClientCookieEncoder extends CookieEncoder {
42
43
44
45
46
47 public static final ClientCookieEncoder STRICT = new ClientCookieEncoder(true);
48
49
50
51
52 public static final ClientCookieEncoder LAX = new ClientCookieEncoder(false);
53
54 private ClientCookieEncoder(boolean strict) {
55 super(strict);
56 }
57
58
59
60
61
62
63
64
65 public String encode(String name, String value) {
66 return encode(new DefaultCookie(name, value));
67 }
68
69
70
71
72
73
74
75 public String encode(Cookie cookie) {
76 if (cookie == null) {
77 throw new NullPointerException("cookie");
78 }
79 StringBuilder buf = new StringBuilder();
80 encode(buf, cookie);
81 return stripTrailingSeparator(buf);
82 }
83
84
85
86
87
88
89
90 public String encode(Cookie... cookies) {
91 if (cookies == null) {
92 throw new NullPointerException("cookies");
93 }
94 if (cookies.length == 0) {
95 return null;
96 }
97
98 StringBuilder buf = new StringBuilder();
99 for (Cookie c : cookies) {
100 if (c == null) {
101 break;
102 }
103
104 encode(buf, c);
105 }
106 return stripTrailingSeparatorOrNull(buf);
107 }
108
109
110
111
112
113
114
115 public String encode(Iterable<? extends Cookie> cookies) {
116 if (cookies == null) {
117 throw new NullPointerException("cookies");
118 }
119 Iterator<? extends Cookie> cookiesIt = cookies.iterator();
120 if (!cookiesIt.hasNext()) {
121 return null;
122 }
123
124 StringBuilder buf = new StringBuilder();
125 while (cookiesIt.hasNext()) {
126 Cookie c = cookiesIt.next();
127 if (c == null) {
128 break;
129 }
130
131 encode(buf, c);
132 }
133 return stripTrailingSeparatorOrNull(buf);
134 }
135
136 private void encode(StringBuilder buf, Cookie c) {
137 final String name = c.name();
138 final String value = c.value() != null ? c.value() : "";
139
140 validateCookie(name, value);
141
142 if (c.wrap()) {
143 addQuoted(buf, name, value);
144 } else {
145 add(buf, name, value);
146 }
147 }
148 }