/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.apache.jmeter.protocol.http.sampler;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

import org.apache.commons.httpclient.HttpVersion;
import org.apache.commons.httpclient.ProtocolException;
import org.apache.commons.httpclient.params.HttpParams;
import org.apache.jorphan.logging.LoggingManager;
import org.apache.jorphan.util.JOrphanUtils;
import org.apache.log.Logger;

/*
 * Utility class to set up default HttpClient parameters from a file
 */
public class HttpClientDefaultParameters {

    private static final Logger log = LoggingManager.getLoggerForClass();

    // Non-instantiable
    private HttpClientDefaultParameters(){
    }

    public static void load(String file, HttpParams params){
        log.info("Reading httpclient parameters from "+file);
        File f = new File(file);
        InputStream is = null;
        Properties props = new Properties();
        try {
            is = new FileInputStream(f);
            props.load(is);
            Iterator<Map.Entry<Object, Object>> pi = props.entrySet().iterator();
            while(pi.hasNext()){
                Map.Entry<Object, Object> me = pi.next();
                String key = (String) me.getKey();
                String value = (String)me.getValue();
                int typeSep = key.indexOf("$"); // $NON-NLS-1$
                try {
                    if (typeSep > 0){
                        String type = key.substring(typeSep+1);// get past separator
                        String name=key.substring(0,typeSep);
                        log.info("Defining "+name+ " as "+value+" ("+type+")");
                        if (type.equals("Integer")){
                            params.setParameter(name, Integer.valueOf(value));
                        } else if (type.equals("Long")){
                            params.setParameter(name, Long.valueOf(value));
                        } else if (type.equals("Boolean")){
                            params.setParameter(name, Boolean.valueOf(value));
                        } else if (type.equals("HttpVersion")){
                            params.setParameter(name, HttpVersion.parse("HTTP/"+value));
                        } else {
                            log.warn("Unexpected type: "+type);
                        }
                    } else {
                            log.info("Defining "+key+ " as "+value);
                            params.setParameter(key, value);
                    }
                } catch (NumberFormatException e) {
                    log.error("Error in property: "+key+"="+value+" "+e.toString());
                } catch (ProtocolException e) {
                    log.error("Error in property: "+key+"="+value+" "+e.toString());
                }
            }
        } catch (FileNotFoundException e) {
            log.error("Problem loading properties "+e.toString());
        } catch (IOException e) {
            log.error("Problem loading properties "+e.toString());
        } finally {
            JOrphanUtils.closeQuietly(is);
        }
    }
//    public static void main(String args[]){
//        load("bin/httpclient.parameters",
//            org.apache.commons.httpclient.params.DefaultHttpParams.getDefaultParams());
//    }
}

@@ -1082,170 +1082,8 @@
 s;%0A%0A
-import org.apache.commons.httpclient.HttpVersion;%0Aimport org.apache.commons.httpclient.ProtocolException;%0Aimport org.apache.commons.httpclient.params.HttpParams;%0A
 impo
@@ -1275,16 +1275,84 @@
 m a file
+.%0A * %0A * Supports both Commons HttpClient and Apache HttpClient.%0A * 
 %0A */%0Apub
@@ -1549,45 +1549,1437 @@
     
-public static void load(String file, 
+// Helper class (callback) for applying parameter definitions%0A    private static abstract class GenericHttpParams %7B%0A        public abstract void setParameter(String name, Object value);%0A    %7D%0A%0A    /**%0A     * Loads a property file and converts parameters as necessary.%0A     * %0A     * @param file the file to load%0A     * @param params Commons HttpClient parameter instance%0A     */%0A    public static void load(String file, %0A            final org.apache.commons.httpclient.params.HttpParams params)%7B%0A        load(file, %0A                new GenericHttpParams ()%7B%0A                    @Override%0A                    public void setParameter(String name, Object value) %7B%0A                        params.setParameter(name, value);%0A                    %7D            %0A                %7D%0A            );%0A    %7D%0A%0A    /**%0A     * Loads a property file and converts parameters as necessary.%0A     * %0A     * @param file the file to load%0A     * @param params Apache HttpClient parameter instance%0A     */%0A    public static void load(String file, %0A            final org.apache.http.params.HttpParams params)%7B%0A        load(file, %0A                new GenericHttpParams ()%7B%0A                    @Override%0A                    public void setParameter(String name, Object value) %7B%0A                        params.setParameter(name, value);%0A                    %7D            %0A                %7D%0A            );%0A    %7D%0A%0A    private static void load(String file, Generic
 Http
@@ -4339,24 +4339,51 @@
 pVersion%22))%7B
+ // Commons HttpClient only
 %0A           
@@ -4425,16 +4425,83 @@
 r(name, 
+%0A                                    org.apache.commons.httpclient.
 HttpVers
@@ -4621,16 +4621,34 @@
 : %22+type
++%22 for name %22+name
 );%0A     
@@ -4878,153 +4878,8 @@
 ch (
-NumberFormatException e) %7B%0A                    log.error(%22Error in property: %22+key+%22=%22+value+%22 %22+e.toString());%0A                %7D catch (Protocol
 Exce
@@ -5301,194 +5301,7 @@
   %7D%0A
-//    public static void main(String args%5B%5D)%7B%0A//        load(%22bin/httpclient.parameters%22,%0A//            org.apache.commons.httpclient.params.DefaultHttpParams.getDefaultParams());%0A//    %7D
 %0A%7D%0A

Make generic so it supports both Commons and Apache HttpClient implementations