KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > protocol > http > util > HTTPArgument


1 // $Header: /home/cvs/jakarta-jmeter/src/protocol/http/org/apache/jmeter/protocol/http/util/HTTPArgument.java,v 1.13.2.2 2004/10/13 00:30:31 sebb Exp $
2
/*
3  * Copyright 2001-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17 */

18
19 package org.apache.jmeter.protocol.http.util;
20
21 import java.io.Serializable JavaDoc;
22 import java.io.UnsupportedEncodingException JavaDoc;
23 import java.util.LinkedList JavaDoc;
24 import java.util.List JavaDoc;
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 /*
38  *
39  * @author unattributed
40  * @version $Revision: 1.13.2.2 $ $Date: 2004/10/13 00:30:31 $
41  */

42 public class HTTPArgument extends Argument implements Serializable JavaDoc
43 {
44     private static final Logger log = LoggingManager.getLoggerForClass();
45     private static final String JavaDoc ALWAYS_ENCODE = "HTTPArgument.always_encode";
46     private static final String JavaDoc USE_EQUALS = "HTTPArgument.use_equals";
47
48     private static EncoderCache cache = new EncoderCache(1000);
49
50     /**
51      * Constructor for the Argument object.
52      */

53     public HTTPArgument(String JavaDoc name, String JavaDoc value, String JavaDoc 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     /**
96      * Constructor for the Argument object.
97      */

98     public HTTPArgument(String JavaDoc name, String JavaDoc value)
99     {
100         this(name, value, false);
101     }
102
103     public HTTPArgument(String JavaDoc name, String JavaDoc 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 JavaDoc e)
114             {
115                 // UTF-8 unsupported? You must be joking!
116
log.error("UTF-8 encoding not supported!");
117                 throw new Error JavaDoc(e.toString());
118             }
119         }
120         setName(name);
121         setValue(value);
122         setMetaData("=");
123     }
124
125     public HTTPArgument(
126         String JavaDoc name,
127         String JavaDoc value,
128         String JavaDoc 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     /**
141      * Constructor for the Argument object
142      */

143     public HTTPArgument()
144     {}
145
146     /**
147      * Sets the Name attribute of the Argument object.
148      *
149      * @param newName the new Name value
150      */

151     public void setName(String JavaDoc newName)
152     {
153         if (newName == null || !newName.equals(getName()))
154         {
155             super.setName(newName);
156         }
157     }
158
159     public String JavaDoc getEncodedValue()
160     {
161         if (isAlwaysEncoded())
162         {
163             return cache.getEncoded(getValue());
164         }
165         else
166         {
167             return getValue();
168         }
169     }
170
171     public String JavaDoc 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 JavaDoc newArguments = new LinkedList JavaDoc();
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 JavaDoc name)
207         {
208             super(name);
209         }
210
211         public void testCloning() throws Exception JavaDoc
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 JavaDoc
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