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.util.Date;
020    
021    import org.apache.abdera.model.Entry;
022    import org.apache.abdera.model.Feed;
023    import org.apache.camel.Consumer;
024    import org.apache.camel.Exchange;
025    import org.apache.camel.Processor;
026    import org.apache.camel.Producer;
027    import org.apache.camel.impl.DefaultPollingEndpoint;
028    import org.apache.camel.util.ObjectHelper;
029    
030    /**
031     * An <a href="http://activemq.apache.org/camel/atom.html">Atom Endpoint</a>.
032     *
033     * @version $Revision: 656933 $
034     */
035    public class AtomEndpoint extends DefaultPollingEndpoint<Exchange> {
036    
037        /**
038         * Header key for the {@link org.apache.abdera.model.Feed} object is stored on the in message on the exchange.
039         */
040        public static final String HEADER_ATOM_FEED = "org.apache.camel.component.atom.feed";
041    
042        private String atomUri;
043        private boolean splitEntries = true;
044        private Date lastUpdate;
045        private boolean filter = true;
046    
047        public AtomEndpoint(String endpointUri, AtomComponent component, String atomUri) {
048            super(endpointUri, component);
049            this.atomUri = atomUri;
050    
051            ObjectHelper.notNull(atomUri, "atomUri property");
052        }
053    
054        public AtomEndpoint(String endpointUri, String atomUri) {
055            this(endpointUri);
056            this.atomUri = atomUri;
057    
058            ObjectHelper.notNull(atomUri, "atomUri property");
059        }
060    
061        public AtomEndpoint(String endpointUri) {
062            super(endpointUri);
063        }
064    
065        public boolean isSingleton() {
066            return true;
067        }
068    
069        public Producer<Exchange> createProducer() throws Exception {
070            throw new UnsupportedOperationException("AtomProducer is not implemented");
071        }
072    
073        public Consumer<Exchange> createConsumer(Processor processor) throws Exception {
074            AtomConsumerSupport answer;
075            if (isSplitEntries()) {
076                answer = new AtomEntryPollingConsumer(this, processor, filter, lastUpdate);
077            } else {
078                answer = new AtomPollingConsumer(this, processor);
079            }
080            // ScheduledPollConsumer default delay is 500 millis and that is too often for polling a feed,
081            // so we override with a new default value. End user can override this value by providing a consumer.delay parameter
082            answer.setDelay(AtomConsumerSupport.DEFAULT_CONSUMER_DELAY);
083            configureConsumer(answer);
084            return answer;
085        }
086    
087        /**
088         * Creates an Exchange with the entries as the in body.
089         *
090         * @param feed   the atom feed
091         * @return the created exchange
092         */
093        public Exchange createExchange(Feed feed) {
094            Exchange exchange = createExchange();
095            exchange.getIn().setBody(feed.getEntries());
096            exchange.getIn().setHeader(HEADER_ATOM_FEED, feed);
097            return exchange;
098        }
099    
100        /**
101         * Creates an Exchange with the given entry as the in body.
102         *
103         * @param feed   the atom feed
104         * @param entry  the entry as the in body
105         * @return the created exchange
106         */
107        public Exchange createExchange(Feed feed, Entry entry) {
108            Exchange exchange = createExchange();
109            exchange.getIn().setBody(entry);
110            exchange.getIn().setHeader(HEADER_ATOM_FEED, feed);
111            return exchange;
112        }
113    
114        // Properties
115        //-------------------------------------------------------------------------
116    
117        public String getAtomUri() {
118            return atomUri;
119        }
120    
121        public void setAtomUri(String atomUri) {
122            this.atomUri = atomUri;
123        }
124    
125        public boolean isSplitEntries() {
126            return splitEntries;
127        }
128    
129        /**
130         * Sets whether or not entries should be sent individually or whether the entire
131         * feed should be sent as a single message
132         */
133        public void setSplitEntries(boolean splitEntries) {
134            this.splitEntries = splitEntries;
135        }
136    
137        public Date getLastUpdate() {
138            return lastUpdate;
139        }
140    
141        /**
142         * Sets the timestamp to be used for filtering entries from the atom feeds.
143         * This options is only in conjunction with the splitEntries.
144         */
145        public void setLastUpdate(Date lastUpdate) {
146            this.lastUpdate = lastUpdate;
147        }
148    
149        public boolean isFilter() {
150            return filter;
151        }
152    
153        /**
154         * Sets wether to use filtering or not of the entries.
155         */
156        public void setFilter(boolean filter) {
157            this.filter = filter;
158        }
159    
160        // Implementation methods
161        //-------------------------------------------------------------------------
162    
163    }