avalon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From craft...@apache.org
Subject cvs commit: avalon-excalibur/sourceresolve/src/java/org/apache/excalibur/source/impl HTTPClientSource.java HTTPClientSourceFactory.java
Date Thu, 03 Jul 2003 09:46:32 GMT
crafterm    2003/07/03 02:46:32

  Modified:    sourceresolve/src/java/org/apache/excalibur/source/impl
                        HTTPClientSource.java HTTPClientSourceFactory.java
  Log:
  Added support for HTTP PUT and HTTP proxies.
  
  Revision  Changes    Path
  1.2       +131 -6    avalon-excalibur/sourceresolve/src/java/org/apache/excalibur/source/impl/HTTPClientSource.java
  
  Index: HTTPClientSource.java
  ===================================================================
  RCS file: /home/cvs/avalon-excalibur/sourceresolve/src/java/org/apache/excalibur/source/impl/HTTPClientSource.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- HTTPClientSource.java	2 Jul 2003 13:23:58 -0000	1.1
  +++ HTTPClientSource.java	3 Jul 2003 09:46:32 -0000	1.2
  @@ -57,19 +57,29 @@
   import java.io.IOException;
   import java.io.InputStream;
   import java.util.Date;
  +import java.util.Iterator;
   import java.util.Map;
   
   import org.apache.avalon.framework.CascadingRuntimeException;
   import org.apache.avalon.framework.activity.Initializable;
   import org.apache.avalon.framework.activity.Disposable;
   import org.apache.avalon.framework.logger.AbstractLogEnabled;
  +import org.apache.avalon.framework.parameters.ParameterException;
  +import org.apache.avalon.framework.parameters.Parameterizable;
  +import org.apache.avalon.framework.parameters.Parameters;
   import org.apache.commons.httpclient.Header;
  +import org.apache.commons.httpclient.HostConfiguration;
   import org.apache.commons.httpclient.HttpClient;
  +import org.apache.commons.httpclient.HttpMethod;
   import org.apache.commons.httpclient.HttpStatus;
  +import org.apache.commons.httpclient.NameValuePair;
   import org.apache.commons.httpclient.URIException;
   import org.apache.commons.httpclient.methods.GetMethod;
  +import org.apache.commons.httpclient.methods.PostMethod;
   import org.apache.excalibur.source.Source;
   import org.apache.excalibur.source.SourceNotFoundException;
  +import org.apache.excalibur.source.SourceParameters;
  +import org.apache.excalibur.source.SourceResolver;
   import org.apache.excalibur.source.SourceValidity;
   import org.apache.excalibur.source.impl.validity.TimeStampValidity;
   
  @@ -82,9 +92,29 @@
    * @version CVS $Id$
    */
   public class HTTPClientSource extends AbstractLogEnabled 
  -    implements Source, Initializable, Disposable
  +    implements Source, Initializable, Parameterizable, Disposable
   {
       /**
  +     * Constant used for identifying POST requests.
  +     */
  +    public static final String POST           = "POST";
  +
  +    /**
  +     * Constant used for identifying GET requests.
  +     */
  +    public static final String GET            = "GET";
  +
  +    /**
  +     * Constant used for configuring the proxy hostname.
  +     */
  +    public static final String PROXY_HOST     = "proxy.host";
  +
  +    /**
  +     * Constant used for configuring the proxy port number.
  +     */
  +    public static final String PROXY_PORT     = "proxy.port";
  +
  +    /**
        * Constant used when obtaining the Content-Type from HTTP Headers
        */
       public static final String CONTENT_TYPE   = "Content-Type";
  @@ -117,7 +147,17 @@
       /**
        * The {@link HttpMethod} being performed on the {@link HttpClient}.
        */
  -    private GetMethod m_method;
  +    private HttpMethod m_method;
  +
  +    /**
  +     * Proxy port if set via configuration.
  +     */
  +    private int m_proxyPort;
  +
  +    /**
  +     * Proxy host if set via configuration.
  +     */
  +    private String m_proxyHost;
   
       /**
        * HTTP response returned from server after the {@link HttpMethod}
  @@ -128,7 +168,7 @@
       /**
        * Stored {@link SourceValidity} object.
        */
  -    private  SourceValidity m_cachedValidity;
  +    private SourceValidity m_cachedValidity;
   
       /**
        * Cached last modification date.
  @@ -150,6 +190,30 @@
       }
   
       /**
  +     * Parameterizes this {@link HTTPClientSource} instance.
  +     *
  +     * @param params a {@link Parameters} instance.
  +     * @exception ParameterException if an error occurs
  +     */
  +    public void parameterize( final Parameters params )
  +        throws ParameterException
  +    {
  +        m_proxyHost = params.getParameter( PROXY_HOST, null );
  +        m_proxyPort = params.getParameterAsInteger( PROXY_PORT, -1 );
  +
  +        if ( getLogger().isDebugEnabled() )
  +        {
  +            final String message =
  +                m_proxyHost == null || m_proxyPort == -1
  +                ? "No proxy configured"
  +                : "Configured with proxy host " 
  +                  + m_proxyHost + " port " + m_proxyPort;
  +
  +            getLogger().debug( message );
  +        }
  +    }
  +
  +    /**
        * Initializes this {@link HTTPClientSource} instance.
        *
        * @exception Exception if an error occurs
  @@ -158,9 +222,70 @@
       {
           m_client = new HttpClient();
   
  -        // REVISIT(MC): assume HTTP GET for the moment
  -        m_method = new GetMethod( m_uri );
  +        if ( m_proxyHost != null && m_proxyPort != -1 )
  +        {
  +            m_client.getHostConfiguration().setProxy( m_proxyHost, m_proxyPort );
  +        }
  +
  +        m_method = getMethod();
           m_response = m_client.executeMethod( m_method );
  +    }
  +
  +    /**
  +     * Helper method to create the required {@link HttpMethod} object
  +     * based on parameters passed to this {@link HTTPClientSource} object.
  +     *
  +     * @return a {@link HttpMethod} object.
  +     */
  +    private HttpMethod getMethod()
  +    {
  +        final SourceParameters params =
  +            (SourceParameters) m_parameters.get( SourceResolver.URI_PARAMETERS );
  +        final String method =
  +            (String) m_parameters.get( SourceResolver.METHOD );
  +
  +        // create a POST method if requested
  +        if ( POST.equals( method ) )
  +        {
  +            return createPostMethod( params );
  +        }
  +
  +        // default method is GET
  +        return new GetMethod( m_uri );
  +    }
  +
  +    /**
  +     * Helper method to create a new {@link PostMethod} with the given
  +     * {@link SourceParameters} object.
  +     *
  +     * @param params {@link SourceParameters}
  +     * @return a {@link PostMethod} instance
  +     */
  +    private PostMethod createPostMethod( final SourceParameters params )
  +    {
  +        final PostMethod post = new PostMethod( m_uri );
  +
  +        if ( params == null )
  +        {
  +            return post;
  +        }
  +
  +        for ( final Iterator names = params.getParameterNames();
  +              names.hasNext();
  +        )
  +        {
  +            final String name = (String) names.next();
  +
  +            for ( final Iterator values = params.getParameterValues( name );
  +                  values.hasNext();
  +            )
  +            {
  +                final String value = (String) values.next();
  +                post.addParameter( new NameValuePair( name, value ) );
  +            }
  +        }
  +
  +        return post;
       }
   
       /**
  
  
  
  1.2       +25 -4     avalon-excalibur/sourceresolve/src/java/org/apache/excalibur/source/impl/HTTPClientSourceFactory.java
  
  Index: HTTPClientSourceFactory.java
  ===================================================================
  RCS file: /home/cvs/avalon-excalibur/sourceresolve/src/java/org/apache/excalibur/source/impl/HTTPClientSourceFactory.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- HTTPClientSourceFactory.java	2 Jul 2003 13:23:58 -0000	1.1
  +++ HTTPClientSourceFactory.java	3 Jul 2003 09:46:32 -0000	1.2
  @@ -62,6 +62,9 @@
   import org.apache.avalon.framework.container.ContainerUtil;
   import org.apache.avalon.framework.logger.AbstractLogEnabled;
   import org.apache.avalon.framework.thread.ThreadSafe;
  +import org.apache.avalon.framework.parameters.ParameterException;
  +import org.apache.avalon.framework.parameters.Parameterizable;
  +import org.apache.avalon.framework.parameters.Parameters;
   import org.apache.excalibur.source.Source;
   import org.apache.excalibur.source.SourceException;
   import org.apache.excalibur.source.SourceFactory;
  @@ -78,19 +81,25 @@
    * @version CVS $Id$
    */
   public final class HTTPClientSourceFactory extends AbstractLogEnabled
  -    implements SourceFactory, ThreadSafe
  +    implements SourceFactory, Parameterizable, ThreadSafe
   {
       /**
  +     * Configuration information.
  +     */
  +    private Parameters m_parameters;
  +
  +    /**
        * Creates a {@link HTTPClientSource} instance.
        */
  -    public Source getSource( final String uri, final Map parameters )
  +    public Source getSource( final String uri, final Map sourceParams )
           throws MalformedURLException, IOException
       {
           try
           {
               final HTTPClientSource source = 
  -                new HTTPClientSource( uri, parameters );
  +                new HTTPClientSource( uri, sourceParams );
               ContainerUtil.enableLogging( source, getLogger() );
  +            ContainerUtil.parameterize( source, m_parameters );
               ContainerUtil.initialize( source );
               return source;
           }
  @@ -110,6 +119,18 @@
   
               throw new SourceException( message.toString(), e );
           }
  +    }
  +
  +    /**
  +     * Parameterize this {@link SourceFactory}.
  +     *
  +     * @param params {@link Parameters} instance
  +     * @exception ParameterException if an error occurs
  +     */
  +    public void parameterize( final Parameters params )
  +        throws ParameterException
  +    {
  +        m_parameters = params;
       }
   
       /**
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: cvs-unsubscribe@avalon.apache.org
For additional commands, e-mail: cvs-help@avalon.apache.org


Mime
View raw message