You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by bu...@apache.org on 2005/01/27 08:35:09 UTC

cvs commit: jakarta-commons-sandbox/feedparser/src/java/org/apache/commons/feedparser/network HTTPClientNetworkResource.java

burton      2005/01/26 23:35:09

  Modified:    feedparser/src/java/org/apache/commons/feedparser/network
                        HTTPClientNetworkResource.java
  Log:
  Refactored Jakarta HttpClient network implementation to compile correctly...
  
  Revision  Changes    Path
  1.2       +27 -64    jakarta-commons-sandbox/feedparser/src/java/org/apache/commons/feedparser/network/HTTPClientNetworkResource.java
  
  Index: HTTPClientNetworkResource.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/feedparser/src/java/org/apache/commons/feedparser/network/HTTPClientNetworkResource.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- HTTPClientNetworkResource.java	25 Jan 2005 07:55:19 -0000	1.1
  +++ HTTPClientNetworkResource.java	27 Jan 2005 07:35:08 -0000	1.2
  @@ -24,6 +24,10 @@
   import org.apache.commons.httpclient.methods.*;
   
   /**
  + *
  + * This is an exprimental ResourceRequest which used Jakarta HttpClient as the
  + * backend.  Current its only meant for use in development as its not as
  + * reliable and stable as the URLResourceRequest.
    * 
    * @author <a href="mailto:burton@openprivacy.org">Kevin A. Burton</a>
    * @version $Id$
  @@ -32,9 +36,7 @@
   
       public static final int TIMEOUT = 3 * 1000 * 60;
       
  -    public static HttpConnectionManager hcm = new HttpConnectionManager();
  -
  -    private HttpConnection conn = null;
  +    private HttpClient client = new HttpClient();
       
       /**
        * 
  @@ -42,25 +44,17 @@
        *
        * @author <a href="mailto:burton@openprivacy.org">Kevin A. Burton (burtonator)</a>
        */
  -    public void init() throws IOException {
  +    public void init() throws NetworkException {
   
           try { 
  -            
  -            this.conn = hcm.getConnection( getResource() );
  -            
  -            if ( this.conn.isOpen() == false ) {
  -                
  -                //set socket timeout...
  -                this.conn.setSoTimeout( TIMEOUT );
  -                
  -            } 
  -            
  -        } catch ( HttpException e ) {
   
  -            IOException e1 = new IOException( e.getMessage() );
  -            e1.initCause( e );
  -            
  -            throw e1;
  +            client.setConnectionTimeout( TIMEOUT );
  +            client.setStrictMode( false );
  +            //client.setFollowRedirects( true );
  +
  +        } catch ( Exception e ) {
  +
  +            throw new NetworkException( e );
               
           }
   
  @@ -73,58 +67,27 @@
        */
       public InputStream getInputStream() throws IOException {
   
  -//         try {
  -
  -//             //now get the method so that we can execute it.
  -//             HttpMethod method = new GetMethod( new HttpMethod( getResource() ).getPath() );
  -//             method.setFollowRedirects( true );
  -
  -//             int result = method.execute( new HttpState(), conn );
  -
  -//             //FIXME: 302 isn't being followed.
  +        try {
   
  -//             //302 redirect
  -//             if ( method.getStatusCode() == HttpURLConnection.HTTP_MOVED_TEMP ) {
  +            //now get the method so that we can execute it.
  +            HttpMethod method = new GetMethod( getResource() );
  +            method.setFollowRedirects( true );
   
  -//                 //technically we shouldn't get this.. but if we do
  +            int result = client.executeMethod( method);
   
  -//                 String location = method.getResponseHeader( "Location" ).getValue();
  -
  -//                 Log.message( "Redirect: " + location );
  -
  -//                 HTTPClientNetworkResource redirect = new HTTPClientNetworkResource();
  -//                 redirect.setResource( location );
  -//                 redirect.init();
  -                
  -//                 return redirect.getInputStream();
  -                
  -//             }
  -            
  -//             // when should we throw an exception?  404?
  -
  -//             if ( method.getStatusCode() != 200 ) {
  -//                 throw new IOException( "HTTP " + method.getStatusCode() + " - " + method.getStatusText() );
  -//             } 
  +            if ( result >= 400 && result < 500 ) {
  +                throw new NetworkException( "HTTP " + result + " - " + method.getStatusText() );
  +            } 
   
  -//             InputStream is = new AdvancedInputStream( method.getResponseBodyAsStream(), this );
  +            InputStream is = new AdvancedInputStream( method.getResponseBodyAsStream(), this );
   
  -//             return is;
  +            return is;
   
  -//         } catch ( HttpException e ) {
  +        } catch ( Exception e ) {
               
  -//             throw new IOException( e.getMessage() );
  -            
  -//         } finally {
  -
  -//             //we have to release this connection when done or else we run out of them.
  -//             hcm.releaseConnection( conn );
  -
  -//         }
  +            throw new NetworkException( e );
   
  -        //NOTE: the above code does not compile and we are not using this code
  -        //so returning null is acceptable.
  -        
  -        return null;
  +        }
           
       }
   
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org