2 // ========================================================================
3 // Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
4 // ------------------------------------------------------------------------
5 // All rights reserved. This program and the accompanying materials
6 // are made available under the terms of the Eclipse Public License v1.0
7 // and Apache License v2.0 which accompanies this distribution.
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
19 package org.eclipse.jetty.http;
21 import java.util.concurrent.TimeUnit;
23 public class HttpCookie
25 private final String _name;
26 private final String _value;
27 private final String _comment;
28 private final String _domain;
29 private final long _maxAge;
30 private final String _path;
31 private final boolean _secure;
32 private final int _version;
33 private final boolean _httpOnly;
34 private final long _expiration;
36 public HttpCookie(String name, String value)
38 this(name, value, -1);
41 public HttpCookie(String name, String value, String domain, String path)
43 this(name, value, domain, path, -1, false, false);
46 public HttpCookie(String name, String value, long maxAge)
48 this(name, value, null, null, maxAge, false, false);
51 public HttpCookie(String name, String value, String domain, String path, long maxAge, boolean httpOnly, boolean secure)
53 this(name, value, domain, path, maxAge, httpOnly, secure, null, 0);
56 public HttpCookie(String name, String value, String domain, String path, long maxAge, boolean httpOnly, boolean secure, String comment, int version)
67 _expiration = maxAge < 0 ? -1 : System.nanoTime() + TimeUnit.SECONDS.toNanos(maxAge);
71 * @return the cookie name
73 public String getName()
79 * @return the cookie value
81 public String getValue()
87 * @return the cookie comment
89 public String getComment()
95 * @return the cookie domain
97 public String getDomain()
103 * @return the cookie max age in seconds
105 public long getMaxAge()
111 * @return the cookie path
113 public String getPath()
119 * @return whether the cookie is valid for secure domains
121 public boolean isSecure()
127 * @return the cookie version
129 public int getVersion()
135 * @return whether the cookie is valid for the http protocol only
137 public boolean isHttpOnly()
143 * @param timeNanos the time to check for cookie expiration, in nanoseconds
144 * @return whether the cookie is expired by the given time
146 public boolean isExpired(long timeNanos)
148 return _expiration >= 0 && timeNanos >= _expiration;
152 * @return a string representation of this cookie
154 public String asString()
156 StringBuilder builder = new StringBuilder();
157 builder.append(getName()).append("=").append(getValue());
158 if (getDomain() != null)
159 builder.append(";$Domain=").append(getDomain());
160 if (getPath() != null)
161 builder.append(";$Path=").append(getPath());
162 return builder.toString();