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: package org.apache.cocoon;
018:
019: import com.gargoylesoftware.htmlunit.WebResponse;
020: import com.gargoylesoftware.htmlunit.SubmitMethod;
021:
022: import org.apache.commons.httpclient.HttpClient;
023: import org.apache.commons.httpclient.HttpMethodBase;
024:
025: import java.io.InputStream;
026: import java.io.IOException;
027: import java.net.URL;
028: import java.util.List;
029:
030: /**
031: * Wrap the result of an httpclient Method execution into an HtmlUnit
032: * WebResponse in order to be used by HtmlUnitTestCase.
033: *
034: * This class may become obsolete when HtmlUnit supports WebDAV methods.
035: * For progress on this issue see:
036: * https://sourceforge.net/tracker/?func=detail&atid=448269&aid=1166661&group_id=47038
037: *
038: * @version $Id: $
039: */
040: class HttpClientResponse implements WebResponse {
041: private URL url;
042: private HttpMethodBase method;
043: private int statusCode;
044: private long loadTime;
045:
046: HttpClientResponse(URL url, HttpMethodBase method)
047: throws IOException {
048: long t0 = System.currentTimeMillis();
049: this .url = url;
050: this .method = method;
051: HttpClient client = new HttpClient();
052: statusCode = client.executeMethod(method);
053: long t1 = System.currentTimeMillis();
054: this .loadTime = t1 - t0;
055: }
056:
057: public int getStatusCode() {
058: return statusCode;
059: }
060:
061: public String getStatusMessage() {
062: return method.getStatusText();
063: }
064:
065: public String getContentType() {
066: return method.getResponseHeader("Content-type").getValue();
067: }
068:
069: public String getContentAsString() {
070: try {
071: return method.getResponseBodyAsString();
072: } catch (IOException ex) {
073: return null;
074: }
075: }
076:
077: public InputStream getContentAsStream() throws IOException {
078: return method.getResponseBodyAsStream();
079: }
080:
081: public URL getUrl() {
082: return url;
083: }
084:
085: public SubmitMethod getRequestMethod() {
086: // we'll implement this if/when we need it
087: throw new Error(
088: "HttpClientResponse.getRequestMethod() is not implemented yet");
089: }
090:
091: public List getResponseHeaders() {
092: // we'll implement this if/when we need it
093: throw new Error(
094: "HttpClientResponse.getResponseHeaders() is not implemented yet");
095: }
096:
097: public String getResponseHeaderValue(String headerName) {
098: return method.getResponseHeader(headerName).getValue();
099: }
100:
101: public long getLoadTimeInMilliSeconds() {
102: return loadTime;
103: }
104:
105: public String getContentCharSet() {
106: return method.getResponseCharSet();
107: }
108:
109: public byte[] getResponseBody() {
110: try {
111: return method.getResponseBody();
112: } catch (IOException ex) {
113: return null;
114: }
115: }
116: }
|