From: Santi B. <san...@us...> - 2008-01-08 23:12:14
|
Update of /cvsroot/babeldoc/babeldoc/modules/jabber/src/com/babeldoc/jabber/pipeline/stage In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv27603/modules/jabber/src/com/babeldoc/jabber/pipeline/stage Added Files: MessageStatusException.java PipelineMessageEventNotificationListener.java MessageDeliveryStatus.java XmppWriterPipelineStage.java XmppPipelineMessageListener.java Messages.java Log Message: First version of Jabber module added. --- 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"; public static final String ERROR = "ERROR"; public static final String NOT_INITIATED = "NOT INITIATED"; 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(); // } /** * This metod was modified to don't raise a exception. * We can assume that if the singleton is not init we can do safely */ public String getDeliveryStatus (String packetID){ if(messageStatus == null) messageStatus = new MessageDeliveryStatus(); if(!map.containsKey(packetID)) setDeliveryStatus(packetID, MessageDeliveryStatus.NOT_INITIATED); 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); // } // } /** * The same as for the getDeliveryStatus. */ public synchronized void setDeliveryStatus(String packetID, String status){ if(messageStatus == null) messageStatus = new MessageDeliveryStatus(); if(map.containsKey(packetID) && map.get(packetID)==status){ setDeliveryStatus(packetID, MessageDeliveryStatus.ERROR); } else map.put(packetID, status); } } --- NEW FILE: Messages.java --- package com.babeldoc.jabber.pipeline.stage; import java.util.MissingResourceException; import java.util.ResourceBundle; public class Messages { private static final String BUNDLE_NAME = "i18n.messages"; //$NON-NLS-1$ private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle .getBundle(BUNDLE_NAME); private Messages() { } public static String getString(String key) { try { return RESOURCE_BUNDLE.getString(key); } catch (MissingResourceException e) { return '!' + key + '!'; } } } --- NEW FILE: XmppWriterPipelineStage.java --- /* ==================================================================== * The Apache Software License, Version 1.1 * * Copyright (c) 2000 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Apache" and "Apache Software Foundation" must * not be used to endorse or promote products derived from this * software without prior written permission. For written * permission, please contact ap...@ap.... * * 5. Products derived from this software may not be called "Apache", * nor may "Apache" appear in their name, without prior written * permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. * * Portions of this software are based upon public domain software * originally written at the National Center for Supercomputing Applications, * University of Illinois, Urbana-Champaign. * ==================================================================== * * Babeldoc: The Universal Document Processor * * $Header: /cvsroot/babeldoc/babeldoc/modules/jabber/src/com/babeldoc/jabber/pipeline/stage/XmppWriterPipelineStage.java,v 1.1 2008/01/08 23:12:16 santibegue Exp $ * $DateTime$ * $Author: santibegue $ * */ package com.babeldoc.jabber.pipeline.stage; import java.util.ArrayList; import java.util.Collection; import org.apache.commons.lang.StringUtils; import org.jivesoftware.smack.Chat; import org.jivesoftware.smack.ConnectionConfiguration; 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; import com.babeldoc.core.option.ConfigOption; import com.babeldoc.core.option.IConfigOptionType; import com.babeldoc.core.pipeline.PipelineException; import com.babeldoc.core.pipeline.PipelineStageInfo; import com.babeldoc.core.pipeline.PipelineStageResult; import com.babeldoc.core.pipeline.stage.GenericWriterPipelineStage; /** * This class is using for sending messaging using XMPP protocol (jabber). * * @author dejank * */ 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"; } public Collection getTypeSpecificOptions() { 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; } }); } /* * (non-Javadoc) * * @see com.babeldoc.core.pipeline.PipelineStage#process() */ 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); } if(deliveryStatus.getDeliveryStatus(message.getPacketID()) == MessageDeliveryStatus.ERROR || deliveryStatus.getDeliveryStatus(message.getPacketID()) == MessageDeliveryStatus.NOT_INITIATED){ throw new PipelineException("Can't determine de delivery status."); } if(deliveryStatus.getDeliveryStatus(message.getPacketID()) == MessageDeliveryStatus.OFFLINE) throw new PipelineException("Can't send the message, the user is offline."); } 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(); } 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(); deliveredStatus.setDeliveryStatus(arg0, arg1); } public void displayedNotification(String arg0, String arg1) { // Message was displayed, piepline success deliveredStatus = MessageDeliveryStatus.getMessageDeliveryStatus(); deliveredStatus.setDeliveryStatus(arg0, arg1); } public void offlineNotification(String arg0, String arg1) { // The user is offline, pipeline fails deliveredStatus = MessageDeliveryStatus.getMessageDeliveryStatus(); deliveredStatus.setDeliveryStatus(arg0, arg1); } } --- NEW FILE: XmppPipelineMessageListener.java --- /** * */ package com.babeldoc.jabber.pipeline.stage; import org.jivesoftware.smack.Chat; import org.jivesoftware.smack.MessageListener; import org.jivesoftware.smack.XMPPException; import org.jivesoftware.smack.packet.Message; import com.babeldoc.core.pipeline.PipelineException; /** * @author santi * */ public class XmppPipelineMessageListener implements MessageListener { /* * (non-Javadoc) * * @see org.jivesoftware.smack.MessageListener#processMessage(org.jivesoftware.smack.Chat, * org.jivesoftware.smack.packet.Message) */ public void processMessage(Chat chat, Message message){ // Simple message processing parrot-bot try { chat.sendMessage(Messages.getString("XmppPipelineMessageListener.0") + message.toString()); //$NON-NLS-1$ } catch (XMPPException e) { // throw new pipeline exception new PipelineException(Messages.getString("XmppPipelineMessageListener.1"),e); //$NON-NLS-1$ } } } --- 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); } } |