/* * Funambol is a mobile platform developed by Funambol, Inc. * Copyright (C) 2003 - 2007 Funambol, Inc. * * This program is free software; you can redistribute it and/or modify it under * the terms of the GNU Affero General Public License version 3 as published by * the Free Software Foundation with the addition of the following permission * added to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED * WORK IN WHICH THE COPYRIGHT IS OWNED BY FUNAMBOL, FUNAMBOL DISCLAIMS THE * WARRANTY OF NON INFRINGEMENT OF THIRD PARTY RIGHTS. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU Affero General Public License * along with this program; if not, see http://www.gnu.org/licenses or write to * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, * MA 02110-1301 USA. * * You can contact Funambol, Inc. headquarters at 643 Bair Island Road, Suite * 305, Redwood City, CA 94063, USA, or at email address info@funambol.com. * * The interactive user interfaces in modified source and object code versions * of this program must display Appropriate Legal Notices, as required under * Section 5 of the GNU Affero General Public License version 3. * * In accordance with Section 7(b) of the GNU Affero General Public License * version 3, these Appropriate Legal Notices must retain the display of the * "Powered by Funambol" logo. If the display of the logo is not reasonably * feasible for technical reasons, the Appropriate Legal Notices must display * the words "Powered by Funambol". */ package br.gov.serpro.funambol.pushmail; import com.funambol.framework.notification.NotificationException; import com.funambol.framework.server.Sync4jDevice; import com.funambol.framework.notification.Message; import com.funambol.framework.logging.FunambolLogger; import com.funambol.framework.logging.FunambolLoggerFactory; import com.funambol.framework.logging.Sync4jLoggerName; import com.funambol.framework.tools.DbgTools; import com.funambol.framework.notification.sender.Sender; import com.funambol.server.notification.sender.WDPHeader; import com.funambol.server.notification.sender.WSPHeader; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.URL; /** * * Sends a SMS WAP PushMail Notification to a SMS Gateway thru HTTP * After install this module, you must set the parameters in file * Funambol/config/com/funambol/server/notification/PushMailSMSSender.xml * Author: emerson-faria.nobre@serpro.gov.br - mar/2009 */ public class PushMailSMSSender implements Sender { private String user; private String pwd; private String gatewayIP; private String gatewayPort; private String gatewayURL; private WDPHeader wdpHeader; private WSPHeader wspHeader; // ------------------------------------------------------------- Constructor public PushMailSMSSender() { user = null; pwd = null; gatewayIP = null; gatewayURL = "http://%s:%s/cgi-bin/sendsms?user=%s&pass=%s&to=%s&udh=%s&text=%s"; wdpHeader = null; wspHeader = null; } // ------------------------------------------------------------ Private data /** * The logger */ private FunambolLogger log = FunambolLoggerFactory.getLogger(Sync4jLoggerName.SERVER_NOTIFICATION); // ---------------------------------------------------------- Public methods // It is the first called method. // It builds the message and send thru HTTP. // A binary notification message is composed by WdpHeader + WspHeader + Body. public void sendNotificationMessage(Sync4jDevice device, Message msg) throws NotificationException { // get the message body byte message[] = msg.getMessageContent(); // get the destination phone number String deviceAddress = device.getMsisdn(); // Create the Wireless Datagram Protocol Header (WDPHeader) object if (device.getDeviceId().startsWith("fwm")) { wdpHeader = new WDPHeader(); // for Windows Mobile Client } else { wdpHeader = new WDPHeader(50001); // for JavaME Client } // if there is no phone number, then whe can not send the SMS. if (deviceAddress.length() == 0) { return; } // Create the Wireless Session Protocol Header (WSPHeader) object wspHeader = new WSPHeader((byte) -50); // Convert from byte to hexadecimal. URL Encode the hexadecimal to send thru HTTP. String WDPHeaderURLEncoded = bytesToHexURLEncoded(wdpHeader.getHeader()); String WSPHeaderURLEncoded = bytesToHexURLEncoded(wspHeader.getHeader()); String MessageBodyURLEncoded = bytesToHexURLEncoded(message); // Append WSPHeader and message body String WSPHeaderPlusMessageBody = (new StringBuilder()).append(WSPHeaderURLEncoded).append(MessageBodyURLEncoded ).toString(); // Build the URL to send to gateway(HTTP server) // http://:/cgi-bin/sendsms?user=&pass=&to=&udh=&text= String Url = String.format(gatewayURL, new Object[]{this.getGatewayIP(), this.getGatewayPort(), this.getUser(), this.getPwd(), deviceAddress, WDPHeaderURLEncoded, WSPHeaderPlusMessageBody}); try { URL ur = new URL(Url); if (log.isInfoEnabled()) log.info("PushMailSMSSender - URL: " + ur.toString()); // Connect to gateway java.io.InputStream rsStream = ur.openStream(); BufferedReader rReader = new BufferedReader(new InputStreamReader(rsStream)); // Get the Gateway Response String Id = rReader.readLine(); log.info((new StringBuilder()).append("PushMailSMSSender - respose: ").append(Id).toString()); rReader.close(); } catch (Exception e) { // Connection to gateway cound not be estabilished throw new NotificationException("PushMailSMSSender - Error sending notification to gateway: ", e); } } // Include the % char before each hexadecimal octet private String bytesToHexURLEncoded(byte [] b) { StringBuffer buf = new StringBuffer(""); for (int i=0; i< b.length;i++) buf.append("%").append(DbgTools.byteToHex(b[i])); return buf.toString(); } // Getters and Setters public String getUser() { return this.user; } public void setUser(String user) { this.user = user; } public String getPwd() { return this.pwd; } public void setPwd(String pwd) { this.pwd = pwd; } public void setGatewayIP(String gatewayIP) { this.gatewayIP = gatewayIP; } public String getGatewayIP() { return this.gatewayIP; } public void setGatewayPort(String gatewayPort) { this.gatewayPort = gatewayPort; } public String getGatewayPort() { return this.gatewayPort; } public void setGatewayURL(String gatewayURL) { this.gatewayURL = gatewayURL; } public String getGatewayURL() { return this.gatewayURL; } }