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 org.apache.camel.Exchange;
020 import org.apache.camel.Message;
021 import org.apache.camel.Processor;
022 import org.apache.camel.impl.DefaultConsumer;
023 import org.apache.camel.impl.DefaultMessage;
024 import org.apache.camel.util.ExchangeHelper;
025 import org.cometd.bayeux.server.ServerMessage;
026 import org.cometd.bayeux.server.ServerSession;
027 import org.cometd.server.AbstractService;
028 import org.cometd.server.BayeuxServerImpl;
029
030 /**
031 * A Consumer for receiving messages using Cometd and Bayeux protocol.
032 */
033 public class CometdConsumer extends DefaultConsumer implements CometdProducerConsumer {
034
035 private BayeuxServerImpl bayeux;
036 private final CometdEndpoint endpoint;
037 private ConsumerService service;
038
039 public CometdConsumer(CometdEndpoint endpoint, Processor processor) {
040 super(endpoint, processor);
041 this.endpoint = endpoint;
042 }
043
044 @Override
045 public void start() throws Exception {
046 super.start();
047 // must connect first
048 endpoint.connect(this);
049 service = new ConsumerService(endpoint.getPath(), bayeux, this);
050 }
051
052 @Override
053 public void stop() throws Exception {
054 endpoint.disconnect(this);
055 super.stop();
056 }
057
058 public void setBayeux(BayeuxServerImpl bayeux) {
059 this.bayeux = bayeux;
060 }
061
062 public CometdEndpoint getEndpoint() {
063 return endpoint;
064 }
065
066 public ConsumerService getConsumerService() {
067 return service;
068 }
069
070 public static class ConsumerService extends AbstractService {
071
072 private final CometdEndpoint endpoint;
073 private final CometdConsumer consumer;
074
075 public ConsumerService(String channel, BayeuxServerImpl bayeux, CometdConsumer consumer) {
076 super(bayeux, channel);
077 this.consumer = consumer;
078 this.endpoint = consumer.getEndpoint();
079 addService(channel, "push");
080 }
081
082 public void push(ServerSession remote, String channelName, ServerMessage cometdMessage, String messageId) throws Exception {
083 Object data = null;
084
085 if (cometdMessage != null) {
086 data = cometdMessage.getData();
087 }
088
089 Message message = new DefaultMessage();
090 message.setBody(data);
091
092 Exchange exchange = endpoint.createExchange();
093 exchange.setIn(message);
094
095 consumer.getProcessor().process(exchange);
096
097 if (ExchangeHelper.isOutCapable(exchange)) {
098 Message camelOutMessage = exchange.getOut();
099 remote.deliver(getServerSession(), channelName, camelOutMessage.getBody(), null);
100 }
101 }
102 }
103
104 }