/*
 * Copyright 2003-2005 The Apache Software Foundation.
 *
 * Licensed 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.net.URL;

import org.apache.jmeter.samplers.SampleResult;
import org.apache.jorphan.logging.LoggingManager;
import org.apache.log.Logger;

/**
 * This is a specialisation of the SampleResult class for the HTTP protocol.
 * 
 * @author <a href="mailto:jsalvata@apache.org">Jordi Salvat i Alabart</a>
 * @version $Revision$ updated on $Date$
 */
public class HTTPSampleResult extends SampleResult {
	private static Logger log = LoggingManager.getLoggerForClass();

	public HTTPSampleResult() {
		super();
	}

	public HTTPSampleResult(long elapsed) {
		super(elapsed, true);
	}

	/**
	 * Construct a 'parent' result for an already-existing result, essentially
	 * cloning it
	 * 
	 * @param res
	 *            existing sample result
	 */
	public HTTPSampleResult(HTTPSampleResult res) {
		super(res);
		method=res.method;
		setURL(res.getURL());
		cookies=res.cookies;
        queryString=res.queryString;
        redirectLocation=res.redirectLocation;
	}

	private String method;

	public void setHTTPMethod(String method) {
		this.method = method;
	}

	public String getHTTPMethod() {
		return method;
	}

	private String redirectLocation;

	public void setRedirectLocation(String redirectLocation) {
		this.redirectLocation = redirectLocation;
	}

	public String getRedirectLocation() {
		return redirectLocation;
	}

	/**
	 * Determine whether this result is a redirect.
	 * 
	 * @return true iif res is an HTTP redirect response
	 */
	public boolean isRedirect() {
		final String[] REDIRECT_CODES = { "301", "302", "303" }; // NOT 304!
		String code = getResponseCode();
		for (int i = 0; i < REDIRECT_CODES.length; i++) {
			if (REDIRECT_CODES[i].equals(code))
				return true;
		}
		return false;
	}

	/*
	 * (non-Javadoc)
     * Overrides version in Sampler data to provide more details
	 * 
	 * @see org.apache.jmeter.samplers.SampleResult#getSamplerData()
	 */
	public String getSamplerData() {
		StringBuffer sb = new StringBuffer();
		sb.append(method);
		URL u = super.getURL();
		if (u != null) {
			sb.append(' ');
			sb.append(u.toString());
			if ("POST".equals(method)) {
				sb.append(queryString);
			}
			sb.append("\n");
            if (cookies.length()>0){
                sb.append("\nCookie Data:\n");
    			sb.append(cookies);
                sb.append("\n");
            }
		}
		return sb.toString();
	}

	private String cookies = ""; // never null

	/**
	 * @return cookies as a string
	 */
	public String getCookies() {
		return cookies;
	}

	/**
	 * @param string
	 *            representing the cookies
	 */
	public void setCookies(String string) {
        if (string == null) {
            cookies="";
        } else {
    		cookies = string;
        }
	}

	private String queryString = ""; // never null

	/**
	 * Fetch the query string
	 * 
	 * @return the query string
	 */
	public String getQueryString() {
		return queryString;
	}

	/**
	 * Save the query string
	 * 
	 * @param string
	 *            the query string
	 */
	public void setQueryString(String string) {
        if (string == null ) {
            queryString="";
        } else {
    		queryString = string;
        }
	}

}

@@ -1091,16 +1091,22 @@
  static 
+final 
 Logger l
@@ -1146,16 +1146,16 @@
 lass();%0A
-
 %0A%09public
@@ -2750,14 +2750,28 @@
 if (
-%22
+HTTPSamplerBase.
 POST
-%22
 .equ
@@ -3325,24 +3325,38 @@
  cookies=%22%22;
+// $NON-NLS-1$
 %0A        %7D e
@@ -3773,16 +3773,30 @@
 ring=%22%22;
+// $NON-NLS-1$
 %0A       
@@ -3843,14 +3843,943 @@
       %7D%0A
-
 %09%7D
+%0A    %0A    /**%0A     * Set Encoding and DataType from ContentType%0A     * @param ct - content type (may be null)%0A     */%0A    public void setEncodingAndType(String ct)%7B%0A        if (ct != null) %7B%0A            // Extract charset and store as DataEncoding%0A            // TODO do we need process http-equiv META tags, e.g.:%0A            // %3CMETA http-equiv=%22content-type%22 content=%22text/html;%0A            // charset=foobar%22%3E%0A            // or can we leave that to the renderer ?%0A            String de = ct.toLowerCase();%0A            final String cs = %22charset=%22; // $NON-NLS-1$%0A            int cset = de.indexOf(cs);%0A            if (cset %3E= 0) %7B%0A                setDataEncoding(de.substring(cset + cs.length()));%0A            %7D%0A            if (ct.startsWith(%22image/%22)) %7B// $NON-NLS-1$%0A                setDataType(HTTPSampleResult.BINARY);%0A            %7D else %7B%0A                setDataType(HTTPSampleResult.TEXT);%0A            %7D%0A        %7D%0A    %7D
 %0A%0A%7D%0A

Add setEncodingAndType() utility method