Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
NetworkException |
|
| 1.7777777777777777;1.778 |
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 | 0 | private static Logger log = Logger.getLogger( NetworkException.class ); |
33 | ||
34 | 0 | private ResourceRequest request = null; |
35 | ||
36 | 0 | public Exception e = null; |
37 | ||
38 | 0 | private URL _url = null; |
39 | ||
40 | 0 | private URLConnection _urlConnection = null; |
41 | ||
42 | 0 | private int responseCode = -1; |
43 | ||
44 | /** | |
45 | * | |
46 | * Create a new <code>NetworkException</code> instance. | |
47 | * | |
48 | * | |
49 | */ | |
50 | public NetworkException( String message ) { | |
51 | 0 | super( message ); |
52 | 0 | } |
53 | ||
54 | public NetworkException( Throwable t) { | |
55 | 0 | super( t.getMessage() ); |
56 | 0 | } |
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 | 0 | super( message ); //why doesn't java.io.IOException support nesting? |
71 | 0 | this.e = e; |
72 | 0 | this.request = request; |
73 | 0 | this._url = _url; |
74 | 0 | this._urlConnection = _urlConnection; |
75 | 0 | initCause( e ); |
76 | ||
77 | 0 | } |
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 | 0 | super( e.getMessage() ); //why doesn't java.io.IOException support nesting? |
91 | 0 | this.e = e; |
92 | 0 | this.request = request; |
93 | 0 | this._url = _url; |
94 | 0 | this._urlConnection = _urlConnection; |
95 | 0 | initCause( e ); |
96 | ||
97 | 0 | } |
98 | ||
99 | public ResourceRequest getResourceRequest() { | |
100 | 0 | return request; |
101 | } | |
102 | ||
103 | public URL getURL() { | |
104 | 0 | return _url; |
105 | } | |
106 | ||
107 | public URLConnection getURLConnection() { | |
108 | 0 | return _urlConnection; |
109 | } | |
110 | ||
111 | public Exception getException() { | |
112 | 0 | 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 | 0 | if ( _urlConnection == null ) { |
129 | 0 | return -1; |
130 | } | |
131 | ||
132 | 0 | if ( responseCode == -1 ) { |
133 | ||
134 | //parse the exception | |
135 | 0 | String status = (String)_urlConnection.getHeaderField( null ); |
136 | ||
137 | 0 | if ( status == null ) { |
138 | 0 | return -1; |
139 | } | |
140 | ||
141 | 0 | int begin = "HTTP/1.1 ".length(); |
142 | 0 | int offset = "200".length(); |
143 | 0 | int end = begin + offset; |
144 | ||
145 | try { | |
146 | ||
147 | 0 | responseCode = Integer.parseInt( status.substring( begin, end ) ); |
148 | ||
149 | 0 | } catch ( NumberFormatException e ) { |
150 | ||
151 | 0 | log.warn( "Unable to parse response code in header: " + status ); |
152 | 0 | return -1; |
153 | ||
154 | 0 | } |
155 | ||
156 | } | |
157 | ||
158 | 0 | return responseCode; |
159 | ||
160 | } | |
161 | ||
162 | } |