001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.jmeter.protocol.http.util;
020:
021: import java.io.Serializable;
022: import java.io.UnsupportedEncodingException;
023: import java.net.URLDecoder;
024: import java.util.LinkedList;
025: import java.util.List;
026:
027: import org.apache.jmeter.config.Argument;
028: import org.apache.jmeter.config.Arguments;
029: import org.apache.jmeter.testelement.property.BooleanProperty;
030: import org.apache.jmeter.testelement.property.PropertyIterator;
031: import org.apache.jorphan.logging.LoggingManager;
032: import org.apache.log.Logger;
033:
034: //For unit tests, @see TestHTTPArgument
035:
036: /*
037: *
038: * @version $Revision: 552959 $ $Date: 2007-07-03 20:39:51 +0100 (Tue, 03 Jul 2007) $
039: */
040: public class HTTPArgument extends Argument implements Serializable {
041: private static final Logger log = LoggingManager
042: .getLoggerForClass();
043:
044: private static final String ALWAYS_ENCODE = "HTTPArgument.always_encode";
045:
046: private static final String USE_EQUALS = "HTTPArgument.use_equals";
047:
048: private static EncoderCache cache = new EncoderCache(1000);
049:
050: /**
051: * Constructor for the Argument object.
052: */
053: public HTTPArgument(String name, String value, String metadata) {
054: this (name, value, false);
055: this .setMetaData(metadata);
056: }
057:
058: public void setUseEquals(boolean ue) {
059: if (ue) {
060: setMetaData("=");
061: } else {
062: setMetaData("");
063: }
064: setProperty(new BooleanProperty(USE_EQUALS, ue));
065: }
066:
067: public boolean isUseEquals() {
068: boolean eq = getPropertyAsBoolean(USE_EQUALS);
069: if (getMetaData().equals("=")
070: || (getValue() != null && getValue().length() > 0)) {
071: setUseEquals(true);
072: return true;
073: }
074: return eq;
075:
076: }
077:
078: public void setAlwaysEncoded(boolean ae) {
079: setProperty(new BooleanProperty(ALWAYS_ENCODE, ae));
080: }
081:
082: public boolean isAlwaysEncoded() {
083: return getPropertyAsBoolean(ALWAYS_ENCODE);
084: }
085:
086: /**
087: * Constructor for the Argument object.
088: */
089: public HTTPArgument(String name, String value) {
090: this (name, value, false);
091: }
092:
093: public HTTPArgument(String name, String value,
094: boolean alreadyEncoded) {
095: // We assume the argument value is encoded according to the HTTP spec, i.e. UTF-8
096: this (name, value, alreadyEncoded,
097: EncoderCache.URL_ARGUMENT_ENCODING);
098: }
099:
100: /**
101: * Construct a new HTTPArgument instance
102: *
103: * @param name the name of the parameter
104: * @param value the value of the parameter
105: * @param alreadyEncoded true if the name and value is already encoded
106: * @param contentEncoding the encoding used for the parameter value
107: */
108: public HTTPArgument(String name, String value,
109: boolean alreadyEncoded, String contentEncoding) {
110: setAlwaysEncoded(true);
111: if (alreadyEncoded) {
112: try {
113: // We assume the name is always encoded according to spec
114: name = URLDecoder.decode(name,
115: EncoderCache.URL_ARGUMENT_ENCODING);
116: // The value is encoded in the specified encoding
117: value = URLDecoder.decode(value, contentEncoding);
118: } catch (UnsupportedEncodingException e) {
119: log.error(contentEncoding + " encoding not supported!");
120: throw new Error(e.toString());
121: }
122: }
123: setName(name);
124: setValue(value);
125: setMetaData("=");
126: }
127:
128: public HTTPArgument(String name, String value, String metaData,
129: boolean alreadyEncoded) {
130: // We assume the argument value is encoded according to the HTTP spec, i.e. UTF-8
131: this (name, value, metaData, alreadyEncoded,
132: EncoderCache.URL_ARGUMENT_ENCODING);
133: }
134:
135: /**
136: * Construct a new HTTPArgument instance
137: *
138: * @param name the name of the parameter
139: * @param value the value of the parameter
140: * @param metaData the separator to use between name and value
141: * @param alreadyEncoded true if the name and value is already encoded
142: * @param contentEncoding the encoding used for the parameter value
143: */
144: public HTTPArgument(String name, String value, String metaData,
145: boolean alreadyEncoded, String contentEncoding) {
146: this (name, value, alreadyEncoded, contentEncoding);
147: setMetaData(metaData);
148: }
149:
150: public HTTPArgument(Argument arg) {
151: this (arg.getName(), arg.getValue(), arg.getMetaData());
152: }
153:
154: /**
155: * Constructor for the Argument object
156: */
157: public HTTPArgument() {
158: }
159:
160: /**
161: * Sets the Name attribute of the Argument object.
162: *
163: * @param newName
164: * the new Name value
165: */
166: public void setName(String newName) {
167: if (newName == null || !newName.equals(getName())) {
168: super .setName(newName);
169: }
170: }
171:
172: /**
173: * Get the argument value encoded using UTF-8
174: *
175: * @return the argument value encoded in UTF-8
176: */
177: public String getEncodedValue() {
178: // Encode according to the HTTP spec, i.e. UTF-8
179: try {
180: return getEncodedValue(EncoderCache.URL_ARGUMENT_ENCODING);
181: } catch (UnsupportedEncodingException e) {
182: // This can't happen (how should utf8 not be supported!?!),
183: // so just throw an Error:
184: throw new Error("Should not happen: " + e.toString());
185: }
186: }
187:
188: /**
189: * Get the argument value encoded in the specified encoding
190: *
191: * @param contentEncoding the encoding to use when encoding the argument value
192: * @return the argument value encoded in the specified encoding
193: * @throws UnsupportedEncodingException
194: */
195: public String getEncodedValue(String contentEncoding)
196: throws UnsupportedEncodingException {
197: if (isAlwaysEncoded()) {
198: return cache.getEncoded(getValue(), contentEncoding);
199: } else {
200: return getValue();
201: }
202: }
203:
204: public String getEncodedName() {
205: if (isAlwaysEncoded()) {
206: return cache.getEncoded(getName());
207: } else {
208: return getName();
209: }
210:
211: }
212:
213: public static void convertArgumentsToHTTP(Arguments args) {
214: List newArguments = new LinkedList();
215: PropertyIterator iter = args.getArguments().iterator();
216: while (iter.hasNext()) {
217: Argument arg = (Argument) iter.next().getObjectValue();
218: if (!(arg instanceof HTTPArgument)) {
219: newArguments.add(new HTTPArgument(arg));
220: } else {
221: newArguments.add(arg);
222: }
223: }
224: args.removeAllArguments();
225: args.setArguments(newArguments);
226: }
227: }
|