001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.jmeter.protocol.http.control;
020:
021: import java.io.Serializable;
022:
023: import org.apache.jmeter.config.ConfigElement;
024: import org.apache.jmeter.testelement.AbstractTestElement;
025: import org.apache.jmeter.testelement.property.BooleanProperty;
026: import org.apache.jmeter.testelement.property.LongProperty;
027: import org.apache.jorphan.util.JOrphanUtils;
028:
029: /**
030: * This class is a Cookie encapsulator.
031: *
032: * @author <a href="mailto:sdowd@arcmail.com">Sean Dowd</a>
033: */
034: public class Cookie extends AbstractTestElement implements Serializable {
035: private static final String TAB = "\t";
036:
037: private static String VALUE = "Cookie.value"; //$NON-NLS-1$
038:
039: private static String DOMAIN = "Cookie.domain"; //$NON-NLS-1$
040:
041: private static String EXPIRES = "Cookie.expires"; //$NON-NLS-1$
042:
043: private static String SECURE = "Cookie.secure"; //$NON-NLS-1$
044:
045: private static String PATH = "Cookie.path"; //$NON-NLS-1$
046:
047: private static String PATH_SPECIFIED = "Cookie.path_specified"; //$NON-NLS-1$
048:
049: private static String DOMAIN_SPECIFIED = "Cookie.domain_specified"; //$NON-NLS-1$
050:
051: /**
052: * create the coookie
053: */
054: public Cookie() {
055: this ("", "", "", "", false, 0, false, false);
056: }
057:
058: /**
059: * create the coookie
060: *
061: * @param expires - this is in seconds
062: *
063: */
064: public Cookie(String name, String value, String domain,
065: String path, boolean secure, long expires) {
066: this (name, value, domain, path, secure, expires, true, true);
067: }
068:
069: /**
070: * create the coookie
071: *
072: * @param expires - this is in seconds
073: * @param hasPath - was the path explicitly specified?
074: * @param hasDomain - was the domain explicitly specified?
075: *
076: */
077: public Cookie(String name, String value, String domain,
078: String path, boolean secure, long expires, boolean hasPath,
079: boolean hasDomain) {
080: this .setName(name);
081: this .setValue(value);
082: this .setDomain(domain);
083: this .setPath(path);
084: this .setSecure(secure);
085: this .setExpires(expires);
086: this .setPathSpecified(hasPath);
087: this .setDomainSpecified(hasDomain);
088: }
089:
090: public void addConfigElement(ConfigElement config) {
091: }
092:
093: /**
094: * get the value for this object.
095: */
096: public synchronized String getValue() {
097: return getPropertyAsString(VALUE);
098: }
099:
100: /**
101: * set the value for this object.
102: */
103: public synchronized void setValue(String value) {
104: this .setProperty(VALUE, value);
105: }
106:
107: /**
108: * get the domain for this object.
109: */
110: public synchronized String getDomain() {
111: return getPropertyAsString(DOMAIN);
112: }
113:
114: /**
115: * set the domain for this object.
116: */
117: public synchronized void setDomain(String domain) {
118: setProperty(DOMAIN, domain);
119: }
120:
121: /**
122: * get the expiry time for the cookie
123: *
124: * @return Expiry time in seconds since the Java epoch
125: */
126: public synchronized long getExpires() {
127: return getPropertyAsLong(EXPIRES);
128: }
129:
130: /**
131: * get the expiry time for the cookie
132: *
133: * @return Expiry time in milli-seconds since the Java epoch,
134: * i.e. same as System.currentTimeMillis()
135: */
136: public synchronized long getExpiresMillis() {
137: return getPropertyAsLong(EXPIRES) * 1000;
138: }
139:
140: /**
141: * set the expiry time for the cookie
142: * @param expires - expiry time in seconds since the Java epoch
143: */
144: public synchronized void setExpires(long expires) {
145: setProperty(new LongProperty(EXPIRES, expires));
146: }
147:
148: /**
149: * get the secure for this object.
150: */
151: public synchronized boolean getSecure() {
152: return getPropertyAsBoolean(SECURE);
153: }
154:
155: /**
156: * set the secure for this object.
157: */
158: public synchronized void setSecure(boolean secure) {
159: setProperty(new BooleanProperty(SECURE, secure));
160: }
161:
162: /**
163: * get the path for this object.
164: */
165: public synchronized String getPath() {
166: return getPropertyAsString(PATH);
167: }
168:
169: /**
170: * set the path for this object.
171: */
172: public synchronized void setPath(String path) {
173: setProperty(PATH, path);
174: }
175:
176: public void setPathSpecified(boolean b) {
177: setProperty(PATH_SPECIFIED, b);
178: }
179:
180: public boolean isPathSpecified() {
181: return getPropertyAsBoolean(PATH_SPECIFIED);
182: }
183:
184: public void setDomainSpecified(boolean b) {
185: setProperty(DOMAIN_SPECIFIED, b);
186: }
187:
188: public boolean isDomainSpecified() {
189: return getPropertyAsBoolean(DOMAIN_SPECIFIED);
190: }
191:
192: /**
193: * creates a string representation of this cookie
194: */
195: public String toString() {
196: StringBuffer sb = new StringBuffer(80);
197: sb.append(getDomain());
198: // flag - if all machines within a given domain can access the variable.
199: //(from http://www.cookiecentral.com/faq/ 3.5)
200: sb.append(TAB).append("TRUE");
201: sb.append(TAB).append(getPath());
202: sb.append(TAB)
203: .append(JOrphanUtils.booleanToSTRING(getSecure()));
204: sb.append(TAB).append(getExpires());
205: sb.append(TAB).append(getName());
206: sb.append(TAB).append(getValue());
207: return sb.toString();
208: }
209:
210: }
|