KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > HttpClientResponse


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon;
17
18 import com.gargoylesoftware.htmlunit.WebResponse;
19 import com.gargoylesoftware.htmlunit.SubmitMethod;
20
21 import org.apache.commons.httpclient.HttpClient;
22 import org.apache.commons.httpclient.HttpMethodBase;
23
24 import java.io.InputStream JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.net.URL JavaDoc;
27 import java.util.List JavaDoc;
28
29 /**
30  * Wrap the result of an httpclient Method execution into an HtmlUnit
31  * WebResponse in order to be used by HtmlUnitTestCase.
32  *
33  * This class may become obsolete when HtmlUnit supports WebDAV methods.
34  * For progress on this issue see:
35  * https://sourceforge.net/tracker/?func=detail&atid=448269&aid=1166661&group_id=47038
36  *
37  * @version $Id: $
38  */

39 class HttpClientResponse
40     implements WebResponse
41 {
42     private URL JavaDoc url;
43     private HttpMethodBase method;
44     private int statusCode;
45     private long loadTime;
46
47     HttpClientResponse(URL JavaDoc url, HttpMethodBase method)
48         throws IOException JavaDoc
49     {
50         long t0 = System.currentTimeMillis();
51         this.url = url;
52         this.method = method;
53         HttpClient client = new HttpClient();
54         statusCode = client.executeMethod(method);
55         long t1 = System.currentTimeMillis();
56         this.loadTime = t1-t0;
57     }
58
59     public int getStatusCode() {
60         return statusCode;
61     }
62
63     public String JavaDoc getStatusMessage() {
64         return method.getStatusText();
65     }
66
67     public String JavaDoc getContentType() {
68         return method.getResponseHeader("Content-type").getValue();
69     }
70
71     public String JavaDoc getContentAsString() {
72         try {
73             return method.getResponseBodyAsString();
74         }
75         catch(IOException JavaDoc ex) {
76             return null;
77         }
78     }
79
80     public InputStream JavaDoc getContentAsStream() throws IOException JavaDoc {
81         return method.getResponseBodyAsStream();
82     }
83
84     public URL JavaDoc getUrl() {
85         return url;
86     }
87
88     public SubmitMethod getRequestMethod() {
89             // we'll implement this if/when we need it
90
throw new Error JavaDoc("HttpClientResponse.getRequestMethod() is not implemented yet");
91     }
92     
93     public List JavaDoc getResponseHeaders() {
94         // we'll implement this if/when we need it
95
throw new Error JavaDoc("HttpClientResponse.getResponseHeaders() is not implemented yet");
96     }
97     
98     public String JavaDoc getResponseHeaderValue(String JavaDoc headerName) {
99         return method.getResponseHeader(headerName).getValue();
100     }
101
102     public long getLoadTimeInMilliSeconds() {
103         return loadTime;
104     }
105
106     public String JavaDoc getContentCharSet() {
107         return method.getResponseCharSet();
108     }
109
110     public byte[] getResponseBody() {
111         try {
112             return method.getResponseBody();
113         }
114         catch(IOException JavaDoc ex) {
115             return null;
116         }
117     }
118 }
119
Popular Tags