001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.camel.component.atom;
018    
019    import java.io.IOException;
020    import java.util.Date;
021    import java.util.List;
022    
023    import org.apache.abdera.model.Document;
024    import org.apache.abdera.model.Entry;
025    import org.apache.abdera.model.Feed;
026    import org.apache.abdera.parser.ParseException;
027    import org.apache.camel.Exchange;
028    import org.apache.camel.Processor;
029    import org.apache.camel.component.feed.EntryFilter;
030    import org.apache.camel.component.feed.FeedEntryPollingConsumer;
031    
032    /**
033     * Consumer to poll atom feeds and return each entry from the feed step by step.
034     *
035     * @version $Revision: 724434 $
036     */
037    public class AtomEntryPollingConsumer extends FeedEntryPollingConsumer {
038        private Document<Feed> document;
039    
040        public AtomEntryPollingConsumer(AtomEndpoint endpoint, Processor processor, boolean filter, Date lastUpdate) {
041            super(endpoint, processor, filter, lastUpdate);
042        }   
043        
044        private Document<Feed> getDocument() throws IOException, ParseException {
045            if (document == null) {
046                document = AtomUtils.parseDocument(endpoint.getFeedUri());
047                Feed root = document.getRoot();
048                if (endpoint.isSortEntries()) {
049                    sortEntries(root);
050                }             
051                list = root.getEntries();            
052                entryIndex = list.size() - 1;
053            }
054            return document;
055        }
056        
057        protected void sortEntries(Feed feed) {
058            feed.sortEntriesByUpdated(true);
059        }
060        
061        @Override
062        protected void populateList(Object feed) throws ParseException, IOException {
063            // list is populated already in the createFeed method
064        }
065    
066        @Override
067        protected Object createFeed() throws IOException {
068            return getDocument().getRoot();
069        }
070    
071        @Override
072        protected void resetList() {
073            document = null;    
074        }
075        
076        @Override
077        protected EntryFilter createEntryFilter(Date lastUpdate) {
078            return new UpdatedDateFilter(lastUpdate);
079        }
080    }