1 27 28 package org.apache.commons.httpclient.contrib.utils; 29 30 import org.apache.commons.httpclient.Header; 31 import org.apache.commons.httpclient.HostConfiguration; 32 import org.apache.commons.httpclient.HttpMethod; 33 import org.apache.commons.httpclient.HttpMethodBase; 34 import org.apache.commons.httpclient.methods.EntityEnclosingMethod; 35 36 46 47 public class HttpMethodCloner { 48 49 private static void copyEntityEnclosingMethod( 50 EntityEnclosingMethod m, EntityEnclosingMethod copy ) 51 throws java.io.IOException 52 { 53 copy.setRequestBody(m.getRequestBodyAsString()); 54 copy.setUseExpectHeader(m.getUseExpectHeader()); 55 } 56 57 private static void copyHttpMethodBase( 58 HttpMethodBase m, HttpMethodBase copy) { 59 if (m.getHostConfiguration() != null) { 60 copy.setHostConfiguration( 61 new HostConfiguration(m.getHostConfiguration())); 62 } 63 copy.setHttp11(m.isHttp11()); 64 copy.setStrictMode(m.isStrictMode()); 65 } 66 67 80 public static HttpMethod clone(HttpMethod m) 81 throws java.io.IOException 82 { 83 HttpMethod copy = null; 84 85 try { 87 copy = (HttpMethod) m.getClass().newInstance(); 88 } catch (InstantiationException iEx) { 89 } catch (IllegalAccessException iaEx) { 90 } 91 if ( copy == null ) { 92 return null; 93 } 94 copy.setDoAuthentication(m.getDoAuthentication()); 95 copy.setFollowRedirects(m.getFollowRedirects()); 96 copy.setPath( m.getPath() ); 97 copy.setQueryString(m.getQueryString()); 98 99 Header[] h = m.getRequestHeaders(); 101 int size = (h == null) ? 0 : h.length; 102 103 for (int i = 0; i < size; i++) { 104 copy.setRequestHeader( 105 new Header(h[i].getName(), h[i].getValue())); 106 } 107 copy.setStrictMode(m.isStrictMode()); 108 if (m instanceof HttpMethodBase) { 109 copyHttpMethodBase( 110 (HttpMethodBase)m, 111 (HttpMethodBase)copy); 112 } 113 if (m instanceof EntityEnclosingMethod) { 114 copyEntityEnclosingMethod( 115 (EntityEnclosingMethod)m, 116 (EntityEnclosingMethod)copy); 117 } 118 return copy; 119 } 120 } 121 | Popular Tags |