001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019package lucee.commons.net.http.httpclient3;
020 
021 import org.apache.commons.httpclient.Header;
022import org.apache.commons.httpclient.HostConfiguration;
023import org.apache.commons.httpclient.HttpMethod;
024import org.apache.commons.httpclient.HttpMethodBase;
025import org.apache.commons.httpclient.methods.EntityEnclosingMethod;
026import org.apache.commons.httpclient.params.HttpMethodParams;
027
028/**
029  * In this class are only methods to copy a HttpMethod:
030  * PUT, GET, POST,DELETE, TRACE, ...
031  */
032  
033  public class HttpMethodCloner {
034  
035  private static void copyEntityEnclosingMethod(EntityEnclosingMethod m, EntityEnclosingMethod copy ) {
036          copy.setRequestEntity(m.getRequestEntity());
037  }
038  
039  private static void copyHttpMethodBase(HttpMethodBase m, HttpMethodBase copy) {
040          if (m.getHostConfiguration() != null) {
041                  copy.setHostConfiguration(new HostConfiguration(m.getHostConfiguration()));
042          }
043          try {
044                  copy.setParams((HttpMethodParams)m.getParams().clone());
045          }
046          catch (CloneNotSupportedException e) {}
047  }
048  
049  /**
050  * Clones a HttpMethod. &ltbr>
051  * &ltb&gtAttention:</b> You have to clone a method before it has
052  * been executed, because the URI can change if followRedirects
053  * is set to true.
054  *
055  * @param m the HttpMethod to clone
056  *
057  * @return the cloned HttpMethod, null if the HttpMethod could
058  * not be instantiated
059  *
060  * @throws java.io.IOException if the request body couldn't be read
061  */
062  public static HttpMethod clone(HttpMethod m) {
063          HttpMethod copy = null;
064          try {
065                  copy = m.getClass().newInstance();
066          } 
067          catch (InstantiationException iEx) {} 
068          catch (IllegalAccessException iaEx) {}
069          if ( copy == null ) {
070                  return null;
071          }
072          copy.setDoAuthentication(m.getDoAuthentication());
073          copy.setFollowRedirects(m.getFollowRedirects());
074          copy.setPath( m.getPath() );
075          copy.setQueryString(m.getQueryString());
076 
077          Header[] h = m.getRequestHeaders();
078          int size = (h == null) ? 0 : h.length;
079 
080          for (int i = 0; i < size; i++) {
081                  copy.setRequestHeader(new Header(h[i].getName(), h[i].getValue()));
082          }
083          copy.setStrictMode(m.isStrictMode());
084          if (m instanceof HttpMethodBase) {
085                  copyHttpMethodBase((HttpMethodBase)m,(HttpMethodBase)copy);
086          }
087          if (m instanceof EntityEnclosingMethod) {
088                  copyEntityEnclosingMethod((EntityEnclosingMethod)m,(EntityEnclosingMethod)copy);
089          }
090          return copy;
091  }
092}