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.net.MalformedURLException; 29 import java.net.URL; 30 import java.util.ArrayList; 31 import java.util.Collection; 32 import java.util.StringTokenizer; 33 34 import org.apache.jmeter.config.ConfigElement; 35 import org.apache.jmeter.config.ConfigTestElement; 36 import org.apache.jmeter.junit.JMeterTestCase; 37 import org.apache.jmeter.protocol.http.util.Base64Encoder; 38 import org.apache.jmeter.testelement.TestElement; 39 import org.apache.jmeter.testelement.property.CollectionProperty; 40 import org.apache.jmeter.testelement.property.PropertyIterator; 41 import org.apache.jmeter.testelement.property.TestElementProperty; 42 import org.apache.jmeter.util.JMeterUtils; 43 import org.apache.jorphan.logging.LoggingManager; 44 import org.apache.log.Logger; 45 46 54 public class AuthManager 55 extends ConfigTestElement 56 implements ConfigElement, Serializable 57 { 58 private static final Logger log = LoggingManager.getLoggerForClass(); 59 60 private final static String AUTH_LIST = "AuthManager.auth_list"; 61 62 private final static int columnCount = 3; 63 private final static String[] columnNames = 64 { 65 JMeterUtils.getResString("auth_base_url"), 66 JMeterUtils.getResString("username"), 67 JMeterUtils.getResString("password")}; 68 69 72 public AuthManager() 73 { 74 setProperty(new CollectionProperty(AUTH_LIST, new ArrayList())); 75 } 76 77 public void clear() 78 { 79 super.clear(); 80 setProperty(new CollectionProperty(AUTH_LIST, new ArrayList())); 81 } 82 83 86 public void set(int index, String url, String user, String pass) 87 { 88 Authorization auth = new Authorization(url, user, pass); 89 if (index >= 0) 90 { 91 getAuthObjects().set( 92 index, 93 new TestElementProperty(auth.getName(), auth)); 94 } 95 else 96 { 97 getAuthObjects().addItem(auth); 98 } 99 } 100 101 public void setName(String newName) 102 { 103 setProperty(TestElement.NAME, newName); 104 } 105 106 public CollectionProperty getAuthObjects() 107 { 108 return (CollectionProperty) getProperty(AUTH_LIST); 109 } 110 111 public int getColumnCount() 112 { 113 return columnCount; 114 } 115 116 public String getColumnName(int column) 117 { 118 return columnNames[column]; 119 } 120 121 public Class getColumnClass(int column) 122 { 123 return columnNames[column].getClass(); 124 } 125 126 public Authorization getAuthObjectAt(int row) 127 { 128 return (Authorization) getAuthObjects().get(row).getObjectValue(); 129 } 130 131 public boolean isEditable() 132 { 133 return true; 134 } 135 136 public String getClassLabel() 137 { 138 return JMeterUtils.getResString("auth_manager_title"); 139 } 140 141 public Class getGuiClass() 142 { 143 return org.apache.jmeter.protocol.http.gui.AuthPanel.class; 144 } 145 146 public Collection getAddList() 147 { 148 return null; 149 } 150 151 154 public Authorization get(int i) 155 { 156 return (Authorization) getAuthObjects().get(i); 157 } 158 159 public String getAuthHeaderForURL(URL url) 160 { 161 Authorization auth = getAuthForURL(url); 162 if (auth == null) return null; 163 return "Basic " + Base64Encoder.encode(auth.getUser() + ":" + auth.getPass()); 164 } 165 166 public Authorization getAuthForURL(URL url) 167 { 168 if (! isSupportedProtocol(url)) 169 { 170 return null; 171 } 172 173 176 URL url2= null; 177 178 try 179 { 180 if (url.getPort() == -1) 181 { 182 int port= url.getProtocol().equalsIgnoreCase("http") ? 80 : 443; 184 url2= 186 new URL( 187 url.getProtocol(), 188 url.getHost(), 189 port, 190 url.getPath()); 191 } 192 else if ( 193 (url.getPort() == 80 194 && url.getProtocol().equalsIgnoreCase("http")) 195 || (url.getPort() == 443 196 && url.getProtocol().equalsIgnoreCase("https"))) 197 { 198 url2= new URL(url.getProtocol(), url.getHost(), url.getPath()); 199 } 200 } 201 catch (MalformedURLException e) 202 { 203 log.error("Internal error!", e); } 206 207 String s1= url.toString(); 208 String s2= null; 209 if (url2 != null) s2= url2.toString(); 210 211 for (PropertyIterator enum = getAuthObjects().iterator(); 213 enum.hasNext(); 214 ) 215 { 216 Authorization auth = (Authorization) enum.next().getObjectValue(); 217 218 String uRL = auth.getURL(); 219 if (s1.startsWith(uRL) || s2 != null && s2.startsWith(uRL)) 220 { 221 return auth; 222 } 223 } 224 return null; 225 } 226 227 public String getName() 228 { 229 return getPropertyAsString(TestElement.NAME); 230 } 231 232 public void addConfigElement(ConfigElement config) 233 { 234 } 235 236 public void addAuth(Authorization auth) 237 { 238 getAuthObjects().addItem(auth); 239 } 240 241 public void addAuth() 242 { 243 getAuthObjects().addItem(new Authorization()); 244 } 245 246 public boolean expectsModification() 247 { 248 return false; 249 } 250 251 public void uncompile() 252 { 253 } 254 255 258 public void save(String authFile) throws IOException 259 { 260 File file = new File(authFile); 261 if (!file.isAbsolute()) 262 { 263 file = 264 new File( 265 System.getProperty("user.dir") + File.separator + authFile); 266 } 267 PrintWriter writer = new PrintWriter(new FileWriter(file)); 268 writer.println("# JMeter generated Authorization file"); 269 for (int i = 0; i < getAuthObjects().size(); i++) 270 { 271 Authorization auth = (Authorization) getAuthObjects().get(i); 272 writer.println(auth.toString()); 273 } 274 writer.flush(); 275 writer.close(); 276 } 277 278 281 public void addFile(String authFile) throws IOException 282 { 283 File file = new File(authFile); 284 if (!file.isAbsolute()) 285 { 286 file = 287 new File( 288 System.getProperty("user.dir") + File.separator + authFile); 289 } 290 BufferedReader reader = null; 291 if (file.canRead()) 292 { 293 reader = new BufferedReader(new FileReader(file)); 294 } 295 else 296 { 297 throw new IOException("The file you specified cannot be read."); 298 } 299 300 String line; 301 while ((line = reader.readLine()) != null) 302 { 303 try 304 { 305 if (line.startsWith("#") || line.trim().length() == 0) 306 { 307 continue; 308 } 309 StringTokenizer st = new StringTokenizer(line, "\t"); 310 String url = st.nextToken(); 311 String user = st.nextToken(); 312 String pass = st.nextToken(); 313 Authorization auth = new Authorization(url, user, pass); 314 getAuthObjects().addItem(auth); 315 } 316 catch (Exception e) 317 { 318 reader.close(); 319 throw new IOException( 320 "Error parsing auth line\n\t'" + line + "'\n\t" + e); 321 } 322 } 323 reader.close(); 324 } 325 326 329 public void remove(int index) 330 { 331 getAuthObjects().remove(index); 332 } 333 334 337 public int size() 338 { 339 return getAuthObjects().size(); 340 } 341 342 private static boolean isSupportedProtocol(URL url) 343 { 344 return url.getProtocol().toUpperCase().equals("HTTP") 345 || url.getProtocol().toUpperCase().equals("HTTPS"); 346 } 347 348 350 public static class Test extends JMeterTestCase{ 351 public Test(String name) 352 { 353 super(name); 354 } 355 356 public void testHttp() throws Exception 357 { 358 assertTrue(isSupportedProtocol(new URL("http:"))); 359 } 360 public void testHttps() throws Exception 361 { 362 assertTrue(isSupportedProtocol(new URL("https:"))); 363 } 364 public void testFile() throws Exception 365 { 366 AuthManager am = new AuthManager(); 367 CollectionProperty ao = am.getAuthObjects(); 368 assertEquals(0,ao.size()); 369 am.addFile("testfiles/TestAuth.txt"); 370 assertEquals(5,ao.size()); 371 Authorization at; 372 at = am.getAuthForURL(new URL("http://a.b.c/")); 373 assertEquals("login",at.getUser()); 374 assertEquals("password",at.getPass()); 375 at = am.getAuthForURL(new URL("http://a.b.c/1")); 376 assertEquals("login1",at.getUser()); 377 assertEquals("password1",at.getPass()); 378 } 379 } 380 } 381 | Popular Tags |