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 org.jboss.netty.handler.codec.http.HttpHeaderDateFormat;
21  import org.jboss.netty.handler.codec.http.HttpRequest;
22  
23  import java.util.ArrayList;
24  import java.util.Collection;
25  import java.util.Collections;
26  import java.util.Date;
27  import java.util.List;
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  public final class ServerCookieEncoder extends CookieEncoder {
46  
47      
48  
49  
50  
51      public static final ServerCookieEncoder STRICT = new ServerCookieEncoder(true);
52  
53      
54  
55  
56      public static final ServerCookieEncoder LAX = new ServerCookieEncoder(false);
57  
58      private ServerCookieEncoder(boolean strict) {
59          super(strict);
60      }
61  
62      
63  
64  
65  
66  
67  
68  
69      public String encode(String name, String value) {
70          return encode(new DefaultCookie(name, value));
71      }
72  
73      
74  
75  
76  
77  
78  
79      public String encode(Cookie cookie) {
80          if (cookie == null) {
81              throw new NullPointerException("cookie");
82          }
83          final String name = cookie.name();
84          final String value = cookie.value() != null ? cookie.value() : "";
85  
86          validateCookie(name, value);
87  
88          StringBuilder buf = new StringBuilder();
89  
90          if (cookie.wrap()) {
91              addQuoted(buf, name, value);
92          } else {
93              add(buf, name, value);
94          }
95  
96          if (cookie.maxAge() != Integer.MIN_VALUE) {
97              add(buf, CookieHeaderNames.MAX_AGE, cookie.maxAge());
98              Date expires = new Date((long) cookie.maxAge() * 1000 + System.currentTimeMillis());
99              add(buf, CookieHeaderNames.EXPIRES, HttpHeaderDateFormat.get().format(expires));
100         }
101 
102         if (cookie.path() != null) {
103             add(buf, CookieHeaderNames.PATH, cookie.path());
104         }
105 
106         if (cookie.domain() != null) {
107             add(buf, CookieHeaderNames.DOMAIN, cookie.domain());
108         }
109         if (cookie.isSecure()) {
110             add(buf, CookieHeaderNames.SECURE);
111         }
112         if (cookie.isHttpOnly()) {
113             add(buf, CookieHeaderNames.HTTPONLY);
114         }
115 
116         return stripTrailingSeparator(buf);
117     }
118 
119     
120 
121 
122 
123 
124 
125     public List<String> encode(Cookie... cookies) {
126         if (cookies == null) {
127             throw new NullPointerException("cookies");
128         }
129         if (cookies.length == 0) {
130             return Collections.emptyList();
131         }
132 
133         List<String> encoded = new ArrayList<String>(cookies.length);
134         for (Cookie c : cookies) {
135             if (c == null) {
136                 break;
137             }
138             encoded.add(encode(c));
139         }
140         return encoded;
141     }
142 
143     
144 
145 
146 
147 
148 
149     public List<String> encode(Collection<? extends Cookie> cookies) {
150         if (cookies == null) {
151             throw new NullPointerException("cookies");
152         }
153         if (cookies.isEmpty()) {
154             return Collections.emptyList();
155         }
156 
157         List<String> encoded = new ArrayList<String>(cookies.size());
158         for (Cookie c : cookies) {
159             if (c == null) {
160                 break;
161             }
162             encoded.add(encode(c));
163         }
164         return encoded;
165     }
166 
167     
168 
169 
170 
171 
172 
173     public List<String> encode(Iterable<? extends Cookie> cookies) {
174         if (cookies == null) {
175             throw new NullPointerException("cookies");
176         }
177         if (cookies.iterator().hasNext()) {
178             return Collections.emptyList();
179         }
180 
181         List<String> encoded = new ArrayList<String>();
182         for (Cookie c : cookies) {
183             if (c == null) {
184                 break;
185             }
186             encoded.add(encode(c));
187         }
188         return encoded;
189     }
190 }