Blame view

src/main/java/pl/labno/bernard/nbp/exchangerate/Updater.java 1.34 KB
bernard authored
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
package pl.labno.bernard.nbp.exchangerate;

import org.apache.commons.httpclient.HttpURL;
import pl.labno.bernard.nbp.exchangerate.rest.ExchangeRateTableUnmarshaller;
import pl.labno.bernard.nbp.exchangerate.rest.generated.ExchangeRateTable;
import yarfraw.core.datamodel.ChannelFeed;
import yarfraw.core.datamodel.ItemEntry;
import yarfraw.core.datamodel.YarfrawException;
import yarfraw.utils.reader.FeedReaderUtils;

import javax.xml.bind.JAXBException;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.Executors;

public class Updater {

    private String feedURL = "http://rss.nbp.pl/kursy/TabelaA.xml";

    public String getFeedURL() {
        return feedURL;
    }

    public void setFeedURL(String feedURL) {
        this.feedURL = feedURL;
    }

    public ExchangeRateTable getRecentExchangeTable() throws IOException, YarfrawException, JAXBException {
        final List<ChannelFeed> feedList = FeedReaderUtils.readAll(Executors.newCachedThreadPool(), new HttpURL(feedURL));
        if(feedList.size() <  1) {
            throw new IllegalArgumentException("Read 0 feeds.");
        }
        final List<ItemEntry> entries = feedList.get(0).getItems();
        if(entries.size() < 1) {
            return null;
        }
        return ExchangeRateTableUnmarshaller.unmarshal(entries.get(0).getEnclosure().getUrl());
    }
}