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;
36  
37  import java.io.IOException;
38  import java.io.OutputStream;
39  
40  import org.marre.mime.MimeHeader;
41  
42  /***
43   * 
44   * @author Markus Eriksson
45   * @version $Id: WspHeaderEncoder.java,v 1.4 2004/11/20 19:03:04 c95men Exp $
46   */
47  public final class WspHeaderEncoder
48  {        
49      private WspHeaderEncoder()
50      {
51          // Static class
52      }
53      
54      public static void writeHeader(byte wspEncodingVersion, OutputStream theOs, MimeHeader theHeader) throws IOException
55      {
56          String headerName = theHeader.getName().toLowerCase();
57          int headerType = WspUtil.getHeaderType(headerName);
58  
59          switch (headerType)
60          {
61          case WapConstants.HEADER_ACCEPT:
62              break;
63          case WapConstants.HEADER_ACCEPT_APPLICATION:
64              break;
65          case WapConstants.HEADER_ACCEPT_CHARSET:
66              break;
67          case WapConstants.HEADER_ACCEPT_ENCODING:
68              break;
69          case WapConstants.HEADER_ACCEPT_LANGUAGE:
70              break;
71          case WapConstants.HEADER_ACCEPT_RANGES:
72              break;
73          case WapConstants.HEADER_AGE:
74              break;
75          case WapConstants.HEADER_ALLOW:
76              break;
77          case WapConstants.HEADER_AUTHORIZATION:
78              break;
79          case WapConstants.HEADER_BEARER_INDICATION:
80              break;
81          case WapConstants.HEADER_CACHE_CONTROL:
82              break;
83          case WapConstants.HEADER_CONNECTION:
84              break;
85          case WapConstants.HEADER_CONTENT_BASE:
86              break;
87          case WapConstants.HEADER_CONTENT_DISPOSITION:
88              break;
89          case WapConstants.HEADER_CONTENT_ID:
90              writeHeaderContentID(wspEncodingVersion, theOs, theHeader.getValue());
91              break;
92          case WapConstants.HEADER_CONTENT_LANGUAGE:
93              break;
94          case WapConstants.HEADER_CONTENT_LENGTH:
95              break;
96          case WapConstants.HEADER_CONTENT_LOCATION:
97              writeHeaderContentLocation(wspEncodingVersion, theOs, theHeader.getValue());
98              break;
99          case WapConstants.HEADER_CONTENT_MD5:
100             break;
101         case WapConstants.HEADER_CONTENT_RANGE:
102             break;
103         case WapConstants.HEADER_CONTENT_TYPE:
104             writeHeaderContentType(wspEncodingVersion, theOs, theHeader);
105             break;
106         case WapConstants.HEADER_COOKIE:
107             break;
108         case WapConstants.HEADER_DATE:
109             break;
110         case WapConstants.HEADER_ENCODING_VERSION:
111             break;
112         case WapConstants.HEADER_ETAG:
113             break;
114         case WapConstants.HEADER_EXPECT:
115             break;
116         case WapConstants.HEADER_EXPIRES:
117             break;
118         case WapConstants.HEADER_FROM:
119             break;
120         case WapConstants.HEADER_HOST:
121             break;
122         case WapConstants.HEADER_IF_MATCH:
123             break;
124         case WapConstants.HEADER_IF_MODIFIED_SINCE:
125             break;
126         case WapConstants.HEADER_IF_NONE_MATCH:
127             break;
128         case WapConstants.HEADER_IF_RANGE:
129             break;
130         case WapConstants.HEADER_IF_UNMODIFIED_SINCE:
131             break;
132         case WapConstants.HEADER_LAST_MODIFIED:
133             break;
134         case WapConstants.HEADER_LOCATION:
135             break;
136         case WapConstants.HEADER_MAX_FORWARDS:
137             break;
138         case WapConstants.HEADER_PRAGMA:
139             break;
140         case WapConstants.HEADER_PROFILE:
141             break;
142         case WapConstants.HEADER_PROFILE_DIFF:
143             break;
144         case WapConstants.HEADER_PROFILE_WARNING:
145             break;
146         case WapConstants.HEADER_PROXY_AUTHENTICATE:
147             break;
148         case WapConstants.HEADER_PROXY_AUTHORIZATION:
149             break;
150         case WapConstants.HEADER_PUBLIC:
151             break;
152         case WapConstants.HEADER_PUSH_FLAG:
153             break;
154         case WapConstants.HEADER_RANGE:
155             break;
156         case WapConstants.HEADER_REFERER:
157             break;
158         case WapConstants.HEADER_RETRY_AFTER:
159             break;
160         case WapConstants.HEADER_SERVER:
161             break;
162         case WapConstants.HEADER_SET_COOKIE:
163             break;
164         case WapConstants.HEADER_TE:
165             break;
166         case WapConstants.HEADER_TRAILER:
167             break;
168         case WapConstants.HEADER_TRANSFER_ENCODING:
169             break;
170         case WapConstants.HEADER_UPGRADE:
171             break;
172         case WapConstants.HEADER_USER_AGENT:
173             break;
174         case WapConstants.HEADER_VARY:
175             break;
176         case WapConstants.HEADER_VIA:
177             break;
178         case WapConstants.HEADER_WARNING:
179             break;
180         case WapConstants.HEADER_WWW_AUTHENTICATE:
181             break;
182         case WapConstants.HEADER_X_WAP_APPLICATION_ID:
183             writeHeaderXWapApplicationId(wspEncodingVersion, theOs, theHeader.getValue());
184             break;
185         case WapConstants.HEADER_X_WAP_CONTENT_URI:
186             break;
187         case WapConstants.HEADER_X_WAP_INITIATOR_URI:
188             break;
189         case WapConstants.HEADER_X_WAP_SECURITY:
190             break;
191         case WapConstants.HEADER_X_WAP_TOD:
192             break;
193 
194         default:
195             // Custom header
196             writeCustomHeader(theOs, theHeader.getName(), theHeader.getValue());
197             break;
198         }
199     }
200     
201     public static void writeCustomHeader(OutputStream theOs, String name, String value) throws IOException
202     {
203         WspUtil.writeTokenText(theOs, name);
204         WspUtil.writeTextString(theOs, value);
205     }
206     
207     /***
208      * Writes a wsp encoded content-id header as specified in
209      * WAP-230-WSP-20010705-a.pdf.
210      * 
211      * Content-ID is introduced in encoding version 1.3.
212      */
213     public static void writeHeaderContentID(byte wspEncodingVersion, OutputStream theOs, String theContentId) throws IOException
214     {
215         int headerId = WspUtil.getWellKnownHeaderId(wspEncodingVersion, WapConstants.HEADER_CONTENT_ID);
216         if (headerId != -1)
217         {
218             WspUtil.writeShortInteger(theOs, headerId);
219             WspUtil.writeQuotedString(theOs, theContentId);
220         }
221         else
222         {
223             WspHeaderEncoder.writeCustomHeader(theOs, "Content-ID", theContentId);
224         }
225     }
226     
227     /***
228      * Writes a wsp encoded content-location header as specified in
229      * WAP-230-WSP-20010705-a.pdf.
230      */
231     public static void writeHeaderContentLocation(byte wspEncodingVersion, OutputStream theOs, String theContentLocation) throws IOException
232     {
233         int headerId = WspUtil.getWellKnownHeaderId(wspEncodingVersion, WapConstants.HEADER_CONTENT_LOCATION);        
234         WspUtil.writeShortInteger(theOs, headerId);
235         WspUtil.writeTextString(theOs, theContentLocation);
236     }
237 
238     public static void writeHeaderContentType(byte wspEncodingVersion, OutputStream theOs, String theContentType) throws IOException
239     {
240         int headerId = WspUtil.getWellKnownHeaderId(wspEncodingVersion, WapConstants.HEADER_CONTENT_TYPE);        
241         WspUtil.writeShortInteger(theOs, headerId);
242         WspUtil.writeContentType(wspEncodingVersion, theOs, theContentType);
243     }
244 
245     public static void writeHeaderContentType(byte wspEncodingVersion, OutputStream theOs, MimeHeader theContentType) throws IOException
246     {
247         int headerId = WspUtil.getWellKnownHeaderId(wspEncodingVersion, WapConstants.HEADER_CONTENT_TYPE);        
248         WspUtil.writeShortInteger(theOs, headerId);
249         WspUtil.writeContentType(wspEncodingVersion, theOs, theContentType);
250     }
251     
252     /***
253      * Writes a wsp encoded X-Wap-Application-Id header as specified in
254      * WAP-230-WSP-20010705-a.pdf.
255      * 
256      * X-Wap-Application-Id is introduced in encoding version 1.2.
257      */
258     public static void writeHeaderXWapApplicationId(byte wspEncodingVersion, OutputStream theOs, String theAppId) throws IOException
259     {
260         int wellKnownAppId = WspUtil.getWellKnownPushAppId(theAppId.toLowerCase());
261         
262         int headerId = WspUtil.getWellKnownHeaderId(wspEncodingVersion, WapConstants.HEADER_X_WAP_APPLICATION_ID);        
263         if (headerId != -1)
264         {
265             WspUtil.writeShortInteger(theOs, headerId);
266             if (wellKnownAppId == -1)
267             {
268                 WspUtil.writeTextString(theOs, theAppId);
269             }
270             else
271             {
272                 WspUtil.writeInteger(theOs, wellKnownAppId);
273             }
274         }
275         else
276         {
277             writeCustomHeader(theOs, "X-Wap-Application-Id", theAppId);
278         }
279     }
280 }