001    /**
002     * Copyright (c) 2000-present 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 java.util.Objects;
023    
024    import javax.servlet.http.Cookie;
025    
026    /**
027     * @author Shuyang Zhou
028     */
029    public class CookieUtil {
030    
031            public static Cookie deserialize(byte[] bytes) {
032                    Deserializer deserializer = new Deserializer(ByteBuffer.wrap(bytes));
033    
034                    String comment = deserializer.readString();
035                    String domain = deserializer.readString();
036                    boolean httpOnly = deserializer.readBoolean();
037                    int maxAge = deserializer.readInt();
038                    String name = deserializer.readString();
039                    String path = deserializer.readString();
040                    boolean secure = deserializer.readBoolean();
041                    String value = deserializer.readString();
042    
043                    if (value.isEmpty()) {
044                            value = null;
045                    }
046    
047                    int version = deserializer.readInt();
048    
049                    Cookie cookie = new Cookie(name, value);
050    
051                    if (!comment.isEmpty()) {
052                            cookie.setComment(comment);
053                    }
054    
055                    if (!domain.isEmpty()) {
056                            cookie.setDomain(domain);
057                    }
058    
059                    cookie.setHttpOnly(httpOnly);
060                    cookie.setMaxAge(maxAge);
061    
062                    if (!path.isEmpty()) {
063                            cookie.setPath(path);
064                    }
065    
066                    cookie.setSecure(secure);
067                    cookie.setVersion(version);
068    
069                    return cookie;
070            }
071    
072            public static boolean equals(Cookie cookie1, Cookie cookie2) {
073                    if (!Objects.equals(cookie1.getComment(), cookie2.getComment())) {
074                            return false;
075                    }
076    
077                    if (!Objects.equals(cookie1.getDomain(), cookie2.getDomain())) {
078                            return false;
079                    }
080    
081                    if (cookie1.getMaxAge() != cookie2.getMaxAge()) {
082                            return false;
083                    }
084    
085                    if (!Objects.equals(cookie1.getName(), cookie2.getName())) {
086                            return false;
087                    }
088    
089                    if (!Objects.equals(cookie1.getPath(), cookie2.getPath())) {
090                            return false;
091                    }
092    
093                    if (cookie1.getSecure() != cookie2.getSecure()) {
094                            return false;
095                    }
096    
097                    if (!Objects.equals(cookie1.getValue(), cookie2.getValue())) {
098                            return false;
099                    }
100    
101                    if (cookie1.getVersion() != cookie2.getVersion()) {
102                            return false;
103                    }
104    
105                    if (cookie1.isHttpOnly() != cookie2.isHttpOnly()) {
106                            return false;
107                    }
108    
109                    return true;
110            }
111    
112            public static byte[] serialize(Cookie cookie) {
113                    Serializer serializer = new Serializer();
114    
115                    String comment = cookie.getComment();
116    
117                    if (comment == null) {
118                            comment = StringPool.BLANK;
119                    }
120    
121                    serializer.writeString(comment);
122    
123                    String domain = cookie.getDomain();
124    
125                    if (domain == null) {
126                            domain = StringPool.BLANK;
127                    }
128    
129                    serializer.writeString(domain);
130    
131                    serializer.writeBoolean(cookie.isHttpOnly());
132                    serializer.writeInt(cookie.getMaxAge());
133                    serializer.writeString(cookie.getName());
134    
135                    String path = cookie.getPath();
136    
137                    if (path == null) {
138                            path = StringPool.BLANK;
139                    }
140    
141                    serializer.writeString(path);
142    
143                    serializer.writeBoolean(cookie.getSecure());
144    
145                    String value = cookie.getValue();
146    
147                    if (value == null) {
148                            value = StringPool.BLANK;
149                    }
150    
151                    serializer.writeString(value);
152    
153                    serializer.writeInt(cookie.getVersion());
154    
155                    ByteBuffer byteBuffer = serializer.toByteBuffer();
156    
157                    return byteBuffer.array();
158            }
159    
160            public static String toString(Cookie cookie) {
161                    StringBundler sb = new StringBundler(19);
162    
163                    sb.append("{comment=");
164                    sb.append(cookie.getComment());
165                    sb.append(", domain=");
166                    sb.append(cookie.getDomain());
167                    sb.append(", httpOnly=");
168                    sb.append(cookie.isHttpOnly());
169                    sb.append(", maxAge=");
170                    sb.append(cookie.getMaxAge());
171                    sb.append(", name=");
172                    sb.append(cookie.getName());
173                    sb.append(", path=");
174                    sb.append(cookie.getPath());
175                    sb.append(", secure=");
176                    sb.append(cookie.getSecure());
177                    sb.append(", value=");
178                    sb.append(cookie.getValue());
179                    sb.append(", version=");
180                    sb.append(cookie.getVersion());
181                    sb.append("}");
182    
183                    return sb.toString();
184            }
185    
186    }