2009. 6. 24. 21:06
ROME 라이브러리를 이용하여 RSS 피드 읽어 오기
2009. 6. 24. 21:06 in JAVA
1. 필요한 라이브러리
2. 피드 읽기 위한 소스
- RSSApi.java
이 프로그램을 응용하면 Bloglines같은 웹 기반의 RSS Reader 프로그램으로 확장할 수 있겠죠. 나중에 시간을 투자하여 한번 만들어 볼까 합니다.
2. 피드 읽기 위한 소스
- RSSApi.java
package client;- SearchObject.java
import java.net.URL;
import java.net.URLEncoder;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.fetcher.impl.HashMapFeedInfoCache;
import com.sun.syndication.fetcher.impl.HttpClientFeedFetcher;
public class RSSApi
{
private final Log log = LogFactory.getLog(this.getClass());
private URL serviceUrl = null;
private HttpClientFeedFetcher fetcher = null;
/**
* Constructs a RSSApi instance.
* @param serviceUrl RSS호출 URL.
* @param useLocalCache 응답을 캐싱할 것인지.
*/
public RSSApi(URL serviceUrl, boolean useLocalCache)
{
super();
this.serviceUrl = serviceUrl;
fetcher = new HttpClientFeedFetcher();
fetcher.setUserAgent("RssAPIFetcher-1.0");
if (useLocalCache)
fetcher.setFeedInfoCache(HashMapFeedInfoCache.getInstance());
}
/**
* Feed 호출.
* @param methodName 실행 함수 이름.
* @param params Map 값.
* @return SyndFeed 객체.
*/
public SyndFeed execute(String methodName, Map<String, String> params)
{
URL feedUrl = buildUrl(methodName, params);
SyndFeed feed = null;
try {
feed = fetcher.retrieveFeed(feedUrl);
} catch (Exception e) {
log.error(e);
}
return feed;
}
/**
* 호출 URL Build
* @param methodName 실행 함수 이름.
* @param params Map 파라미터.
* @return
*/
private URL buildUrl(String methodName, Map<String,String> params)
{
StringBuilder urlBuilder = null;
int numParams = 0;
URL url = null;
try {
urlBuilder = new StringBuilder(serviceUrl.toString());
urlBuilder.append("/").append(methodName);
for (String paramName : params.keySet()) {
String paramValue = params.get(paramName);
if (StringUtils.isBlank(paramValue)) {
continue;
}
paramValue = URLEncoder.encode(paramValue, "UTF-8");
urlBuilder.append(numParams == 0 ? "?" : "&").
append(paramName).
append("=").
append(paramValue);
numParams++;
}
url = new URL(urlBuilder.toString());
if (log.isDebugEnabled())
log.debug("Requesting:[" +
urlBuilder.toString() + "]");
} catch (Exception e) {
log.error(e);
}
return url;
}
}
package client;- RSSReader.java
public class SearchObject
{
private String title;
private String url;
private String summary;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getSummary() {
return summary;
}
public void setSummary(String summary) {
this.summary = summary;
}
}
package client;3. 실행 결과
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndFeed;
public class RSSReader
{
private final Log log = LogFactory.getLog(this.getClass());
private static final String SERVICE_URL = "http://blogrss.paran.com";
private static final boolean USE_CACHE = true;
private RSSApi rssApi;
public RSSReader() throws Exception
{
rssApi = new RSSApi(new URL(SERVICE_URL), USE_CACHE);
}
@SuppressWarnings("unchecked")
public List<SearchObject> getFeedStories()
{
List<SearchObject> results = null;
List<SyndEntry> entries = null;
Map<String,String> clients = null;
try {
clients = new HashMap<String,String>();
clients.put("pmcid", "pmang");
SyndFeed feed = rssApi.execute("RssView2.do", clients);
entries = feed.getEntries();
results = new ArrayList<SearchObject>();
for (SyndEntry entry : entries) {
SearchObject result = new SearchObject();
result.setTitle(entry.getTitle());
result.setUrl(entry.getLink());
result.setSummary(entry.getDescription().getValue());
results.add(result);
}
} catch (Exception e) {
log.error(e);
}
return results;
}
public static void main(String[] args)
{
RSSReader client = null;
List<SearchObject> results = null;
try {
client = new RSSReader();
results = client.getFeedStories();
for (SearchObject result : results) {
System.out.println("TITLE:" + result.getTitle());
System.out.println("URL:" + result.getUrl());
System.out.println("SUMMARY:" + result.getSummary());
System.out.println("----------------------------------");
}
} catch (Exception e) {
System.err.println(e.getMessage());
throw new RuntimeException(e);
}
}
}
이 프로그램을 응용하면 Bloglines같은 웹 기반의 RSS Reader 프로그램으로 확장할 수 있겠죠. 나중에 시간을 투자하여 한번 만들어 볼까 합니다.