1 20 package org.apache.cactus.internal.client; 21 22 import java.lang.reflect.Method ; 23 24 import java.net.HttpURLConnection ; 25 import java.net.URLConnection ; 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 39 public class WebResponseObjectFactory implements ResponseObjectFactory 40 { 41 45 private HttpURLConnection connection; 46 47 51 public WebResponseObjectFactory(HttpURLConnection theConnection) 52 { 53 this.connection = theConnection; 54 } 55 56 59 public Object getResponseObject(String theClassName, Request theRequest) 60 throws ClientException 61 { 62 Object responseObject; 63 64 if (theClassName.equals("com.meterware.httpunit.WebResponse")) 66 { 67 responseObject = createHttpUnitWebResponse(this.connection); 68 69 } 71 else if (theClassName.equals("org.apache.cactus.WebResponse")) 72 { 73 responseObject = new WebResponse((WebRequest) theRequest, 74 this.connection); 75 76 } 78 else if (theClassName.equals("java.net.HttpURLConnection")) 79 { 80 responseObject = this.connection; 81 } 82 else 83 { 84 throw new ClientException("Invalid parameter type [" + theClassName 86 + "]"); 87 } 88 89 return responseObject; 90 } 91 92 105 private Object createHttpUnitWebResponse(HttpURLConnection theConnection) 106 throws ClientException 107 { 108 Object webResponse; 109 110 try 111 { 112 Class responseClass = 113 Class.forName("com.meterware.httpunit.WebResponse"); 114 Method method = responseClass.getMethod("newResponse", 115 new Class [] {URLConnection .class}); 116 117 webResponse = method.invoke(null, new Object [] {theConnection}); 118 } 119 catch (Exception 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 |