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

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

import java.net.URL;

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

import com.thoughtworks.xstream.mapper.Mapper;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;

/**
 * Class for XStream conversion of HTTPResult
 *
 */
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(Mapper arg0) {
        super(arg0);
    }

    /** {@inheritDoc} */
    @Override
    public boolean canConvert(@SuppressWarnings("rawtypes") Class arg0) { // superclass does not support types
        return HTTPSampleResult.class.equals(arg0);
    }

    /** {@inheritDoc} */
    @Override
    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);
    }

    private 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());
        }
        if (save.saveUrl()) {
            writeItem(res.getURL(), context, writer);
        }
    }

    /** {@inheritDoc} */
    @Override
    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;
    }

    private 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);
        }
    }
}
@@ -3771,33 +3771,24 @@
 Item(reader,
- context,
  res, subIte
@@ -3783,32 +3783,32 @@
  res, subItem);%0A
+
             %7D%0A  
@@ -4201,37 +4201,8 @@
 er, 
-UnmarshallingContext context,
 %0A   
@@ -4346,32 +4346,86 @@
 lse 
-if (reader.getNodeName()
+%7B%0A            String nodeName = reader.getNodeName();%0A            if (nodeName
 .equ
@@ -4440,24 +4440,28 @@
 COOKIES)) %7B%0A
+    
             
@@ -4494,32 +4494,36 @@
 bItem);%0A        
+    
 %7D else if (reade
@@ -4517,36 +4517,24 @@
 lse if (
-reader.getN
+n
 odeName
-()
 .equals(
@@ -4544,24 +4544,28 @@
 _METHOD)) %7B%0A
+    
             
@@ -4601,32 +4601,36 @@
 bItem);%0A        
+    
 %7D else if (reade
@@ -4624,36 +4624,24 @@
 lse if (
-reader.getN
+n
 odeName
-()
 .equals(
@@ -4657,24 +4657,28 @@
 _STRING)) %7B%0A
+    
             
@@ -4715,32 +4715,36 @@
 bItem);%0A        
+ 
+   
 %7D else if (reade
@@ -4738,36 +4738,24 @@
 lse if (
-reader.getN
+n
 odeName
-()
 .equals(
@@ -4788,24 +4788,28 @@
             
+ 
+   
 res.setRedir
@@ -4831,32 +4831,46 @@
 ring) subItem);%0A
+            %7D%0A
         %7D%0A    %7D%0A

Remove unused parameter