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.servlet;
016    
017    import com.liferay.portal.kernel.util.CookieUtil;
018    import com.liferay.portal.kernel.util.HashUtil;
019    import com.liferay.portal.kernel.util.Validator;
020    
021    import java.io.Serializable;
022    
023    import javax.servlet.http.Cookie;
024    import javax.servlet.http.HttpServletResponse;
025    
026    /**
027     * @author Michael Young
028     * @author Shuyang Zhou
029     */
030    public class Header implements Serializable {
031    
032            public Header(Cookie cookie) {
033                    if (cookie == null) {
034                            throw new IllegalArgumentException("Cookie is null");
035                    }
036    
037                    _type = Type.COOKIE;
038    
039                    _cookieValue = cookie;
040            }
041    
042            public Header(int integer) {
043                    _type = Type.INTEGER;
044    
045                    _intValue = integer;
046            }
047    
048            public Header(long date) {
049                    _type = Type.DATE;
050    
051                    _dateValue = date;
052            }
053    
054            public Header(String string) {
055                    if (string == null) {
056                            throw new IllegalArgumentException("String is null");
057                    }
058    
059                    _type = Type.STRING;
060    
061                    _stringValue = string;
062            }
063    
064            public void addToResponse(String key, HttpServletResponse response) {
065                    if (_type == Type.COOKIE) {
066                            response.addCookie(_cookieValue);
067                    }
068                    else if (_type == Type.DATE) {
069                            response.addDateHeader(key, _dateValue);
070                    }
071                    else if (_type == Type.INTEGER) {
072                            response.addIntHeader(key, _intValue);
073                    }
074                    else if (_type == Type.STRING) {
075                            response.addHeader(key, _stringValue);
076                    }
077                    else {
078                            throw new IllegalStateException("Invalid type " + _type);
079                    }
080            }
081    
082            @Override
083            public boolean equals(Object obj) {
084                    if (this == obj) {
085                            return true;
086                    }
087    
088                    if (!(obj instanceof Header)) {
089                            return false;
090                    }
091    
092                    Header header = (Header)obj;
093    
094                    if (_type != header._type) {
095                            return false;
096                    }
097    
098                    if (_type == Type.COOKIE) {
099                            return _equals(_cookieValue, header._cookieValue);
100                    }
101                    else if (_type == Type.DATE) {
102                            return _dateValue == header._dateValue;
103                    }
104                    else if (_type == Type.INTEGER) {
105                            return _intValue == header._intValue;
106                    }
107                    else if (_type == Type.STRING) {
108                            return _stringValue.equals(header._stringValue);
109                    }
110                    else {
111                            throw new IllegalStateException("Invalid type " + _type);
112                    }
113            }
114    
115            @Override
116            public int hashCode() {
117                    if (_type == Type.COOKIE) {
118                            return _hashCode(_cookieValue);
119                    }
120                    else if (_type == Type.DATE) {
121                            return (int)(_dateValue ^ (_dateValue >>> 32));
122                    }
123                    else if (_type == Type.INTEGER) {
124                            return _intValue;
125                    }
126                    else if (_type == Type.STRING) {
127                            return _stringValue.hashCode();
128                    }
129                    else {
130                            throw new IllegalStateException("Invalid type " + _type);
131                    }
132            }
133    
134            public void setToResponse(String key, HttpServletResponse response) {
135                    if (_type == Type.COOKIE) {
136                            response.addCookie(_cookieValue);
137                    }
138                    else if (_type == Type.DATE) {
139                            response.setDateHeader(key, _dateValue);
140                    }
141                    else if (_type == Type.INTEGER) {
142                            response.setIntHeader(key, _intValue);
143                    }
144                    else if (_type == Type.STRING) {
145                            response.setHeader(key, _stringValue);
146                    }
147                    else {
148                            throw new IllegalStateException("Invalid type " + _type);
149                    }
150            }
151    
152            @Override
153            public String toString() {
154                    if (_type == Type.COOKIE) {
155                            return CookieUtil.toString(_cookieValue);
156                    }
157                    else if (_type == Type.DATE) {
158                            return String.valueOf(_dateValue);
159                    }
160                    else if (_type == Type.INTEGER) {
161                            return String.valueOf(_intValue);
162                    }
163                    else if (_type == Type.STRING) {
164                            return _stringValue;
165                    }
166                    else {
167                            throw new IllegalStateException("Invalid type " + _type);
168                    }
169            }
170    
171            private boolean _equals(Cookie cookie1, Cookie cookie2) {
172                    if (cookie1 == cookie2) {
173                            return true;
174                    }
175    
176                    if (!Validator.equals(cookie1.getComment(), cookie2.getComment()) ||
177                            !Validator.equals(cookie1.getDomain(), cookie2.getDomain()) ||
178                            (cookie1.getMaxAge() != cookie2.getMaxAge()) ||
179                            !Validator.equals(cookie1.getName(), cookie2.getName()) ||
180                            !Validator.equals(cookie1.getPath(), cookie2.getPath()) ||
181                            (cookie1.getSecure() != cookie2.getSecure()) ||
182                            !Validator.equals(cookie1.getValue(), cookie2.getValue()) ||
183                            (cookie1.getVersion() != cookie2.getVersion())) {
184    
185                            return false;
186                    }
187    
188                    return true;
189            }
190    
191            private int _hashCode(Cookie cookie) {
192                    int hashCode = HashUtil.hash(0, cookie.getComment());
193    
194                    hashCode = HashUtil.hash(hashCode, cookie.getDomain());
195                    hashCode = HashUtil.hash(hashCode, cookie.getMaxAge());
196                    hashCode = HashUtil.hash(hashCode, cookie.getName());
197                    hashCode = HashUtil.hash(hashCode, cookie.getPath());
198                    hashCode = HashUtil.hash(hashCode, cookie.getSecure());
199                    hashCode = HashUtil.hash(hashCode, cookie.getValue());
200                    hashCode = HashUtil.hash(hashCode, cookie.getVersion());
201    
202                    return hashCode;
203            }
204    
205            private Cookie _cookieValue;
206            private long _dateValue;
207            private int _intValue;
208            private String _stringValue;
209            private Type _type;
210    
211            private static enum Type {
212    
213                    COOKIE, DATE, INTEGER, STRING
214    
215            }
216    
217    }