KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cactus > internal > client > WebResponseObjectFactory


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.lang.reflect.Method JavaDoc;
23
24 import java.net.HttpURLConnection JavaDoc;
25 import java.net.URLConnection JavaDoc;
26
27 import org.apache.cactus.Request;
28 import org.apache.cactus.WebRequest;
29 import org.apache.cactus.WebResponse;
30 import org.apache.cactus.spi.client.ResponseObjectFactory;
31
32 /**
33  * Constructs Web response objects. Supports both Cactus
34  * {@link org.apache.cactus.WebResponse} and HttpUnit
35  * <code>com.meterware.httpunit.WebResponse</code> response object creation.
36  *
37  * @version $Id: WebResponseObjectFactory.java,v 1.1 2004/05/22 11:34:47 vmassol Exp $
38  */

39 public class WebResponseObjectFactory implements ResponseObjectFactory
40 {
41     /**
42      * Connection object used to connect to the Cactus server side. Required
43      * to create the response object.
44      */

45     private HttpURLConnection JavaDoc connection;
46     
47     /**
48      * @param theConnection the connection object used to connect to the
49      * Cactus server side
50      */

51     public WebResponseObjectFactory(HttpURLConnection JavaDoc theConnection)
52     {
53         this.connection = theConnection;
54     }
55        
56     /**
57      * @see ResponseObjectFactory#getResponseObject
58      */

59     public Object JavaDoc getResponseObject(String JavaDoc theClassName, Request theRequest)
60         throws ClientException
61     {
62         Object JavaDoc responseObject;
63
64         // Is it a Http Unit WebResponse ?
65
if (theClassName.equals("com.meterware.httpunit.WebResponse"))
66         {
67             responseObject = createHttpUnitWebResponse(this.connection);
68
69             // Is it a Cactus WebResponse ?
70
}
71         else if (theClassName.equals("org.apache.cactus.WebResponse"))
72         {
73             responseObject = new WebResponse((WebRequest) theRequest,
74                 this.connection);
75
76             // Is it an old HttpURLConnection (deprecated) ?
77
}
78         else if (theClassName.equals("java.net.HttpURLConnection"))
79         {
80             responseObject = this.connection;
81         }
82         else
83         {
84             // Else it is an error ...
85
throw new ClientException("Invalid parameter type [" + theClassName
86                 + "]");
87         }
88
89         return responseObject;
90     }
91
92     /**
93      * Create a HttpUnit <code>WebResponse</code> object by reflection (so
94      * that we don't need the HttpUnit jar for users who are not using
95      * the HttpUnit endXXX() signature).
96      *
97      * @param theConnection the HTTP connection that was used when connecting
98      * to the server side and which now contains the returned HTTP
99      * response that we will pass to HttpUnit so that it can construt
100      * a <code>com.meterware.httpunit.WebResponse</code> object.
101      * @return a HttpUnit <code>WebResponse</code> object
102      * @exception ClientException if it failes to create a HttpClient
103      * WebResponse object for any reason
104      */

105     private Object JavaDoc createHttpUnitWebResponse(HttpURLConnection JavaDoc theConnection)
106         throws ClientException
107     {
108         Object JavaDoc webResponse;
109
110         try
111         {
112             Class JavaDoc responseClass =
113                 Class.forName("com.meterware.httpunit.WebResponse");
114             Method JavaDoc method = responseClass.getMethod("newResponse",
115                 new Class JavaDoc[] {URLConnection JavaDoc.class});
116
117             webResponse = method.invoke(null, new Object JavaDoc[] {theConnection});
118         }
119         catch (Exception JavaDoc e)
120         {
121             throw new ClientException("Error calling "
122                 + "[public static com.meterware.httpunit.WebResponse "
123                 + "com.meterware.httpunit.WebResponse.newResponse("
124                 + "java.net.URLConnection) throws java.io.IOException]", e);
125         }
126
127         return webResponse;
128     }
129 }
130
Popular Tags