001 /**
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one or more
004 * contributor license agreements. See the NOTICE file distributed with
005 * this work for additional information regarding copyright ownership.
006 * The ASF licenses this file to You under the Apache License, Version 2.0
007 * (the "License"); you may not use this file except in compliance with
008 * the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018 package org.apache.camel.component.xmpp;
019
020 import org.apache.camel.CamelContext;
021 import org.apache.camel.Consumer;
022 import org.apache.camel.Processor;
023 import org.apache.camel.Producer;
024 import org.apache.camel.impl.DefaultEndpoint;
025 import org.apache.commons.logging.Log;
026 import org.apache.commons.logging.LogFactory;
027 import org.jivesoftware.smack.AccountManager;
028 import org.jivesoftware.smack.XMPPConnection;
029 import org.jivesoftware.smack.XMPPException;
030 import org.jivesoftware.smack.filter.PacketFilter;
031 import org.jivesoftware.smack.packet.Message;
032 import org.jivesoftware.smack.packet.Presence;
033
034 /**
035 * An XMPP Endpoint
036 *
037 * @version $Revision:520964 $
038 */
039 public class XmppEndpoint extends DefaultEndpoint<XmppExchange> {
040 private static final transient Log log = LogFactory.getLog(XmppEndpoint.class);
041 private XmppBinding binding;
042 private XMPPConnection connection;
043 private String host;
044 private int port;
045 private String user;
046 private String password;
047 private String resource = "Camel";
048 private boolean login = true;
049 private PacketFilter filter;
050 private boolean createAccount;
051 private String room;
052 private String participant;
053
054 public XmppEndpoint(String uri, XmppComponent component) {
055 super(uri, component);
056 }
057
058 public Producer<XmppExchange> createProducer() throws Exception {
059 if (room != null) {
060 return createGroupChatProducer(room);
061 }
062 else {
063 if (participant == null) {
064 throw new IllegalArgumentException("No room or participant configured on this endpoint: " + this);
065 }
066 return createPrivateChatProducer(participant);
067 }
068 }
069
070 public Producer<XmppExchange> createGroupChatProducer(String room) throws Exception {
071 return new XmppGroupChatProducer(this, room);
072 }
073
074 public Producer<XmppExchange> createPrivateChatProducer(String participant) throws Exception {
075 return new XmppPrivateChatProducer(this, participant);
076 }
077
078 public Consumer<XmppExchange> createConsumer(Processor processor) throws Exception {
079 return new XmppConsumer(this, processor);
080 }
081
082 public XmppExchange createExchange() {
083 return new XmppExchange(getContext(), getBinding());
084 }
085
086 public XmppExchange createExchange(Message message) {
087 return new XmppExchange(getContext(), getBinding(), message);
088 }
089
090 // Properties
091 //-------------------------------------------------------------------------
092 public XmppBinding getBinding() {
093 if (binding == null) {
094 binding = new XmppBinding();
095 }
096 return binding;
097 }
098
099 /**
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 this.binding = binding;
106 }
107
108 public String getHost() {
109 return host;
110 }
111
112 public void setHost(String host) {
113 this.host = host;
114 }
115
116 public int getPort() {
117 return port;
118 }
119
120 public void setPort(int port) {
121 this.port = port;
122 }
123
124 public String getUser() {
125 return user;
126 }
127
128 public void setUser(String user) {
129 this.user = user;
130 }
131
132 public String getPassword() {
133 return password;
134 }
135
136 public void setPassword(String password) {
137 this.password = password;
138 }
139
140 public String getResource() {
141 return resource;
142 }
143
144 public void setResource(String resource) {
145 this.resource = resource;
146 }
147
148 public boolean isLogin() {
149 return login;
150 }
151
152 public void setLogin(boolean login) {
153 this.login = login;
154 }
155
156 public PacketFilter getFilter() {
157 return filter;
158 }
159
160 public void setFilter(PacketFilter filter) {
161 this.filter = filter;
162 }
163
164 public boolean isCreateAccount() {
165 return createAccount;
166 }
167
168 public void setCreateAccount(boolean createAccount) {
169 this.createAccount = createAccount;
170 }
171
172 public String getRoom() {
173 return room;
174 }
175
176 public void setRoom(String room) {
177 this.room = room;
178 }
179
180 public String getParticipant() {
181 return participant;
182 }
183
184 public void setParticipant(String participant) {
185 this.participant = participant;
186 }
187
188 public XMPPConnection getConnection() throws XMPPException {
189 if (connection == null) {
190 connection = createConnection();
191 }
192 return connection;
193 }
194
195 public void setConnection(XMPPConnection connection) {
196 this.connection = connection;
197 }
198
199 // Implementation methods
200 //-------------------------------------------------------------------------
201 protected XMPPConnection createConnection() throws XMPPException {
202 XMPPConnection connection;
203 if (port > 0) {
204 connection = new XMPPConnection(host, port);
205 }
206 else {
207 connection = new XMPPConnection(host);
208 }
209 if (login && !connection.isAuthenticated()) {
210 if (user != null) {
211 log.info("Logging in to XMPP as user: " + user + " on connection: " + connection);
212 if (password == null) {
213 log.warn("No password configured for user: " + user);
214 }
215
216 if (createAccount) {
217 AccountManager accountManager = new AccountManager(connection);
218 accountManager.createAccount(user, password);
219 }
220 if (resource != null) {
221 connection.login(user, password, resource);
222 }
223 else {
224 connection.login(user, password);
225 }
226 }
227 else {
228 log.info("Logging in anonymously to XMPP on connection: " + connection);
229 connection.loginAnonymously();
230 }
231
232 // now lets send a presence
233 connection.sendPacket(new Presence(Presence.Type.AVAILABLE));
234 }
235 return connection;
236 }
237
238 public boolean isSingleton() {
239 return true;
240 }
241
242 }