/*
 * 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 final 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 (HTTPSamplerBase.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="";// $NON-NLS-1$
        } 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="";// $NON-NLS-1$
        } else {
    		queryString = string;
        }
	}
    
    /**
     * Set Encoding and DataType from ContentType
     * @param ct - content type (may be null)
     */
    public void setEncodingAndType(String ct){
        if (ct != null) {
            // Extract charset and store as DataEncoding
            // TODO do we need process http-equiv META tags, e.g.:
            // <META http-equiv="content-type" content="text/html;
            // charset=foobar">
            // or can we leave that to the renderer ?
            String de = ct.toLowerCase();
            final String cs = "charset="; // $NON-NLS-1$
            int cset = de.indexOf(cs);
            if (cset >= 0) {
                setDataEncoding(de.substring(cset + cs.length()));
            }
            if (ct.startsWith("image/")) {// $NON-NLS-1$
                setDataType(HTTPSampleResult.BINARY);
            } else {
                setDataType(HTTPSampleResult.TEXT);
            }
        }
    }

}

@@ -18,16 +18,17 @@
 003-2005
+6
  The Apa
@@ -737,16 +737,18 @@
 Result;%0A
+//
 import o
@@ -789,16 +789,18 @@
 anager;%0A
+//
 import o
@@ -1081,16 +1081,18 @@
 sult %7B%0A%09
+//
 private 
@@ -2746,16 +2746,45 @@
 ing());%0A
+            sb.append(%22%5Cn%22);%0A
 %09%09%09if (H
@@ -2821,16 +2821,61 @@
 hod)) %7B%0A
+                sb.append(%22%5CnPOST data:%5Cn%22);%0A
 %09%09%09%09sb.a
@@ -2898,16 +2898,24 @@
 g);%0A
-%09%09%09%7D%0A%09%09%09
+                
 sb.a
@@ -2919,32 +2919,46 @@
 b.append(%22%5Cn%22);%0A
+            %7D%0A
             if (
@@ -3048,32 +3048,108 @@
 d(cookies);%0A    
+        %7D else %7B%0A                sb.append(%22%5Cn%5Bno cookies%5D%22);%0A            %7D%0A
             sb.a
@@ -3161,30 +3161,16 @@
 (%22%5Cn%22);%0A
-            %7D%0A
 %09%09%7D%0A%09%09re

Tidy up samplerData layout