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.util.ArrayList;
   23   import java.util.List;
   24   
   25   import org.apache.jmeter.config.ConfigTestElement;
   26   import org.apache.jmeter.testelement.property.CollectionProperty;
   27   import org.apache.jmeter.testelement.property.PropertyIterator;
   28   import org.apache.jmeter.testelement.property.TestElementProperty;
   29   
   30   /**
   31    * A set of HTTPFileArg objects.
   32    *
   33    */
   34   public class HTTPFileArgs extends ConfigTestElement implements Serializable {
   35   
   36       /** The name of the property used to store the files. */
   37       private static final String HTTP_FILE_ARGS = "HTTPFileArgs.files"; //$NON-NLS-1$
   38   
   39       /**
   40        * Create a new HTTPFileArgs object with no files.
   41        */
   42       public HTTPFileArgs() {
   43           setProperty(new CollectionProperty(HTTP_FILE_ARGS, new ArrayList()));
   44       }
   45   
   46       /**
   47        * Get the files.
   48        *
   49        * @return the files
   50        */
   51       public CollectionProperty getHTTPFileArgsCollection() {
   52           return (CollectionProperty) getProperty(HTTP_FILE_ARGS);
   53       }
   54   
   55       /**
   56        * Clear the files.
   57        */
   58       public void clear() {
   59           super.clear();
   60           setProperty(new CollectionProperty(HTTP_FILE_ARGS, new ArrayList()));
   61       }
   62   
   63       /**
   64        * Set the list of files. Any existing files will be lost.
   65        *
   66        * @param files the new files
   67        */
   68       public void setHTTPFileArgs(List files) {
   69           setProperty(new CollectionProperty(HTTP_FILE_ARGS, files));
   70       }
   71   
   72       /**
   73        * Add a new file with the given path.
   74        *
   75        * @param path
   76        *  the path of the file
   77        */
   78       public void addHTTPFileArg(String path) {
   79           addHTTPFileArg(new HTTPFileArg(path));
   80       }
   81   
   82       /**
   83        * Add a new file.
   84        *
   85        * @param file
   86        *  the new file
   87        */
   88       public void addHTTPFileArg(HTTPFileArg file) {
   89           TestElementProperty newHTTPFileArg = new TestElementProperty(file.getPath(), file);
   90           if (isRunningVersion()) {
   91               this.setTemporary(newHTTPFileArg);
   92           }
   93           getHTTPFileArgsCollection().addItem(newHTTPFileArg);
   94       }
   95   
   96       /**
   97        * adds a new File to the HTTPFileArgs list to be uploaded with http
   98        * request.
   99        *
  100        * @param path file full path.
  101        * @param param http parameter name.
  102        * @param mime mime type of file.
  103        */
  104       public void addHTTPFileArg(String path, String param, String mime) {
  105           addHTTPFileArg(new HTTPFileArg(path, param, mime));
  106       }
  107   
  108       /**
  109        * Get a PropertyIterator of the files.
  110        *
  111        * @return an iteration of the files
  112        */
  113       public PropertyIterator iterator() {
  114           return getHTTPFileArgsCollection().iterator();
  115       }
  116   
  117       /**
  118        * Get the current arguments as an array.
  119        *
  120        * @return an array of file arguments
  121        */
  122       public HTTPFileArg[] asArray(){
  123           CollectionProperty props = getHTTPFileArgsCollection();
  124           final int size = props.size();
  125           HTTPFileArg[] args = new HTTPFileArg[size];
  126           for(int i=0; i<size; i++){
  127               args[i]=(HTTPFileArg) props.get(i).getObjectValue();
  128           }
  129           return args;
  130       }
  131       /**
  132        * Create a string representation of the files.
  133        *
  134        * @return the string representation of the files
  135        */
  136       public String toString() {
  137           StringBuffer str = new StringBuffer();
  138           PropertyIterator iter = getHTTPFileArgsCollection().iterator();
  139           while (iter.hasNext()) {
  140               HTTPFileArg file = (HTTPFileArg) iter.next().getObjectValue();
  141               str.append(file.getPath());
  142               if (iter.hasNext()) {
  143                   str.append("&"); //$NON-NLS-1$
  144               }
  145           }
  146           return str.toString();
  147       }
  148   
  149       /**
  150        * Remove the specified file from the list.
  151        *
  152        * @param row
  153        *  the index of the file to remove
  154        */
  155       public void removeHTTPFileArg(int row) {
  156           if (row < getHTTPFileArgCount()) {
  157               getHTTPFileArgsCollection().remove(row);
  158           }
  159       }
  160   
  161       /**
  162        * Remove the specified file from the list.
  163        *
  164        * @param file
  165        *  the file to remove
  166        */
  167       public void removeHTTPFileArg(HTTPFileArg file) {
  168           PropertyIterator iter = getHTTPFileArgsCollection().iterator();
  169           while (iter.hasNext()) {
  170               HTTPFileArg item = (HTTPFileArg) iter.next().getObjectValue();
  171               if (file.equals(item)) {
  172                   iter.remove();
  173               }
  174           }
  175       }
  176   
  177       /**
  178        * Remove the file with the specified path.
  179        *
  180        * @param filePath
  181        *  the path of the file to remove
  182        */
  183       public void removeHTTPFileArg(String filePath) {
  184           PropertyIterator iter = getHTTPFileArgsCollection().iterator();
  185           while (iter.hasNext()) {
  186               HTTPFileArg file = (HTTPFileArg) iter.next().getObjectValue();
  187               if (file.getPath().equals(filePath)) {
  188                   iter.remove();
  189               }
  190           }
  191       }
  192   
  193       /**
  194        * Remove all files from the list.
  195        */
  196       public void removeAllHTTPFileArgs() {
  197           getHTTPFileArgsCollection().clear();
  198       }
  199   
  200       /**
  201        * Add a new empty file to the list. The new file will have the
  202        * empty string as its path.
  203        */
  204       public void addEmptyHTTPFileArg() {
  205           addHTTPFileArg(new HTTPFileArg(""));
  206       }
  207   
  208       /**
  209        * Get the number of files in the list.
  210        *
  211        * @return the number of files
  212        */
  213       public int getHTTPFileArgCount() {
  214           return getHTTPFileArgsCollection().size();
  215       }
  216   
  217       /**
  218        * Get a single file.
  219        *
  220        * @param row
  221        *  the index of the file to return.
  222        * @return the file at the specified index, or null if no file
  223        *  exists at that index.
  224        */
  225       public HTTPFileArg getHTTPFileArg(int row) {
  226           HTTPFileArg file = null;
  227           if (row < getHTTPFileArgCount()) {
  228               file = (HTTPFileArg) getHTTPFileArgsCollection().get(row).getObjectValue();
  229           }
  230           return file;
  231       }
  232   }

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