View Javadoc
1 /* 2 * $Id: JabberMessageBean.java,v 1.4 2003/10/02 16:43:04 smulube Exp $ 3 * 4 * ***** BEGIN LICENSE BLOCK ***** 5 * =========================================================================== 6 * Copyright (c) 2003 Sam Mulube 7 * 8 * Permission is hereby granted, free of charge, to any person obtaining a copy 9 * of this software and associated documentation files (the "Software"), to deal 10 * in the Software without restriction, including without limitation the rights 11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 * copies of the Software, and to permit persons to whom the Software is 13 * furnished to do so, subject to the following conditions: 14 * 15 * The above copyright notice and this permission notice shall be included in 16 * all copies or substantial portions of the Software. 17 * 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 * SOFTWARE.COPYRIGHT AND PERMISSION NOTICE 25 * =========================================================================== 26 * ***** END LICENSE BLOCK ***** 27 */ 28 package org.zerofun.maven.im.beans; 29 30 import org.apache.commons.lang.StringUtils; 31 import org.jivesoftware.smack.Chat; 32 import org.jivesoftware.smack.SSLXMPPConnection; 33 import org.jivesoftware.smack.XMPPConnection; 34 import org.jivesoftware.smack.XMPPException; 35 import org.zerofun.maven.im.MavenIMException; 36 37 /*** 38 * This bean provides the actual functionality for sending a message to XMPP 39 * (Jabber) clients. It's functionality is invoked by the corresponding 40 * JabberMessageTag class. 41 * 42 * @author <a href="mailto:sam@mulube.com">Sam Mulube</a> 43 * @version $Revision: 1.4 $ 44 * 45 */ 46 public class JabberMessageBean { 47 48 private String from = null; 49 private String host = null; 50 private String message = null; 51 private String password = null; 52 private int port = 0; 53 private boolean secure = false; 54 private int securePort = 0; 55 private String to = null; 56 57 /*** 58 * The main method of the bean. This performs the function of creating and 59 * sending the message to all Jabber recipients. 60 * @throws Exception 61 */ 62 public void execute() throws Exception { 63 if (getTo() == null) { 64 throw new MavenIMException("No Jabber recepients specified."); 65 } 66 XMPPConnection connection = getConnection(); 67 String[] recipients = UtilityBean.getRecipients(getTo()); 68 Chat chat = null; 69 for (int i = 0; i < recipients.length; i++) { 70 String recipient = recipients[i]; 71 System.out.println("Sending message to: " + recipient); 72 chat = connection.createChat(recipient); 73 chat.sendMessage(getMessage()); 74 Thread.sleep(1000); 75 } 76 System.out.println("Disconnecting from Jabber server..."); 77 connection.close(); 78 } 79 80 /*** 81 * Creates an XMPPConnection to the specified host, using either a normal 82 * connection, or a SSL secure connection, then logs in the specified user, 83 * before returning the connection ready for creating chats and then 84 * sending messages. 85 * 86 * @return a valid XMPPConnection ready for sending messages 87 * @throws XMPPException 88 */ 89 private XMPPConnection getConnection() throws XMPPException { 90 System.out.println("Connecting to Jabber server..."); 91 XMPPConnection connection = null; 92 if (isSecure()) { 93 System.out.println("Host: " + getHost() + ":" + getSecurePort()); 94 connection = new SSLXMPPConnection(getHost(), getSecurePort()); 95 } else { 96 System.out.println("Host: " + getHost() + ":" + getPort()); 97 connection = new XMPPConnection(getHost(), getPort()); 98 } 99 System.out.println("Logging in user: " + getFrom()); 100 connection.login(getFrom(), getPassword()); 101 return connection; 102 } 103 104 /*** 105 * Gets the username of the sender of the message. This username is used 106 * to log into the XMPP server. 107 * @return the from String 108 */ 109 public String getFrom() { 110 return from; 111 } 112 113 /*** 114 * Gets the host/server to which the specified user can connect to. This 115 * server is then used to send the messages. 116 * @return the host String 117 */ 118 public String getHost() { 119 return host; 120 } 121 122 /*** 123 * Gets the message which will be sent to all recipients. 124 * @return the message String 125 */ 126 public String getMessage() { 127 return message; 128 } 129 130 /*** 131 * Gets the password which corresponds to the specified username, which 132 * will be used to log the user into the server. 133 * @return the password String 134 */ 135 public String getPassword() { 136 return password; 137 } 138 139 /*** 140 * Gets the port to which the plugin should attempt to connect on the 141 * specified server. 142 * @return the port number int 143 */ 144 public int getPort() { 145 return port; 146 } 147 148 /*** 149 * Gets the port to which the plugin should attempt to connect if it is 150 * trying to connect using XMPP secure. 151 * @return the secure port number int 152 */ 153 public int getSecurePort() { 154 return securePort; 155 } 156 157 /*** 158 * Gets the complete string containing all recipients of the message. This 159 * could be a single name, or a comma separated list of recipients. 160 * @return the recipients String 161 */ 162 public String getTo() { 163 return to; 164 } 165 166 /*** 167 * Gets a boolean value indicating whether or not secure XMPP should be used 168 * to send the message. 169 * @return the secure boolean value 170 */ 171 public boolean isSecure() { 172 return secure; 173 } 174 175 /*** 176 * Sets the username which identifies the user the messages will be sent 177 * from. 178 * @param from the username String to set 179 */ 180 public void setFrom(String from) { 181 this.from = from; 182 } 183 184 /*** 185 * Sets the server the plugin will attempt to connect to, to send messages. 186 * @param host the server String to set 187 */ 188 public void setHost(String host) { 189 this.host = host; 190 } 191 192 /*** 193 * Sets the message which will be sent to all recipients. 194 * @param message the message String to set 195 */ 196 public void setMessage(String message) { 197 this.message = message; 198 } 199 200 /*** 201 * Sets the password which will be used to authenticate the specified user 202 * when the plugin connects to the server. 203 * @param password the password String to set 204 */ 205 public void setPassword(String password) { 206 this.password = password; 207 } 208 209 /*** 210 * Sets the port the plugin will attempt to use when connecting to the XMPP 211 * server. 212 * @param port the port number int to set 213 */ 214 public void setPort(int port) { 215 this.port = port; 216 } 217 218 /*** 219 * Sets the boolean value which indicates whether or not the messages should 220 * be sent using XMPP Secure or not. 221 * @param secure the secure boolean value to set 222 */ 223 public void setSecure(boolean secure) { 224 this.secure = secure; 225 } 226 227 /*** 228 * Sets the port the plugin will attempt to use when connecting to the XMPP 229 * server using an encrypted connection. 230 * @param securePort the secure port number int to set 231 */ 232 public void setSecurePort(int securePort) { 233 this.securePort = securePort; 234 } 235 236 /*** 237 * Sets the string containing all of the intended recipients of the message. 238 * This can be a single name, or a comma separated list. 239 * @param to the to String to set 240 */ 241 public void setTo(String to) { 242 this.to = to; 243 } 244 245 } 246 247 /* 248 * $Log: JabberMessageBean.java,v $ 249 * Revision 1.4 2003/10/02 16:43:04 smulube 250 * added thread sleep back in? 251 * 252 * Revision 1.3 2003/10/02 01:25:53 smulube 253 * added better comments. 254 * 255 * Revision 1.2 2003/10/01 17:16:24 smulube 256 * beans now throw exception if no recipients specified. 257 * 258 * Revision 1.1 2003/09/26 01:19:04 smulube 259 * Using Smack API for Jabber 260 * 261 */

This page was automatically generated by Maven