}
String requestUrl = req.getRequestURL().toString();
String base = requestUrl.substring(0, requestUrl.lastIndexOf('/'));
Metadata channelMet = new Metadata();
channelMet.addMetadata("ProductType", productTypeName);
channelMet.addMetadata("ProductTypeId", productTypeId);
channelMet.addMetadata("TopN", String.valueOf(topN));
channelMet.addMetadata("BaseUrl", base);
List products = null;
try {
if (productTypeName.equals("ALL")) {
products = fm.getTopNProducts(top);
} else {
try {
type = fm.getProductTypeById(productTypeId);
} catch (RepositoryManagerException e) {
LOG.log(Level.SEVERE,
"Unable to obtain product type from product type id: ["
+ productTypeId + "]: Message: " + e.getMessage());
return;
}
products = fm.getTopNProducts(top, type);
}
} catch (CatalogException e) {
e.printStackTrace();
LOG
.log(Level.WARNING,
"Exception getting products from Catalog: Message: "
+ e.getMessage());
return;
}
if (products != null && products.size() > 0) {
String channelDesc = null;
if (!productTypeName.equals("ALL")) {
channelDesc = type.getDescription();
} else {
channelDesc = "ALL";
}
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
Document doc = factory.newDocumentBuilder().newDocument();
Element rss = XMLUtils.addNode(doc, doc, "rss");
XMLUtils.addAttribute(doc, rss, "version", "2.0");
XMLUtils
.addAttribute(doc, rss, "xmlns:cas", (String) NS_MAP.get("cas"));
Element channel = XMLUtils.addNode(doc, rss, "channel");
XMLUtils.addNode(doc, channel, "title", productTypeName);
XMLUtils.addNode(doc, channel, "link", RSSUtils.getChannelLink(
this.conf.getChannelLink(), channelMet));
XMLUtils.addNode(doc, channel, "description", channelDesc);
String buildPubDate = dateFormatter.format(new Date());
XMLUtils.addNode(doc, channel, "language", "en-us");
XMLUtils.addNode(doc, channel, "copyright", COPYRIGHT_BOILER_PLATE);
XMLUtils.addNode(doc, channel, "pubDate", buildPubDate);
XMLUtils.addNode(doc, channel, "category", productTypeName);
XMLUtils.addNode(doc, channel, "generator", "CAS File Manager");
XMLUtils.addNode(doc, channel, "lastBuildDate", buildPubDate);
for (Iterator i = products.iterator(); i.hasNext();) {
Product p = (Product) i.next();
String productTypeIdStr = p.getProductType().getProductTypeId();
ProductType productType = null;
try {
productType = fm.getProductTypeById(productTypeIdStr);
} catch (RepositoryManagerException e) {
e.printStackTrace();
LOG.log(Level.SEVERE,
"Unable to obtain product type from product type id: ["
+ ((Product) products.get(0)).getProductType()
.getProductTypeId() + "]: Message: " + e.getMessage());
return;
}
p.setProductType(productType);
p.setProductReferences(safeGetProductReferences(p));
Element item = XMLUtils.addNode(doc, channel, "item");
XMLUtils.addNode(doc, item, "title", p.getProductName());
XMLUtils.addNode(doc, item, "description", p.getProductType()
.getName());
XMLUtils.addNode(doc, item, "link", base + "/data?productID="
+ p.getProductId());
Metadata m = this.safeGetMetadata(p);
String productReceivedTime = m.getMetadata("CAS.ProductReceivedTime");
Date receivedTime = null;
try {
receivedTime = DateConvert.isoParse(productReceivedTime);
} catch (ParseException ignore) {
}
if (receivedTime != null) {
XMLUtils.addNode(doc, item, "pubDate", dateFormatter
.format(receivedTime));
}
// add met field for FileSize for use in RSS envelope
if (p.getProductReferences() != null
&& p.getProductReferences().size() == 1) {
m.addMetadata("FileSize", String.valueOf(p.getProductReferences()
.get(0).getFileSize()));
}
// add additional elements from the RSSConfig
for (RSSTag tag : this.conf.getTags()) {