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. package org.apache.commons.httpclient.contrib.utils;
  28. import org.apache.commons.httpclient.Header;
  29. import org.apache.commons.httpclient.HostConfiguration;
  30. import org.apache.commons.httpclient.HttpMethod;
  31. import org.apache.commons.httpclient.HttpMethodBase;
  32. import org.apache.commons.httpclient.methods.EntityEnclosingMethod;
  33. import org.apache.commons.httpclient.params.HttpMethodParams;
  34. /**
  35. * In this class are only methods to copy a HttpMethod:
  36. * PUT, GET, POST,DELETE, TRACE, ...
  37. *
  38. * @author <a href="mailto:mathis@vtg.at">Thomas Mathis</a>
  39. *
  40. * @deprecated
  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. public class HttpMethodCloner {
  47. private static void copyEntityEnclosingMethod(
  48. EntityEnclosingMethod m, EntityEnclosingMethod copy )
  49. throws java.io.IOException
  50. {
  51. copy.setRequestEntity(m.getRequestEntity());
  52. }
  53. private static void copyHttpMethodBase(
  54. HttpMethodBase m, HttpMethodBase copy) {
  55. if (m.getHostConfiguration() != null) {
  56. copy.setHostConfiguration(
  57. new HostConfiguration(m.getHostConfiguration()));
  58. }
  59. try {
  60. copy.setParams((HttpMethodParams)m.getParams().clone());
  61. } catch (CloneNotSupportedException e) {
  62. // Should never happen
  63. }
  64. }
  65. /**
  66. * Clones a HttpMethod. <br>
  67. * <b>Attention:</b> You have to clone a method before it has
  68. * been executed, because the URI can change if followRedirects
  69. * is set to true.
  70. *
  71. * @param m the HttpMethod to clone
  72. *
  73. * @return the cloned HttpMethod, null if the HttpMethod could
  74. * not be instantiated
  75. *
  76. * @throws java.io.IOException if the request body couldn't be read
  77. */
  78. public static HttpMethod clone(HttpMethod m)
  79. throws java.io.IOException
  80. {
  81. HttpMethod copy = null;
  82. // copy the HttpMethod
  83. try {
  84. copy = (HttpMethod) m.getClass().newInstance();
  85. } catch (InstantiationException iEx) {
  86. } catch (IllegalAccessException iaEx) {
  87. }
  88. if ( copy == null ) {
  89. return null;
  90. }
  91. copy.setDoAuthentication(m.getDoAuthentication());
  92. copy.setFollowRedirects(m.getFollowRedirects());
  93. copy.setPath( m.getPath() );
  94. copy.setQueryString(m.getQueryString());
  95. // clone the headers
  96. Header[] h = m.getRequestHeaders();
  97. int size = (h == null) ? 0 : h.length;
  98. for (int i = 0; i < size; i++) {
  99. copy.setRequestHeader(
  100. new Header(h[i].getName(), h[i].getValue()));
  101. }
  102. copy.setStrictMode(m.isStrictMode());
  103. if (m instanceof HttpMethodBase) {
  104. copyHttpMethodBase(
  105. (HttpMethodBase)m,
  106. (HttpMethodBase)copy);
  107. }
  108. if (m instanceof EntityEnclosingMethod) {
  109. copyEntityEnclosingMethod(
  110. (EntityEnclosingMethod)m,
  111. (EntityEnclosingMethod)copy);
  112. }
  113. return copy;
  114. }
  115. }