|
|||||||||||||||||||
| 30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| WebResponseObjectFactory.java | 37.5% | 24.1% | 50% | 29.3% |
|
||||||||||||||
| 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;
|
|
| 21 |
|
|
| 22 |
import java.io.InputStream;
|
|
| 23 |
import java.lang.reflect.Constructor;
|
|
| 24 |
import java.lang.reflect.Method;
|
|
| 25 |
|
|
| 26 |
import java.net.HttpURLConnection;
|
|
| 27 |
import java.net.URL;
|
|
| 28 |
import java.net.URLConnection;
|
|
| 29 |
|
|
| 30 |
import org.apache.cactus.Request;
|
|
| 31 |
import org.apache.cactus.WebRequest;
|
|
| 32 |
import org.apache.cactus.WebResponse;
|
|
| 33 |
import org.apache.cactus.spi.client.ResponseObjectFactory;
|
|
| 34 |
|
|
| 35 |
/**
|
|
| 36 |
* Constructs Web response objects. Supports both Cactus
|
|
| 37 |
* {@link org.apache.cactus.WebResponse} and HttpUnit
|
|
| 38 |
* <code>com.meterware.httpunit.WebResponse</code> response object creation.
|
|
| 39 |
*
|
|
| 40 |
* @version $Id: WebResponseObjectFactory.java 293031 2005-10-01 22:00:33Z nchalumeau $
|
|
| 41 |
*/
|
|
| 42 |
public class WebResponseObjectFactory implements ResponseObjectFactory |
|
| 43 |
{
|
|
| 44 |
/**
|
|
| 45 |
* Connection object used to connect to the Cactus server side. Required
|
|
| 46 |
* to create the response object.
|
|
| 47 |
*/
|
|
| 48 |
private HttpURLConnection connection;
|
|
| 49 |
|
|
| 50 |
/**
|
|
| 51 |
* @param theConnection the connection object used to connect to the
|
|
| 52 |
* Cactus server side
|
|
| 53 |
*/
|
|
| 54 | 48 |
public WebResponseObjectFactory(HttpURLConnection theConnection)
|
| 55 |
{
|
|
| 56 | 48 |
this.connection = theConnection;
|
| 57 |
} |
|
| 58 |
|
|
| 59 |
/**
|
|
| 60 |
* @see ResponseObjectFactory#getResponseObject
|
|
| 61 |
*/
|
|
| 62 | 11 |
public Object getResponseObject(String theClassName, Request theRequest)
|
| 63 |
throws ClientException
|
|
| 64 |
{
|
|
| 65 | 11 |
Object responseObject; |
| 66 |
|
|
| 67 |
// Is it a Http Unit WebResponse ?
|
|
| 68 | 11 |
if (theClassName.equals("com.meterware.httpunit.WebResponse")) |
| 69 |
{
|
|
| 70 | 0 |
responseObject = createHttpUnitWebResponse(this.connection);
|
| 71 |
|
|
| 72 |
// Is it a Html Unit WebResponse ?
|
|
| 73 |
} |
|
| 74 | 11 |
else if (theClassName.equals( |
| 75 |
"com.gargoylesoftware.htmlunit.WebResponse"))
|
|
| 76 |
{
|
|
| 77 | 0 |
responseObject = createHtmlUnitWebResponse(this.connection);
|
| 78 |
|
|
| 79 |
// Is it a Cactus WebResponse ?
|
|
| 80 |
} |
|
| 81 | 11 |
else if (theClassName.equals("org.apache.cactus.WebResponse")) |
| 82 |
{
|
|
| 83 | 11 |
responseObject = new WebResponse((WebRequest) theRequest,
|
| 84 |
this.connection);
|
|
| 85 |
|
|
| 86 |
// Is it an old HttpURLConnection (deprecated) ?
|
|
| 87 |
} |
|
| 88 | 0 |
else if (theClassName.equals("java.net.HttpURLConnection")) |
| 89 |
{
|
|
| 90 | 0 |
responseObject = this.connection;
|
| 91 |
} |
|
| 92 |
else
|
|
| 93 |
{
|
|
| 94 |
// Else it is an error ...
|
|
| 95 | 0 |
throw new ClientException("Invalid parameter type [" + theClassName |
| 96 |
+ "]");
|
|
| 97 |
} |
|
| 98 |
|
|
| 99 | 11 |
return responseObject;
|
| 100 |
} |
|
| 101 |
|
|
| 102 |
/**
|
|
| 103 |
* Create a HttpUnit <code>WebResponse</code> object by reflection (so
|
|
| 104 |
* that we don't need the HttpUnit jar for users who are not using
|
|
| 105 |
* the HttpUnit endXXX() signature).
|
|
| 106 |
*
|
|
| 107 |
* @param theConnection the HTTP connection that was used when connecting
|
|
| 108 |
* to the server side and which now contains the returned HTTP
|
|
| 109 |
* response that we will pass to HttpUnit so that it can construt
|
|
| 110 |
* a <code>com.meterware.httpunit.WebResponse</code> object.
|
|
| 111 |
* @return a HttpUnit <code>WebResponse</code> object
|
|
| 112 |
* @exception ClientException if it failes to create a HttpClient
|
|
| 113 |
* WebResponse object for any reason
|
|
| 114 |
*/
|
|
| 115 | 0 |
private Object createHttpUnitWebResponse(HttpURLConnection theConnection)
|
| 116 |
throws ClientException
|
|
| 117 |
{
|
|
| 118 | 0 |
Object webResponse; |
| 119 |
|
|
| 120 | 0 |
try
|
| 121 |
{
|
|
| 122 | 0 |
Class responseClass = |
| 123 |
Class.forName("com.meterware.httpunit.WebResponse");
|
|
| 124 | 0 |
Method method = responseClass.getMethod("newResponse",
|
| 125 |
new Class[] {URLConnection.class}); |
|
| 126 |
|
|
| 127 | 0 |
webResponse = method.invoke(null, new Object[] {theConnection}); |
| 128 |
} |
|
| 129 |
catch (Exception e)
|
|
| 130 |
{
|
|
| 131 | 0 |
throw new ClientException("Error calling " |
| 132 |
+ "[public static com.meterware.httpunit.WebResponse "
|
|
| 133 |
+ "com.meterware.httpunit.WebResponse.newResponse("
|
|
| 134 |
+ "java.net.URLConnection) throws java.io.IOException]", e);
|
|
| 135 |
} |
|
| 136 |
|
|
| 137 | 0 |
return webResponse;
|
| 138 |
} |
|
| 139 |
|
|
| 140 |
/**
|
|
| 141 |
* Create a HtmlUnit <code>WebResponse</code> object by reflection (so
|
|
| 142 |
* that we don't need the HtmlUnit jar for users who are not using
|
|
| 143 |
* the HttpUnit endXXX() signature).
|
|
| 144 |
*
|
|
| 145 |
* @param theConnection the HTTP connection that was used when connecting
|
|
| 146 |
* to the server side and which now contains the returned HTTP
|
|
| 147 |
* response that we will pass to HttpUnit so that it can construct
|
|
| 148 |
* a <code>com.gargoylesoftware.htmlunit.WebResponse</code> object.
|
|
| 149 |
* @return a HtmlUnit <code>WebResponse</code> object
|
|
| 150 |
* @exception ClientException if it failes to create a HttpClient
|
|
| 151 |
* WebResponse object for any reason
|
|
| 152 |
*/
|
|
| 153 | 0 |
private Object createHtmlUnitWebResponse(HttpURLConnection theConnection)
|
| 154 |
throws ClientException
|
|
| 155 |
{
|
|
| 156 | 0 |
Object webResponse; |
| 157 |
|
|
| 158 | 0 |
try
|
| 159 |
{
|
|
| 160 | 0 |
Class responseClass = Class.forName( |
| 161 |
"com.gargoylesoftware.htmlunit.StringWebResponse");
|
|
| 162 | 0 |
Constructor method = responseClass.getConstructor( |
| 163 |
new Class[] {String.class, URL.class}); |
|
| 164 |
|
|
| 165 | 0 |
InputStream input = theConnection.getInputStream(); |
| 166 | 0 |
byte[] buffer = new byte[input.available()]; |
| 167 | 0 |
input.read(buffer); |
| 168 | 0 |
webResponse = method.newInstance(new Object[] {new String(buffer), |
| 169 |
theConnection.getURL()}); |
|
| 170 |
} |
|
| 171 |
catch (Exception e)
|
|
| 172 |
{
|
|
| 173 | 0 |
throw new ClientException("Error calling " |
| 174 |
+ "[public static com.meterware.httpunit.WebResponse "
|
|
| 175 |
+ "com.meterware.httpunit.WebResponse.newResponse("
|
|
| 176 |
+ "java.net.URLConnection) throws java.io.IOException]", e);
|
|
| 177 |
} |
|
| 178 |
|
|
| 179 | 0 |
return webResponse;
|
| 180 |
} |
|
| 181 |
} |
|
| 182 |
|
|
||||||||||