RSSReader.java (4374B)
1 /* 2 * Copyright (C) 2010 A. Horn 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.mcsoxford.rss; 18 19 import java.io.IOException; 20 import java.io.InputStream; 21 22 import org.apache.http.HttpEntity; 23 import org.apache.http.HttpResponse; 24 import org.apache.http.HttpStatus; 25 import org.apache.http.StatusLine; 26 import org.apache.http.client.ClientProtocolException; 27 import org.apache.http.client.HttpClient; 28 import org.apache.http.client.methods.HttpGet; 29 import org.apache.http.impl.client.DefaultHttpClient; 30 31 /** 32 * HTTP client to retrieve and parse RSS 2.0 feeds. Callers must call 33 * {@link RSSReader#close()} to release all resources. 34 * 35 * @author Mr Horn 36 */ 37 public class RSSReader implements java.io.Closeable { 38 39 /** 40 * Thread-safe {@link HttpClient} implementation. 41 */ 42 private final HttpClient httpclient; 43 44 /** 45 * Thread-safe RSS parser SPI. 46 */ 47 private final RSSParserSPI parser; 48 49 /** 50 * Instantiate a thread-safe HTTP client to retrieve RSS feeds. The injected 51 * {@link HttpClient} implementation must be thread-safe. 52 * 53 * @param httpclient thread-safe HTTP client implementation 54 * @param parser thread-safe RSS parser SPI implementation 55 */ 56 public RSSReader(HttpClient httpclient, RSSParserSPI parser) { 57 this.httpclient = httpclient; 58 this.parser = parser; 59 } 60 61 /** 62 * Instantiate a thread-safe HTTP client to retrieve RSS feeds. The injected 63 * {@link HttpClient} implementation must be thread-safe. Internal memory 64 * consumption and load performance can be tweaked with {@link RSSConfig}. 65 * 66 * @param httpclient thread-safe HTTP client implementation 67 * @param config RSS configuration 68 */ 69 public RSSReader(HttpClient httpclient, RSSConfig config) { 70 this(httpclient, new RSSParser(config)); 71 } 72 73 /** 74 * Instantiate a thread-safe HTTP client to retrieve and parse RSS feeds. 75 * Internal memory consumption and load performance can be tweaked with 76 * {@link RSSConfig}. 77 */ 78 public RSSReader(RSSConfig config) { 79 this(new DefaultHttpClient(), new RSSParser(config)); 80 } 81 82 /** 83 * Instantiate a thread-safe HTTP client to retrieve and parse RSS feeds. 84 * Default RSS configuration capacity values are used. 85 */ 86 public RSSReader() { 87 this(new DefaultHttpClient(), new RSSParser(new RSSConfig())); 88 } 89 90 /** 91 * Send HTTP GET request and parse the XML response to construct an in-memory 92 * representation of an RSS 2.0 feed. 93 * 94 * @param uri RSS 2.0 feed URI 95 * @return in-memory representation of downloaded RSS feed 96 * @throws RSSReaderException if RSS feed could not be retrieved because of 97 * HTTP error 98 * @throws RSSFault if an unrecoverable IO error has occurred 99 */ 100 public RSSFeed load(String uri) throws RSSReaderException { 101 final HttpGet httpget = new HttpGet(uri); 102 103 InputStream feedStream = null; 104 try { 105 // Send GET request to URI 106 final HttpResponse response = httpclient.execute(httpget); 107 108 // Check if server response is valid 109 final StatusLine status = response.getStatusLine(); 110 if (status.getStatusCode() != HttpStatus.SC_OK) { 111 throw new RSSReaderException(status.getStatusCode(), 112 status.getReasonPhrase()); 113 } 114 115 // Extract content stream from HTTP response 116 HttpEntity entity = response.getEntity(); 117 feedStream = entity.getContent(); 118 119 RSSFeed feed = parser.parse(feedStream); 120 121 if (feed.getLink() == null) { 122 feed.setLink(android.net.Uri.parse(uri)); 123 } 124 125 return feed; 126 } catch (ClientProtocolException e) { 127 throw new RSSFault(e); 128 } catch (IOException e) { 129 throw new RSSFault(e); 130 } finally { 131 Resources.closeQuietly(feedStream); 132 } 133 } 134 135 /** 136 * Release all HTTP client resources. 137 */ 138 public void close() { 139 httpclient.getConnectionManager().shutdown(); 140 } 141 142 } 143