1 18 19 package org.apache.jmeter.protocol.http.util; 20 21 import java.io.Serializable ; 22 import java.io.UnsupportedEncodingException ; 23 import java.util.LinkedList ; 24 import java.util.List ; 25 26 import junit.framework.TestCase; 27 28 import org.apache.jmeter.config.Argument; 29 import org.apache.jmeter.config.Arguments; 30 import org.apache.jmeter.testelement.property.BooleanProperty; 31 import org.apache.jmeter.testelement.property.CollectionProperty; 32 import org.apache.jmeter.testelement.property.PropertyIterator; 33 import org.apache.jorphan.logging.LoggingManager; 34 import org.apache.jorphan.util.JOrphanUtils; 35 import org.apache.log.Logger; 36 37 42 public class HTTPArgument extends Argument implements Serializable 43 { 44 private static final Logger log = LoggingManager.getLoggerForClass(); 45 private static final String ALWAYS_ENCODE = "HTTPArgument.always_encode"; 46 private static final String USE_EQUALS = "HTTPArgument.use_equals"; 47 48 private static EncoderCache cache = new EncoderCache(1000); 49 50 53 public HTTPArgument(String name, String value, String metadata) 54 { 55 this(name, value, false); 56 this.setMetaData(metadata); 57 } 58 59 public void setUseEquals(boolean ue) 60 { 61 if(ue) 62 { 63 setMetaData("="); 64 } 65 else 66 { 67 setMetaData(""); 68 } 69 setProperty(new BooleanProperty(USE_EQUALS,ue)); 70 } 71 72 public boolean isUseEquals() 73 { 74 boolean eq = getPropertyAsBoolean(USE_EQUALS); 75 if (getMetaData().equals("=") 76 || (getValue() != null && getValue().length() > 0)) 77 { 78 setUseEquals(true); 79 return true; 80 } 81 return eq; 82 83 } 84 85 public void setAlwaysEncoded(boolean ae) 86 { 87 setProperty(new BooleanProperty(ALWAYS_ENCODE, ae)); 88 } 89 90 public boolean isAlwaysEncoded() 91 { 92 return getPropertyAsBoolean(ALWAYS_ENCODE); 93 } 94 95 98 public HTTPArgument(String name, String value) 99 { 100 this(name, value, false); 101 } 102 103 public HTTPArgument(String name, String value, boolean alreadyEncoded) 104 { 105 setAlwaysEncoded(true); 106 if (alreadyEncoded) 107 { 108 try 109 { 110 name = JOrphanUtils.decode(name, "UTF-8"); 111 value = JOrphanUtils.decode(value, "UTF-8"); 112 } 113 catch (UnsupportedEncodingException e) 114 { 115 log.error("UTF-8 encoding not supported!"); 117 throw new Error (e.toString()); 118 } 119 } 120 setName(name); 121 setValue(value); 122 setMetaData("="); 123 } 124 125 public HTTPArgument( 126 String name, 127 String value, 128 String metaData, 129 boolean alreadyEncoded) 130 { 131 this(name, value, alreadyEncoded); 132 setMetaData(metaData); 133 } 134 135 public HTTPArgument(Argument arg) 136 { 137 this(arg.getName(), arg.getValue(), arg.getMetaData()); 138 } 139 140 143 public HTTPArgument() 144 {} 145 146 151 public void setName(String newName) 152 { 153 if (newName == null || !newName.equals(getName())) 154 { 155 super.setName(newName); 156 } 157 } 158 159 public String getEncodedValue() 160 { 161 if (isAlwaysEncoded()) 162 { 163 return cache.getEncoded(getValue()); 164 } 165 else 166 { 167 return getValue(); 168 } 169 } 170 171 public String getEncodedName() 172 { 173 if (isAlwaysEncoded()) 174 { 175 return cache.getEncoded(getName()); 176 } 177 else 178 { 179 return getName(); 180 } 181 182 } 183 184 public static void convertArgumentsToHTTP(Arguments args) 185 { 186 List newArguments = new LinkedList (); 187 PropertyIterator iter = args.getArguments().iterator(); 188 while (iter.hasNext()) 189 { 190 Argument arg = (Argument) iter.next().getObjectValue(); 191 if (!(arg instanceof HTTPArgument)) 192 { 193 newArguments.add(new HTTPArgument(arg)); 194 } 195 else 196 { 197 newArguments.add(arg); 198 } 199 } 200 args.removeAllArguments(); 201 args.setArguments(newArguments); 202 } 203 204 public static class Test extends TestCase 205 { 206 public Test(String name) 207 { 208 super(name); 209 } 210 211 public void testCloning() throws Exception 212 { 213 HTTPArgument arg = new HTTPArgument("name.?", "value_ here"); 214 assertEquals("name.%3F", arg.getEncodedName()); 215 assertEquals("value_+here", arg.getEncodedValue()); 216 HTTPArgument clone = (HTTPArgument) arg.clone(); 217 assertEquals("name.%3F", clone.getEncodedName()); 218 assertEquals("value_+here", clone.getEncodedValue()); 219 } 220 221 public void testConversion() throws Exception 222 { 223 Arguments args = new Arguments(); 224 args.addArgument("name.?", "value_ here"); 225 args.addArgument("name$of property", "value_.+"); 226 HTTPArgument.convertArgumentsToHTTP(args); 227 CollectionProperty argList = args.getArguments(); 228 HTTPArgument httpArg = 229 (HTTPArgument) argList.get(0).getObjectValue(); 230 assertEquals("name.%3F", httpArg.getEncodedName()); 231 assertEquals("value_+here", httpArg.getEncodedValue()); 232 httpArg = (HTTPArgument) argList.get(1).getObjectValue(); 233 assertEquals("name%24of+property", httpArg.getEncodedName()); 234 assertEquals("value_.%2B", httpArg.getEncodedValue()); 235 } 236 } 237 } 238 | Popular Tags |