001: /*
002: * ====================================================================
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one or more
005: * contributor license agreements. See the NOTICE file distributed with
006: * this work for additional information regarding copyright ownership.
007: * The ASF licenses this file to You under the Apache License, Version 2.0
008: * (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: * ====================================================================
019: *
020: * This software consists of voluntary contributions made by many
021: * individuals on behalf of the Apache Software Foundation. For more
022: * information on the Apache Software Foundation, please see
023: * <http://www.apache.org/>.
024: *
025: * [Additional notices, if required by prior licensing conditions]
026: *
027: */
028:
029: package org.apache.commons.httpclient.contrib.utils;
030:
031: import org.apache.commons.httpclient.Header;
032: import org.apache.commons.httpclient.HttpMethod;
033: import org.apache.commons.httpclient.HttpMethodBase;
034: import org.apache.commons.httpclient.methods.EntityEnclosingMethod;
035: import org.apache.commons.httpclient.params.HttpMethodParams;
036:
037: /**
038: * In this class are only methods to copy a HttpMethod:
039: * PUT, GET, POST,DELETE, TRACE, ...
040: *
041: * @author <a href="mailto:mathis@vtg.at">Thomas Mathis</a>
042: *
043: * @deprecated
044: *
045: * DISCLAIMER: HttpClient developers DO NOT actively support this component.
046: * The component is provided as a reference material, which may be inappropriate
047: * to be used without additional customization.
048: */
049:
050: public class HttpMethodCloner {
051:
052: private static void copyEntityEnclosingMethod(
053: EntityEnclosingMethod m, EntityEnclosingMethod copy)
054: throws java.io.IOException {
055: copy.setRequestEntity(m.getRequestEntity());
056: }
057:
058: private static void copyHttpMethodBase(HttpMethodBase m,
059: HttpMethodBase copy) {
060: try {
061: copy.setParams((HttpMethodParams) m.getParams().clone());
062: } catch (CloneNotSupportedException e) {
063: // Should never happen
064: }
065: }
066:
067: /**
068: * Clones a HttpMethod. <br>
069: * <b>Attention:</b> You have to clone a method before it has
070: * been executed, because the URI can change if followRedirects
071: * is set to true.
072: *
073: * @param m the HttpMethod to clone
074: *
075: * @return the cloned HttpMethod, null if the HttpMethod could
076: * not be instantiated
077: *
078: * @throws java.io.IOException if the request body couldn't be read
079: */
080: public static HttpMethod clone(HttpMethod m)
081: throws java.io.IOException {
082: HttpMethod copy = null;
083:
084: // copy the HttpMethod
085: try {
086: copy = (HttpMethod) m.getClass().newInstance();
087: } catch (InstantiationException iEx) {
088: } catch (IllegalAccessException iaEx) {
089: }
090: if (copy == null) {
091: return null;
092: }
093: copy.setDoAuthentication(m.getDoAuthentication());
094: copy.setFollowRedirects(m.getFollowRedirects());
095: copy.setPath(m.getPath());
096: copy.setQueryString(m.getQueryString());
097:
098: // clone the headers
099: Header[] h = m.getRequestHeaders();
100: int size = (h == null) ? 0 : h.length;
101:
102: for (int i = 0; i < size; i++) {
103: copy.setRequestHeader(new Header(h[i].getName(), h[i]
104: .getValue()));
105: }
106: copy.setStrictMode(m.isStrictMode());
107: if (m instanceof HttpMethodBase) {
108: copyHttpMethodBase((HttpMethodBase) m,
109: (HttpMethodBase) copy);
110: }
111: if (m instanceof EntityEnclosingMethod) {
112: copyEntityEnclosingMethod((EntityEnclosingMethod) m,
113: (EntityEnclosingMethod) copy);
114: }
115: return copy;
116: }
117: }
|