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.IOException;
38  import java.io.OutputStream;
39  import java.util.Date;
40  
41  import org.marre.wap.wbxml.WbxmlDocument;
42  import org.marre.wap.wbxml.WbxmlWriter;
43  import org.marre.xml.XmlAttribute;
44  import org.marre.xml.XmlWriter;
45  
46  /***
47   * Represents a WAP Service Loading Push message.
48   * 
49   * @author Markus
50   * @version $Id: WapSLPush.java,v 1.1 2005/08/02 19:57:32 c95men Exp $
51   */
52  public class WapSLPush implements WbxmlDocument
53  {
54      /*** WBXML content type */
55      public static final String WBXML_CONTENT_TYPE = "application/vnd.wap.slc";
56      /*** XML content type */
57      public static final String XML_CONTENT_TYPE = "text/vnd.wap.sl";
58  
59      /*** Action, execute-low */
60      public static final String ACTION_EXECUTE_LOW = "execute-low";
61      /*** Action, execute-high */
62      public static final String ACTION_EXECUTE_HIGH = "execute-high";
63      /*** Action, execute-cache */
64      public static final String ACTION_EXECUTE_CACHE = "execute-cache";
65      
66      /*** WBXML tag tokens for wap sl push. */
67      public static final String[] SL_TAG_TOKENS = {
68              "sl", // 05
69      };
70  
71      /*** WBXML attr start tokens for wap sl push. */
72      public static final String[] SL_ATTR_START_TOKENS = {
73              "action=execute-low", // 05
74              "action=execute-high", // 06
75              "action=cache", // 07
76              "href", // 08
77              "href=http://", // 09
78              "href=http://www.", // 0A
79              "href=https://", // 0B
80              "href=https://www.", // 0C
81      };
82  
83      /*** WBXML attr value tokens for wap sl push. */
84      public static final String[] SL_ATTR_VALUE_TOKENS = {
85              ".com/", // 85
86              ".edu/", // 86
87              ".net/", // 87
88              ".org/", // 88
89      };
90  
91  
92      /*** The uri. */
93      protected String uri_;
94      /*** The action. */
95      protected String action_;
96      
97      /***
98       * Constructor.
99       * 
100      * @param uri
101      */
102     public WapSLPush(String uri)
103     {
104         uri_ = uri;
105     }
106 
107     /***
108      * Returns the URI.
109      * 
110      * @return
111      */
112     public String getUri()
113     {
114         return uri_;
115     }
116 
117     /***
118      * Sets the URI.
119      * @param uri
120      */
121     public void setUri(String uri)
122     {
123         uri_ = uri;
124     }
125 
126     /***
127      * Retrieves the current set action.
128      * 
129      * @return Action or null if not set.
130      */
131     public String getAction()
132     {
133         return action_;
134     }
135 
136     /***
137      * Set the action. 
138      * 
139      * @param action Can be ACTION_EXECUTE_LOW, ACTION_EXECUTE_HIGH or ACTION_EXECUTE_CACHE.
140      */
141     public void setAction(String action)
142     {
143         if (! (ACTION_EXECUTE_CACHE.equals(action) || 
144                ACTION_EXECUTE_HIGH.equals(action) ||
145                ACTION_EXECUTE_LOW.equals(action)) ) {
146             throw new IllegalArgumentException("Action can only be execute-high, execute-low or cache.");
147         }
148         action_ = action;
149     }
150 
151     /***
152      * Writes the xml document to the given writer.
153      * 
154      * @param writer
155      */
156     public void writeXmlTo(XmlWriter writer) throws IOException
157     {
158         writer.setDoctype("sl", "-//WAPFORUM//DTD SL 1.0//EN", "http://www.wapforum.org/DTD/sl.dtd");
159 
160         if (action_ == null) {
161             writer.addEmptyElement("sl", new XmlAttribute[]{new XmlAttribute("href", uri_)});
162         } else {
163             writer.addEmptyElement("sl", new XmlAttribute[]{new XmlAttribute("href", uri_),
164                                                             new XmlAttribute("action", action_)});
165         }
166 
167         writer.flush();
168     }
169 
170     /***
171      * Returns a wbxml writer.
172      * 
173      * @param os The os to write to.
174      * @return Wbxml writer.
175      */
176     public XmlWriter getWbxmlWriter(OutputStream os)
177     {
178         return new WbxmlWriter(os, WapSLPush.SL_TAG_TOKENS, WapSLPush.SL_ATTR_START_TOKENS, WapSLPush.SL_ATTR_VALUE_TOKENS);
179     }
180 
181     /***
182      * Returns the wbxml content type.
183      * 
184      * @return wbxml content type.
185      */
186     public String getWbxmlContentType()
187     {
188         return WBXML_CONTENT_TYPE;
189     }
190 
191     /***
192      * Returns the text content type.
193      * 
194      * @return Content type
195      */
196     public String getContentType()
197     {
198         return XML_CONTENT_TYPE;
199     }
200 }