/*
 * Copyright 2004-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.
 *  
 */

/*
 * Created on Sep 14, 2004
 *
 */
package org.apache.jmeter.protocol.http.util;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;

import org.apache.jmeter.protocol.http.sampler.HTTPSampleResult;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.samplers.SampleSaveConfiguration;
import org.apache.jmeter.save.converters.SampleResultConverter;

import com.thoughtworks.xstream.alias.ClassMapper;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;

/**
 * @author mstover
 * 
 */
public class HTTPResultConverter extends SampleResultConverter {

	/**
	 * Returns the converter version; used to check for possible
	 * incompatibilities
	 */
	public static String getVersion() {
		return "$Revision$";  //$NON-NLS-1$
	}

	/**
	 * @param arg0
	 */
	public HTTPResultConverter(ClassMapper arg0) {
		super(arg0);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.thoughtworks.xstream.converters.Converter#canConvert(java.lang.Class)
	 */
	public boolean canConvert(Class arg0) {
		return HTTPSampleResult.class.equals(arg0);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.thoughtworks.xstream.converters.Converter#marshal(java.lang.Object,
	 *      com.thoughtworks.xstream.io.HierarchicalStreamWriter,
	 *      com.thoughtworks.xstream.converters.MarshallingContext)
	 */
	public void marshal(Object obj, HierarchicalStreamWriter writer, MarshallingContext context) {
		HTTPSampleResult res = (HTTPSampleResult) obj;
		SampleSaveConfiguration save = res.getSaveConfig();
		setAttributes(writer, context, res, save);
		saveAssertions(writer, context, res, save);
		saveSubResults(writer, context, res, save);
		saveResponseHeaders(writer, context, res, save);
		saveRequestHeaders(writer, context, res, save);
		saveResponseData(writer, context, res, save);
		saveSamplerData(writer, context, res, save);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.apache.jmeter.save.converters.SampleResultConverter#saveSamplerData(com.thoughtworks.xstream.io.HierarchicalStreamWriter,
	 *      org.apache.jmeter.samplers.SampleResult,
	 *      org.apache.jmeter.samplers.SampleSaveConfiguration)
	 */
	protected void saveSamplerData(HierarchicalStreamWriter writer, MarshallingContext context, HTTPSampleResult res,
			SampleSaveConfiguration save) {
		if (save.saveSamplerData(res)) {
			writeString(writer, TAG_COOKIES, res.getCookies());
			writeString(writer, TAG_METHOD, res.getHTTPMethod());
			writeString(writer, TAG_QUERY_STRING, res.getQueryString());
			writeString(writer, TAG_REDIRECT_LOCATION, res.getRedirectLocation());
			writeItem(res.getURL(), context, writer);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.thoughtworks.xstream.converters.Converter#unmarshal(com.thoughtworks.xstream.io.HierarchicalStreamReader,
	 *      com.thoughtworks.xstream.converters.UnmarshallingContext)
	 */
	public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
		HTTPSampleResult res = (HTTPSampleResult) createCollection(context.getRequiredType());
		retrieveAttributes(reader, context, res);
		while (reader.hasMoreChildren()) {
			reader.moveDown();
			Object subItem = readItem(reader, context, res);
			if (!retrieveItem(reader, context, res, subItem)) {
				retrieveHTTPItem(reader, context, res, subItem);
			}
			reader.moveUp();
		}

        // If we have a file, but no data, then read the file
        String resultFileName = res.getResultFileName();
        if (resultFileName.length()>0 
        &&  res.getResponseData().length == 0) {
            readFile(resultFileName,res);
        }
		return res;
	}

    protected void retrieveHTTPItem(HierarchicalStreamReader reader, UnmarshallingContext context,
			HTTPSampleResult res, Object subItem) {
		if (subItem instanceof URL) {
			res.setURL((URL) subItem);
		} else if (reader.getNodeName().equals(TAG_COOKIES)) {
			res.setCookies((String) subItem);
		} else if (reader.getNodeName().equals(TAG_METHOD)) {
			res.setHTTPMethod((String) subItem);
		} else if (reader.getNodeName().equals(TAG_QUERY_STRING)) {
			res.setQueryString((String) subItem);
		} else if (reader.getNodeName().equals(TAG_REDIRECT_LOCATION)) {
			res.setRedirectLocation((String) subItem);
		}
	}
}
@@ -706,165 +706,8 @@
 l;%0A%0A
-import java.io.ByteArrayOutputStream;%0Aimport java.io.File;%0Aimport java.io.FileInputStream;%0Aimport java.io.FileNotFoundException;%0Aimport java.io.IOException;%0A
 impo
@@ -793,56 +793,8 @@
 lt;%0A
-import org.apache.jmeter.samplers.SampleResult;%0A
 impo
@@ -3272,25 +3272,68 @@
 ation());%0A%09%09
-%09
+%7D%0A        if (save.saveUrl()) %7B%0A            
 writeItem(re
@@ -3362,18 +3362,24 @@
 riter);%0A
-%09%09
+        
 %7D%0A%09%7D%0A%0A%09/

Save URL separately from samplerData