Author: vsiveton
Date: Sat Jun 13 13:59:38 2009
New Revision: 784394

URL: http://svn.apache.org/viewvc?rev=784394&view=rev
Log:
DOXIA-341: Error 503 when validate XHTML files

o refactor to use HttpClient

Modified:
    maven/doxia/doxia/trunk/doxia-core/pom.xml
    
maven/doxia/doxia/trunk/doxia-core/src/main/java/org/apache/maven/doxia/parser/AbstractXmlParser.java

Modified: maven/doxia/doxia/trunk/doxia-core/pom.xml
URL: 
http://svn.apache.org/viewvc/maven/doxia/doxia/trunk/doxia-core/pom.xml?rev=784394&r1=784393&r2=784394&view=diff
==============================================================================
--- maven/doxia/doxia/trunk/doxia-core/pom.xml (original)
+++ maven/doxia/doxia/trunk/doxia-core/pom.xml Sat Jun 13 13:59:38 2009
@@ -60,6 +60,11 @@
       <artifactId>commons-lang</artifactId>
       <version>2.4</version>
     </dependency>
+    <dependency>
+      <groupId>commons-httpclient</groupId>
+      <artifactId>commons-httpclient</artifactId>
+      <version>3.1</version>
+    </dependency>
 
     <!-- test -->
   </dependencies>

Modified: 
maven/doxia/doxia/trunk/doxia-core/src/main/java/org/apache/maven/doxia/parser/AbstractXmlParser.java
URL: 
http://svn.apache.org/viewvc/maven/doxia/doxia/trunk/doxia-core/src/main/java/org/apache/maven/doxia/parser/AbstractXmlParser.java?rev=784394&r1=784393&r2=784394&view=diff
==============================================================================
--- 
maven/doxia/doxia/trunk/doxia-core/src/main/java/org/apache/maven/doxia/parser/AbstractXmlParser.java
 (original)
+++ 
maven/doxia/doxia/trunk/doxia-core/src/main/java/org/apache/maven/doxia/parser/AbstractXmlParser.java
 Sat Jun 13 13:59:38 2009
@@ -39,6 +39,12 @@
 
 import javax.xml.XMLConstants;
 
+import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
+import org.apache.commons.httpclient.HttpClient;
+import org.apache.commons.httpclient.HttpException;
+import org.apache.commons.httpclient.HttpStatus;
+import org.apache.commons.httpclient.methods.GetMethod;
+import org.apache.commons.httpclient.params.HttpMethodParams;
 import org.apache.maven.doxia.logging.Log;
 import org.apache.maven.doxia.macro.MacroExecutionException;
 import org.apache.maven.doxia.markup.XmlMarkup;
@@ -967,33 +973,68 @@
         }
 
         /**
-         * Wrap {...@link IOUtil#toByteArray(java.io.InputStream)} to throw 
SAXException.
+         * If url is not an http/https urls, call {...@link 
IOUtil#toByteArray(java.io.InputStream)} to get the url
+         * content.
+         * Otherwise, call {...@link GetMethod#getResponseBody()} from 
HttpClient to get the http content.
+         * Wrap all internal exceptions to throw SAXException.
          *
          * @param url not null
          * @return return an array of byte
          * @throws SAXException if any
-         * @see {...@link IOUtil#toByteArray(java.io.InputStream)}
          */
         private static byte[] toByteArray( URL url )
             throws SAXException
         {
-            InputStream is = null;
+            if ( !( url.getProtocol().equalsIgnoreCase( "http" ) || 
url.getProtocol().equalsIgnoreCase( "https" ) ) )
+            {
+                InputStream is = null;
+                try
+                {
+                    is = url.openStream();
+                    if ( is == null )
+                    {
+                        throw new SAXException( "Cannot open stream from the 
url: " + url.toString() );
+                    }
+                    return IOUtil.toByteArray( is );
+                }
+                catch ( IOException e )
+                {
+                    throw new SAXException( "IOException: " + e.getMessage(), 
e );
+                }
+                finally
+                {
+                    IOUtil.close( is );
+                }
+            }
+
+            // it is an HTTP url, using HttpClient...
+            HttpClient client = new HttpClient();
+            GetMethod method = new GetMethod( url.toString() );
+
+            method.getParams().setParameter( HttpMethodParams.RETRY_HANDLER,
+                                             new 
DefaultHttpMethodRetryHandler( 3, false ) );
+
             try
             {
-                is = url.openStream();
-                if ( is == null )
+                int statusCode = client.executeMethod( method );
+                if ( statusCode != HttpStatus.SC_OK )
                 {
-                    throw new SAXException( "Cannot open stream from the url: 
" + url.toString() );
+                    throw new IOException( "Method failed: " + 
method.getStatusLine() );
                 }
-                return IOUtil.toByteArray( is );
+
+                return method.getResponseBody();
+            }
+            catch ( HttpException e )
+            {
+                throw new SAXException( "HttpException: Fatal protocol 
violation: " + e.getMessage(), e );
             }
             catch ( IOException e )
             {
-                throw new SAXException( "IOException: " + e.getMessage(), e );
+                throw new SAXException( "IOException: Fatal transport error: " 
+ e.getMessage(), e );
             }
             finally
             {
-                IOUtil.close( is );
+                method.releaseConnection();
             }
         }
 


Reply via email to