1 20 package org.apache.cactus.internal.client.connector.http; 21 22 import java.net.HttpURLConnection ; 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 46 public class DefaultHttpClient 47 { 48 51 protected WebConfiguration configuration; 52 53 58 public DefaultHttpClient(WebConfiguration theConfiguration) 59 { 60 this.configuration = theConfiguration; 61 } 62 63 76 public HttpURLConnection doTest(WebRequest theRequest) throws Throwable 77 { 78 HttpURLConnection connection = callRunTest(theRequest); 81 82 WebTestResult result = null; 84 85 try 86 { 87 result = callGetResult(theRequest); 88 } 89 catch (ParsingException e) 90 { 91 String url = this.configuration.getRedirectorURL(theRequest); 92 throw new ChainedRuntimeException("Failed to get the test " 93 + "results at [" + url + "]", e); 94 } 95 96 if (result.hasException()) 100 { 101 111 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 147 private HttpURLConnection callRunTest(WebRequest theRequest) 148 throws Throwable 149 { 150 theRequest.addParameter(HttpServiceDefinition.SERVICE_NAME_PARAM, 152 ServiceEnumeration.CALL_TEST_SERVICE.toString(), 153 WebRequest.GET_METHOD); 154 155 HttpClientConnectionHelper helper = 158 new HttpClientConnectionHelper( 159 this.configuration.getRedirectorURL(theRequest)); 160 161 HttpURLConnection connection = 162 helper.connect(theRequest, this.configuration); 163 164 connection = new AutoReadHttpURLConnection(connection); 167 168 connection.getInputStream(); 170 171 return connection; 172 } 173 174 183 private WebTestResult callGetResult(WebRequest theOriginalRequest) 184 throws Throwable 185 { 186 WebRequest resultsRequest = new WebRequestImpl(this.configuration); 187 RequestDirectives directives = new RequestDirectives(resultsRequest); 188 directives.setService(ServiceEnumeration.GET_RESULTS_SERVICE); 189 190 resultsRequest.setRedirectorName( 192 theOriginalRequest.getRedirectorName()); 193 194 if (theOriginalRequest.getAuthentication() != null) 196 { 197 resultsRequest.setAuthentication( 198 theOriginalRequest.getAuthentication()); 199 } 200 201 HttpClientConnectionHelper helper = 203 new HttpClientConnectionHelper( 204 this.configuration.getRedirectorURL(resultsRequest)); 205 206 HttpURLConnection 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 WebTestResultParser parser = new WebTestResultParser(); 218 return parser.parse( 219 IoUtil.getText(resultConnection.getInputStream(), "UTF-8")); 220 } 221 } 222 | Popular Tags |