001    package railo.runtime.net.http;
002     
003     import org.apache.commons.httpclient.Header;
004    import org.apache.commons.httpclient.HostConfiguration;
005    import org.apache.commons.httpclient.HttpMethod;
006    import org.apache.commons.httpclient.HttpMethodBase;
007    import org.apache.commons.httpclient.methods.EntityEnclosingMethod;
008    import org.apache.commons.httpclient.params.HttpMethodParams;
009    
010    /**
011      * In this class are only methods to copy a HttpMethod:
012      * PUT, GET, POST,DELETE, TRACE, ...
013      */
014      
015      public class HttpMethodCloner {
016      
017      private static void copyEntityEnclosingMethod(EntityEnclosingMethod m, EntityEnclosingMethod copy ) {
018              copy.setRequestEntity(m.getRequestEntity());
019      }
020      
021      private static void copyHttpMethodBase(HttpMethodBase m, HttpMethodBase copy) {
022              if (m.getHostConfiguration() != null) {
023                      copy.setHostConfiguration(new HostConfiguration(m.getHostConfiguration()));
024              }
025              try {
026                      copy.setParams((HttpMethodParams)m.getParams().clone());
027              }
028              catch (CloneNotSupportedException e) {}
029      }
030      
031      /**
032      * Clones a HttpMethod. &ltbr>
033      * &ltb&gtAttention:</b> You have to clone a method before it has
034      * been executed, because the URI can change if followRedirects
035      * is set to true.
036      *
037      * @param m the HttpMethod to clone
038      *
039      * @return the cloned HttpMethod, null if the HttpMethod could
040      * not be instantiated
041      *
042      * @throws java.io.IOException if the request body couldn't be read
043      */
044      public static HttpMethod clone(HttpMethod m) {
045              HttpMethod copy = null;
046              try {
047                      copy = m.getClass().newInstance();
048              } 
049              catch (InstantiationException iEx) {} 
050              catch (IllegalAccessException iaEx) {}
051              if ( copy == null ) {
052                      return null;
053              }
054              copy.setDoAuthentication(m.getDoAuthentication());
055              copy.setFollowRedirects(m.getFollowRedirects());
056              copy.setPath( m.getPath() );
057              copy.setQueryString(m.getQueryString());
058     
059              Header[] h = m.getRequestHeaders();
060              int size = (h == null) ? 0 : h.length;
061     
062              for (int i = 0; i < size; i++) {
063                      copy.setRequestHeader(new Header(h[i].getName(), h[i].getValue()));
064              }
065              copy.setStrictMode(m.isStrictMode());
066              if (m instanceof HttpMethodBase) {
067                      copyHttpMethodBase((HttpMethodBase)m,(HttpMethodBase)copy);
068              }
069              if (m instanceof EntityEnclosingMethod) {
070                      copyEntityEnclosingMethod((EntityEnclosingMethod)m,(EntityEnclosingMethod)copy);
071              }
072              return copy;
073      }
074    }