1 /*
2 * Copyright 1999,2004 The Apache Software Foundation.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package org.apache.commons.feedparser.network;
18
19 import java.io.IOException;
20 import java.net.URL;
21 import java.net.URLConnection;
22
23 import org.apache.log4j.Logger;
24
25 /**
26 *
27 * @author <a href="mailto:burton@openprivacy.org">Kevin A. Burton</a>
28 * @version $Id: NetworkException.java 373622 2006-01-30 22:53:00Z mvdb $
29 */
30 public class NetworkException extends IOException {
31
32 private static Logger log = Logger.getLogger( NetworkException.class );
33
34 private ResourceRequest request = null;
35
36 public Exception e = null;
37
38 private URL _url = null;
39
40 private URLConnection _urlConnection = null;
41
42 private int responseCode = -1;
43
44 /**
45 *
46 * Create a new <code>NetworkException</code> instance.
47 *
48 *
49 */
50 public NetworkException( String message ) {
51 super( message );
52 }
53
54 public NetworkException( Throwable t) {
55 super( t.getMessage() );
56 }
57
58 /**
59 *
60 * Create a new <code>NetworkException</code> instance.
61 *
62 *
63 */
64 public NetworkException( String message,
65 Exception e,
66 ResourceRequest request,
67 URL _url,
68 URLConnection _urlConnection ) {
69
70 super( message ); //why doesn't java.io.IOException support nesting?
71 this.e = e;
72 this.request = request;
73 this._url = _url;
74 this._urlConnection = _urlConnection;
75 initCause( e );
76
77 }
78
79 /**
80 *
81 * Create a new <code>NetworkException</code> instance.
82 *
83 *
84 */
85 public NetworkException( Exception e,
86 ResourceRequest request,
87 URL _url,
88 URLConnection _urlConnection ) {
89
90 super( e.getMessage() ); //why doesn't java.io.IOException support nesting?
91 this.e = e;
92 this.request = request;
93 this._url = _url;
94 this._urlConnection = _urlConnection;
95 initCause( e );
96
97 }
98
99 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 }