001
014
015 package com.liferay.portal.kernel.servlet;
016
017 import com.liferay.portal.kernel.util.HashUtil;
018 import com.liferay.portal.kernel.util.StringBundler;
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
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 (obj == null) {
085 return false;
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 StringBundler sb = new StringBundler(17);
156
157 sb.append("{comment=");
158 sb.append(_cookieValue.getComment());
159 sb.append(", domain=");
160 sb.append(_cookieValue.getDomain());
161 sb.append(", maxAge=");
162 sb.append(_cookieValue.getMaxAge());
163 sb.append(", name=");
164 sb.append(_cookieValue.getName());
165 sb.append(", path=");
166 sb.append(_cookieValue.getPath());
167 sb.append(", secure=");
168 sb.append(_cookieValue.getSecure());
169 sb.append(", value=");
170 sb.append(_cookieValue.getValue());
171 sb.append(", version=");
172 sb.append(_cookieValue.getVersion());
173 sb.append("}");
174
175 return sb.toString();
176 }
177 else if (_type == Type.DATE) {
178 return String.valueOf(_dateValue);
179 }
180 else if (_type == Type.INTEGER) {
181 return String.valueOf(_intValue);
182 }
183 else if (_type == Type.STRING) {
184 return _stringValue;
185 }
186 else {
187 throw new IllegalStateException("Invalid type " + _type);
188 }
189 }
190
191 private boolean _equals(Cookie cookie1, Cookie cookie2) {
192 if (cookie1 == cookie2) {
193 return true;
194 }
195
196 if (!Validator.equals(cookie1.getComment(), cookie2.getComment()) ||
197 !Validator.equals(cookie1.getDomain(), cookie2.getDomain()) ||
198 (cookie1.getMaxAge() != cookie2.getMaxAge()) ||
199 !Validator.equals(cookie1.getName(), cookie2.getName()) ||
200 !Validator.equals(cookie1.getPath(), cookie2.getPath()) ||
201 (cookie1.getSecure() != cookie2.getSecure()) ||
202 !Validator.equals(cookie1.getValue(), cookie2.getValue()) ||
203 (cookie1.getVersion() != cookie2.getVersion())) {
204
205 return false;
206 }
207
208 return true;
209 }
210
211 private int _hashCode(Cookie cookie) {
212 int hashCode = HashUtil.hash(0, cookie.getComment());
213
214 hashCode = HashUtil.hash(hashCode, cookie.getDomain());
215 hashCode = HashUtil.hash(hashCode, cookie.getMaxAge());
216 hashCode = HashUtil.hash(hashCode, cookie.getName());
217 hashCode = HashUtil.hash(hashCode, cookie.getPath());
218 hashCode = HashUtil.hash(hashCode, cookie.getSecure());
219 hashCode = HashUtil.hash(hashCode, cookie.getValue());
220 hashCode = HashUtil.hash(hashCode, cookie.getVersion());
221
222 return hashCode;
223 }
224
225 private Cookie _cookieValue;
226 private long _dateValue;
227 private int _intValue;
228 private String _stringValue;
229 private Type _type;
230
231 private static enum Type {
232
233 COOKIE, DATE, INTEGER, STRING
234
235 }
236
237 }