📄 cookiemanager.java
字号:
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
// For unit tests @see TestCookieManager
package org.apache.jmeter.protocol.http.control;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Serializable;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.cookie.CookieSpec;
import org.apache.commons.httpclient.cookie.MalformedCookieException;
import org.apache.jmeter.config.ConfigTestElement;
import org.apache.jmeter.engine.event.LoopIterationEvent;
import org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase;
import org.apache.jmeter.testelement.TestListener;
import org.apache.jmeter.testelement.property.BooleanProperty;
import org.apache.jmeter.testelement.property.CollectionProperty;
import org.apache.jmeter.testelement.property.PropertyIterator;
import org.apache.jmeter.threads.JMeterContext;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jorphan.logging.LoggingManager;
import org.apache.jorphan.util.JOrphanUtils;
import org.apache.log.Logger;
/**
* This class provides an interface to the netscape cookies file to pass cookies
* along with a request.
*
* Now uses Commons HttpClient parsing and matching code (since 2.1.2)
*
*/
public class CookieManager extends ConfigTestElement implements TestListener, Serializable {
private static final Logger log = LoggingManager.getLoggerForClass();
public static final String CLEAR = "CookieManager.clearEachIteration";// $NON-NLS-1$
public static final String COOKIES = "CookieManager.cookies";// $NON-NLS-1$
public static final String POLICY = "CookieManager.policy"; //$NON-NLS-1$
private static final String TAB = "\t"; //$NON-NLS-1$
// See bug 33796
private static final boolean DELETE_NULL_COOKIES
= JMeterUtils.getPropDefault("CookieManager.delete_null_cookies", true);// $NON-NLS-1$
// See bug 28715
private static final boolean ALLOW_VARIABLE_COOKIES
= JMeterUtils.getPropDefault("CookieManager.allow_variable_cookies", true);// $NON-NLS-1$
private transient CookieSpec cookieSpec;
private transient CollectionProperty initialCookies;
public static final String DEFAULT_POLICY = CookiePolicy.BROWSER_COMPATIBILITY;
public CookieManager() {
setProperty(new CollectionProperty(COOKIES, new ArrayList()));
setProperty(new BooleanProperty(CLEAR, false));
setCookiePolicy(DEFAULT_POLICY);
}
// ensure that the initial cookies are copied to the per-thread instances
public Object clone(){
CookieManager clone = (CookieManager) super.clone();
clone.initialCookies = initialCookies;
return clone;
}
public String getPolicy() {
return getPropertyAsString(POLICY,DEFAULT_POLICY);
}
public void setCookiePolicy(String policy){
cookieSpec = CookiePolicy.getCookieSpec(policy);
if (!DEFAULT_POLICY.equals(policy)){// Don't clutter the JMX file
setProperty(POLICY,policy);
}
}
public CollectionProperty getCookies() {
return (CollectionProperty) getProperty(COOKIES);
}
public int getCookieCount() {// Used by GUI
return getCookies().size();
}
public boolean getClearEachIteration() {
return getPropertyAsBoolean(CLEAR);
}
public void setClearEachIteration(boolean clear) {
setProperty(new BooleanProperty(CLEAR, clear));
}
/**
* Save the static cookie data to a file.
* Cookies are only taken from the GUI - runtime cookies are not included.
*/
public void save(String authFile) throws IOException {
File file = new File(authFile);
if (!file.isAbsolute())
file = new File(System.getProperty("user.dir") // $NON-NLS-1$
+ File.separator + authFile);
PrintWriter writer = new PrintWriter(new FileWriter(file));
writer.println("# JMeter generated Cookie file");// $NON-NLS-1$
PropertyIterator cookies = getCookies().iterator();
long now = System.currentTimeMillis();
while (cookies.hasNext()) {
Cookie cook = (Cookie) cookies.next().getObjectValue();
final long expiresMillis = cook.getExpiresMillis();
if (expiresMillis == 0 || expiresMillis > now) { // only save unexpired cookies
writer.println(cookieToString(cook));
}
}
writer.flush();
writer.close();
}
/**
* Add cookie data from a file.
*/
public void addFile(String cookieFile) throws IOException {
File file = new File(cookieFile);
if (!file.isAbsolute())
file = new File(System.getProperty("user.dir") // $NON-NLS-1$
+ File.separator + cookieFile);
BufferedReader reader = null;
if (file.canRead()) {
reader = new BufferedReader(new FileReader(file));
} else {
throw new IOException("The file you specified cannot be read.");
}
// N.B. this must agree with the save() and cookieToString() methods
String line;
try {
final CollectionProperty cookies = getCookies();
while ((line = reader.readLine()) != null) {
try {
if (line.startsWith("#") || line.trim().length() == 0)//$NON-NLS-1$
continue;
String[] st = JOrphanUtils.split(line, TAB, false);
final int _domain = 0;
//final int _ignored = 1;
final int _path = 2;
final int _secure = 3;
final int _expires = 4;
final int _name = 5;
final int _value = 6;
final int _fields = 7;
if (st.length!=_fields) {
throw new IOException("Expected "+_fields+" fields, found "+st.length+" in "+line);
}
if (st[_path].length()==0)
st[_path] = "/"; //$NON-NLS-1$
boolean secure = Boolean.valueOf(st[_secure]).booleanValue();
long expires = new Long(st[_expires]).longValue();
if (expires==Long.MAX_VALUE) expires=0;
//long max was used to represent a non-expiring cookie, but that caused problems
Cookie cookie = new Cookie(st[_name], st[_value], st[_domain], st[_path], secure, expires);
cookies.addItem(cookie);
} catch (NumberFormatException e) {
throw new IOException("Error parsing cookie line\n\t'" + line + "'\n\t" + e);
}
}
} finally {
reader.close();
}
}
private String cookieToString(Cookie c){
StringBuffer sb=new StringBuffer(80);
sb.append(c.getDomain());
//flag - if all machines within a given domain can access the variable.
//(from http://www.cookiecentral.com/faq/ 3.5)
sb.append(TAB).append("TRUE");
sb.append(TAB).append(c.getPath());
sb.append(TAB).append(JOrphanUtils.booleanToSTRING(c.getSecure()));
sb.append(TAB).append(c.getExpires());
sb.append(TAB).append(c.getName());
sb.append(TAB).append(c.getValue());
return sb.toString();
}
public void recoverRunningVersion() {
// do nothing, the cookie manager has to accept changes.
}
public void setRunningVersion(boolean running) {
// do nothing, the cookie manager has to accept changes.
}
/**
* Add a cookie.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -