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 org.apache.camel.Consumer;
020    import org.apache.camel.Processor;
021    import org.apache.camel.Producer;
022    import org.apache.camel.impl.DefaultEndpoint;
023    import org.apache.commons.logging.Log;
024    import org.apache.commons.logging.LogFactory;
025    
026    import org.jivesoftware.smack.AccountManager;
027    import org.jivesoftware.smack.XMPPConnection;
028    import org.jivesoftware.smack.XMPPException;
029    import org.jivesoftware.smack.filter.PacketFilter;
030    import org.jivesoftware.smack.packet.Message;
031    import org.jivesoftware.smack.packet.Presence;
032    
033    /**
034     * An XMPP Endpoint
035     * 
036     * @version $Revision:520964 $
037     */
038    public class XmppEndpoint extends DefaultEndpoint<XmppExchange> {
039        private static final transient Log LOG = LogFactory.getLog(XmppEndpoint.class);
040        private XmppBinding binding;
041        private XMPPConnection connection;
042        private String host;
043        private int port;
044        private String user;
045        private String password;
046        private String resource = "Camel";
047        private boolean login = true;
048        private PacketFilter filter;
049        private boolean createAccount;
050        private String room;
051        private String participant;
052    
053        public XmppEndpoint(String uri, XmppComponent component) {
054            super(uri, component);
055        }
056    
057        public Producer<XmppExchange> createProducer() throws Exception {
058            if (room != null) {
059                return createGroupChatProducer(room);
060            } else {
061                if (participant == null) {
062                    throw new IllegalArgumentException("No room or participant configured on this endpoint: " + this);
063                }
064                return createPrivateChatProducer(participant);
065            }
066        }
067    
068        public Producer<XmppExchange> createGroupChatProducer(String room) throws Exception {
069            return new XmppGroupChatProducer(this, room);
070        }
071    
072        public Producer<XmppExchange> createPrivateChatProducer(String participant) throws Exception {
073            return new XmppPrivateChatProducer(this, participant);
074        }
075    
076        public Consumer<XmppExchange> createConsumer(Processor processor) throws Exception {
077            return new XmppConsumer(this, processor);
078        }
079    
080        public XmppExchange createExchange() {
081            return new XmppExchange(getContext(), getBinding());
082        }
083    
084        public XmppExchange createExchange(Message message) {
085            return new XmppExchange(getContext(), getBinding(), message);
086        }
087    
088        // Properties
089        // -------------------------------------------------------------------------
090        public XmppBinding getBinding() {
091            if (binding == null) {
092                binding = new XmppBinding();
093            }
094            return binding;
095        }
096    
097        /**
098         * Sets the binding used to convert from a Camel message to and from an XMPP
099         * message
100         * 
101         * @param binding the binding to use
102         */
103        public void setBinding(XmppBinding binding) {
104            this.binding = binding;
105        }
106    
107        public String getHost() {
108            return host;
109        }
110    
111        public void setHost(String host) {
112            this.host = host;
113        }
114    
115        public int getPort() {
116            return port;
117        }
118    
119        public void setPort(int port) {
120            this.port = port;
121        }
122    
123        public String getUser() {
124            return user;
125        }
126    
127        public void setUser(String user) {
128            this.user = user;
129        }
130    
131        public String getPassword() {
132            return password;
133        }
134    
135        public void setPassword(String password) {
136            this.password = password;
137        }
138    
139        public String getResource() {
140            return resource;
141        }
142    
143        public void setResource(String resource) {
144            this.resource = resource;
145        }
146    
147        public boolean isLogin() {
148            return login;
149        }
150    
151        public void setLogin(boolean login) {
152            this.login = login;
153        }
154    
155        public PacketFilter getFilter() {
156            return filter;
157        }
158    
159        public void setFilter(PacketFilter filter) {
160            this.filter = filter;
161        }
162    
163        public boolean isCreateAccount() {
164            return createAccount;
165        }
166    
167        public void setCreateAccount(boolean createAccount) {
168            this.createAccount = createAccount;
169        }
170    
171        public String getRoom() {
172            return room;
173        }
174    
175        public void setRoom(String room) {
176            this.room = room;
177        }
178    
179        public String getParticipant() {
180            return participant;
181        }
182    
183        public void setParticipant(String participant) {
184            this.participant = participant;
185        }
186    
187        public XMPPConnection getConnection() throws XMPPException {
188            if (connection == null) {
189                connection = createConnection();
190            }
191            return connection;
192        }
193    
194        public void setConnection(XMPPConnection connection) {
195            this.connection = connection;
196        }
197    
198        // Implementation methods
199        // -------------------------------------------------------------------------
200        protected XMPPConnection createConnection() throws XMPPException {
201            XMPPConnection connection;
202            if (port > 0) {
203                connection = new XMPPConnection(host, port);
204            } else {
205                connection = new XMPPConnection(host);
206            }
207            if (login && !connection.isAuthenticated()) {
208                if (user != null) {
209                    LOG.info("Logging in to XMPP as user: " + user + " on connection: " + connection);
210                    if (password == null) {
211                        LOG.warn("No password configured for user: " + user);
212                    }
213    
214                    if (createAccount) {
215                        AccountManager accountManager = new AccountManager(connection);
216                        accountManager.createAccount(user, password);
217                    }
218                    if (resource != null) {
219                        connection.login(user, password, resource);
220                    } else {
221                        connection.login(user, password);
222                    }
223                } else {
224                    LOG.info("Logging in anonymously to XMPP on connection: " + connection);
225                    connection.loginAnonymously();
226                }
227    
228                // now lets send a presence
229                connection.sendPacket(new Presence(Presence.Type.AVAILABLE));
230            }
231            return connection;
232        }
233    
234        public boolean isSingleton() {
235            return true;
236        }
237    
238    }