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.cometd;
018    
019    import java.net.URI;
020    import java.net.URISyntaxException;
021    import java.util.Map;
022    
023    import org.apache.camel.Consumer;
024    import org.apache.camel.Processor;
025    import org.apache.camel.Producer;
026    import org.apache.camel.impl.DefaultEndpoint;
027    import org.apache.camel.util.ObjectHelper;
028    
029    /**
030     * Endpoint for Camel Cometd.
031     */
032    public class CometdEndpoint extends DefaultEndpoint {
033    
034        private String baseResource;
035        private int timeout = 240000;
036        private int interval;
037        private int maxInterval = 30000;
038        private int multiFrameInterval = 1500;
039        private boolean jsonCommented = true;
040        private int logLevel = 1;
041        private URI uri;
042        private CometdComponent component;
043    
044        public CometdEndpoint(CometdComponent component, String uri, String remaining, Map<String, Object> parameters) {
045            super(uri, component);
046            this.component = component;
047            try {
048                this.uri = new URI(uri);
049            } catch (URISyntaxException e) {
050                throw new IllegalArgumentException(e);
051            }
052        }
053    
054        public Producer createProducer() throws Exception {
055            ObjectHelper.notNull(component, "component");
056            CometdProducer producer = new CometdProducer(this);
057            return producer;
058        }
059    
060        public Consumer createConsumer(Processor processor) throws Exception {
061            ObjectHelper.notNull(component, "component");
062            CometdConsumer consumer = new CometdConsumer(this, processor);
063            return consumer;
064        }
065    
066        public void connect(CometdProducerConsumer prodcons) throws Exception {
067            component.connect(prodcons);
068        }
069    
070        public void disconnect(CometdProducerConsumer prodcons) throws Exception {
071            component.disconnect(prodcons);
072        }
073    
074        public CometdComponent getComponent() {
075            return component;
076        }
077    
078        public boolean isSingleton() {
079            return false;
080        }
081    
082        public String getPath() {
083            return uri.getPath();
084        }
085    
086        public int getPort() {
087            if (uri.getPort() == -1) {
088                if ("cometds".equals(getProtocol())) {
089                    return 443;
090                } else {
091                    return 80;
092                }
093            }
094            return uri.getPort();
095        }
096    
097        public String getProtocol() {
098            return uri.getScheme();
099        }
100    
101        public URI getUri() {
102            return uri;
103        }
104    
105        public String getBaseResource() {
106            return baseResource;
107        }
108    
109        public void setBaseResource(String baseResource) {
110            this.baseResource = baseResource;
111        }
112    
113        public int getTimeout() {
114            return timeout;
115        }
116    
117        public void setTimeout(int timeout) {
118            this.timeout = timeout;
119        }
120    
121        public int getInterval() {
122            return interval;
123        }
124    
125        public void setInterval(int interval) {
126            this.interval = interval;
127        }
128    
129        public int getMaxInterval() {
130            return maxInterval;
131        }
132    
133        public void setMaxInterval(int maxInterval) {
134            this.maxInterval = maxInterval;
135        }
136    
137        public int getMultiFrameInterval() {
138            return multiFrameInterval;
139        }
140    
141        public void setMultiFrameInterval(int multiFrameInterval) {
142            this.multiFrameInterval = multiFrameInterval;
143        }
144    
145        public boolean isJsonCommented() {
146            return jsonCommented;
147        }
148    
149        public void setJsonCommented(boolean commented) {
150            jsonCommented = commented;
151        }
152    
153        public int getLogLevel() {
154            return logLevel;
155        }
156    
157        public void setLogLevel(int logLevel) {
158            this.logLevel = logLevel;
159        }
160    }