001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
013: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
014: * License for the specific language governing permissions and limitations
015: * under the License.
016: *
017: */
018:
019: /*
020: * Created on Sep 14, 2004
021: *
022: */
023: package org.apache.jmeter.protocol.http.util;
024:
025: import java.net.URL;
026:
027: import org.apache.jmeter.protocol.http.sampler.HTTPSampleResult;
028: import org.apache.jmeter.samplers.SampleSaveConfiguration;
029: import org.apache.jmeter.save.converters.SampleResultConverter;
030:
031: import com.thoughtworks.xstream.mapper.Mapper;
032: import com.thoughtworks.xstream.converters.MarshallingContext;
033: import com.thoughtworks.xstream.converters.UnmarshallingContext;
034: import com.thoughtworks.xstream.io.HierarchicalStreamReader;
035: import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
036:
037: /**
038: * @author mstover
039: *
040: */
041: public class HTTPResultConverter extends SampleResultConverter {
042:
043: /**
044: * Returns the converter version; used to check for possible
045: * incompatibilities
046: */
047: public static String getVersion() {
048: return "$Revision: 514283 $"; //$NON-NLS-1$
049: }
050:
051: /**
052: * @param arg0
053: */
054: public HTTPResultConverter(Mapper arg0) {
055: super (arg0);
056: }
057:
058: /*
059: * (non-Javadoc)
060: *
061: * @see com.thoughtworks.xstream.converters.Converter#canConvert(java.lang.Class)
062: */
063: public boolean canConvert(Class arg0) {
064: return HTTPSampleResult.class.equals(arg0);
065: }
066:
067: /*
068: * (non-Javadoc)
069: *
070: * @see com.thoughtworks.xstream.converters.Converter#marshal(java.lang.Object,
071: * com.thoughtworks.xstream.io.HierarchicalStreamWriter,
072: * com.thoughtworks.xstream.converters.MarshallingContext)
073: */
074: public void marshal(Object obj, HierarchicalStreamWriter writer,
075: MarshallingContext context) {
076: HTTPSampleResult res = (HTTPSampleResult) obj;
077: SampleSaveConfiguration save = res.getSaveConfig();
078: setAttributes(writer, context, res, save);
079: saveAssertions(writer, context, res, save);
080: saveSubResults(writer, context, res, save);
081: saveResponseHeaders(writer, context, res, save);
082: saveRequestHeaders(writer, context, res, save);
083: saveResponseData(writer, context, res, save);
084: saveSamplerData(writer, context, res, save);
085: }
086:
087: /*
088: * (non-Javadoc)
089: *
090: * @see org.apache.jmeter.save.converters.SampleResultConverter#saveSamplerData(com.thoughtworks.xstream.io.HierarchicalStreamWriter,
091: * org.apache.jmeter.samplers.SampleResult,
092: * org.apache.jmeter.samplers.SampleSaveConfiguration)
093: */
094: protected void saveSamplerData(HierarchicalStreamWriter writer,
095: MarshallingContext context, HTTPSampleResult res,
096: SampleSaveConfiguration save) {
097: if (save.saveSamplerData(res)) {
098: writeString(writer, TAG_COOKIES, res.getCookies());
099: writeString(writer, TAG_METHOD, res.getHTTPMethod());
100: writeString(writer, TAG_QUERY_STRING, res.getQueryString());
101: writeString(writer, TAG_REDIRECT_LOCATION, res
102: .getRedirectLocation());
103: }
104: if (save.saveUrl()) {
105: writeItem(res.getURL(), context, writer);
106: }
107: }
108:
109: /*
110: * (non-Javadoc)
111: *
112: * @see com.thoughtworks.xstream.converters.Converter#unmarshal(com.thoughtworks.xstream.io.HierarchicalStreamReader,
113: * com.thoughtworks.xstream.converters.UnmarshallingContext)
114: */
115: public Object unmarshal(HierarchicalStreamReader reader,
116: UnmarshallingContext context) {
117: HTTPSampleResult res = (HTTPSampleResult) createCollection(context
118: .getRequiredType());
119: retrieveAttributes(reader, context, res);
120: while (reader.hasMoreChildren()) {
121: reader.moveDown();
122: Object subItem = readItem(reader, context, res);
123: if (!retrieveItem(reader, context, res, subItem)) {
124: retrieveHTTPItem(reader, context, res, subItem);
125: }
126: reader.moveUp();
127: }
128:
129: // If we have a file, but no data, then read the file
130: String resultFileName = res.getResultFileName();
131: if (resultFileName.length() > 0
132: && res.getResponseData().length == 0) {
133: readFile(resultFileName, res);
134: }
135: return res;
136: }
137:
138: protected void retrieveHTTPItem(HierarchicalStreamReader reader,
139: UnmarshallingContext context, HTTPSampleResult res,
140: Object subItem) {
141: if (subItem instanceof URL) {
142: res.setURL((URL) subItem);
143: } else if (reader.getNodeName().equals(TAG_COOKIES)) {
144: res.setCookies((String) subItem);
145: } else if (reader.getNodeName().equals(TAG_METHOD)) {
146: res.setHTTPMethod((String) subItem);
147: } else if (reader.getNodeName().equals(TAG_QUERY_STRING)) {
148: res.setQueryString((String) subItem);
149: } else if (reader.getNodeName().equals(TAG_REDIRECT_LOCATION)) {
150: res.setRedirectLocation((String) subItem);
151: }
152: }
153: }
|