1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35 package org.marre.wap.push;
36
37 import java.io.ByteArrayOutputStream;
38 import java.io.IOException;
39
40 import org.marre.mime.MimeBodyPart;
41 import org.marre.mime.MimeContentType;
42 import org.marre.sms.SmsConstants;
43 import org.marre.sms.SmsPortAddressedMessage;
44 import org.marre.sms.SmsUserData;
45 import org.marre.wap.WapConstants;
46 import org.marre.wap.WapMimeEncoder;
47 import org.marre.wap.WspUtil;
48 import org.marre.wap.wbxml.WbxmlDocument;
49
50 /***
51 * Connectionless WAP push message with SMS as bearer.
52 *
53 * It supports the "content-type" and "X-Wap-Application-Id" headers.
54 *
55 * @author Markus Eriksson
56 * @version 1.0
57 */
58 public class SmsWapPushMessage extends SmsPortAddressedMessage
59 {
60 protected byte myWspEncodingVersion = WapConstants.WSP_ENCODING_VERSION_1_2;
61 protected MimeBodyPart myPushMsg;
62
63 protected SmsWapPushMessage()
64 {
65 super(SmsConstants.PORT_WAP_PUSH, SmsConstants.PORT_WAP_WSP);
66 }
67
68 public SmsWapPushMessage(MimeBodyPart thePushMsg)
69 {
70 this();
71
72 myPushMsg = thePushMsg;
73 }
74
75 public SmsWapPushMessage(WbxmlDocument thePushMsg, MimeContentType theContentType)
76 {
77 this();
78
79
80 theContentType.setParam("charset", "utf-8");
81 myPushMsg = new MimeBodyPart(buildPushMessage(thePushMsg), theContentType);
82 }
83
84 public SmsWapPushMessage(WbxmlDocument thePushMsg, String theContentType)
85 {
86 this();
87
88 MimeContentType ct = new MimeContentType(theContentType);
89
90 ct.setParam("charset", "utf-8");
91 myPushMsg = new MimeBodyPart(buildPushMessage(thePushMsg), ct);
92 }
93
94 public SmsWapPushMessage(WbxmlDocument thePushMsg)
95 {
96 this(thePushMsg, thePushMsg.getWbxmlContentType());
97 }
98
99 public SmsWapPushMessage(byte[] thePushMsg, MimeContentType theContentType)
100 {
101 this();
102
103 myPushMsg = new MimeBodyPart(thePushMsg, theContentType);
104 }
105
106 public SmsWapPushMessage(byte[] thePushMsg, String theContentType)
107 {
108 this();
109
110 myPushMsg = new MimeBodyPart(thePushMsg, theContentType);
111 }
112
113 protected byte[] buildPushMessage(WbxmlDocument thePushMsg)
114 {
115 ByteArrayOutputStream baos = new ByteArrayOutputStream();
116
117 try
118 {
119
120 thePushMsg.writeXmlTo(thePushMsg.getWbxmlWriter(baos));
121
122
123 baos.close();
124 }
125 catch (IOException ex)
126 {
127 throw new RuntimeException(ex.getMessage());
128 }
129
130 return baos.toByteArray();
131 }
132
133 public void setWspEncodingVersion(byte wspEncodingVersion)
134 {
135 myWspEncodingVersion = wspEncodingVersion;
136 }
137
138 public SmsUserData getUserData()
139 {
140 WapMimeEncoder wapMimeEncoder = new WapMimeEncoder(WapConstants.WSP_ENCODING_VERSION_1_2);
141 ByteArrayOutputStream baos = new ByteArrayOutputStream();
142
143 try
144 {
145
146
147
148
149
150
151 WspUtil.writeUint8(baos, 0x00);
152
153
154 WspUtil.writeUint8(baos, WapConstants.PDU_TYPE_PUSH);
155
156
157
158
159
160
161 ByteArrayOutputStream headers = new ByteArrayOutputStream();
162
163
164 wapMimeEncoder.writeContentType(headers, myPushMsg);
165
166
167 wapMimeEncoder.writeHeaders(headers, myPushMsg);
168
169
170 headers.close();
171
172
173
174
175 WspUtil.writeUintvar(baos, headers.size());
176
177
178 baos.write(headers.toByteArray());
179
180
181 wapMimeEncoder.writeBody(baos, myPushMsg);
182
183
184 baos.close();
185 }
186 catch (IOException ex)
187 {
188 throw new RuntimeException(ex.getMessage());
189 }
190
191 return new SmsUserData(baos.toByteArray());
192 }
193
194 public void setXWapApplicationId(String appId)
195 {
196 myPushMsg.addHeader("X-Wap-Application-Id", appId);
197 }
198
199 public void setXWapContentURI(String contentUri)
200 {
201 myPushMsg.addHeader("X-Wap-Content-URI", contentUri);
202 }
203
204 public void setXWapInitiatorURI(String initiatorUri)
205 {
206 myPushMsg.addHeader("X-Wap-Initiator-URI", initiatorUri);
207 }
208 }