KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > server > notification > HttpSender


1 /**
2  * Copyright (C) 2003-2005 Funambol
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18
19 package sync4j.server.notification;
20
21 import java.io.BufferedOutputStream JavaDoc;
22 import java.net.Socket JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.util.logging.Level JavaDoc;
25 import java.util.logging.Logger JavaDoc;
26
27 import sync4j.framework.logging.Sync4jLogger;
28 import sync4j.framework.notification.NotificationConstants;
29 import sync4j.framework.notification.NotificationException;
30 import sync4j.framework.notification.Sender;
31 import sync4j.framework.logging.Sync4jLoggerName;
32
33
34 /**
35  * This sender sends notification message via http (to use with SCTS DM)
36  * <p/>
37  * To obtain the same result of the SCTS server you must to set:
38  * <ui>SessionId: 12345</ui>
39  * <ui>ServerId: Scts</ui>
40  * <ui>ServerPwd: SimpleClient</ui>
41  * <ui>ServerNonce: AOVuAA== (this is already in base 64 format. Corresponds to 00E56E00)</ui>
42  * <br/>and in <code>sync4j.server.notification.NotificationEngineImpl</code> you must to set:
43  * <ui>SYNCML_DM_PROTOCOL_VERSION = 1.1f</ui>
44  *
45  * @author Stefano Nichele @ Funambol
46  * @version $Id: HttpSender.java,v 1.1 2005/05/16 17:32:56 nichele Exp $
47  */

48 public class HttpSender implements Sender {
49
50     // -------------------------------------------------------------- Constants
51
// -------------------------------------------------------------- Properties
52
private String JavaDoc deviceAddress = null;
53
54     // ------------------------------------------------------------ Private data
55
private Logger JavaDoc log = null;
56
57     // --------------------------------------------------------------- Constructor
58
public HttpSender() {
59         log = Sync4jLogger.getLogger(Sync4jLoggerName.SERVER_DM_NOTIFICATION);
60     }
61
62     // ------------------------------------------------------------ Public Methods
63

64     /**
65      * Gets the address device
66      *
67      * @return the address device
68      */

69     public String JavaDoc getDeviceAddress() {
70         return deviceAddress;
71     }
72
73     /**
74      * Sets the address device
75      *
76      * @param the address device
77      */

78     public void setDeviceAddress(String JavaDoc deviceAddress) {
79         this.deviceAddress = deviceAddress;
80     }
81
82     /**
83      * Send the given message via http
84      * @param messageType int
85      * @param sessionId String
86      * @param phoneNumber String
87      * @param message byte[]
88      */

89     public void sendMessage(int messageType, String JavaDoc phoneNumber, byte[] message, String JavaDoc info)
90         throws NotificationException {
91
92         if (log.isLoggable(Level.INFO)) {
93             log.info("sendMessage: " + message + " (length: " + message.length + ")");
94         }
95
96
97         if (messageType != NotificationConstants.MESSAGE_TYPE_NOTIFICATION_GENERIC) {
98             throw new NotificationException("This sender is usable only for notification message");
99         }
100
101         byte[] nestedRequest = buildNestedRequest(message);;
102         byte[] request = buildRequest(nestedRequest);
103
104         try {
105             sendHttpRequest(request);
106         } catch (Exception JavaDoc e) {
107             throw new NotificationException("Error sending the message via http", e);
108         }
109
110     }
111
112     /**
113      * Sends more messages
114      * @param messageType the type of the message. See {@link NotificationConstants}
115      * @param contentType the contentType
116      * @param macs the macs list
117      * @param authMethods the authentication methods
118      * @param phoneNumbers the phone number list
119      * @param messages the messages to send
120      * @param info application specific info
121      * @throws NotificationException
122      */

123     public void sendMessages(int messageType,
124                              String JavaDoc contentType,
125                              String JavaDoc[] digest,
126                              int[] authMethods,
127                              String JavaDoc[] phoneNumbers,
128                              byte[][] messages,
129                              String JavaDoc info) throws NotificationException {
130
131         if (log.isLoggable(Level.INFO)) {
132             log.info("sendMessage with empty implementation");
133         }
134     }
135
136     /**
137      * Sends messages
138      * @param messageType the type of the message. See {@link NotificationConstants}
139      * @param phoneNumbers the phone numbers list of the target device
140      * @param messages the messages to send
141      * @param info application specific info
142      * @throws NotificationException
143      */

144     public void sendMessages(int messageType,
145                              String JavaDoc[] phoneNumbers,
146                              byte[][] messages,
147                              String JavaDoc info) throws NotificationException {
148
149         for (int i=0; i<phoneNumbers.length; i++) {
150
151             //
152
// For the first message not wait
153
//
154
if (i != 0) {
155                 System.out.println("Wait 20 s for test with SCTS");
156                 try {
157                     Thread.sleep(20000);
158                 } catch (InterruptedException JavaDoc ex) {
159                 }
160
161             }
162             sendMessage(messageType, phoneNumbers[i], messages[i], info);
163         }
164     }
165
166
167     private void sendHttpRequest(byte[] message) throws Exception JavaDoc {
168         if (log.isLoggable(Level.INFO)) {
169             log.info("Send message to: " + deviceAddress);
170         }
171         URL JavaDoc url = new URL JavaDoc(deviceAddress);
172         String JavaDoc host = url.getHost();
173         int port = url.getPort();
174
175         Socket JavaDoc conn = new Socket JavaDoc(host, port);
176
177         // Send data
178
BufferedOutputStream JavaDoc wr = new BufferedOutputStream JavaDoc(conn.getOutputStream());
179
180         wr.write(message);
181         wr.flush();
182
183         wr.close();
184     }
185
186     private byte[] buildRequest(byte[] nestedRequest) {
187         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("POST /wappush HTTP/1.1\r\n");
188         sb.append("Host: 127.0.0.1\r\n");
189         sb.append("Content-Type: application/http\r\n");
190         sb.append("Content-Length: ").append(nestedRequest.length).append("\r\n");
191         sb.append("X-Wap-Push-OTA-Version: 1.0\r\n\r\n");
192
193         sb.append(new String JavaDoc(nestedRequest));
194
195         return sb.toString().getBytes();
196
197     }
198
199
200     private byte[] buildNestedRequest(byte[] message) {
201         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("HTTP/1.1 200 OK\r\n");
202         sb.append("Content-Language: en\r\n");
203         sb.append("Content-Length: ").append(message.length).append("\r\n");
204         sb.append("Content-Type: application/vnd.syncml.notification\r\n\r\n");
205
206         byte[] header = sb.toString().getBytes();
207
208         byte[] nestedRequest = new byte[header.length + message.length];
209
210         System.arraycopy(header, 0, nestedRequest, 0, header.length);
211         System.arraycopy(message, 0, nestedRequest, header.length, message.length);
212
213         return nestedRequest;
214
215
216     }
217
218 }
Popular Tags