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,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.jmeter.protocol.http.sampler;
020:
021: import java.net.URL;
022:
023: import org.apache.jmeter.protocol.http.util.HTTPConstants;
024: import org.apache.jmeter.samplers.SampleResult;
025:
026: /**
027: * This is a specialisation of the SampleResult class for the HTTP protocol.
028: *
029: * author <a href="mailto:jsalvata@apache.org">Jordi Salvat i Alabart</a>
030: */
031: public class HTTPSampleResult extends SampleResult {
032:
033: private String cookies = ""; // never null
034:
035: private String method;
036:
037: private String redirectLocation;
038:
039: private String queryString = ""; // never null
040:
041: public HTTPSampleResult() {
042: super ();
043: setDataEncoding(DEFAULT_HTTP_ENCODING); // default if encoding not provided be the page
044: }
045:
046: public HTTPSampleResult(long elapsed) {
047: super (elapsed, true);
048: }
049:
050: /**
051: * Construct a 'parent' result for an already-existing result, essentially
052: * cloning it
053: *
054: * @param res
055: * existing sample result
056: */
057: public HTTPSampleResult(HTTPSampleResult res) {
058: super (res);
059: method = res.method;
060: cookies = res.cookies;
061: queryString = res.queryString;
062: redirectLocation = res.redirectLocation;
063: }
064:
065: public void setHTTPMethod(String method) {
066: this .method = method;
067: }
068:
069: public String getHTTPMethod() {
070: return method;
071: }
072:
073: public void setRedirectLocation(String redirectLocation) {
074: this .redirectLocation = redirectLocation;
075: }
076:
077: public String getRedirectLocation() {
078: return redirectLocation;
079: }
080:
081: /**
082: * Determine whether this result is a redirect.
083: *
084: * @return true iif res is an HTTP redirect response
085: */
086: public boolean isRedirect() {
087: final String[] REDIRECT_CODES = { "301", "302", "303" }; // NOT 304!
088: String code = getResponseCode();
089: for (int i = 0; i < REDIRECT_CODES.length; i++) {
090: if (REDIRECT_CODES[i].equals(code))
091: return true;
092: }
093: return false;
094: }
095:
096: /*
097: * (non-Javadoc)
098: * Overrides version in Sampler data to provide more details
099: *
100: * @see org.apache.jmeter.samplers.SampleResult#getSamplerData()
101: */
102: public String getSamplerData() {
103: StringBuffer sb = new StringBuffer();
104: sb.append(method);
105: URL u = super .getURL();
106: if (u != null) {
107: sb.append(' ');
108: sb.append(u.toString());
109: sb.append("\n");
110: // Include request body if it is a post or put
111: if (HTTPConstants.POST.equals(method)
112: || HTTPConstants.PUT.equals(method)) {
113: sb.append("\nPOST data:\n");
114: sb.append(queryString);
115: sb.append("\n");
116: }
117: if (cookies.length() > 0) {
118: sb.append("\nCookie Data:\n");
119: sb.append(cookies);
120: } else {
121: sb.append("\n[no cookies]");
122: }
123: sb.append("\n");
124: }
125: return sb.toString();
126: }
127:
128: /**
129: * @return cookies as a string
130: */
131: public String getCookies() {
132: return cookies;
133: }
134:
135: /**
136: * @param string
137: * representing the cookies
138: */
139: public void setCookies(String string) {
140: if (string == null) {
141: cookies = "";// $NON-NLS-1$
142: } else {
143: cookies = string;
144: }
145: }
146:
147: /**
148: * Fetch the query string
149: *
150: * @return the query string
151: */
152: public String getQueryString() {
153: return queryString;
154: }
155:
156: /**
157: * Save the query string
158: *
159: * @param string
160: * the query string
161: */
162: public void setQueryString(String string) {
163: if (string == null) {
164: queryString = "";// $NON-NLS-1$
165: } else {
166: queryString = string;
167: }
168: }
169:
170: /**
171: * Overrides the method from SampleResult - so the encoding can be extracted from
172: * the Meta content-type if necessary.
173: *
174: * Updates the dataEncoding field if the content-type is found.
175: *
176: * @return the dataEncoding value as a String
177: */
178: public String getDataEncodingWithDefault() {
179: if (getDataEncodingNoDefault() == null
180: && getContentType().startsWith("text/html")) { // $NON-NLS-1$
181: byte[] bytes = getResponseData();
182: // get the start of the file
183: String prefix = new String(bytes, 0, Math.min(bytes.length,
184: 1000)).toLowerCase();
185: // Extract the content-type if present
186: final String METATAG = "<meta http-equiv=\"content-type\" content=\""; // $NON-NLS-1$
187: int tagstart = prefix.indexOf(METATAG);
188: if (tagstart != -1) {
189: tagstart += METATAG.length();
190: int tagend = prefix.indexOf("\"", tagstart); // $NON-NLS-1$
191: if (tagend != -1) {
192: final String ct = new String(bytes, tagstart,
193: tagend - tagstart);
194: setEncodingAndType(ct);// Update the dataEncoding
195: }
196: }
197: }
198: return super.getDataEncodingWithDefault();
199: }
200: }
|