1 18 19 package org.apache.jmeter.protocol.http.control; 20 21 import java.io.BufferedReader ; 22 import java.io.File ; 23 import java.io.FileReader ; 24 import java.io.FileWriter ; 25 import java.io.IOException ; 26 import java.io.PrintWriter ; 27 import java.io.Serializable ; 28 import java.util.ArrayList ; 29 import java.util.Enumeration ; 30 import java.util.StringTokenizer ; 31 import java.util.Vector ; 32 33 import org.apache.jmeter.config.ConfigTestElement; 34 import org.apache.jmeter.testelement.property.CollectionProperty; 35 import org.apache.jmeter.util.JMeterUtils; 36 37 44 public class HeaderManager extends ConfigTestElement implements Serializable 45 { 46 47 public static final String HEADERS = "HeaderManager.headers"; 48 49 private final static int columnCount = 2; 50 private final static String [] columnNames = 51 { JMeterUtils.getResString("name"), JMeterUtils.getResString("value")}; 52 58 private Object SOAPHeader = null; 59 60 public HeaderManager() 61 { 62 setProperty(new CollectionProperty(HEADERS, new ArrayList ())); 63 } 64 65 public CollectionProperty getHeaders() 66 { 67 return (CollectionProperty) getProperty(HEADERS); 68 } 69 70 public int getColumnCount() 71 { 72 return columnCount; 73 } 74 75 public String getColumnName(int column) 76 { 77 return columnNames[column]; 78 } 79 80 public Class getColumnClass(int column) 81 { 82 return columnNames[column].getClass(); 83 } 84 85 public Header getHeader(int row) 86 { 87 return (Header) getHeaders().get(row).getObjectValue(); 88 } 89 90 93 public void save(String headFile) throws IOException 94 { 95 File file = new File (headFile); 96 if (!file.isAbsolute()) 97 { 98 file = 99 new File ( 100 System.getProperty("user.dir") + File.separator + headFile); 101 } 102 PrintWriter writer = new PrintWriter (new FileWriter (file)); 103 writer.println("# JMeter generated Header file"); 104 for (int i = 0; i < getHeaders().size(); i++) 105 { 106 Header head = (Header) getHeaders().get(i); 107 writer.println(head.toString()); 108 } 109 writer.flush(); 110 writer.close(); 111 } 112 113 116 public void addFile(String headerFile) throws IOException 117 { 118 File file = new File (headerFile); 119 if (!file.isAbsolute()) 120 { 121 file = 122 new File ( 123 System.getProperty("user.dir") 124 + File.separator 125 + headerFile); 126 } 127 BufferedReader reader = null; 128 if (file.canRead()) 129 { 130 reader = new BufferedReader (new FileReader (file)); 131 } 132 else 133 { 134 throw new IOException ("The file you specified cannot be read."); 135 } 136 137 String line; 138 while ((line = reader.readLine()) != null) 139 { 140 try 141 { 142 if (line.startsWith("#") || line.trim().length() == 0) 143 { 144 continue; 145 } 146 String [] st = split(line, "\t", " "); 147 int name = 0; 148 int value = 1; 149 Header header = new Header(st[name], st[value]); 150 getHeaders().addItem(header); 151 } 152 catch (Exception e) 153 { 154 throw new IOException ( 155 "Error parsing header line\n\t'" + line + "'\n\t" + e); 156 } 157 } 158 reader.close(); 159 } 160 161 164 public void add(Header h) 165 { 166 getHeaders().addItem(h); 167 } 168 169 172 public void add() 173 { 174 getHeaders().addItem(new Header()); 175 } 176 177 180 public void remove(int index) 181 { 182 getHeaders().remove(index); 183 } 184 185 188 public int size() 189 { 190 return getHeaders().size(); 191 } 192 193 196 public Header get(int i) 197 { 198 return (Header) getHeaders().get(i).getObjectValue(); 199 } 200 201 237 238 288 public void removeHeaderNamed(String name) 289 { 290 Vector removeIndices = new Vector (); 291 for (int i = getHeaders().size() - 1; i >= 0; i--) 292 { 293 Header header = (Header) getHeaders().get(i).getObjectValue(); 294 if (header == null) 295 { 296 continue; 297 } 298 if (header.getName().equalsIgnoreCase(name)) 299 { 300 removeIndices.addElement(new Integer (i)); 301 } 302 } 303 304 for (Enumeration e = removeIndices.elements(); e.hasMoreElements();) 305 { 306 getHeaders().remove(((Integer ) e.nextElement()).intValue()); 307 } 308 } 309 310 320 public String [] split(String splittee, String splitChar, String def) 321 { 322 if (splittee == null || splitChar == null) 323 { 324 return new String [0]; 325 } 326 StringTokenizer tokens; 327 String temp; 328 int spot; 329 while ((spot = splittee.indexOf(splitChar + splitChar)) != -1) 330 { 331 splittee = 332 splittee.substring(0, spot + splitChar.length()) 333 + def 334 + splittee.substring( 335 spot + (1 * splitChar.length()), 336 splittee.length()); 337 } 338 Vector returns = new Vector (); 339 tokens = new StringTokenizer (splittee, splitChar); 340 while (tokens.hasMoreTokens()) 341 { 342 temp = (String ) tokens.nextToken(); 343 returns.addElement(temp); 344 } 345 String [] values = new String [returns.size()]; 346 returns.copyInto(values); 347 return values; 348 } 349 350 public String getClassLabel() 351 { 352 return JMeterUtils.getResString("header_manager_title"); 353 } 354 355 360 public Object getSOAPHeader(){ 361 return this.SOAPHeader; 362 } 363 364 372 public void setSOAPHeader(Object header){ 373 this.SOAPHeader = header; 374 } 375 } 376 | Popular Tags |