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.StringPool;
020    import com.liferay.portal.kernel.util.Validator;
021    
022    import java.io.Externalizable;
023    import java.io.IOException;
024    import java.io.ObjectInput;
025    import java.io.ObjectOutput;
026    
027    import javax.servlet.http.Cookie;
028    import javax.servlet.http.HttpServletResponse;
029    
030    /**
031     * @author Michael Young
032     * @author Shuyang Zhou
033     */
034    public class Header implements Externalizable {
035    
036            /**
037             * The empty constructor is required by {@link java.io.Externalizable}. Do
038             * not use this for any other purpose.
039             */
040            public Header() {
041            }
042    
043            public Header(Cookie cookie) {
044                    if (cookie == null) {
045                            throw new IllegalArgumentException("Cookie is null");
046                    }
047    
048                    _type = Type.COOKIE;
049    
050                    _cookieValue = cookie;
051            }
052    
053            public Header(int integer) {
054                    _type = Type.INTEGER;
055    
056                    _intValue = integer;
057            }
058    
059            public Header(long date) {
060                    _type = Type.DATE;
061    
062                    _dateValue = date;
063            }
064    
065            public Header(String string) {
066                    if (string == null) {
067                            throw new IllegalArgumentException("String is null");
068                    }
069    
070                    _type = Type.STRING;
071    
072                    _stringValue = string;
073            }
074    
075            public void addToResponse(String key, HttpServletResponse response) {
076                    if (_type == Type.COOKIE) {
077                            response.addCookie(_cookieValue);
078                    }
079                    else if (_type == Type.DATE) {
080                            response.addDateHeader(key, _dateValue);
081                    }
082                    else if (_type == Type.INTEGER) {
083                            response.addIntHeader(key, _intValue);
084                    }
085                    else if (_type == Type.STRING) {
086                            response.addHeader(key, _stringValue);
087                    }
088                    else {
089                            throw new IllegalStateException("Invalid type " + _type);
090                    }
091            }
092    
093            @Override
094            public boolean equals(Object obj) {
095                    if (this == obj) {
096                            return true;
097                    }
098    
099                    if (!(obj instanceof Header)) {
100                            return false;
101                    }
102    
103                    Header header = (Header)obj;
104    
105                    if (_type != header._type) {
106                            return false;
107                    }
108    
109                    if (_type == Type.COOKIE) {
110                            return _equals(_cookieValue, header._cookieValue);
111                    }
112                    else if (_type == Type.DATE) {
113                            return _dateValue == header._dateValue;
114                    }
115                    else if (_type == Type.INTEGER) {
116                            return _intValue == header._intValue;
117                    }
118                    else if (_type == Type.STRING) {
119                            return _stringValue.equals(header._stringValue);
120                    }
121                    else {
122                            throw new IllegalStateException("Invalid type " + _type);
123                    }
124            }
125    
126            @Override
127            public int hashCode() {
128                    if (_type == Type.COOKIE) {
129                            return _hashCode(_cookieValue);
130                    }
131                    else if (_type == Type.DATE) {
132                            return (int)(_dateValue ^ (_dateValue >>> 32));
133                    }
134                    else if (_type == Type.INTEGER) {
135                            return _intValue;
136                    }
137                    else if (_type == Type.STRING) {
138                            return _stringValue.hashCode();
139                    }
140                    else {
141                            throw new IllegalStateException("Invalid type " + _type);
142                    }
143            }
144    
145            @Override
146            public void readExternal(ObjectInput objectInput) throws IOException {
147                    if (objectInput.readBoolean()) {
148                            int size = objectInput.readInt();
149    
150                            byte[] data = new byte[size];
151    
152                            objectInput.readFully(data);
153    
154                            _cookieValue = CookieUtil.deserialize(data);
155                    }
156    
157                    _dateValue = objectInput.readLong();
158                    _intValue = objectInput.readInt();
159    
160                    String stringValue = objectInput.readUTF();
161    
162                    if (!stringValue.isEmpty()) {
163                            _stringValue = stringValue;
164                    }
165    
166                    _type = Type.values()[objectInput.readInt()];
167            }
168    
169            public void setToResponse(String key, HttpServletResponse response) {
170                    if (_type == Type.COOKIE) {
171                            response.addCookie(_cookieValue);
172                    }
173                    else if (_type == Type.DATE) {
174                            response.setDateHeader(key, _dateValue);
175                    }
176                    else if (_type == Type.INTEGER) {
177                            response.setIntHeader(key, _intValue);
178                    }
179                    else if (_type == Type.STRING) {
180                            response.setHeader(key, _stringValue);
181                    }
182                    else {
183                            throw new IllegalStateException("Invalid type " + _type);
184                    }
185            }
186    
187            @Override
188            public String toString() {
189                    if (_type == Type.COOKIE) {
190                            return CookieUtil.toString(_cookieValue);
191                    }
192                    else if (_type == Type.DATE) {
193                            return String.valueOf(_dateValue);
194                    }
195                    else if (_type == Type.INTEGER) {
196                            return String.valueOf(_intValue);
197                    }
198                    else if (_type == Type.STRING) {
199                            return _stringValue;
200                    }
201                    else {
202                            throw new IllegalStateException("Invalid type " + _type);
203                    }
204            }
205    
206            @Override
207            public void writeExternal(ObjectOutput objectOutput) throws IOException {
208                    if (_cookieValue == null) {
209                            objectOutput.writeBoolean(false);
210                    }
211                    else {
212                            objectOutput.writeBoolean(true);
213    
214                            byte[] data = CookieUtil.serialize(_cookieValue);
215    
216                            objectOutput.writeInt(data.length);
217                            objectOutput.write(data);
218                    }
219    
220                    objectOutput.writeLong(_dateValue);
221                    objectOutput.writeInt(_intValue);
222    
223                    if (_stringValue == null) {
224                            objectOutput.writeUTF(StringPool.BLANK);
225                    }
226                    else {
227                            objectOutput.writeUTF(_stringValue);
228                    }
229    
230                    objectOutput.writeInt(_type.ordinal());
231            }
232    
233            private boolean _equals(Cookie cookie1, Cookie cookie2) {
234                    if (cookie1 == cookie2) {
235                            return true;
236                    }
237    
238                    if (!Validator.equals(cookie1.getComment(), cookie2.getComment()) ||
239                            !Validator.equals(cookie1.getDomain(), cookie2.getDomain()) ||
240                            (cookie1.getMaxAge() != cookie2.getMaxAge()) ||
241                            !Validator.equals(cookie1.getName(), cookie2.getName()) ||
242                            !Validator.equals(cookie1.getPath(), cookie2.getPath()) ||
243                            (cookie1.getSecure() != cookie2.getSecure()) ||
244                            !Validator.equals(cookie1.getValue(), cookie2.getValue()) ||
245                            (cookie1.getVersion() != cookie2.getVersion())) {
246    
247                            return false;
248                    }
249    
250                    return true;
251            }
252    
253            private int _hashCode(Cookie cookie) {
254                    int hashCode = HashUtil.hash(0, cookie.getComment());
255    
256                    hashCode = HashUtil.hash(hashCode, cookie.getDomain());
257                    hashCode = HashUtil.hash(hashCode, cookie.getMaxAge());
258                    hashCode = HashUtil.hash(hashCode, cookie.getName());
259                    hashCode = HashUtil.hash(hashCode, cookie.getPath());
260                    hashCode = HashUtil.hash(hashCode, cookie.getSecure());
261                    hashCode = HashUtil.hash(hashCode, cookie.getValue());
262                    hashCode = HashUtil.hash(hashCode, cookie.getVersion());
263    
264                    return hashCode;
265            }
266    
267            private Cookie _cookieValue;
268            private long _dateValue;
269            private int _intValue;
270            private String _stringValue;
271            private Type _type;
272    
273            private static enum Type {
274    
275                    COOKIE, DATE, INTEGER, STRING
276    
277            }
278    
279    }