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    
022    import org.apache.abdera.model.Document;
023    import org.apache.abdera.model.Feed;
024    import org.apache.abdera.parser.ParseException;
025    import org.apache.camel.Processor;
026    import org.apache.camel.component.feed.EntryFilter;
027    import org.apache.camel.component.feed.FeedEntryPollingConsumer;
028    
029    /**
030     * Consumer to poll atom feeds and return each entry from the feed step by step.
031     *
032     * @version $Revision: 816070 $
033     */
034    public class AtomEntryPollingConsumer extends FeedEntryPollingConsumer {
035        private Document<Feed> document;
036    
037        public AtomEntryPollingConsumer(AtomEndpoint endpoint, Processor processor, boolean filter, Date lastUpdate) {
038            super(endpoint, processor, filter, lastUpdate);
039        }   
040        
041        private Document<Feed> getDocument() throws IOException, ParseException {
042            if (document == null) {
043                document = AtomUtils.parseDocument(endpoint.getFeedUri());
044                Feed root = document.getRoot();
045                if (endpoint.isSortEntries()) {
046                    sortEntries(root);
047                }             
048                list = root.getEntries();            
049                entryIndex = list.size() - 1;
050            }
051            return document;
052        }
053        
054        protected void sortEntries(Feed feed) {
055            feed.sortEntriesByUpdated(true);
056        }
057        
058        @Override
059        protected void populateList(Object feed) throws ParseException, IOException {
060            // list is populated already in the createFeed method
061        }
062    
063        @Override
064        protected Object createFeed() throws IOException {
065            return getDocument().getRoot();
066        }
067    
068        @Override
069        protected void resetList() {
070            document = null;    
071        }
072        
073        @Override
074        protected EntryFilter createEntryFilter(Date lastUpdate) {
075            return new UpdatedDateFilter(lastUpdate);
076        }
077    }