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  import java.io.OutputStream;
40  
41  import org.marre.mime.MimeBodyPart;
42  import org.marre.sms.SmsUserData;
43  import org.marre.util.StringUtil;
44  import org.marre.wap.mms.MmsConstants;
45  import org.marre.wap.mms.MmsHeaderEncoder;
46  
47  /***
48   * Simple MMS notification message sent over Sms.
49   * 
50   * @version $Id: SmsMmsNotificationMessage.java,v 1.1 2004/12/14 19:55:00 c95men Exp $
51   */
52  public class SmsMmsNotificationMessage extends SmsWapPushMessage
53  {
54      private static final int DEFAULT_TRANSACTION_ID_LENGTH = 5;
55      private static final long DEFAULT_EXPIRY = 3 * 24 * 60 * 60; // 3 days
56  
57      protected String myTransactionId;
58      protected String myFrom;
59      protected String mySubject;
60      protected int myMessageClassId = MmsConstants.X_MMS_MESSAGE_CLASS_ID_PERSONAL;
61      protected long mySize;
62      protected long myExpiry;
63      protected String myContentLocation;
64  
65      public SmsMmsNotificationMessage(String theContentLocation, long size)
66      {
67          super();
68  
69          myContentLocation = theContentLocation;
70          myTransactionId = StringUtil.randString(DEFAULT_TRANSACTION_ID_LENGTH);
71          myExpiry = DEFAULT_EXPIRY;
72          mySize = size;
73      }
74      
75      protected void writeNotificationTo(OutputStream os) throws IOException
76      {
77          // X-Mms-Message-Type (m-notification-ind)
78          MmsHeaderEncoder.writeHeaderXMmsMessageType(os, MmsConstants.X_MMS_MESSAGE_TYPE_ID_M_NOTIFICATION_IND);
79          MmsHeaderEncoder.writeHeaderXMmsTransactionId(os, myTransactionId);
80          MmsHeaderEncoder.writeHeaderXMmsMmsVersion(os, MmsConstants.X_MMS_MMS_VERSION_ID_1_0);
81  
82          if ((myFrom != null) && (myFrom.length() > 0))
83          {
84              MmsHeaderEncoder.writeHeaderFrom(os, myFrom);
85          }
86  
87          if ((mySubject != null) && (mySubject.length() > 0))
88          {
89              MmsHeaderEncoder.writeHeaderSubject(os, mySubject);
90          }
91  
92          MmsHeaderEncoder.writeHeaderXMmsMessageClass(os, myMessageClassId);
93          MmsHeaderEncoder.writeHeaderXMmsMessageSize(os, mySize);
94          MmsHeaderEncoder.writeHeaderXMmsExpiryRelative(os, myExpiry);
95          MmsHeaderEncoder.writeHeaderContentLocation(os, myContentLocation);
96      }
97  
98      public void setMessageClass(int messageClassId)
99      {
100         myMessageClassId = messageClassId;
101     }
102 
103     public void setSubject(String theSubject)
104     {
105         mySubject = theSubject;
106     }
107 
108     public void setExpiry(int i)
109     {
110         myExpiry = i;
111     }
112 
113     public void setFrom(String string)
114     {
115         myFrom = string;
116     }
117 
118     public void setTransactionId(String transactionId)
119     {
120         myTransactionId = transactionId;
121     }
122     
123     public SmsUserData getUserData()
124     {
125         ByteArrayOutputStream baos = new ByteArrayOutputStream(256);
126         
127         try
128         {
129             writeNotificationTo(baos);
130             baos.close();
131         }
132         catch (IOException ex)
133         {
134             throw new RuntimeException(ex.getMessage());
135         }        
136         
137         myPushMsg = new MimeBodyPart(baos.toByteArray(), "application/vnd.wap.mms-message");        
138         setXWapApplicationId("x-wap-application:mms.ua");
139 
140         return super.getUserData();
141     }
142 }