Save This Page
Home » jakarta-jmeter-2.3.4_src » org.apache.jmeter.protocol.http.util » [javadoc | source]
    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  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;
   22   import java.io.UnsupportedEncodingException;
   23   import java.net.URLDecoder;
   24   import java.util.LinkedList;
   25   import java.util.List;
   26   
   27   import org.apache.jmeter.config.Argument;
   28   import org.apache.jmeter.config.Arguments;
   29   import org.apache.jmeter.testelement.property.BooleanProperty;
   30   import org.apache.jmeter.testelement.property.PropertyIterator;
   31   import org.apache.jorphan.logging.LoggingManager;
   32   import org.apache.log.Logger;
   33   
   34   //For unit tests, @see TestHTTPArgument
   35   
   36   /*
   37    *
   38    * Represents an Argument for HTTP requests.
   39    */
   40   public class HTTPArgument extends Argument implements Serializable {
   41       private static final Logger log = LoggingManager.getLoggerForClass();
   42   
   43       private static final String ALWAYS_ENCODE = "HTTPArgument.always_encode";
   44   
   45       private static final String USE_EQUALS = "HTTPArgument.use_equals";
   46   
   47       private static final EncoderCache cache = new EncoderCache(1000);
   48   
   49       /**
   50        * Constructor for the Argument object.
   51        */
   52       public HTTPArgument(String name, String value, String metadata) {
   53           this(name, value, false);
   54           this.setMetaData(metadata);
   55       }
   56   
   57       public void setUseEquals(boolean ue) {
   58           if (ue) {
   59               setMetaData("=");
   60           } else {
   61               setMetaData("");
   62           }
   63           setProperty(new BooleanProperty(USE_EQUALS, ue));
   64       }
   65   
   66       public boolean isUseEquals() {
   67           boolean eq = getPropertyAsBoolean(USE_EQUALS);
   68           if (getMetaData().equals("=") || (getValue() != null && getValue().length() > 0)) {
   69               setUseEquals(true);
   70               return true;
   71           }
   72           return eq;
   73   
   74       }
   75   
   76       public void setAlwaysEncoded(boolean ae) {
   77           setProperty(new BooleanProperty(ALWAYS_ENCODE, ae));
   78       }
   79   
   80       public boolean isAlwaysEncoded() {
   81           return getPropertyAsBoolean(ALWAYS_ENCODE);
   82       }
   83   
   84       /**
   85        * Constructor for the Argument object.
   86        */
   87       public HTTPArgument(String name, String value) {
   88           this(name, value, false);
   89       }
   90   
   91       public HTTPArgument(String name, String value, boolean alreadyEncoded) {
   92           // We assume the argument value is encoded according to the HTTP spec, i.e. UTF-8
   93           this(name, value, alreadyEncoded, EncoderCache.URL_ARGUMENT_ENCODING);
   94       }
   95   
   96       /**
   97        * Construct a new HTTPArgument instance
   98        *
   99        * @param name the name of the parameter
  100        * @param value the value of the parameter
  101        * @param alreadyEncoded true if the name and value is already encoded
  102        * @param contentEncoding the encoding used for the parameter value
  103        */
  104       public HTTPArgument(String name, String value, boolean alreadyEncoded, String contentEncoding) {
  105           setAlwaysEncoded(true);
  106           if (alreadyEncoded) {
  107               try {
  108                   // We assume the name is always encoded according to spec
  109                   name = URLDecoder.decode(name, EncoderCache.URL_ARGUMENT_ENCODING);
  110                   // The value is encoded in the specified encoding
  111                   value = URLDecoder.decode(value, contentEncoding);
  112               } catch (UnsupportedEncodingException e) {
  113                   log.error(contentEncoding + " encoding not supported!");
  114                   throw new Error(e.toString());
  115               }
  116           }
  117           setName(name);
  118           setValue(value);
  119           setMetaData("=");
  120       }
  121   
  122       public HTTPArgument(String name, String value, String metaData, boolean alreadyEncoded) {
  123           // We assume the argument value is encoded according to the HTTP spec, i.e. UTF-8
  124           this(name, value, metaData, alreadyEncoded, EncoderCache.URL_ARGUMENT_ENCODING);
  125       }
  126   
  127       /**
  128        * Construct a new HTTPArgument instance
  129        *
  130        * @param name the name of the parameter
  131        * @param value the value of the parameter
  132        * @param metaData the separator to use between name and value
  133        * @param alreadyEncoded true if the name and value is already encoded
  134        * @param contentEncoding the encoding used for the parameter value
  135        */
  136       public HTTPArgument(String name, String value, String metaData, boolean alreadyEncoded, String contentEncoding) {
  137           this(name, value, alreadyEncoded, contentEncoding);
  138           setMetaData(metaData);
  139       }
  140   
  141       public HTTPArgument(Argument arg) {
  142           this(arg.getName(), arg.getValue(), arg.getMetaData());
  143       }
  144   
  145       /**
  146        * Constructor for the Argument object
  147        */
  148       public HTTPArgument() {
  149       }
  150   
  151       /**
  152        * Sets the Name attribute of the Argument object.
  153        *
  154        * @param newName
  155        *            the new Name value
  156        */
  157       public void setName(String newName) {
  158           if (newName == null || !newName.equals(getName())) {
  159               super.setName(newName);
  160           }
  161       }
  162   
  163       /**
  164        * Get the argument value encoded using UTF-8
  165        *
  166        * @return the argument value encoded in UTF-8
  167        */
  168       public String getEncodedValue() {
  169           // Encode according to the HTTP spec, i.e. UTF-8
  170           try {
  171               return getEncodedValue(EncoderCache.URL_ARGUMENT_ENCODING);
  172           } catch (UnsupportedEncodingException e) {
  173               // This can't happen (how should utf8 not be supported!?!),
  174               // so just throw an Error:
  175               throw new Error("Should not happen: " + e.toString());
  176           }
  177       }
  178   
  179       /**
  180        * Get the argument value encoded in the specified encoding
  181        *
  182        * @param contentEncoding the encoding to use when encoding the argument value
  183        * @return the argument value encoded in the specified encoding
  184        * @throws UnsupportedEncodingException
  185        */
  186       public String getEncodedValue(String contentEncoding) throws UnsupportedEncodingException {
  187           if (isAlwaysEncoded()) {
  188               return cache.getEncoded(getValue(), contentEncoding);
  189           } else {
  190               return getValue();
  191           }
  192       }
  193   
  194       public String getEncodedName() {
  195           if (isAlwaysEncoded()) {
  196               return cache.getEncoded(getName());
  197           } else {
  198               return getName();
  199           }
  200   
  201       }
  202   
  203       public static void convertArgumentsToHTTP(Arguments args) {
  204           List newArguments = new LinkedList();
  205           PropertyIterator iter = args.getArguments().iterator();
  206           while (iter.hasNext()) {
  207               Argument arg = (Argument) iter.next().getObjectValue();
  208               if (!(arg instanceof HTTPArgument)) {
  209                   newArguments.add(new HTTPArgument(arg));
  210               } else {
  211                   newArguments.add(arg);
  212               }
  213           }
  214           args.removeAllArguments();
  215           args.setArguments(newArguments);
  216       }
  217   }

Save This Page
Home » jakarta-jmeter-2.3.4_src » org.apache.jmeter.protocol.http.util » [javadoc | source]