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.xmpp;
018
019 import java.util.Iterator;
020
021 import org.apache.camel.Processor;
022 import org.apache.camel.impl.DefaultConsumer;
023 import org.apache.commons.logging.Log;
024 import org.apache.commons.logging.LogFactory;
025
026 import org.jivesoftware.smack.PacketListener;
027 import org.jivesoftware.smack.packet.Message;
028 import org.jivesoftware.smack.packet.Packet;
029 import org.jivesoftware.smack.packet.RosterPacket;
030
031 /**
032 * A {@link Consumer} which listens to XMPP packets
033 *
034 * @version $Revision: 563665 $
035 */
036 public class XmppConsumer extends DefaultConsumer<XmppExchange> implements PacketListener {
037 private static final transient Log LOG = LogFactory.getLog(XmppConsumer.class);
038 private final XmppEndpoint endpoint;
039
040 public XmppConsumer(XmppEndpoint endpoint, Processor processor) {
041 super(endpoint, processor);
042 this.endpoint = endpoint;
043 }
044
045 @Override
046 protected void doStart() throws Exception {
047 super.doStart();
048 endpoint.getConnection().addPacketListener(this, endpoint.getFilter());
049 }
050
051 @Override
052 protected void doStop() throws Exception {
053 endpoint.getConnection().removePacketListener(this);
054 super.doStop();
055 }
056
057 public void processPacket(Packet packet) {
058
059 if (packet instanceof Message) {
060 Message message = (Message)packet;
061 if (LOG.isDebugEnabled()) {
062 LOG.debug("<<<< message: " + message.getBody());
063 }
064 XmppExchange exchange = endpoint.createExchange(message);
065 try {
066 getProcessor().process(exchange);
067 } catch (Exception e) {
068 // TODO: what should we do when a processing failure occurs??
069 e.printStackTrace();
070 }
071 } else if (packet instanceof RosterPacket) {
072 RosterPacket rosterPacket = (RosterPacket)packet;
073 if (LOG.isDebugEnabled()) {
074 LOG.debug("Roster packet with : " + rosterPacket.getRosterItemCount() + " item(s)");
075 Iterator rosterItems = rosterPacket.getRosterItems();
076 while (rosterItems.hasNext()) {
077 Object item = rosterItems.next();
078 LOG.debug("Roster item: " + item);
079 }
080 }
081 } else {
082 if (LOG.isDebugEnabled()) {
083 LOG.debug("<<<< ignored packet: " + packet);
084 }
085
086 }
087 }
088 }