View Javadoc

1   /* ***** BEGIN LICENSE BLOCK *****
2    * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3    *
4    * The contents of this file are subject to the Mozilla Public License Version
5    * 1.1 (the "License"); you may not use this file except in compliance with
6    * the License. You may obtain a copy of the License at
7    * http://www.mozilla.org/MPL/
8    *
9    * Software distributed under the License is distributed on an "AS IS" basis,
10   * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11   * for the specific language governing rights and limitations under the
12   * License.
13   *
14   * The Original Code is "SMS Library for the Java platform".
15   *
16   * The Initial Developer of the Original Code is Markus Eriksson.
17   * Portions created by the Initial Developer are Copyright (C) 2002
18   * the Initial Developer. All Rights Reserved.
19   *
20   * Contributor(s):
21   *
22   * Alternatively, the contents of this file may be used under the terms of
23   * either the GNU General Public License Version 2 or later (the "GPL"), or
24   * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
25   * in which case the provisions of the GPL or the LGPL are applicable instead
26   * of those above. If you wish to allow use of your version of this file only
27   * under the terms of either the GPL or the LGPL, and not to allow others to
28   * use your version of this file under the terms of the MPL, indicate your
29   * decision by deleting the provisions above and replace them with the notice
30   * and other provisions required by the GPL or the LGPL. If you do not delete
31   * the provisions above, a recipient may use your version of this file under
32   * the terms of any one of the MPL, the GPL or the LGPL.
33   *
34   * ***** END LICENSE BLOCK ***** */
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          // The current wbxml encoder can only output utf-8
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          // The current wbxml encoder can only output utf-8
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             // Data
120             thePushMsg.writeXmlTo(thePushMsg.getWbxmlWriter(baos));
121 
122             // Done
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             // WSP HEADER
147             //
148 
149             // TID - Transaction ID
150             // FIXME: Should perhaps set TID to something useful?
151             WspUtil.writeUint8(baos, 0x00);
152 
153             // Type
154             WspUtil.writeUint8(baos, WapConstants.PDU_TYPE_PUSH);
155 
156             //
157             // WAP PUSH FIELDS
158             //
159 
160             // Create headers first
161             ByteArrayOutputStream headers = new ByteArrayOutputStream();
162             
163             // Content-type
164             wapMimeEncoder.writeContentType(headers, myPushMsg);
165 
166             // WAP-HEADERS
167             wapMimeEncoder.writeHeaders(headers, myPushMsg);
168                         
169             // Done with the headers...
170             headers.close();
171 
172             // Headers created, write headers lenght and headers to baos
173 
174             // HeadersLen - Length of Content-type and Headers
175             WspUtil.writeUintvar(baos, headers.size());
176 
177             // Headers
178             baos.write(headers.toByteArray());
179 
180             // Data
181             wapMimeEncoder.writeBody(baos, myPushMsg);
182 
183             // Done
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  }