Coverage Report - org.apache.camel.component.xmpp.XmppEndpoint
 
Classes in this File Line Coverage Branch Coverage Complexity
XmppEndpoint
27% 
0% 
0
 
 1  
 /**
 2  
  *
 3  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 4  
  * contributor license agreements.  See the NOTICE file distributed with
 5  
  * this work for additional information regarding copyright ownership.
 6  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 7  
  * (the "License"); you may not use this file except in compliance with
 8  
  * the License.  You may obtain a copy of the License at
 9  
  *
 10  
  * http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing, software
 13  
  * distributed under the License is distributed on an "AS IS" BASIS,
 14  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15  
  * See the License for the specific language governing permissions and
 16  
  * limitations under the License.
 17  
  */
 18  
 package org.apache.camel.component.xmpp;
 19  
 
 20  
 import org.apache.camel.CamelContext;
 21  
 import org.apache.camel.Consumer;
 22  
 import org.apache.camel.Processor;
 23  
 import org.apache.camel.Producer;
 24  
 import org.apache.camel.impl.DefaultEndpoint;
 25  
 import org.apache.commons.logging.Log;
 26  
 import org.apache.commons.logging.LogFactory;
 27  
 import org.jivesoftware.smack.AccountManager;
 28  
 import org.jivesoftware.smack.XMPPConnection;
 29  
 import org.jivesoftware.smack.XMPPException;
 30  
 import org.jivesoftware.smack.filter.PacketFilter;
 31  
 import org.jivesoftware.smack.packet.Message;
 32  
 import org.jivesoftware.smack.packet.Presence;
 33  
 
 34  
 /**
 35  
  * An XMPP Endpoint
 36  
  *
 37  
  * @version $Revision:520964 $
 38  
  */
 39  0
 public class XmppEndpoint extends DefaultEndpoint<XmppExchange> {
 40  1
     private static final transient Log log = LogFactory.getLog(XmppEndpoint.class);
 41  
     private XmppBinding binding;
 42  
     private XMPPConnection connection;
 43  
     private String host;
 44  
     private int port;
 45  
     private String user;
 46  
     private String password;
 47  2
     private String resource = "Camel";
 48  2
     private boolean login = true;
 49  
     private PacketFilter filter;
 50  
     private boolean createAccount;
 51  
     private String room;
 52  
     private String participant;
 53  
 
 54  
     public XmppEndpoint(String uri, XmppComponent component) {
 55  2
         super(uri, component);
 56  2
     }
 57  
 
 58  
     public Producer<XmppExchange> createProducer() throws Exception {
 59  0
         if (room != null) {
 60  0
             return createGroupChatProducer(room);
 61  
         }
 62  
         else {
 63  0
             if (participant == null) {
 64  0
                 throw new IllegalArgumentException("No room or participant configured on this endpoint: " + this);
 65  
             }
 66  0
             return createPrivateChatProducer(participant);
 67  
         }
 68  
     }
 69  
 
 70  
     public Producer<XmppExchange> createGroupChatProducer(String room) throws Exception {
 71  0
         return new XmppGroupChatProducer(this, room);
 72  
     }
 73  
 
 74  
     public Producer<XmppExchange> createPrivateChatProducer(String participant) throws Exception {
 75  0
         return new XmppPrivateChatProducer(this, participant);
 76  
     }
 77  
 
 78  
     public Consumer<XmppExchange> createConsumer(Processor processor) throws Exception {
 79  0
         return new XmppConsumer(this, processor);
 80  
     }
 81  
 
 82  
     public XmppExchange createExchange() {
 83  0
         return new XmppExchange(getContext(), getBinding());
 84  
     }
 85  
 
 86  
     public XmppExchange createExchange(Message message) {
 87  0
         return new XmppExchange(getContext(), getBinding(), message);
 88  
     }
 89  
 
 90  
     // Properties
 91  
     //-------------------------------------------------------------------------
 92  
     public XmppBinding getBinding() {
 93  0
         if (binding == null) {
 94  0
             binding = new XmppBinding();
 95  
         }
 96  0
         return binding;
 97  
     }
 98  
 
 99  
     /**
 100  
      * Sets the binding used to convert from a Camel message to and from an XMPP message
 101  
      *
 102  
      * @param binding the binding to use
 103  
      */
 104  
     public void setBinding(XmppBinding binding) {
 105  0
         this.binding = binding;
 106  0
     }
 107  
 
 108  
     public String getHost() {
 109  2
         return host;
 110  
     }
 111  
 
 112  
     public void setHost(String host) {
 113  2
         this.host = host;
 114  2
     }
 115  
 
 116  
     public int getPort() {
 117  2
         return port;
 118  
     }
 119  
 
 120  
     public void setPort(int port) {
 121  2
         this.port = port;
 122  2
     }
 123  
 
 124  
     public String getUser() {
 125  2
         return user;
 126  
     }
 127  
 
 128  
     public void setUser(String user) {
 129  2
         this.user = user;
 130  2
     }
 131  
 
 132  
     public String getPassword() {
 133  0
         return password;
 134  
     }
 135  
 
 136  
     public void setPassword(String password) {
 137  0
         this.password = password;
 138  0
     }
 139  
 
 140  
     public String getResource() {
 141  0
         return resource;
 142  
     }
 143  
 
 144  
     public void setResource(String resource) {
 145  0
         this.resource = resource;
 146  0
     }
 147  
 
 148  
     public boolean isLogin() {
 149  0
         return login;
 150  
     }
 151  
 
 152  
     public void setLogin(boolean login) {
 153  0
         this.login = login;
 154  0
     }
 155  
 
 156  
     public PacketFilter getFilter() {
 157  0
         return filter;
 158  
     }
 159  
 
 160  
     public void setFilter(PacketFilter filter) {
 161  0
         this.filter = filter;
 162  0
     }
 163  
 
 164  
     public boolean isCreateAccount() {
 165  0
         return createAccount;
 166  
     }
 167  
 
 168  
     public void setCreateAccount(boolean createAccount) {
 169  0
         this.createAccount = createAccount;
 170  0
     }
 171  
 
 172  
     public String getRoom() {
 173  1
         return room;
 174  
     }
 175  
 
 176  
     public void setRoom(String room) {
 177  1
         this.room = room;
 178  1
     }
 179  
 
 180  
     public String getParticipant() {
 181  1
         return participant;
 182  
     }
 183  
 
 184  
     public void setParticipant(String participant) {
 185  1
         this.participant = participant;
 186  1
     }
 187  
 
 188  
     public XMPPConnection getConnection() throws XMPPException {
 189  0
         if (connection == null) {
 190  0
             connection = createConnection();
 191  
         }
 192  0
         return connection;
 193  
     }
 194  
 
 195  
     public void setConnection(XMPPConnection connection) {
 196  0
         this.connection = connection;
 197  0
     }
 198  
 
 199  
     // Implementation methods
 200  
     //-------------------------------------------------------------------------
 201  
     protected XMPPConnection createConnection() throws XMPPException {
 202  
         XMPPConnection connection;
 203  0
         if (port > 0) {
 204  0
             connection = new XMPPConnection(host, port);
 205  0
         }
 206  
         else {
 207  0
             connection = new XMPPConnection(host);
 208  
         }
 209  0
         if (login && !connection.isAuthenticated()) {
 210  0
             if (user != null) {
 211  0
                 log.info("Logging in to XMPP as user: " + user + " on connection: " + connection);
 212  0
                 if (password == null) {
 213  0
                     log.warn("No password configured for user: " + user);
 214  
                 }
 215  
 
 216  0
                 if (createAccount) {
 217  0
                     AccountManager accountManager = new AccountManager(connection);
 218  0
                     accountManager.createAccount(user, password);
 219  
                 }
 220  0
                 if (resource != null) {
 221  0
                     connection.login(user, password, resource);
 222  0
                 }
 223  
                 else {
 224  0
                     connection.login(user, password);
 225  
                 }
 226  0
             }
 227  
             else {
 228  0
                 log.info("Logging in anonymously to XMPP on connection: " + connection);
 229  0
                 connection.loginAnonymously();
 230  
             }
 231  
 
 232  
             // now lets send a presence
 233  0
             connection.sendPacket(new Presence(Presence.Type.AVAILABLE));
 234  
         }
 235  0
         return connection;
 236  
     }
 237  
     
 238  
         public boolean isSingleton() {
 239  2
                 return true;
 240  
         }
 241  
 
 242  
 }