001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.util;
016    
017    import com.liferay.portal.kernel.io.Deserializer;
018    import com.liferay.portal.kernel.io.Serializer;
019    
020    import java.nio.ByteBuffer;
021    
022    import javax.servlet.http.Cookie;
023    
024    /**
025     * @author Shuyang Zhou
026     */
027    public class CookieUtil {
028    
029            public static Cookie deserialize(byte[] bytes) {
030                    Deserializer deserializer = new Deserializer(ByteBuffer.wrap(bytes));
031    
032                    String comment = deserializer.readString();
033                    String domain = deserializer.readString();
034                    boolean httpOnly = deserializer.readBoolean();
035                    int maxAge = deserializer.readInt();
036                    String name = deserializer.readString();
037                    String path = deserializer.readString();
038                    boolean secure = deserializer.readBoolean();
039                    String value = deserializer.readString();
040    
041                    if (value.isEmpty()) {
042                            value = null;
043                    }
044    
045                    int version = deserializer.readInt();
046    
047                    Cookie cookie = new Cookie(name, value);
048    
049                    if (!comment.isEmpty()) {
050                            cookie.setComment(comment);
051                    }
052    
053                    if (!domain.isEmpty()) {
054                            cookie.setDomain(domain);
055                    }
056    
057                    cookie.setHttpOnly(httpOnly);
058                    cookie.setMaxAge(maxAge);
059    
060                    if (!path.isEmpty()) {
061                            cookie.setPath(path);
062                    }
063    
064                    cookie.setSecure(secure);
065                    cookie.setVersion(version);
066    
067                    return cookie;
068            }
069    
070            public static boolean equals(Cookie cookie1, Cookie cookie2) {
071                    if (!Validator.equals(cookie1.getComment(), cookie2.getComment())) {
072                            return false;
073                    }
074    
075                    if (!Validator.equals(cookie1.getDomain(), cookie2.getDomain())) {
076                            return false;
077                    }
078    
079                    if (!Validator.equals(cookie1.getMaxAge(), cookie2.getMaxAge())) {
080                            return false;
081                    }
082    
083                    if (!Validator.equals(cookie1.getName(), cookie2.getName())) {
084                            return false;
085                    }
086    
087                    if (!Validator.equals(cookie1.getPath(), cookie2.getPath())) {
088                            return false;
089                    }
090    
091                    if (!Validator.equals(cookie1.getSecure(), cookie2.getSecure())) {
092                            return false;
093                    }
094    
095                    if (!Validator.equals(cookie1.getValue(), cookie2.getValue())) {
096                            return false;
097                    }
098    
099                    if (!Validator.equals(cookie1.getVersion(), cookie2.getVersion())) {
100                            return false;
101                    }
102    
103                    if (!Validator.equals(cookie1.isHttpOnly(), cookie2.isHttpOnly())) {
104                            return false;
105                    }
106    
107                    return true;
108            }
109    
110            public static byte[] serialize(Cookie cookie) {
111                    Serializer serializer = new Serializer();
112    
113                    String comment = cookie.getComment();
114    
115                    if (comment == null) {
116                            comment = StringPool.BLANK;
117                    }
118    
119                    serializer.writeString(comment);
120    
121                    String domain = cookie.getDomain();
122    
123                    if (domain == null) {
124                            domain = StringPool.BLANK;
125                    }
126    
127                    serializer.writeString(domain);
128    
129                    serializer.writeBoolean(cookie.isHttpOnly());
130                    serializer.writeInt(cookie.getMaxAge());
131                    serializer.writeString(cookie.getName());
132    
133                    String path = cookie.getPath();
134    
135                    if (path == null) {
136                            path = StringPool.BLANK;
137                    }
138    
139                    serializer.writeString(path);
140    
141                    serializer.writeBoolean(cookie.getSecure());
142    
143                    String value = cookie.getValue();
144    
145                    if (value == null) {
146                            value = StringPool.BLANK;
147                    }
148    
149                    serializer.writeString(value);
150    
151                    serializer.writeInt(cookie.getVersion());
152    
153                    ByteBuffer byteBuffer = serializer.toByteBuffer();
154    
155                    return byteBuffer.array();
156            }
157    
158            public static String toString(Cookie cookie) {
159                    StringBundler sb = new StringBundler(19);
160    
161                    sb.append("{comment=");
162                    sb.append(cookie.getComment());
163                    sb.append(", domain=");
164                    sb.append(cookie.getDomain());
165                    sb.append(", httpOnly=");
166                    sb.append(cookie.isHttpOnly());
167                    sb.append(", maxAge=");
168                    sb.append(cookie.getMaxAge());
169                    sb.append(", name=");
170                    sb.append(cookie.getName());
171                    sb.append(", path=");
172                    sb.append(cookie.getPath());
173                    sb.append(", secure=");
174                    sb.append(cookie.getSecure());
175                    sb.append(", value=");
176                    sb.append(cookie.getValue());
177                    sb.append(", version=");
178                    sb.append(cookie.getVersion());
179                    sb.append("}");
180    
181                    return sb.toString();
182            }
183    
184    }