KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cactus > internal > client > connector > http > DefaultHttpClient


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

20 package org.apache.cactus.internal.client.connector.http;
21
22 import java.net.HttpURLConnection JavaDoc;
23
24 import org.apache.cactus.WebRequest;
25 import org.apache.cactus.internal.HttpServiceDefinition;
26 import org.apache.cactus.internal.RequestDirectives;
27 import org.apache.cactus.internal.ServiceEnumeration;
28 import org.apache.cactus.internal.WebRequestImpl;
29 import org.apache.cactus.internal.WebTestResult;
30 import org.apache.cactus.internal.client.AssertionFailedErrorWrapper;
31 import org.apache.cactus.internal.client.ParsingException;
32 import org.apache.cactus.internal.client.ServletExceptionWrapper;
33 import org.apache.cactus.internal.client.WebTestResultParser;
34 import org.apache.cactus.internal.configuration.WebConfiguration;
35 import org.apache.cactus.internal.util.IoUtil;
36 import org.apache.cactus.util.ChainedRuntimeException;
37
38 /**
39  * Performs the steps necessary to run a test. It involves
40  * opening a first HTTP connection to a server redirector, reading the output
41  * stream and then opening a second HTTP connection to retrieve the test
42  * result.
43  *
44  * @version $Id: DefaultHttpClient.java,v 1.1 2004/05/22 11:34:46 vmassol Exp $
45  */

46 public class DefaultHttpClient
47 {
48     /**
49      * Cactus configuration.
50      */

51     protected WebConfiguration configuration;
52     
53     /**
54      * Initialize the Http client.
55      *
56      * @param theConfiguration the Cactus configuration
57      */

58     public DefaultHttpClient(WebConfiguration theConfiguration)
59     {
60         this.configuration = theConfiguration;
61     }
62
63     /**
64      * Calls the test method indirectly by calling the Redirector servlet and
65      * then open a second HTTP connection to retrieve the test results.
66      *
67      * @param theRequest the request containing all data to pass to the
68      * redirector servlet.
69      *
70      * @return the <code>HttpURLConnection</code> that contains the HTTP
71      * response when the test was called.
72      *
73      * @exception Throwable if an error occured in the test method or in the
74      * redirector servlet.
75      */

76     public HttpURLConnection JavaDoc doTest(WebRequest theRequest) throws Throwable JavaDoc
77     {
78         // Open the first connection to the redirector to execute the test on
79
// the server side
80
HttpURLConnection JavaDoc connection = callRunTest(theRequest);
81
82         // Open the second connection to get the test results
83
WebTestResult result = null;
84
85         try
86         {
87             result = callGetResult(theRequest);
88         }
89         catch (ParsingException e)
90         {
91             String JavaDoc url = this.configuration.getRedirectorURL(theRequest);
92             throw new ChainedRuntimeException("Failed to get the test "
93                 + "results at [" + url + "]", e);
94         }
95
96         // Check if the returned result object returned contains an error or
97
// not. If yes, we need to raise an exception so that the JUnit
98
// framework can catch it
99
if (result.hasException())
100         {
101             // Wrap the exception message and stack trace into a fake
102
// exception class with overloaded <code>printStackTrace()</code>
103
// methods so that when JUnit calls this method it will print the
104
// stack trace that was set on the server side.
105
// If the error was an AssertionFailedError or ComparisonFailure
106
// then we use an instance of AssertionFailedErrorWrapper (so that
107
// JUnit recognize it is an AssertionFailedError exception and
108
// print it differently in it's runner console). Otherwise we use
109
// an instance of ServletExceptionWrapper.
110

111             // Note: We have to test the exceptions by string name as the JUnit
112
// AssertionFailedError class is unfortunately not serializable...
113

114             if ((result.getExceptionClassName().equals(
115                 "junit.framework.AssertionFailedError"))
116                 || (result.getExceptionClassName().equals(
117                 "junit.framework.ComparisonFailure")))
118             {
119                 throw new AssertionFailedErrorWrapper(
120                     result.getExceptionMessage(),
121                     result.getExceptionClassName(),
122                     result.getExceptionStackTrace());
123             }
124             else
125             {
126                 throw new ServletExceptionWrapper(
127                     result.getExceptionMessage(),
128                     result.getExceptionClassName(),
129                     result.getExceptionStackTrace());
130             }
131         }
132
133         return connection;
134     }
135
136     /**
137      * Execute the test by calling the redirector.
138      *
139      * @param theRequest the request containing all data to pass to the
140      * redirector servlet.
141      * @return the <code>HttpURLConnection</code> that contains the HTTP
142      * response when the test was called.
143      *
144      * @exception Throwable if an error occured in the test method or in the
145      * redirector servlet.
146      */

147     private HttpURLConnection JavaDoc callRunTest(WebRequest theRequest)
148         throws Throwable JavaDoc
149     {
150         // Specify the service to call on the redirector side
151
theRequest.addParameter(HttpServiceDefinition.SERVICE_NAME_PARAM,
152             ServiceEnumeration.CALL_TEST_SERVICE.toString(),
153             WebRequest.GET_METHOD);
154
155         // Open the first connection to the redirector to execute the test on
156
// the server side
157
HttpClientConnectionHelper helper =
158             new HttpClientConnectionHelper(
159                 this.configuration.getRedirectorURL(theRequest));
160         
161         HttpURLConnection JavaDoc connection =
162             helper.connect(theRequest, this.configuration);
163
164         // Wrap the connection to ensure that all servlet output is read
165
// before we ask for results
166
connection = new AutoReadHttpURLConnection(connection);
167
168         // Trigger the transfer of data
169
connection.getInputStream();
170
171         return connection;
172     }
173
174     /**
175      * Get the test result from the redirector.
176      *
177      * @param theOriginalRequest the request that was used to run the test
178      * @return the result that was returned by the redirector.
179      *
180      * @exception Throwable if an error occured in the test method or in the
181      * redirector servlet.
182      */

183     private WebTestResult callGetResult(WebRequest theOriginalRequest)
184         throws Throwable JavaDoc
185     {
186         WebRequest resultsRequest = new WebRequestImpl(this.configuration);
187         RequestDirectives directives = new RequestDirectives(resultsRequest);
188         directives.setService(ServiceEnumeration.GET_RESULTS_SERVICE);
189
190         // Use the same redirector as was used by the original request
191
resultsRequest.setRedirectorName(
192             theOriginalRequest.getRedirectorName());
193         
194         // Add authentication details
195
if (theOriginalRequest.getAuthentication() != null)
196         {
197             resultsRequest.setAuthentication(
198                 theOriginalRequest.getAuthentication());
199         }
200
201         // Open the second connection to get the test results
202
HttpClientConnectionHelper helper =
203             new HttpClientConnectionHelper(
204                 this.configuration.getRedirectorURL(resultsRequest));
205
206         HttpURLConnection JavaDoc resultConnection =
207             helper.connect(resultsRequest, this.configuration);
208
209         if (resultConnection.getResponseCode() != 200)
210         {
211             throw new ParsingException("Not a valid response ["
212                 + resultConnection.getResponseCode() + " "
213                 + resultConnection.getResponseMessage() + "]");
214         }
215
216         // Read the test result
217
WebTestResultParser parser = new WebTestResultParser();
218         return parser.parse(
219             IoUtil.getText(resultConnection.getInputStream(), "UTF-8"));
220     }
221 }
222
Popular Tags