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