You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@excalibur.apache.org by un...@apache.org on 2004/07/27 14:47:16 UTC

svn commit: rev 30781 - excalibur/trunk/components/sourceresolve/src/java/org/apache/excalibur/source/impl

Author: unico
Date: Tue Jul 27 05:47:15 2004
New Revision: 30781

Modified:
   excalibur/trunk/components/sourceresolve/src/java/org/apache/excalibur/source/impl/HTTPClientSource.java
   excalibur/trunk/components/sourceresolve/src/java/org/apache/excalibur/source/impl/HTTPClientSourceFactory.java
Log:
- add support for stateful client/server communication
- make executeMethod protected in order to allow subclassing (eg Webdav)
- make sure connections are released


Modified: excalibur/trunk/components/sourceresolve/src/java/org/apache/excalibur/source/impl/HTTPClientSource.java
==============================================================================
--- excalibur/trunk/components/sourceresolve/src/java/org/apache/excalibur/source/impl/HTTPClientSource.java	(original)
+++ excalibur/trunk/components/sourceresolve/src/java/org/apache/excalibur/source/impl/HTTPClientSource.java	Tue Jul 27 05:47:15 2004
@@ -36,6 +36,7 @@
 import org.apache.commons.httpclient.Header;
 import org.apache.commons.httpclient.HttpClient;
 import org.apache.commons.httpclient.HttpMethod;
+import org.apache.commons.httpclient.HttpState;
 import org.apache.commons.httpclient.HttpStatus;
 import org.apache.commons.httpclient.NameValuePair;
 import org.apache.commons.httpclient.methods.DeleteMethod;
@@ -43,7 +44,15 @@
 import org.apache.commons.httpclient.methods.HeadMethod;
 import org.apache.commons.httpclient.methods.PostMethod;
 import org.apache.commons.httpclient.methods.PutMethod;
-import org.apache.excalibur.source.*;
+import org.apache.excalibur.source.ModifiableSource;
+import org.apache.excalibur.source.Source;
+import org.apache.excalibur.source.SourceException;
+import org.apache.excalibur.source.SourceFactory;
+import org.apache.excalibur.source.SourceNotFoundException;
+import org.apache.excalibur.source.SourceParameters;
+import org.apache.excalibur.source.SourceResolver;
+import org.apache.excalibur.source.SourceUtil;
+import org.apache.excalibur.source.SourceValidity;
 import org.apache.excalibur.source.impl.validity.TimeStampValidity;
 
 /**
@@ -91,7 +100,7 @@
      * Constant used when obtaining the Last-Modified date from HTTP Headers
      */
     public static final String LAST_MODIFIED  = "Last-Modified";
-
+    
     /**
      * The URI being accessed.
      */
@@ -101,6 +110,11 @@
      * Contextual parameters passed via the {@link SourceFactory}.
      */
     private final Map m_parameters;
+    
+    /**
+     * Optional http state passed from SourceFactory
+     */
+    private final HttpState m_httpState;
 
     /**
      * The {@link HttpClient} object.
@@ -159,12 +173,12 @@
      * @param parameters contextual parameters passed to this instance
      * @exception Exception if an error occurs
      */
-    public HTTPClientSource( final String uri, final Map parameters )
+    public HTTPClientSource( final String uri, final Map parameters, final HttpState httpState )
         throws Exception
     {
         m_uri = uri;
-        m_parameters = 
-            parameters == null ? Collections.EMPTY_MAP : parameters;
+        m_parameters = parameters == null ? Collections.EMPTY_MAP : parameters;
+        m_httpState = httpState;
     }
 
     /**
@@ -204,6 +218,10 @@
         {
             m_client.getHostConfiguration().setProxy( m_proxyHost, m_proxyPort );
         }
+        if (m_httpState != null)
+        {
+            m_client.setState(m_httpState);
+        }
 
         m_dataValid = false;
     }
@@ -364,11 +382,10 @@
         {
             if ( GET.equals( findMethodType() ) )
             {
+                final HttpMethod head = createHeadMethod( m_uri );
                 try
                 {
-                    final HttpMethod head = createHeadMethod( m_uri );
                     executeMethod( head );
-                    head.releaseConnection();
                     return;
                 }
                 catch ( final IOException e )
@@ -380,6 +397,9 @@
                         );
                     }
                 }
+                finally {
+                    head.releaseConnection();
+                }
             }
 
             // default values when response data is not available
@@ -399,9 +419,10 @@
      * @return response code from server
      * @exception IOException if an error occurs
      */
-    private int executeMethod( final HttpMethod method )
+    protected int executeMethod( final HttpMethod method )
         throws IOException
     {
+
         final int response = m_client.executeMethod( method );
 
         updateExists( method );
@@ -464,7 +485,7 @@
         throws IOException, SourceNotFoundException
     {
         final HttpMethod method = getMethod();
-        final int response = executeMethod( method );
+        int response = executeMethod( method );
         m_dataValid = true;
 
         // throw SourceNotFoundException - according to Source API we
@@ -480,7 +501,7 @@
 
             throw new SourceNotFoundException( error.toString() );
         }
-
+        
         return method.getResponseBodyAsStream();
     }
 
@@ -742,7 +763,7 @@
         private void upload()
             throws IOException
         {
-            HttpMethod uploader = null;
+            final HttpMethod uploader = createPutMethod( m_uri, m_file );
 
             if ( m_logger.isDebugEnabled() )
             {
@@ -751,7 +772,6 @@
 
             try
             {
-                uploader = createPutMethod( m_uri, m_file );
                 final int response = executeMethod( uploader );
 
                 if ( !successfulUpload( response ) )
@@ -799,10 +819,10 @@
      */
     public void delete() throws SourceException
     {
+        final DeleteMethod delete = createDeleteMethod( m_uri );
         try
         {
-            final int response =
-                executeMethod( createDeleteMethod( m_uri ) );
+            final int response = executeMethod( delete );
 
             if ( !deleteSuccessful( response ) )
             {
@@ -821,6 +841,10 @@
             throw new SourceException(
                 "IOException thrown during delete", e 
             );
+        }
+        finally 
+        {
+            delete.releaseConnection();
         }
     }
 

Modified: excalibur/trunk/components/sourceresolve/src/java/org/apache/excalibur/source/impl/HTTPClientSourceFactory.java
==============================================================================
--- excalibur/trunk/components/sourceresolve/src/java/org/apache/excalibur/source/impl/HTTPClientSourceFactory.java	(original)
+++ excalibur/trunk/components/sourceresolve/src/java/org/apache/excalibur/source/impl/HTTPClientSourceFactory.java	Tue Jul 27 05:47:15 2004
@@ -58,7 +58,7 @@
         try
         {
             final HTTPClientSource source = 
-                new HTTPClientSource( uri, sourceParams );
+                new HTTPClientSource( uri, sourceParams, null );
             ContainerUtil.enableLogging( source, getLogger() );
             ContainerUtil.parameterize( source, m_parameters );
             ContainerUtil.initialize( source );

---------------------------------------------------------------------
To unsubscribe, e-mail: scm-unsubscribe@excalibur.apache.org
For additional commands, e-mail: scm-help@excalibur.apache.org