1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 */ 18 19 package org.apache.jmeter.protocol.http.control; 20 21 import java.io.Serializable; 22 23 import org.apache.jmeter.config.ConfigElement; 24 import org.apache.jmeter.testelement.AbstractTestElement; 25 import org.apache.jmeter.testelement.property.BooleanProperty; 26 import org.apache.jmeter.testelement.property.LongProperty; 27 import org.apache.jorphan.util.JOrphanUtils; 28 29 /** 30 * This class is a Cookie encapsulator. 31 * 32 */ 33 public class Cookie extends AbstractTestElement implements Serializable { 34 private static final String TAB = "\t"; 35 36 private static final String VALUE = "Cookie.value"; //$NON-NLS-1$ 37 38 private static final String DOMAIN = "Cookie.domain"; //$NON-NLS-1$ 39 40 private static final String EXPIRES = "Cookie.expires"; //$NON-NLS-1$ 41 42 private static final String SECURE = "Cookie.secure"; //$NON-NLS-1$ 43 44 private static final String PATH = "Cookie.path"; //$NON-NLS-1$ 45 46 private static final String PATH_SPECIFIED = "Cookie.path_specified"; //$NON-NLS-1$ 47 48 private static final String DOMAIN_SPECIFIED = "Cookie.domain_specified"; //$NON-NLS-1$ 49 50 private static final String VERSION = "Cookie.version"; //$NON-NLS-1$ 51 52 private static final int DEFAULT_VERSION = 1; 53 54 /** 55 * create the coookie 56 */ 57 public Cookie() { 58 this("","","","",false,0,false,false); 59 } 60 61 /** 62 * create the coookie 63 * 64 * @param expires - this is in seconds 65 * 66 */ 67 public Cookie(String name, String value, String domain, String path, boolean secure, long expires) { 68 this(name,value,domain,path,secure,expires,true,true); 69 } 70 71 /** 72 * create the coookie 73 * 74 * @param expires - this is in seconds 75 * @param hasPath - was the path explicitly specified? 76 * @param hasDomain - was the domain explicitly specified? 77 * 78 */ 79 public Cookie(String name, String value, String domain, String path, 80 boolean secure, long expires, boolean hasPath, boolean hasDomain) { 81 this(name, value, domain, path, secure, expires, hasPath, hasDomain, DEFAULT_VERSION); 82 } 83 84 /** 85 * Create a JMeter Cookie. 86 * 87 * @param name 88 * @param value 89 * @param domain 90 * @param path 91 * @param secure 92 * @param expires - this is in seconds 93 * @param hasPath - was the path explicitly specified? 94 * @param hasDomain - was the domain explicitly specified? 95 * @param version - cookie spec. version 96 */ 97 public Cookie(String name, String value, String domain, String path, 98 boolean secure, long expires, boolean hasPath, boolean hasDomain, int version) { 99 this.setName(name); 100 this.setValue(value); 101 this.setDomain(domain); 102 this.setPath(path); 103 this.setSecure(secure); 104 this.setExpires(expires); 105 this.setPathSpecified(hasPath); 106 this.setDomainSpecified(hasDomain); 107 this.setVersion(version); 108 } 109 110 public void addConfigElement(ConfigElement config) { 111 } 112 113 /** 114 * get the value for this object. 115 */ 116 public synchronized String getValue() { 117 return getPropertyAsString(VALUE); 118 } 119 120 /** 121 * set the value for this object. 122 */ 123 public synchronized void setValue(String value) { 124 this.setProperty(VALUE, value); 125 } 126 127 /** 128 * get the domain for this object. 129 */ 130 public synchronized String getDomain() { 131 return getPropertyAsString(DOMAIN); 132 } 133 134 /** 135 * set the domain for this object. 136 */ 137 public synchronized void setDomain(String domain) { 138 setProperty(DOMAIN, domain); 139 } 140 141 /** 142 * get the expiry time for the cookie 143 * 144 * @return Expiry time in seconds since the Java epoch 145 */ 146 public synchronized long getExpires() { 147 return getPropertyAsLong(EXPIRES); 148 } 149 150 /** 151 * get the expiry time for the cookie 152 * 153 * @return Expiry time in milli-seconds since the Java epoch, 154 * i.e. same as System.currentTimeMillis() 155 */ 156 public synchronized long getExpiresMillis() { 157 return getPropertyAsLong(EXPIRES)*1000; 158 } 159 160 /** 161 * set the expiry time for the cookie 162 * @param expires - expiry time in seconds since the Java epoch 163 */ 164 public synchronized void setExpires(long expires) { 165 setProperty(new LongProperty(EXPIRES, expires)); 166 } 167 168 /** 169 * get the secure for this object. 170 */ 171 public synchronized boolean getSecure() { 172 return getPropertyAsBoolean(SECURE); 173 } 174 175 /** 176 * set the secure for this object. 177 */ 178 public synchronized void setSecure(boolean secure) { 179 setProperty(new BooleanProperty(SECURE, secure)); 180 } 181 182 /** 183 * get the path for this object. 184 */ 185 public synchronized String getPath() { 186 return getPropertyAsString(PATH); 187 } 188 189 /** 190 * set the path for this object. 191 */ 192 public synchronized void setPath(String path) { 193 setProperty(PATH, path); 194 } 195 196 public void setPathSpecified(boolean b) { 197 setProperty(PATH_SPECIFIED, b); 198 } 199 200 public boolean isPathSpecified(){ 201 return getPropertyAsBoolean(PATH_SPECIFIED); 202 } 203 204 public void setDomainSpecified(boolean b) { 205 setProperty(DOMAIN_SPECIFIED, b); 206 } 207 208 public boolean isDomainSpecified(){ 209 return getPropertyAsBoolean(DOMAIN_SPECIFIED); 210 } 211 212 /** 213 * creates a string representation of this cookie 214 */ 215 public String toString() { 216 StringBuffer sb=new StringBuffer(80); 217 sb.append(getDomain()); 218 // flag - if all machines within a given domain can access the variable. 219 //(from http://www.cookiecentral.com/faq/ 3.5) 220 sb.append(TAB).append("TRUE"); 221 sb.append(TAB).append(getPath()); 222 sb.append(TAB).append(JOrphanUtils.booleanToSTRING(getSecure())); 223 sb.append(TAB).append(getExpires()); 224 sb.append(TAB).append(getName()); 225 sb.append(TAB).append(getValue()); 226 return sb.toString(); 227 } 228 229 /** 230 * @return the version 231 */ 232 public synchronized int getVersion() { 233 return getPropertyAsInt(VERSION, DEFAULT_VERSION); 234 } 235 236 /** 237 * @param version the version to set 238 */ 239 public synchronized void setVersion(int version) { 240 setProperty(VERSION, version, DEFAULT_VERSION); 241 } 242 243 244 }