KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > httpclient > contrib > utils > HttpMethodCloner


1 /*
2  * ====================================================================
3  *
4  * Copyright 2002-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  * This software consists of voluntary contributions made by many
20  * individuals on behalf of the Apache Software Foundation. For more
21  * information on the Apache Software Foundation, please see
22  * <http://www.apache.org/>.
23  *
24  * [Additional notices, if required by prior licensing conditions]
25  *
26  */

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 /**
37  * In this class are only methods to copy a HttpMethod:
38  * PUT, GET, POST,DELETE, TRACE, ...
39  *
40  * @author <a HREF="mailto:mathis@vtg.at">Thomas Mathis</a>
41  *
42  * DISCLAIMER: HttpClient developers DO NOT actively support this component.
43  * The component is provided as a reference material, which may be inappropriate
44  * to be used without additional customization.
45  */

46
47 public class HttpMethodCloner {
48
49     private static void copyEntityEnclosingMethod(
50       EntityEnclosingMethod m, EntityEnclosingMethod copy )
51         throws java.io.IOException JavaDoc
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     /**
68      * Clones a HttpMethod. <br>
69      * <b>Attention:</b> You have to clone a method before it has
70      * been executed, because the URI can change if followRedirects
71      * is set to true.
72      *
73      * @param m the HttpMethod to clone
74      *
75      * @return the cloned HttpMethod, null if the HttpMethod could
76      * not be instantiated
77      *
78      * @throws java.io.IOException if the request body couldn't be read
79      */

80     public static HttpMethod clone(HttpMethod m)
81       throws java.io.IOException JavaDoc
82     {
83         HttpMethod copy = null;
84
85         // copy the HttpMethod
86
try {
87             copy = (HttpMethod) m.getClass().newInstance();
88         } catch (InstantiationException JavaDoc iEx) {
89         } catch (IllegalAccessException JavaDoc 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         // clone the headers
100
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