You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@hc.apache.org by Oleg Kalnichevski <o....@dplanet.ch> on 2003/03/13 18:44:32 UTC

First org.apache.commons.httpclient.contrib package component?

Folks
How about making this utility class the first contribution to our yet
non-existent org.apache.commons.httpclient.contrib package?
Ideas, suggestions, objections?
Cheers
Oleg

On Thu, 2003-03-13 at 14:47, mathis@vtg.at wrote:
> Here's the code:
> ================
> 
> package at.vtg.httpclient;
> // or whatever you want ;-)
> 
> import org.apache.commons.httpclient.Header;
> import org.apache.commons.httpclient.HostConfiguration;
> import org.apache.commons.httpclient.HttpMethod;
> import org.apache.commons.httpclient.HttpMethodBase;
> import org.apache.commons.httpclient.methods.EntityEnclosingMethod;
> import org.apache.commons.httpclient.URI;
> import org.apache.commons.httpclient.URIException;
> 
> import org.apache.commons.logging.Log;
> import org.apache.commons.logging.LogFactory;
> 
> /**
>  * In this class are only methods to copy a HttpMethod: PUT, GET, POST,
> DELETE, TRACE, ...
>  *
>  * @author Thomas Mathis
>  * @version $Revision: 1.4 $
>  */
> public class HttpMethodUtil {
> 
>     private static Log log = LogFactory.getLog(HttpMethodUtil.class);
> 
>     private static void copyEntityEnclosingMethod( EntityEnclosingMethod m,
> EntityEnclosingMethod copy )
>             throws java.io.IOException
>     {
>         log.debug( "copy EntityEnclosingMethod" );
> 
>         copy.setRequestBody( m.getRequestBodyAsString() );
>         copy.setUseExpectHeader(m.getUseExpectHeader());
>     }
> 
>     private static void copyHttpMethodBase(HttpMethodBase m, HttpMethodBase
> copy) {
>         log.debug( "copy HttpMethodBase" );
> 
>         if ( m.getHostConfiguration() != null ) {
>             copy.setHostConfiguration( new HostConfiguration(
> m.getHostConfiguration() ) );
>         }
>         copy.setHttp11(m.isHttp11());
>         copy.setStrictMode(m.isStrictMode());
>     }
> 
>     /**
>      * Clones a HttpMethod. <br>
>      * <b>Attention:</b> You have to clone a method before it has been
> executed, because the URI
>      * can change if followRedirects is set to true.
>      *
>      * @param m the HttpMethod to clone
>      *
>      * @return the cloned HttpMethod, null if the HttpMethod could not be
> instantiated
>      *
>      * @throws java.io.IOException if the request body couldn't be read
>      */
>     public static HttpMethod clone(HttpMethod m) throws java.io.IOException
> {
>         log.debug( "clone HttpMethod" );
>         HttpMethod copy = null;
> 
>         // copy the HttpMethod
>         try {
>             copy = (HttpMethod) m.getClass().newInstance();
>         } catch (InstantiationException iEx) {
>         } catch (IllegalAccessException iaEx) {
>         }
>         if ( copy == null ) {
>             return null;
>         }
>         copy.setDoAuthentication(m.getDoAuthentication());
>         copy.setFollowRedirects(m.getFollowRedirects());
>         copy.setPath( m.getPath() );
>         copy.setQueryString(m.getQueryString());
> 
>         // clone the headers
>         Header[] h = m.getRequestHeaders();
>         int size = (h == null) ? 0 : h.length;
> 
>         for (int i = 0; i < size; i++) {
>             copy.setRequestHeader(new Header(h[i].getName(),
> h[i].getValue()));
>         }
> 
>         copy.setStrictMode(m.isStrictMode());
> 
>         if ( m instanceof HttpMethodBase ) {
>             copyHttpMethodBase( (HttpMethodBase) m, (HttpMethodBase) copy );
>         }
>         if ( m instanceof EntityEnclosingMethod ) {
>             copyEntityEnclosingMethod( (EntityEnclosingMethod) m,
> (EntityEnclosingMethod) copy );
>         }
> 
>         return copy;
>     }
> }
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-httpclient-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-httpclient-dev-help@jakarta.apache.org
> 


Re: First org.apache.commons.httpclient.contrib package component?

Posted by Eric Johnson <er...@tibco.com>.
Oleg,

It looks like a fine submission to me.  I think your package name 
suggestion is a good one.  Perhaps a different class name, though.  I'm 
thinking HttpMethodCloner.

-Elric.

Oleg Kalnichevski wrote:

>Folks
>How about making this utility class the first contribution to our yet
>non-existent org.apache.commons.httpclient.contrib package?
>Ideas, suggestions, objections?
>Cheers
>Oleg
>
>On Thu, 2003-03-13 at 14:47, mathis@vtg.at wrote:
>  
>
>>Here's the code:
>>================
>>
>>package at.vtg.httpclient;
>>// or whatever you want ;-)
>>
>>import org.apache.commons.httpclient.Header;
>>import org.apache.commons.httpclient.HostConfiguration;
>>import org.apache.commons.httpclient.HttpMethod;
>>import org.apache.commons.httpclient.HttpMethodBase;
>>import org.apache.commons.httpclient.methods.EntityEnclosingMethod;
>>import org.apache.commons.httpclient.URI;
>>import org.apache.commons.httpclient.URIException;
>>
>>import org.apache.commons.logging.Log;
>>import org.apache.commons.logging.LogFactory;
>>
>>/**
>> * In this class are only methods to copy a HttpMethod: PUT, GET, POST,
>>DELETE, TRACE, ...
>> *
>> * @author Thomas Mathis
>> * @version $Revision: 1.4 $
>> */
>>public class HttpMethodUtil {
>>
>>    private static Log log = LogFactory.getLog(HttpMethodUtil.class);
>>
>>    private static void copyEntityEnclosingMethod( EntityEnclosingMethod m,
>>EntityEnclosingMethod copy )
>>            throws java.io.IOException
>>    {
>>        log.debug( "copy EntityEnclosingMethod" );
>>
>>        copy.setRequestBody( m.getRequestBodyAsString() );
>>        copy.setUseExpectHeader(m.getUseExpectHeader());
>>    }
>>
>>    private static void copyHttpMethodBase(HttpMethodBase m, HttpMethodBase
>>copy) {
>>        log.debug( "copy HttpMethodBase" );
>>
>>        if ( m.getHostConfiguration() != null ) {
>>            copy.setHostConfiguration( new HostConfiguration(
>>m.getHostConfiguration() ) );
>>        }
>>        copy.setHttp11(m.isHttp11());
>>        copy.setStrictMode(m.isStrictMode());
>>    }
>>
>>    /**
>>     * Clones a HttpMethod. <br>
>>     * <b>Attention:</b> You have to clone a method before it has been
>>executed, because the URI
>>     * can change if followRedirects is set to true.
>>     *
>>     * @param m the HttpMethod to clone
>>     *
>>     * @return the cloned HttpMethod, null if the HttpMethod could not be
>>instantiated
>>     *
>>     * @throws java.io.IOException if the request body couldn't be read
>>     */
>>    public static HttpMethod clone(HttpMethod m) throws java.io.IOException
>>{
>>        log.debug( "clone HttpMethod" );
>>        HttpMethod copy = null;
>>
>>        // copy the HttpMethod
>>        try {
>>            copy = (HttpMethod) m.getClass().newInstance();
>>        } catch (InstantiationException iEx) {
>>        } catch (IllegalAccessException iaEx) {
>>        }
>>        if ( copy == null ) {
>>            return null;
>>        }
>>        copy.setDoAuthentication(m.getDoAuthentication());
>>        copy.setFollowRedirects(m.getFollowRedirects());
>>        copy.setPath( m.getPath() );
>>        copy.setQueryString(m.getQueryString());
>>
>>        // clone the headers
>>        Header[] h = m.getRequestHeaders();
>>        int size = (h == null) ? 0 : h.length;
>>
>>        for (int i = 0; i < size; i++) {
>>            copy.setRequestHeader(new Header(h[i].getName(),
>>h[i].getValue()));
>>        }
>>
>>        copy.setStrictMode(m.isStrictMode());
>>
>>        if ( m instanceof HttpMethodBase ) {
>>            copyHttpMethodBase( (HttpMethodBase) m, (HttpMethodBase) copy );
>>        }
>>        if ( m instanceof EntityEnclosingMethod ) {
>>            copyEntityEnclosingMethod( (EntityEnclosingMethod) m,
>>(EntityEnclosingMethod) copy );
>>        }
>>
>>        return copy;
>>    }
>>}
>>
>>---------------------------------------------------------------------
>>To unsubscribe, e-mail: commons-httpclient-dev-unsubscribe@jakarta.apache.org
>>For additional commands, e-mail: commons-httpclient-dev-help@jakarta.apache.org
>>
>>    
>>
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: commons-httpclient-dev-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: commons-httpclient-dev-help@jakarta.apache.org
>
>  
>