001: /*
002: * ========================================================================
003: *
004: * Copyright 2001-2004 The Apache Software Foundation.
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: *
018: * ========================================================================
019: */
020: package org.apache.cactus.internal.client;
021:
022: import java.lang.reflect.Method;
023:
024: import java.net.HttpURLConnection;
025: import java.net.URLConnection;
026:
027: import org.apache.cactus.Request;
028: import org.apache.cactus.WebRequest;
029: import org.apache.cactus.WebResponse;
030: import org.apache.cactus.spi.client.ResponseObjectFactory;
031:
032: /**
033: * Constructs Web response objects. Supports both Cactus
034: * {@link org.apache.cactus.WebResponse} and HttpUnit
035: * <code>com.meterware.httpunit.WebResponse</code> response object creation.
036: *
037: * @version $Id: WebResponseObjectFactory.java 238991 2004-05-22 11:34:50Z vmassol $
038: */
039: public class WebResponseObjectFactory implements ResponseObjectFactory {
040: /**
041: * Connection object used to connect to the Cactus server side. Required
042: * to create the response object.
043: */
044: private HttpURLConnection connection;
045:
046: /**
047: * @param theConnection the connection object used to connect to the
048: * Cactus server side
049: */
050: public WebResponseObjectFactory(HttpURLConnection theConnection) {
051: this .connection = theConnection;
052: }
053:
054: /**
055: * @see ResponseObjectFactory#getResponseObject
056: */
057: public Object getResponseObject(String theClassName,
058: Request theRequest) throws ClientException {
059: Object responseObject;
060:
061: // Is it a Http Unit WebResponse ?
062: if (theClassName.equals("com.meterware.httpunit.WebResponse")) {
063: responseObject = createHttpUnitWebResponse(this .connection);
064:
065: // Is it a Cactus WebResponse ?
066: } else if (theClassName.equals("org.apache.cactus.WebResponse")) {
067: responseObject = new WebResponse((WebRequest) theRequest,
068: this .connection);
069:
070: // Is it an old HttpURLConnection (deprecated) ?
071: } else if (theClassName.equals("java.net.HttpURLConnection")) {
072: responseObject = this .connection;
073: } else {
074: // Else it is an error ...
075: throw new ClientException("Invalid parameter type ["
076: + theClassName + "]");
077: }
078:
079: return responseObject;
080: }
081:
082: /**
083: * Create a HttpUnit <code>WebResponse</code> object by reflection (so
084: * that we don't need the HttpUnit jar for users who are not using
085: * the HttpUnit endXXX() signature).
086: *
087: * @param theConnection the HTTP connection that was used when connecting
088: * to the server side and which now contains the returned HTTP
089: * response that we will pass to HttpUnit so that it can construt
090: * a <code>com.meterware.httpunit.WebResponse</code> object.
091: * @return a HttpUnit <code>WebResponse</code> object
092: * @exception ClientException if it failes to create a HttpClient
093: * WebResponse object for any reason
094: */
095: private Object createHttpUnitWebResponse(
096: HttpURLConnection theConnection) throws ClientException {
097: Object webResponse;
098:
099: try {
100: Class responseClass = Class
101: .forName("com.meterware.httpunit.WebResponse");
102: Method method = responseClass.getMethod("newResponse",
103: new Class[] { URLConnection.class });
104:
105: webResponse = method.invoke(null,
106: new Object[] { theConnection });
107: } catch (Exception e) {
108: throw new ClientException(
109: "Error calling "
110: + "[public static com.meterware.httpunit.WebResponse "
111: + "com.meterware.httpunit.WebResponse.newResponse("
112: + "java.net.URLConnection) throws java.io.IOException]",
113: e);
114: }
115:
116: return webResponse;
117: }
118: }
|