Update of /cvsroot/babeldoc/modules/jabber/src/com/babeldoc/jabber/pipeline/stage In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv24096/src/com/babeldoc/jabber/pipeline/stage Modified Files: XmppWriterPipelineStage.java Added Files: MessageStatusException.java PipelineMessageEventNotificationListener.java MessageDeliveryStatus.java Log Message: Upgrade of smack library and some changes to send messages and keep delivery status. Index: XmppWriterPipelineStage.java =================================================================== RCS file: /cvsroot/babeldoc/modules/jabber/src/com/babeldoc/jabber/pipeline/stage/XmppWriterPipelineStage.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** XmppWriterPipelineStage.java 4 Dec 2007 00:27:42 -0000 1.3 --- XmppWriterPipelineStage.java 9 Dec 2007 01:01:43 -0000 1.4 *************** *** 74,77 **** --- 74,79 ---- import org.jivesoftware.smack.XMPPConnection; import org.jivesoftware.smack.XMPPException; + import org.jivesoftware.smack.packet.Message; + import org.jivesoftware.smackx.MessageEventManager; import org.jivesoftware.smackx.muc.MultiUserChat; *************** *** 91,111 **** public class XmppWriterPipelineStage extends GenericWriterPipelineStage { ! public static final String HOST = "host"; ! public static final String PORT = "port"; ! public static final String USERNAME = "username"; ! public static final String PASSWORD = "password"; ! public static final String RECIPIENTS = "recipients"; public static final String CHATROOM = "chatroom"; //$NON-NLS-1$ ! public static final String NICKNAME = "nickname"; ! public static final String SSL = Messages.getString("XmppWriterPipelineStage.7"); public XmppWriterPipelineStage() { super(new PipelineStageInfo() { public String getName() { ! return "XmppWriter"; } public String getDescription() { ! return Messages.getString("XmppWriterPipelineStage.9"); //$NON-NLS-1$ } --- 93,127 ---- public class XmppWriterPipelineStage extends GenericWriterPipelineStage { ! /** ! * ! */ ! private static final long serialVersionUID = -4487658270290844665L; ! public static final String HOST = "host"; //$NON-NLS-1$ ! public static final String PORT = "port"; //$NON-NLS-1$ ! public static final String USERNAME = "username"; //$NON-NLS-1$ ! public static final String PASSWORD = "password"; //$NON-NLS-1$ ! public static final String RECIPIENTS = "recipients"; //$NON-NLS-1$ public static final String CHATROOM = "chatroom"; //$NON-NLS-1$ ! public static final String NICKNAME = "nickname"; //$NON-NLS-1$ ! public static final String SSL = "ssl"; //$NON-NLS-1$ ! public static final String WAIT_DELIVERY = "waitUntilDelivery"; //$NON-NLS-1$ ! public static final String MESSAGE = "notificationMessage"; //$NON-NLS-1$ ! public static final String ANSWER = "answer"; //$NON-NLS-1$ ! ! MessageDeliveryStatus deliveryStatus = null; public XmppWriterPipelineStage() { super(new PipelineStageInfo() { + /** + * + */ + private static final long serialVersionUID = 1L; + public String getName() { ! return "XmppWriter"; //$NON-NLS-1$ } public String getDescription() { ! return "Pipeline stage that use XMPP (Jabber) protocol for sending messages"; } *************** *** 113,139 **** ArrayList options = new ArrayList(); ! options.add(new ConfigOption(HOST, IConfigOptionType.STRING, ! "jabber.org", false, Messages.getString("XmppWriterPipelineStage.11"))); options.add(new ConfigOption(PORT, IConfigOptionType.INTEGER, ! "5222", false, ! Messages.getString("XmppWriterPipelineStage.13"))); //$NON-NLS-1$ options.add(new ConfigOption(USERNAME, ! IConfigOptionType.STRING, null, true, ! Messages.getString("XmppWriterPipelineStage.14"))); //$NON-NLS-1$ options.add(new ConfigOption(PASSWORD, ! IConfigOptionType.STRING, null, true, ! Messages.getString("XmppWriterPipelineStage.15"))); //$NON-NLS-1$ ! options ! .add(new ConfigOption(RECIPIENTS, ! IConfigOptionType.STRING, null, false, ! Messages.getString("XmppWriterPipelineStage.16"))); //$NON-NLS-1$ options.add(new ConfigOption(CHATROOM, ! IConfigOptionType.STRING, null, false, ! Messages.getString("XmppWriterPipelineStage.17"))); //$NON-NLS-1$ options.add(new ConfigOption(NICKNAME, ! IConfigOptionType.STRING, null, false, ! Messages.getString("XmppWriterPipelineStage.18"))); //$NON-NLS-1$ ! options.add(new ConfigOption(SSL, IConfigOptionType.BOOLEAN, ! "false", false, Messages.getString("XmppWriterPipelineStage.20"))); //$NON-NLS-1$ //$NON-NLS-2$ return options; --- 129,164 ---- ArrayList options = new ArrayList(); ! options ! .add(new ConfigOption( ! HOST, ! IConfigOptionType.STRING, ! "jabber.org", false, "Jabber server name")); //$NON-NLS-1$ options.add(new ConfigOption(PORT, IConfigOptionType.INTEGER, ! "5222", false, //$NON-NLS-1$ ! "Port using for connecting. Defaults to 5222")); options.add(new ConfigOption(USERNAME, ! IConfigOptionType.STRING, null, true, "Jabber account name")); options.add(new ConfigOption(PASSWORD, ! IConfigOptionType.STRING, null, true, "Jabber account password")); ! options.add(new ConfigOption(RECIPIENTS, ! IConfigOptionType.STRING, null, false, "Comma separated list of recipients that should receive IM")); options.add(new ConfigOption(CHATROOM, ! IConfigOptionType.STRING, null, false, "Name of chatroom that should receive IM")); options.add(new ConfigOption(NICKNAME, ! IConfigOptionType.STRING, null, false, "Nickname used for chatrooms")); ! options ! .add(new ConfigOption( ! SSL, ! IConfigOptionType.BOOLEAN, ! "false", false, "Use ssl connections")); //$NON-NLS-1$ ! options ! .add(new ConfigOption( ! WAIT_DELIVERY, ! IConfigOptionType.BOOLEAN, ! "false", false, "Wait until delivery event notification")); //$NON-NLS-1$ ! options.add(new ConfigOption(MESSAGE, IConfigOptionType.STRING, ! null, false, "Notification message to send")); ! options.add(new ConfigOption(ANSWER, IConfigOptionType.STRING, ! null, false, "Expected answer for send document")); return options; *************** *** 148,194 **** */ public PipelineStageResult[] process() throws PipelineException { ! String host = this.getInfo().getStrValue(HOST); ! int port = this.getInfo().getIntValue(PORT); ! String username = this.getInfo().getStrValue(USERNAME); ! String password = this.getInfo().getStrValue(PASSWORD); ! String[] recipients = StringUtils.split(this.getInfo().getStrValue( ! RECIPIENTS), ","); ! String chatroom = this.getInfo().getStrValue(CHATROOM); ! String nickname = this.getInfo().getStrValue(NICKNAME); ! boolean useSSL = this.getInfo().getBooleanValue(SSL); ConnectionConfiguration connConfig = null; XMPPConnection conn = null; MultiUserChat groupChat = null; ! String message = this.getContent(); try { if (useSSL) { // conn = new SSLXMPPConnection(host); ! throw new PipelineException("SSL Protocol not supported"); } else { ! connConfig = new ConnectionConfiguration(host,port); conn = new XMPPConnection(connConfig); ! } conn.login(username, password); for (int i = 0; i < recipients.length; i++) { Chat chat = conn.getChatManager().createChat(recipients[i], new XmppPipelineMessageListener()); chat.sendMessage(message); } ! if (chatroom != null && !"".equals(chatroom)) { // GroupChat chat = conn.createGroupChat(chatroom); // chat.sendMessage(message); groupChat.join(nickname, password); groupChat.sendMessage(message); } conn.disconnect(); } catch (XMPPException xme) { ! throw new PipelineException("Error sending message", xme); // xme.printStackTrace(); } return this.processHelper(); } - } --- 173,261 ---- */ public PipelineStageResult[] process() throws PipelineException { ! final String host = this.getInfo().getStrValue(HOST); ! final int port = this.getInfo().getIntValue(PORT); ! final String username = this.getInfo().getStrValue(USERNAME); ! final String password = this.getInfo().getStrValue(PASSWORD); ! final String[] recipients = StringUtils.split(this.getInfo().getStrValue( ! RECIPIENTS), ","); //$NON-NLS-1$ ! final String chatroom = this.getInfo().getStrValue(CHATROOM); ! final String nickname = this.getInfo().getStrValue(NICKNAME); ! final boolean useSSL = this.getInfo().getBooleanValue(SSL); ! boolean waitDelivery = this.getInfo().getBooleanValue(WAIT_DELIVERY); ! final String notificationMessage = this.getInfo().getStrValue(MESSAGE); ! final String answer = this.getInfo().getStrValue(ANSWER); ! ! deliveryStatus = MessageDeliveryStatus.getMessageDeliveryStatus(); ! ConnectionConfiguration connConfig = null; XMPPConnection conn = null; MultiUserChat groupChat = null; ! Message message = null; ! try { if (useSSL) { // conn = new SSLXMPPConnection(host); ! throw new PipelineException("SSL Protocol not supported"); } else { ! connConfig = new ConnectionConfiguration(host, port); conn = new XMPPConnection(connConfig); ! } + MessageEventManager messageEventManager = new MessageEventManager( + conn); + messageEventManager + .addMessageEventNotificationListener(new PipelineMessageEventNotificationListener()); + conn.login(username, password); for (int i = 0; i < recipients.length; i++) { Chat chat = conn.getChatManager().createChat(recipients[i], new XmppPipelineMessageListener()); + message = new Message(); + message.setBody(notificationMessage); + message.setType(Message.Type.chat); + MessageEventManager.addNotificationsRequests(message, true, + true, true, true); + deliveryStatus.setDeliveryStatus(message.getPacketID(), + MessageDeliveryStatus.SENDING); chat.sendMessage(message); + } ! if (chatroom != null && !"".equals(chatroom)) { //$NON-NLS-1$ // GroupChat chat = conn.createGroupChat(chatroom); // chat.sendMessage(message); + + // If send message to a group dont wait for delivery + waitDelivery = false; groupChat.join(nickname, password); + message = groupChat.createMessage(); + message.setSubject(notificationMessage); + message.setBody(notificationMessage); + message.setType(Message.Type.groupchat); + // MessageEventManager.addNotificationsRequests(message, true, true, true, true); groupChat.sendMessage(message); } + if (waitDelivery) { + try { + while (deliveryStatus.getDeliveryStatus(message + .getPacketID()) == MessageDeliveryStatus.SENDING) { + + Thread.sleep(15); + } + } catch (InterruptedException e) { + throw new PipelineException( + "Pipeline aborted while waiting for delivery."); + + } + } conn.disconnect(); } catch (XMPPException xme) { ! throw new PipelineException("Error sending message", xme); // xme.printStackTrace(); + } catch (MessageStatusException e) { + throw new PipelineException("Can't read message status."); } return this.processHelper(); } } --- NEW FILE: PipelineMessageEventNotificationListener.java --- package com.babeldoc.jabber.pipeline.stage; import org.jivesoftware.smackx.MessageEventNotificationListener; public class PipelineMessageEventNotificationListener implements MessageEventNotificationListener { private static MessageDeliveryStatus deliveredStatus = null; public void cancelledNotification(String arg0, String arg1) { // Ignore this notification } public void composingNotification(String arg0, String arg1) { // Ignore this notification } public void deliveredNotification(String arg0, String arg1) { // Message was delivered, wait until displayed deliveredStatus = MessageDeliveryStatus.getMessageDeliveryStatus(); try { deliveredStatus.setDeliveryStatus(arg0, arg1); } catch (MessageStatusException e) { //TODO some sort of pipeline exception System.out.println("Singleton error: " + e.getMessage()); } } public void displayedNotification(String arg0, String arg1) { // Message was displayed, piepline success deliveredStatus = MessageDeliveryStatus.getMessageDeliveryStatus(); try { deliveredStatus.setDeliveryStatus(arg0, arg1); } catch (MessageStatusException e) { //TODO some sort of pipeline exception System.out.println("Singleton error: " + e.getMessage()); } } public void offlineNotification(String arg0, String arg1) { // The user is offline, pipeline fails deliveredStatus = MessageDeliveryStatus.getMessageDeliveryStatus(); try { deliveredStatus.setDeliveryStatus(arg0, arg1); } catch (MessageStatusException e) { //TODO some sort of pipeline exception System.out.println("Singleton error: " + e.getMessage()); } } } --- NEW FILE: MessageDeliveryStatus.java --- package com.babeldoc.jabber.pipeline.stage; import java.util.HashMap; import org.jivesoftware.smackx.packet.MessageEvent; public class MessageDeliveryStatus { public static final String DELIVERED = MessageEvent.DELIVERED; public static final String OFFLINE = MessageEvent.OFFLINE; public static final String CANCELLED = MessageEvent.CANCELLED; public static final String COMPOSING = MessageEvent.COMPOSING; public static final String DISPLAYED = MessageEvent.DISPLAYED; public static final String SENDING = "SENDING"; private HashMap map; private MessageDeliveryStatus() { } private static MessageDeliveryStatus messageStatus; public MessageDeliveryStatus newMessage(String packetID) { if (this.map.containsKey(packetID)){ return (this); } else { this.map.put(packetID, "false"); return(this); } } public static synchronized MessageDeliveryStatus getMessageDeliveryStatus() { if(messageStatus == null) messageStatus = new MessageDeliveryStatus(); return messageStatus; } public String getDeliveryStatus (String packetID) throws MessageStatusException { if(messageStatus == null){ throw new MessageStatusException("Message Status not init."); } else if (!map.containsKey(packetID)){ throw new MessageStatusException("This message is not init."); } else return map.get(packetID).toString(); } public synchronized void setDeliveryStatus(String packetID, String status) throws MessageStatusException { if(messageStatus == null) throw new MessageStatusException("Message not registered."); if(map.containsKey(packetID)){ // They are the same if(map.get(packetID) == status) throw new MessageStatusException("Can't put the same state twice"); } else { map.put(packetID, status); } } } --- NEW FILE: MessageStatusException.java --- package com.babeldoc.jabber.pipeline.stage; import com.babeldoc.core.GeneralException; public class MessageStatusException extends GeneralException { /** * */ private static final long serialVersionUID = -3324619817342835619L; /** * */ public MessageStatusException() { super(); } /** * @param message * @param exception */ public MessageStatusException(String message, Throwable exception) { super(message, exception); } /** * @param message */ public MessageStatusException(String message) { super(message); } } |