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.net.URL;
021    import java.net.URLConnection;
022    
023    import org.apache.log4j.Logger;
024    
025    /**
026     * 
027     * @author <a href="mailto:burton@openprivacy.org">Kevin A. Burton</a>
028     * @version $Id: NetworkException.java 373622 2006-01-30 22:53:00Z mvdb $
029     */
030    public class NetworkException extends IOException {
031    
032        private static Logger log = Logger.getLogger( NetworkException.class );
033    
034        private ResourceRequest request = null;
035    
036        public Exception e = null;
037    
038        private URL _url = null;
039    
040        private URLConnection _urlConnection = null;
041    
042        private int responseCode = -1;
043    
044        /**
045         * 
046         * Create a new <code>NetworkException</code> instance.
047         *
048         * 
049         */
050        public NetworkException( String message ) {
051            super( message );
052        }
053    
054        public NetworkException( Throwable t) {
055            super( t.getMessage() );
056        }
057    
058        /**
059         * 
060         * Create a new <code>NetworkException</code> instance.
061         *
062         * 
063         */
064        public NetworkException( String message,
065                                 Exception e,
066                                 ResourceRequest request,
067                                 URL _url,
068                                 URLConnection _urlConnection ) {
069    
070            super( message ); //why doesn't java.io.IOException support nesting?
071            this.e = e;
072            this.request = request;
073            this._url = _url;
074            this._urlConnection = _urlConnection;
075            initCause( e );
076            
077        }
078    
079        /**
080         * 
081         * Create a new <code>NetworkException</code> instance.
082         *
083         * 
084         */
085        public NetworkException( Exception e,
086                                 ResourceRequest request,
087                                 URL _url,
088                                 URLConnection _urlConnection ) {
089    
090            super( e.getMessage() ); //why doesn't java.io.IOException support nesting?
091            this.e = e;
092            this.request = request;
093            this._url = _url;
094            this._urlConnection = _urlConnection;
095            initCause( e );
096            
097        }
098    
099        public ResourceRequest getResourceRequest() {
100            return request;
101        }
102    
103        public URL getURL() {
104            return _url;
105        }
106    
107        public URLConnection getURLConnection() {
108            return _urlConnection;
109        }
110    
111        public Exception getException() {
112            return e;
113        }
114    
115        public int getResponseCode() {
116    
117            //FIXME: 
118            //        java.lang.NumberFormatException: For input string: "fie"
119            //         at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
120            //         at java.lang.Integer.parseInt(Integer.java:468)
121            //         at java.lang.Integer.parseInt(Integer.java:518)
122            //         at org.peerfear.newsmonster.network.NetworkException.getResponseCode(NetworkException.java:142)
123            //         at ksa.robot.FeedTask._doTaskLogFailure(FeedTask.java:264)
124            //         at ksa.robot.FeedTask.run(FeedTask.java:202)
125            //         at ksa.robot.TaskThread.doProcessTask(TaskThread.java:298)
126            //         at ksa.robot.TaskThread.run(TaskThread.java:111)
127            
128            if ( _urlConnection == null ) {
129                return -1;
130            } 
131    
132            if ( responseCode == -1 ) {
133    
134                //parse the exception
135                String status = (String)_urlConnection.getHeaderField( null );
136    
137                if ( status == null ) {
138                    return -1;
139                } 
140    
141                int begin = "HTTP/1.1 ".length();
142                int offset = "200".length();
143                int end = begin + offset;
144    
145                try {
146    
147                    responseCode = Integer.parseInt( status.substring( begin, end ) );
148                    
149                } catch ( NumberFormatException e ) {
150    
151                    log.warn( "Unable to parse response code in header: " + status );
152                    return -1;
153                    
154                }
155    
156            } 
157    
158            return responseCode;
159            
160        }
161        
162    }