该组件实现分为以下几个部分ChannelEItem,ChannelItem,RssBuildFactory,RssComponentBean
/*
* ChannelEItem.java
* Copyright (C) 2009 <JustinLei@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
package org.lambdasoft.components.rss.ejb;
/**
* @author lei.tang (justinlei@gmail.com)
* @date
* @version
*/
public class ChannelEItem extends ChannelItem{
private static final long serialVersionUID = 1L;
private String enclosure;// 流媒体文件
public String getEnclosure() {
return enclosure;
}
public void setEnclosure(String enclosure) {
this.enclosure = enclosure;
}
}
/*
* ChannelItem.java
* Copyright (C) 2009 <JustinLei@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
package org.lambdasoft.components.rss.ejb;
import java.io.Serializable;
import java.util.Date;
/**
* @author lei.tang (justinlei@gmail.com)
* @date
* @version
*/
public class ChannelItem implements Serializable{
private static final long serialVersionUID = 1L;
private String title;// Rss文件中Item的标题
private String link;// Rss文件中Item对应的连接
private String description;// Item的描述
private Date pubDate;// Item发布的时间
private String author;// Item作者
private String category;// Item所属的频道范畴
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getLink() {
return link;
}
public void setLink(String link) {
this.link = link;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Date getPubDate() {
return pubDate;
}
public void setPubDate(Date pubDate) {
this.pubDate = pubDate;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
}
/*
* RssBuildFactory.java
* Copyright (C) 2009 <JustinLei@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
package org.lambdasoft.components.rss.ejb;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.sun.syndication.feed.synd.SyndCategory;
import com.sun.syndication.feed.synd.SyndCategoryImpl;
import com.sun.syndication.feed.synd.SyndContent;
import com.sun.syndication.feed.synd.SyndContentImpl;
import com.sun.syndication.feed.synd.SyndEnclosure;
import com.sun.syndication.feed.synd.SyndEnclosureImpl;
import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndEntryImpl;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.feed.synd.SyndFeedImpl;
import com.sun.syndication.io.FeedException;
import com.sun.syndication.io.SyndFeedOutput;
/**
* @author lei.tang (justinlei@gmail.com)
* @date
* @version
*/
public class RssBuildFactory {
private SyndFeed feed;
@SuppressWarnings("unchecked")
private List entries;
private SyndEntry entry;
@SuppressWarnings("unchecked")
public RssBuildFactory() {
feed = new SyndFeedImpl();
feed.setFeedType("rss_2.0");
entries = new ArrayList();
}
/**
* 创建一个频道
*
* @param title 频道标题
* @param link 频道对应的连接
* @param description 频道描述
* @param language 频道所用语言
* @param pubDate 频道发布时期
* @param copyright 版权所有
* @throws Exception
*/
public void buildChannel(String title, String link, String description,
String language, Date pubDate, String copyright)
throws RuntimeException {
feed.setTitle(title);
feed.setLink(link);
feed.setDescription(description);
feed.setLanguage(language);
feed.setPublishedDate(pubDate);
feed.setCopyright(copyright);
}
/**
* 添加频道的子内容
*
* @param item
* [email={@link]{@link[/email] ChannelItem}
* @throws Exception
*/
@SuppressWarnings("unchecked")
public void buildItems(ChannelItem item) throws RuntimeException {
entry = new SyndEntryImpl();
// 设置新闻标题
entry.setTitle(item.getTitle());
// 设置新闻的连接地址
entry.setLink(item.getLink());
// 设置新闻简介
SyndContent content = new SyndContentImpl();
content.setType("text/plain");
content.setValue(item.getDescription());
entry.setDescription(content);
// 设置发布时间
entry.setPublishedDate(item.getPubDate());
// 设置频道所属的范围
SyndCategory cate = new SyndCategoryImpl();
cate.setName(item.getCategory());
List cateList = new ArrayList();
cateList.add(cate);
entry.setCategories(cateList);
// 设置作者
entry.setAuthor(item.getAuthor());
// 将新闻项添加至数组中
entries.add(entry);
}
/**
* 添加频道的内容项
*
* @param item
* [email={@link]{@link[/email] ChannelEItem}此类继承自ChannelItem类
* @throws Exception
*/
@SuppressWarnings("unchecked")
public void buildItems(ChannelEItem item) throws RuntimeException {
entry = new SyndEntryImpl();
// 设置新闻标题
entry.setTitle(item.getTitle());
// 设置新闻的连接地址
entry.setLink(item.getLink());
// 设置新闻简介
SyndContent content = new SyndContentImpl();
content.setValue(item.getDescription());
entry.setDescription(content);
// 设置发布时间
entry.setPublishedDate(item.getPubDate());
// 设置频道所属的范围
SyndCategory cate = new SyndCategoryImpl();
cate.setName(item.getCategory());
List cateList = new ArrayList();
cateList.add(cate);
entry.setCategories(cateList);
// 设置作者
entry.setAuthor(item.getAuthor());
// 设置流媒体播放文件
SyndEnclosure en = new SyndEnclosureImpl();
en.setUrl(item.getEnclosure());
List enList = new ArrayList();
enList.add(en);
entry.setEnclosures(enList);
// 将新闻项添加至数组中
entries.add(entry);
}
/**
* 生成XML文件
*
* @param outputStream
* @throws Exception
*/
public String buildChannel() throws IOException {
feed.setEntries(entries);
SyndFeedOutput output = new SyndFeedOutput();
Writer writer;
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
writer = new OutputStreamWriter(outputStream, "UTF-8");
try {
output.output(feed, writer);
return new String(outputStream.toByteArray());
} catch (FeedException e) {
throw new IOException();
}
}
}
/*
* RssComponentBean.java
* Copyright (C) 2009 <JustinLei@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
package org.lambdasoft.components.rss.ejb;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.interceptor.Interceptors;
import org.lambdasoft.components.application.RssComponent;
import org.lambdasoft.components.application.param.RssBuildParam;
import org.lambdasoft.ejb.interceptors.EjbCheckParamInterceptor;
/**
* @author lei.tang (justinlei@gmail.com)
* @date
* @version
*/
@Stateless
@Remote
@Interceptors({EjbCheckParamInterceptor.class})
public class RssComponentBean implements RssComponent{
public String buildChannel(RssBuildParam rssBuildParam) throws Exception{
if(rssBuildParam.getChannelEItems() == null)
return null;
RssBuildFactory builder = new RssBuildFactory();
for (ChannelEItem channelEItem : rssBuildParam.getChannelEItems()) {
builder.buildItems(channelEItem);
}
builder.buildChannel(rssBuildParam.getTitle(),rssBuildParam.getLink(), rssBuildParam.getDescription(),
rssBuildParam.getLanguage(), rssBuildParam.getPubDate(), rssBuildParam.getCopyright());
return builder.buildChannel();
}
}