]> WPIA git - gigi.git/blob - lib/jetty/org/eclipse/jetty/http/HttpCookie.java
Merge "Update notes about password security"
[gigi.git] / lib / jetty / org / eclipse / jetty / http / HttpCookie.java
1 //
2 //  ========================================================================
3 //  Copyright (c) 1995-2016 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.
8 //
9 //      The Eclipse Public License is available at
10 //      http://www.eclipse.org/legal/epl-v10.html
11 //
12 //      The Apache License v2.0 is available at
13 //      http://www.opensource.org/licenses/apache2.0.php
14 //
15 //  You may elect to redistribute this code under either of these licenses.
16 //  ========================================================================
17 //
18
19 package org.eclipse.jetty.http;
20
21 import java.util.concurrent.TimeUnit;
22
23 public class HttpCookie
24 {
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;
35
36     public HttpCookie(String name, String value)
37     {
38         this(name, value, -1);
39     }
40
41     public HttpCookie(String name, String value, String domain, String path)
42     {
43         this(name, value, domain, path, -1, false, false);
44     }
45
46     public HttpCookie(String name, String value, long maxAge)
47     {
48         this(name, value, null, null, maxAge, false, false);
49     }
50
51     public HttpCookie(String name, String value, String domain, String path, long maxAge, boolean httpOnly, boolean secure)
52     {
53         this(name, value, domain, path, maxAge, httpOnly, secure, null, 0);
54     }
55
56     public HttpCookie(String name, String value, String domain, String path, long maxAge, boolean httpOnly, boolean secure, String comment, int version)
57     {
58         _name = name;
59         _value = value;
60         _domain = domain;
61         _path = path;
62         _maxAge = maxAge;
63         _httpOnly = httpOnly;
64         _secure = secure;
65         _comment = comment;
66         _version = version;
67         _expiration = maxAge < 0 ? -1 : System.nanoTime() + TimeUnit.SECONDS.toNanos(maxAge);
68     }
69
70     /**
71      * @return the cookie name
72      */
73     public String getName()
74     {
75         return _name;
76     }
77
78     /**
79      * @return the cookie value
80      */
81     public String getValue()
82     {
83         return _value;
84     }
85
86     /**
87      * @return the cookie comment
88      */
89     public String getComment()
90     {
91         return _comment;
92     }
93
94     /**
95      * @return the cookie domain
96      */
97     public String getDomain()
98     {
99         return _domain;
100     }
101
102     /**
103      * @return the cookie max age in seconds
104      */
105     public long getMaxAge()
106     {
107         return _maxAge;
108     }
109
110     /**
111      * @return the cookie path
112      */
113     public String getPath()
114     {
115         return _path;
116     }
117
118     /**
119      * @return whether the cookie is valid for secure domains
120      */
121     public boolean isSecure()
122     {
123         return _secure;
124     }
125
126     /**
127      * @return the cookie version
128      */
129     public int getVersion()
130     {
131         return _version;
132     }
133
134     /**
135      * @return whether the cookie is valid for the http protocol only
136      */
137     public boolean isHttpOnly()
138     {
139         return _httpOnly;
140     }
141
142     /**
143      * @param timeNanos the time to check for cookie expiration, in nanoseconds
144      * @return whether the cookie is expired by the given time
145      */
146     public boolean isExpired(long timeNanos)
147     {
148         return _expiration >= 0 && timeNanos >= _expiration;
149     }
150
151     /**
152      * @return a string representation of this cookie
153      */
154     public String asString()
155     {
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();
163     }
164 }