source: contrib/funambol/PushMailSMSSender/src/main/java/br/gov/serpro/funambol/pushmail/PushMailSMSSender.java @ 1356

Revision 1356, 7.3 KB checked in by emersonfaria, 15 years ago (diff)

Ticket #627 - Feito check-in do modulo PushMailSMSSender.

Line 
1/*
2 * Funambol is a mobile platform developed by Funambol, Inc.
3 * Copyright (C) 2003 - 2007 Funambol, Inc.
4 *
5 * This program is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU Affero General Public License version 3 as published by
7 * the Free Software Foundation with the addition of the following permission
8 * added to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED
9 * WORK IN WHICH THE COPYRIGHT IS OWNED BY FUNAMBOL, FUNAMBOL DISCLAIMS THE
10 * WARRANTY OF NON INFRINGEMENT  OF THIRD PARTY RIGHTS.
11 *
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
15 * details.
16 *
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program; if not, see http://www.gnu.org/licenses or write to
19 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20 * MA 02110-1301 USA.
21 *
22 * You can contact Funambol, Inc. headquarters at 643 Bair Island Road, Suite
23 * 305, Redwood City, CA 94063, USA, or at email address info@funambol.com.
24 *
25 * The interactive user interfaces in modified source and object code versions
26 * of this program must display Appropriate Legal Notices, as required under
27 * Section 5 of the GNU Affero General Public License version 3.
28 *
29 * In accordance with Section 7(b) of the GNU Affero General Public License
30 * version 3, these Appropriate Legal Notices must retain the display of the
31 * "Powered by Funambol" logo. If the display of the logo is not reasonably
32 * feasible for technical reasons, the Appropriate Legal Notices must display
33 * the words "Powered by Funambol".
34 */
35package br.gov.serpro.funambol.pushmail;
36
37import com.funambol.framework.notification.NotificationException;
38import com.funambol.framework.server.Sync4jDevice;
39import com.funambol.framework.notification.Message;
40import com.funambol.framework.logging.FunambolLogger;
41import com.funambol.framework.logging.FunambolLoggerFactory;
42import com.funambol.framework.logging.Sync4jLoggerName;
43import com.funambol.framework.tools.DbgTools;
44import com.funambol.framework.notification.sender.Sender;
45import com.funambol.server.notification.sender.WDPHeader;
46import com.funambol.server.notification.sender.WSPHeader;
47import java.io.BufferedReader;
48import java.io.InputStreamReader;
49import java.net.URL;
50
51/**
52 *
53 * Sends a SMS WAP PushMail Notification to a SMS Gateway thru HTTP
54 * After install this module, you must set the parameters in file
55 * Funambol/config/com/funambol/server/notification/PushMailSMSSender.xml
56 * Author: emerson-faria.nobre@serpro.gov.br - mar/2009
57 */
58public class PushMailSMSSender implements Sender {
59
60    private String user;
61    private String pwd;
62    private String gatewayIP;
63    private String gatewayPort;
64    private String gatewayURL;
65    private WDPHeader wdpHeader;
66    private WSPHeader wspHeader;
67
68    // ------------------------------------------------------------- Constructor
69    public PushMailSMSSender() {
70        user = null;
71        pwd = null;
72        gatewayIP = null;
73        gatewayURL = "http://%s:%s/cgi-bin/sendsms?user=%s&pass=%s&to=%s&udh=%s&text=%s";
74        wdpHeader = null;
75        wspHeader = null;
76    }
77
78    // ------------------------------------------------------------ Private data
79    /**
80     * The logger
81     */
82    private FunambolLogger log =
83            FunambolLoggerFactory.getLogger(Sync4jLoggerName.SERVER_NOTIFICATION);
84
85    // ---------------------------------------------------------- Public methods
86
87    // It is the first called method.
88    // It builds the message and send thru HTTP.
89    // A binary notification message is composed by WdpHeader + WspHeader + Body.
90    public void sendNotificationMessage(Sync4jDevice device, Message msg)
91            throws NotificationException {
92        // get the message body
93        byte message[] = msg.getMessageContent();
94        // get the destination phone number
95        String deviceAddress = device.getMsisdn();
96        // Create the Wireless Datagram Protocol Header (WDPHeader) object
97        if (device.getDeviceId().startsWith("fwm")) {
98            wdpHeader = new WDPHeader(); // for Windows Mobile Client
99        } else {
100            wdpHeader = new WDPHeader(50001); // for JavaME Client
101        }
102        // if there is no phone number, then whe can not send the SMS.
103        if (deviceAddress.length() == 0) {
104            return;
105        }
106        // Create the Wireless Session Protocol Header (WSPHeader) object
107        wspHeader = new WSPHeader((byte) -50);
108        // Convert from byte to hexadecimal. URL Encode the hexadecimal to send thru HTTP.
109        String WDPHeaderURLEncoded = bytesToHexURLEncoded(wdpHeader.getHeader());
110        String WSPHeaderURLEncoded = bytesToHexURLEncoded(wspHeader.getHeader());
111        String MessageBodyURLEncoded = bytesToHexURLEncoded(message);
112        // Append WSPHeader and message body
113        String WSPHeaderPlusMessageBody = (new StringBuilder()).append(WSPHeaderURLEncoded).append(MessageBodyURLEncoded ).toString();
114        // Build the URL to send to gateway(HTTP server)
115        // http://<GatewayIP>:<GatewayPort>/cgi-bin/sendsms?user=<USER>&pass=<PWD>&to=<deviceAddress>&udh=<WDPHeaderURLEncoded>&text=<WSPHeaderPlusMessageBody>
116        String Url = String.format(gatewayURL, new Object[]{this.getGatewayIP(), this.getGatewayPort(), this.getUser(), this.getPwd(), deviceAddress, WDPHeaderURLEncoded, WSPHeaderPlusMessageBody});
117        try {
118            URL ur = new URL(Url);
119            if (log.isInfoEnabled()) log.info("PushMailSMSSender - URL: " + ur.toString());
120            // Connect to gateway
121            java.io.InputStream rsStream = ur.openStream();
122            BufferedReader rReader = new BufferedReader(new InputStreamReader(rsStream));
123            // Get the Gateway Response
124            String Id = rReader.readLine();
125            log.info((new StringBuilder()).append("PushMailSMSSender - respose: ").append(Id).toString());
126            rReader.close();
127        } catch (Exception e) {
128            // Connection to gateway cound not be estabilished
129            throw new NotificationException("PushMailSMSSender - Error sending notification to gateway: ", e);
130        }
131    }
132
133    // Include the % char before each hexadecimal octet
134    private String bytesToHexURLEncoded(byte [] b) {
135        StringBuffer buf = new StringBuffer("");
136        for (int i=0; i< b.length;i++)
137            buf.append("%").append(DbgTools.byteToHex(b[i]));
138        return buf.toString();
139    }
140
141    // Getters and Setters
142    public String getUser() {
143        return this.user;
144    }
145
146    public void setUser(String user) {
147        this.user = user;
148    }
149
150    public String getPwd() {
151        return this.pwd;
152    }
153
154    public void setPwd(String pwd) {
155        this.pwd = pwd;
156    }
157
158    public void setGatewayIP(String gatewayIP) {
159        this.gatewayIP = gatewayIP;
160    }
161
162    public String getGatewayIP() {
163        return this.gatewayIP;
164    }
165
166    public void setGatewayPort(String gatewayPort) {
167        this.gatewayPort = gatewayPort;
168    }
169
170    public String getGatewayPort() {
171        return this.gatewayPort;
172    }
173
174    public void setGatewayURL(String gatewayURL) {
175        this.gatewayURL = gatewayURL;
176    }
177
178    public String getGatewayURL() {
179        return this.gatewayURL;
180    }
181}
Note: See TracBrowser for help on using the repository browser.