001    /*
002     * Copyright 1999,2004 The Apache Software Foundation.
003     * 
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     * 
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     * 
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    package org.apache.commons.feedparser.network;
018    
019    import java.io.IOException;
020    import java.io.InputStream;
021    
022    import org.apache.commons.httpclient.HttpClient;
023    import org.apache.commons.httpclient.HttpMethod;
024    import org.apache.commons.httpclient.methods.GetMethod;
025    
026    /**
027     *
028     * This is an exprimental ResourceRequest which used Jakarta HttpClient as the
029     * backend.  Current its only meant for use in development as its not as
030     * reliable and stable as the URLResourceRequest.
031     * 
032     * Most of this code isn't as functional as the URLResourceRequest including
033     * correct timeout behavior, redirect limits, header and etag support, etc.
034     * 
035     * @author <a href="mailto:burton@openprivacy.org">Kevin A. Burton</a>
036     * @version $Id: HTTPClientNetworkResource.java 373622 2006-01-30 22:53:00Z mvdb $
037     */
038    public class HTTPClientNetworkResource extends BaseResourceRequest implements ResourceRequest {
039    
040        public static final int TIMEOUT = 3 * 1000 * 60;
041        
042        private HttpClient client = new HttpClient();
043        
044        /**
045         * 
046         * Create a new <code>URLNetworkResource</code> instance.
047         *
048         * 
049         */
050        public void init() throws NetworkException {
051    
052            try { 
053    
054                client.setConnectionTimeout( TIMEOUT );
055                client.setStrictMode( false );
056                //client.setFollowRedirects( true );
057    
058            } catch ( Exception e ) {
059    
060                throw new NetworkException( e );
061                
062            }
063    
064        }
065    
066        /**
067         * 
068         *
069         * 
070         */
071        public InputStream getInputStream() throws IOException {
072    
073            try {
074    
075                //now get the method so that we can execute it.
076                HttpMethod method = new GetMethod( getResource() );
077                method.setFollowRedirects( true );
078    
079                int result = client.executeMethod( method);
080    
081                if ( result >= 400 && result < 500 ) {
082                    throw new NetworkException( "HTTP " + result + " - " + method.getStatusText() );
083                } 
084    
085                InputStream is = new AdvancedInputStream( method.getResponseBodyAsStream(), this );
086    
087                return is;
088    
089            } catch ( Exception e ) {
090                
091                throw new NetworkException( e );
092    
093            }
094            
095        }
096    
097    }