bloritsch 01/11/13 08:54:19
Modified: src/org/apache/jmeter/protocol/http/control
CookieManager.java
Log:
small resource optimization with date objects
Revision Changes Path
1.16 +382 -382
jakarta-jmeter/src/org/apache/jmeter/protocol/http/control/CookieManager.java
Index: CookieManager.java
===================================================================
RCS file:
/home/cvs/jakarta-jmeter/src/org/apache/jmeter/protocol/http/control/CookieManager.java,v
retrieving revision 1.15
retrieving revision 1.16
diff -u -r1.15 -r1.16
--- CookieManager.java 2001/11/09 18:26:35 1.15
+++ CookieManager.java 2001/11/13 16:54:19 1.16
@@ -73,390 +73,390 @@
* pass cookies along with a request.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Sean Dowd</a>
- * @version $Revision: 1.15 $ $Date: 2001/11/09 18:26:35 $
+ * @version $Revision: 1.16 $ $Date: 2001/11/13 16:54:19 $
*/
public class CookieManager implements ConfigElement,JMeterComponentModel,Saveable,
- Serializable
+ Serializable
{
- public static final String COOKIES = "cookies";
- /**
- * A vector of Cookies managed by this class.
- * @associates <{org.apache.jmeter.controllers.Cookie}>
- */
- private List cookies = new ArrayList();
- private String name;
- private static SimpleDateFormat dateFormat = new SimpleDateFormat("EEEE,
dd-MMM-yyyy HH:mm:ss zzz");
-
- private final static int columnCount = 6;
- private final static String[] columnNames =
{"Name","Value","Domain","Path","Secure","Expiration"};
-
- private static List addableList = new LinkedList();
-
- static
- {
- // The cookie specification requires that the timezone be GMT.
- // See
http://developer.netscape.com/docs/manuals/communicator/jsguide4/cookies.htm
- // See http://www.cookiecentral.com/faq/
- dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
- }
-
- public CookieManager () {
- }
-
- public Class getTagHandlerClass()
- {
- return org.apache.jmeter.protocol.http.save.CookieManagerHandler.class;
- }
-
- public void uncompile()
- {
- }
-
- public Object clone()
- {
- CookieManager ck = new CookieManager();
- for(int x = 0;x < cookies.size();x++)
- ck.add((Cookie)cookies.get(x));
- ck.setName(this.name);
- return ck;
- }
-
- public boolean expectsModification()
- {
- return true;
- }
-
- public Collection getAddList()
- {
- return addableList;
- }
-
- public void addJMeterComponent(JMeterComponentModel child)
- {
- }
-
- public boolean isEditable()
- {
- return true;
- }
-
- public Class getGuiClass()
- {
- return org.apache.jmeter.protocol.http.gui.CookiePanel.class;
- }
-
- public void setName(String name)
- {
- this.name = name;
- }
-
- public String getName()
- {
- return this.name;
- }
-
- /************************************************************
- * This allows config elements to combine and give a "layered" effect. for
- * example, say there are two HTTPConfigElements, which have properties for
- * domain, path, method, and parameters. If element A has everything filled
- * in, but null for domain, and element B is added, which has only domain
- * filled in, then after adding B to A, A will have the domain from B. If A
- * already had a domain, then the correct behavior is for A to ignore the
- * addition of element B.
- *
- *@param config !ToDo
- ***********************************************************/
- public void addConfigElement(ConfigElement config)
- {
- if (config instanceof CookieManager)
- {
- this.cookies.addAll(((CookieManager)config).getCookies());
- }
- }
-
- public Collection getCookies()
- {
- return cookies;
- }
-
- public int getCookieCount()
- {
- return cookies.size();
- }
-
- public int getColumnCount()
- {
- return columnCount;
- }
-
- public String getColumnName(int column)
- {
- return columnNames[column];
- }
-
- // Incorrect method. Always returns String. I changed CookiePanel code to
perform
- // this lookup.
- //public Class getColumnClass(int column)
- //{
- // return columnNames[column].getClass();
- //}
-
- public Cookie getCookie(int row)
- {
- return (Cookie)cookies.get(row);
- }
-
- /** save the cookie data to a file */
- public void save(String authFile) throws IOException {
- File file = new File(authFile);
- if (!file.isAbsolute()) file = new
File(System.getProperty("user.dir") + File.separator + authFile);
- PrintWriter writer = new PrintWriter(new FileWriter(file));
- writer.println("# JMeter generated Cookie file");
- for (int i = 0; i < cookies.size(); i++) {
- Cookie cook = (Cookie) cookies.get(i);
- writer.println(cook.toString());
- }
- 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") + 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.");
- }
-
- String line;
- while ((line = reader.readLine()) != null) {
- try {
- if (line.startsWith("#") ||
line.trim().length() == 0) continue;
- String[] st = split(line, "\t"," ");
- int domain = 0;
- int foo = 1;
- int path = 2;
- if(st[path].equals(" "))
- st[path] = "/";
- boolean secure = new
Boolean(st[3]).booleanValue();
- long expires = new Long(st[4]).longValue();
- int name = 5;
- int value = 6;
- Cookie cookie = new Cookie(st[name],
st[value], st[domain],
- st[path], secure,
expires);
- cookies.add(cookie);
- } catch (Exception e) {
- throw new IOException("Error parsing cookie
line\n\t'" + line + "'\n\t" + e);
- }
- }
- reader.close();
- }
-
- /** add a cookie */
- public void add(Cookie c) {
- cookies.add(c);
- }
-
- /** add an empty cookie */
- public void add() {
- cookies.add(new Cookie());
- }
-
- /** remove a cookie */
- public void remove(int index) {
- cookies.remove(index);
- }
-
- /** return the number cookies */
- public int size() {
- return cookies.size();
- }
-
- /** return the cookie at index i */
- public Cookie get(int i) {
- return (Cookie) cookies.get(i);
- }
-
- public String convertLongToDateFormatStr(long dateLong)
- {
- return dateFormat.format(new Date(dateLong));
- }
-
- public long convertDateFormatStrToLong(String dateStr)
- {
- long time = 0;
-
- try
- {
- Date date = dateFormat.parse(dateStr);
- time = date.getTime();
- }
- catch (ParseException e)
- {
- // ERROR!!!
- // Later, display error dialog? For now, we have
- // to specify a number that can be converted to
- // a Date. So, I chose 0. The Date will appear as
- // the beginning of the Epoch (Jan 1, 1970 00:00:00 GMT)
- time = 0;
- System.out.println("DateFormat.ParseException: " +
e.getMessage());
- }
-
- return time;
- }
-
- public String getCookieHeaderForURL(URL url) {
- if (! url.getProtocol().toUpperCase().trim().equals("HTTP")
- &&
- ! url.getProtocol().toUpperCase().trim().equals("HTTPS")) return null;
-
- StringBuffer header = new StringBuffer();
- for (Iterator enum = cookies.iterator(); enum.hasNext();) {
- Cookie cookie = (Cookie) enum.next();
- if (url.getHost().endsWith(cookie.getDomain()) &&
-
url.getFile().startsWith(cookie.getPath()) &&
- (System.currentTimeMillis() / 1000)
<= cookie.getExpires()) {
- if (header.length() > 0) {
- header.append("; ");
- }
-
header.append(cookie.getName()).append("=").append(cookie.getValue());
- }
- }
-
- if (header.length() != 0) {
- return header.toString();
- } else {
- return null;
- }
- }
-
- public void addCookieFromHeader(String cookieHeader, URL url) {
- //System.out.println("inside addCookieFromHeader: " + cookieHeader + "
" + url);
- StringTokenizer st = new StringTokenizer(cookieHeader, ";");
- String nvp;
-
- // first n=v is name=value
- nvp = st.nextToken();
- int index = nvp.indexOf("=");
- String name = nvp.substring(0,index);
- String value = nvp.substring(index+1);
- String domain = url.getHost();
- String path = url.getFile();
-
- //System.out.println(name);
- //System.out.println(value);
- //System.out.println(domain);
- //System.out.println(path);
-
- Cookie newCookie = new Cookie(name, value, domain, path, false,
- System.currentTimeMillis() + 1000 * 60 * 60 *
24);
- // check the rest of the headers
- while (st.hasMoreTokens()) {
- nvp = st.nextToken();
- nvp = nvp.trim();
- index = nvp.indexOf("=");
- if(index == -1)
- {
- index = nvp.length();
- }
- String key = nvp.substring(0,index);
- if (key.equalsIgnoreCase("expires")) {
- try {
- String expires =
nvp.substring(index+1);
- Date date =
dateFormat.parse(expires);
- newCookie.setExpires(date.getTime());
- //System.out.println("set expires to "
+ date.getTime());
- //System.out.println("current time: "
+ (new Date()).getTime());
- } catch (ParseException pe) {}
- } else if (key.equalsIgnoreCase("domain")) {
- newCookie.setDomain(nvp.substring(index+1));
- } else if (key.equalsIgnoreCase("path")) {
- newCookie.setPath(nvp.substring(index+1));
- } else if (key.equalsIgnoreCase("secure")) {
- newCookie.setSecure(true);
- }
- }
-
- Vector removeIndices = new Vector();
- for (int i = cookies.size() - 1; i >= 0; i--) {
- Cookie cookie = (Cookie) cookies.get(i);
- if (cookie == null)
- continue;
- if (cookie.getPath().equals(newCookie.getPath()) &&
-
cookie.getDomain().equals(newCookie.getDomain()) &&
-
cookie.getName().equals(newCookie.getName())) {
- //System.out.println("remove cookie #" + i);
- removeIndices.addElement(new Integer(i));
- }
- }
-
- for (Enumeration e = removeIndices.elements(); e.hasMoreElements();)
{
- index = ((Integer) e.nextElement()).intValue();
- //System.out.println("remove cookie index " + index);
- cookies.remove(index);
- }
-
- if (newCookie.getExpires() >= System.currentTimeMillis())
- {
- //System.out.println("cookie expiration is future so add it");
- cookies.add(newCookie);
- }
- }
-
- public void removeCookieNamed(String name) {
- Vector removeIndices = new Vector();
- for (int i = cookies.size() - 1; i > 0; i--) {
- Cookie cookie = (Cookie) cookies.get(i);
- if (cookie == null)
- continue;
- if (cookie.getName().equals(name))
- removeIndices.addElement(new Integer(i));
- }
-
- for (Enumeration e = removeIndices.elements(); e.hasMoreElements();)
{
- cookies.remove(((Integer) e.nextElement()).intValue());
- }
-
- }
-
- /******************************************************
- * Takes a String and a tokenizer character, and returns
- a new array of strings of the string split by the tokenizer
- character.
- @param splittee String to be split
- @param splitChar Character to split the string on
- @param def Default value to place between two split chars that have
- nothing between them
- @return Array of all the tokens.
- ******************************************************/
- public String[] split(String splittee,String splitChar,String def)
- {
- if(splittee == null || splitChar == null)
- return new String[0];
- StringTokenizer tokens;
- String temp;
- int spot;
- while((spot=splittee.indexOf(splitChar+splitChar))!=-1)
- splittee=splittee.substring(0,spot+splitChar.length())+def+
- splittee.substring(spot+1*splitChar.length(),
- splittee.length());
- Vector returns=new Vector();
- tokens=new StringTokenizer(splittee,splitChar);
- while(tokens.hasMoreTokens()) {
- temp=(String)tokens.nextToken();
- returns.addElement(temp);
- }
- String[] values=new String[returns.size()];
- returns.copyInto(values);
- return values;
- }
-
- public String getClassLabel()
- {
- return "HTTP Cookie Manager";
- }
+ public static final String COOKIES = "cookies";
+ /**
+ * A vector of Cookies managed by this class.
+ * @associates <{org.apache.jmeter.controllers.Cookie}>
+ */
+ private List cookies = new ArrayList();
+ private String name;
+ private static SimpleDateFormat dateFormat = new SimpleDateFormat("EEEE,
dd-MMM-yyyy HH:mm:ss zzz");
+
+ private final static int columnCount = 6;
+ private final static String[] columnNames =
{"Name","Value","Domain","Path","Secure","Expiration"};
+
+ private static List addableList = new LinkedList();
+
+ static
+ {
+ // The cookie specification requires that the timezone be GMT.
+ // See
http://developer.netscape.com/docs/manuals/communicator/jsguide4/cookies.htm
+ // See http://www.cookiecentral.com/faq/
+ dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
+ }
+
+ public CookieManager () {
+ }
+
+ public Class getTagHandlerClass()
+ {
+ return org.apache.jmeter.protocol.http.save.CookieManagerHandler.class;
+ }
+
+ public void uncompile()
+ {
+ }
+
+ public Object clone()
+ {
+ CookieManager ck = new CookieManager();
+ for(int x = 0;x < cookies.size();x++)
+ ck.add((Cookie)cookies.get(x));
+ ck.setName(this.name);
+ return ck;
+ }
+
+ public boolean expectsModification()
+ {
+ return true;
+ }
+
+ public Collection getAddList()
+ {
+ return addableList;
+ }
+
+ public void addJMeterComponent(JMeterComponentModel child)
+ {
+ }
+
+ public boolean isEditable()
+ {
+ return true;
+ }
+
+ public Class getGuiClass()
+ {
+ return org.apache.jmeter.protocol.http.gui.CookiePanel.class;
+ }
+
+ public void setName(String name)
+ {
+ this.name = name;
+ }
+
+ public String getName()
+ {
+ return this.name;
+ }
+
+ /************************************************************
+ * This allows config elements to combine and give a "layered" effect. for
+ * example, say there are two HTTPConfigElements, which have properties for
+ * domain, path, method, and parameters. If element A has everything filled
+ * in, but null for domain, and element B is added, which has only domain
+ * filled in, then after adding B to A, A will have the domain from B. If A
+ * already had a domain, then the correct behavior is for A to ignore the
+ * addition of element B.
+ *
+ *@param config !ToDo
+ ***********************************************************/
+ public void addConfigElement(ConfigElement config)
+ {
+ if (config instanceof CookieManager)
+ {
+ this.cookies.addAll(((CookieManager)config).getCookies());
+ }
+ }
+
+ public Collection getCookies()
+ {
+ return cookies;
+ }
+
+ public int getCookieCount()
+ {
+ return cookies.size();
+ }
+
+ public int getColumnCount()
+ {
+ return columnCount;
+ }
+
+ public String getColumnName(int column)
+ {
+ return columnNames[column];
+ }
+
+ // Incorrect method. Always returns String. I changed CookiePanel code to
perform
+ // this lookup.
+ //public Class getColumnClass(int column)
+ //{
+ // return columnNames[column].getClass();
+ //}
+
+ public Cookie getCookie(int row)
+ {
+ return (Cookie)cookies.get(row);
+ }
+
+ /** save the cookie data to a file */
+ public void save(String authFile) throws IOException {
+ File file = new File(authFile);
+ if (!file.isAbsolute()) file = new File(System.getProperty("user.dir") +
File.separator + authFile);
+ PrintWriter writer = new PrintWriter(new FileWriter(file));
+ writer.println("# JMeter generated Cookie file");
+ for (int i = 0; i < cookies.size(); i++) {
+ Cookie cook = (Cookie) cookies.get(i);
+ writer.println(cook.toString());
+ }
+ 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") +
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.");
+ }
+
+ String line;
+ while ((line = reader.readLine()) != null) {
+ try {
+ if (line.startsWith("#") || line.trim().length() == 0)
continue;
+ String[] st = split(line, "\t"," ");
+ int domain = 0;
+ int foo = 1;
+ int path = 2;
+ if(st[path].equals(" "))
+ st[path] = "/";
+ boolean secure = new Boolean(st[3]).booleanValue();
+ long expires = new Long(st[4]).longValue();
+ int name = 5;
+ int value = 6;
+ Cookie cookie = new Cookie(st[name], st[value], st[domain],
+ st[path], secure, expires);
+ cookies.add(cookie);
+ } catch (Exception e) {
+ throw new IOException("Error parsing cookie line\n\t'" + line
+ "'\n\t" + e);
+ }
+ }
+ reader.close();
+ }
+
+ /** add a cookie */
+ public void add(Cookie c) {
+ cookies.add(c);
+ }
+
+ /** add an empty cookie */
+ public void add() {
+ cookies.add(new Cookie());
+ }
+
+ /** remove a cookie */
+ public void remove(int index) {
+ cookies.remove(index);
+ }
+
+ /** return the number cookies */
+ public int size() {
+ return cookies.size();
+ }
+
+ /** return the cookie at index i */
+ public Cookie get(int i) {
+ return (Cookie) cookies.get(i);
+ }
+
+ public String convertLongToDateFormatStr(long dateLong)
+ {
+ return dateFormat.format(new Date(dateLong));
+ }
+
+ public long convertDateFormatStrToLong(String dateStr)
+ {
+ long time = 0;
+
+ try
+ {
+ Date date = dateFormat.parse(dateStr);
+ time = date.getTime();
+ }
+ catch (ParseException e)
+ {
+ // ERROR!!!
+ // Later, display error dialog? For now, we have
+ // to specify a number that can be converted to
+ // a Date. So, I chose 0. The Date will appear as
+ // the beginning of the Epoch (Jan 1, 1970 00:00:00 GMT)
+ time = 0;
+ System.out.println("DateFormat.ParseException: " + e.getMessage());
+ }
+
+ return time;
+ }
+
+ public String getCookieHeaderForURL(URL url) {
+ if (! url.getProtocol().toUpperCase().trim().equals("HTTP")
+ &&
+ ! url.getProtocol().toUpperCase().trim().equals("HTTPS")) return null;
+
+ StringBuffer header = new StringBuffer();
+ for (Iterator enum = cookies.iterator(); enum.hasNext();) {
+ Cookie cookie = (Cookie) enum.next();
+ if (url.getHost().endsWith(cookie.getDomain()) &&
+ url.getFile().startsWith(cookie.getPath()) &&
+ (System.currentTimeMillis() / 1000) <=
cookie.getExpires()) {
+ if (header.length() > 0) {
+ header.append("; ");
+ }
+
header.append(cookie.getName()).append("=").append(cookie.getValue());
+ }
+ }
+
+ if (header.length() != 0) {
+ return header.toString();
+ } else {
+ return null;
+ }
+ }
+
+ public void addCookieFromHeader(String cookieHeader, URL url) {
+ //System.out.println("inside addCookieFromHeader: " + cookieHeader + " " +
url);
+ StringTokenizer st = new StringTokenizer(cookieHeader, ";");
+ String nvp;
+
+ // first n=v is name=value
+ nvp = st.nextToken();
+ int index = nvp.indexOf("=");
+ String name = nvp.substring(0,index);
+ String value = nvp.substring(index+1);
+ String domain = url.getHost();
+ String path = url.getFile();
+
+ //System.out.println(name);
+ //System.out.println(value);
+ //System.out.println(domain);
+ //System.out.println(path);
+
+ Cookie newCookie = new Cookie(name, value, domain, path, false,
+ System.currentTimeMillis() + 1000 * 60 * 60 * 24);
+ // check the rest of the headers
+ while (st.hasMoreTokens()) {
+ nvp = st.nextToken();
+ nvp = nvp.trim();
+ index = nvp.indexOf("=");
+ if(index == -1)
+ {
+ index = nvp.length();
+ }
+ String key = nvp.substring(0,index);
+ if (key.equalsIgnoreCase("expires")) {
+ try {
+ String expires = nvp.substring(index+1);
+ Date date = dateFormat.parse(expires);
+ newCookie.setExpires(date.getTime());
+ //System.out.println("set expires to " + date.getTime());
+ //System.out.println("current time: " +
(System.currentTimeMillis());
+ } catch (ParseException pe) {}
+ } else if (key.equalsIgnoreCase("domain")) {
+ newCookie.setDomain(nvp.substring(index+1));
+ } else if (key.equalsIgnoreCase("path")) {
+ newCookie.setPath(nvp.substring(index+1));
+ } else if (key.equalsIgnoreCase("secure")) {
+ newCookie.setSecure(true);
+ }
+ }
+
+ Vector removeIndices = new Vector();
+ for (int i = cookies.size() - 1; i >= 0; i--) {
+ Cookie cookie = (Cookie) cookies.get(i);
+ if (cookie == null)
+ continue;
+ if (cookie.getPath().equals(newCookie.getPath()) &&
+ cookie.getDomain().equals(newCookie.getDomain()) &&
+ cookie.getName().equals(newCookie.getName())) {
+ //System.out.println("remove cookie #" + i);
+ removeIndices.addElement(new Integer(i));
+ }
+ }
+
+ for (Enumeration e = removeIndices.elements(); e.hasMoreElements();) {
+ index = ((Integer) e.nextElement()).intValue();
+ //System.out.println("remove cookie index " + index);
+ cookies.remove(index);
+ }
+
+ if (newCookie.getExpires() >= System.currentTimeMillis())
+ {
+ //System.out.println("cookie expiration is future so add it");
+ cookies.add(newCookie);
+ }
+ }
+
+ public void removeCookieNamed(String name) {
+ Vector removeIndices = new Vector();
+ for (int i = cookies.size() - 1; i > 0; i--) {
+ Cookie cookie = (Cookie) cookies.get(i);
+ if (cookie == null)
+ continue;
+ if (cookie.getName().equals(name))
+ removeIndices.addElement(new Integer(i));
+ }
+
+ for (Enumeration e = removeIndices.elements(); e.hasMoreElements();) {
+ cookies.remove(((Integer) e.nextElement()).intValue());
+ }
+
+ }
+
+ /******************************************************
+ * Takes a String and a tokenizer character, and returns
+ a new array of strings of the string split by the tokenizer
+ character.
+ @param splittee String to be split
+ @param splitChar Character to split the string on
+ @param def Default value to place between two split chars that have
+ nothing between them
+ @return Array of all the tokens.
+ ******************************************************/
+ public String[] split(String splittee,String splitChar,String def)
+ {
+ if(splittee == null || splitChar == null)
+ return new String[0];
+ StringTokenizer tokens;
+ String temp;
+ int spot;
+ while((spot=splittee.indexOf(splitChar+splitChar))!=-1)
+ splittee=splittee.substring(0,spot+splitChar.length())+def+
+ splittee.substring(spot+1*splitChar.length(),
+ splittee.length());
+ Vector returns=new Vector();
+ tokens=new StringTokenizer(splittee,splitChar);
+ while(tokens.hasMoreTokens()) {
+ temp=(String)tokens.nextToken();
+ returns.addElement(temp);
+ }
+ String[] values=new String[returns.size()];
+ returns.copyInto(values);
+ return values;
+ }
+
+ public String getClassLabel()
+ {
+ return "HTTP Cookie Manager";
+ }
}
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>