/*
Copyright 2006 - 2010 Under Dusken
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package no.dusken.aranea.admin.control;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.io.FeedException;
import com.sun.syndication.io.SyndFeedInput;
import com.sun.syndication.io.XmlReader;
import no.dusken.aranea.model.ExternalPage;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.validation.Errors;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class EditExternalPageController extends EditPageController<ExternalPage> {
private List<String> feeds;
private final static Logger log = LoggerFactory.getLogger(EditExternalPageController.class);
/**
* The directory in which to store rss converted to html
*/
private String cacheDirectory;
/**
* Number of entries to view. Default is 15
*/
private Integer numberOfEntries;
public EditExternalPageController() {
super(ExternalPage.class);
}
@Override
protected Map referenceData(HttpServletRequest request, Object object,
Errors errors) throws Exception {
Map<String, Object> map = super.referenceData(request, object, errors);
List<SyndFeed> feedsList = new ArrayList<SyndFeed>();
for(String s: feeds){
feedsList.add(getFeedEntries(s));
}
map.put("feeds", feedsList);
map.put("numberOfEntries", numberOfEntries);
return map;
}
/**
* get 15 last feedentries from the given url.
*
* @param feedurl - url to a rss-feed
* @return list with feedentries
*/
private SyndFeed getFeedEntries(String feedurl) {
File feedFile = new File(cacheDirectory + "/" + feedurl);
// check if the file is too old, and download if that is the case
if (!feedFile.exists() || System.currentTimeMillis() - feedFile.lastModified() > 100000) {
try {
FileUtils.copyURLToFile(new URL(feedurl), new File(cacheDirectory + "/" + feedurl));
} catch (IOException e) {
// log, continue using the old file and hope the best
log.error("Could not download rss", e);
}
}
SyndFeed feed = null;
try {
SyndFeedInput input = new SyndFeedInput();
feed = input.build(new XmlReader(feedFile));
feed.setEncoding("UTF-8");
} catch (FeedException e) {
log.error("Could not parse feed", e);
} catch (IOException e) {
log.error("Could not read rss file", e);
}
if (feed != null) {
return feed;
} else {
return null;
}
}
@Required
public List<String> getFeeds() {
return feeds;
}
@Required
public void setFeeds(List<String> feeds) {
this.feeds = feeds;
}
@Required
public void setNumberOfEntries(Integer number) {
this.numberOfEntries = number;
}
@Required
public void setCacheDirectory(String dir) {
this.cacheDirectory = dir;
}
}