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
030 /**
031 * Consumer to poll atom feeds and return each entry from the feed step by step.
032 *
033 * @version $Revision: 656976 $
034 */
035 public class AtomEntryPollingConsumer extends AtomPollingConsumer {
036 private Document<Feed> document;
037 private int entryIndex;
038 private EntryFilter entryFilter;
039 private List<Entry> list;
040
041 public AtomEntryPollingConsumer(AtomEndpoint endpoint, Processor processor, boolean filter,
042 Date lastUpdate) {
043 super(endpoint, processor);
044 if (filter) {
045 entryFilter = new UpdatedDateFilter(lastUpdate);
046 }
047 }
048
049 public void poll() throws Exception {
050 getDocument();
051 Feed feed = document.getRoot();
052
053 while (hasNextEntry()) {
054 Entry entry = list.get(entryIndex--);
055
056 boolean valid = true;
057 if (entryFilter != null) {
058 valid = entryFilter.isValidEntry(endpoint, document, entry);
059 }
060 if (valid) {
061 Exchange exchange = endpoint.createExchange(feed, entry);
062 getProcessor().process(exchange);
063 // return and wait for the next poll to continue from last time (this consumer is stateful)
064 return;
065 }
066 }
067
068 // reset document to be able to poll again
069 document = null;
070 }
071
072 private Document<Feed> getDocument() throws IOException, ParseException {
073 if (document == null) {
074 document = AtomUtils.parseDocument(endpoint.getAtomUri());
075 list = document.getRoot().getEntries();
076 entryIndex = list.size() - 1;
077 }
078 return document;
079 }
080
081 private boolean hasNextEntry() {
082 return entryIndex >= 0;
083 }
084
085 }