Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
HTTPClientNetworkResource |
|
| 4.5;4.5 |
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.io.InputStream; | |
21 | ||
22 | import org.apache.commons.httpclient.HttpClient; | |
23 | import org.apache.commons.httpclient.HttpMethod; | |
24 | import org.apache.commons.httpclient.methods.GetMethod; | |
25 | ||
26 | /** | |
27 | * | |
28 | * This is an exprimental ResourceRequest which used Jakarta HttpClient as the | |
29 | * backend. Current its only meant for use in development as its not as | |
30 | * reliable and stable as the URLResourceRequest. | |
31 | * | |
32 | * Most of this code isn't as functional as the URLResourceRequest including | |
33 | * correct timeout behavior, redirect limits, header and etag support, etc. | |
34 | * | |
35 | * @author <a href="mailto:burton@openprivacy.org">Kevin A. Burton</a> | |
36 | * @version $Id: HTTPClientNetworkResource.java 373622 2006-01-30 22:53:00Z mvdb $ | |
37 | */ | |
38 | 0 | public class HTTPClientNetworkResource extends BaseResourceRequest implements ResourceRequest { |
39 | ||
40 | public static final int TIMEOUT = 3 * 1000 * 60; | |
41 | ||
42 | 0 | private HttpClient client = new HttpClient(); |
43 | ||
44 | /** | |
45 | * | |
46 | * Create a new <code>URLNetworkResource</code> instance. | |
47 | * | |
48 | * | |
49 | */ | |
50 | public void init() throws NetworkException { | |
51 | ||
52 | try { | |
53 | ||
54 | 0 | client.setConnectionTimeout( TIMEOUT ); |
55 | 0 | client.setStrictMode( false ); |
56 | //client.setFollowRedirects( true ); | |
57 | ||
58 | 0 | } catch ( Exception e ) { |
59 | ||
60 | 0 | throw new NetworkException( e ); |
61 | ||
62 | 0 | } |
63 | ||
64 | 0 | } |
65 | ||
66 | /** | |
67 | * | |
68 | * | |
69 | * | |
70 | */ | |
71 | public InputStream getInputStream() throws IOException { | |
72 | ||
73 | try { | |
74 | ||
75 | //now get the method so that we can execute it. | |
76 | 0 | HttpMethod method = new GetMethod( getResource() ); |
77 | 0 | method.setFollowRedirects( true ); |
78 | ||
79 | 0 | int result = client.executeMethod( method); |
80 | ||
81 | 0 | if ( result >= 400 && result < 500 ) { |
82 | 0 | throw new NetworkException( "HTTP " + result + " - " + method.getStatusText() ); |
83 | } | |
84 | ||
85 | 0 | InputStream is = new AdvancedInputStream( method.getResponseBodyAsStream(), this ); |
86 | ||
87 | 0 | return is; |
88 | ||
89 | 0 | } catch ( Exception e ) { |
90 | ||
91 | 0 | throw new NetworkException( e ); |
92 | ||
93 | } | |
94 | ||
95 | } | |
96 | ||
97 | } |