KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lutris > airsent > presentation > DeviceUtils


1 /*
2  * Copyright (c) 1999-2001 Lutris Technologies, Inc. All Rights
3  * Reserved.
4  *
5  * This source code file is distributed by Lutris Technologies, Inc. for
6  * use only by licensed users of product(s) that include this source
7  * file. Use of this source file or the software that uses it is covered
8  * by the terms and conditions of the Lutris Enhydra Development License
9  * Agreement included with this product.
10  *
11  * This Software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF
12  * ANY KIND, either express or implied. See the License for the specific terms
13  * governing rights and limitations under the License.
14  *
15  * Contributor(s):
16  *
17  * $Id: DeviceUtils.java,v 1.2 2005/01/21 13:19:09 slobodan Exp $
18  */

19
20 package com.lutris.airsent.presentation;
21
22 import com.lutris.airsent.presentation.*;
23 import com.lutris.appserver.server.httpPresentation.*;
24 import javax.servlet.http.HttpServletRequest JavaDoc;
25 import org.enhydra.xml.xmlc.*;
26 import org.w3c.dom.*;
27 /**
28  * Provides utilities for outputing and forewarding to devices.
29  *
30  *
31  *
32  */

33 public class DeviceUtils {
34     /**
35      * Method that determines which content to output based on
36      * the accept type.
37      * Current types:
38      * text/html
39      * text/xml
40      * text/vnd.wap.wml
41      *
42      *
43      */

44     public static void rerouteForContent(HttpPresentationComms comms)
45         throws AirSentPresentationException {
46     String JavaDoc header = null;
47     String JavaDoc userAgent = null;
48     String JavaDoc flashClient = null;
49     try {
50         flashClient = comms.request.getParameter("flash");
51         userAgent = comms.request.getHeader("User-Agent");
52         if (flashClient != null) {
53         comms.response.setEncoding("ISO8859_1");
54         throw new ClientPageRedirectException(AirSentConstants.XML_PAGE);
55         }
56         else if(userAgent != null && userAgent.indexOf("DoCoMo") != -1) {
57         throw new ClientPageRedirectException(AirSentConstants.CHTML_PAGE);
58         }
59         else if(userAgent != null && userAgent.indexOf("Pixo") != -1) {
60         throw new ClientPageRedirectException(AirSentConstants.CHTML_PAGE);
61         }
62         else if(userAgent != null && userAgent.indexOf("RIM") != -1) {
63         throw new ClientPageRedirectException(AirSentConstants.CHTML_PAGE);
64         }
65         else if(userAgent != null && userAgent.indexOf("Digital Paths") != -1) {
66         throw new ClientPageRedirectException(AirSentConstants.CHTML_PAGE);
67         }
68         else if(userAgent != null && userAgent.indexOf("UP.") != -1) {
69         throw new ClientPageRedirectException(AirSentConstants.WAP_PAGE);
70         }
71         else if ((header = comms.request.getHeader(AirSentConstants.ACCEPT))
72              == null) {
73         // Just display HTML
74
return;
75         }else if (header != null && header.indexOf(AirSentConstants.HTML_CONTENT) != -1 ||
76                  flashClient != null) {
77             return;
78         }
79         else if (header != null && header.indexOf(AirSentConstants.XML_CONTENT) != -1 ||
80              flashClient != null) {
81         throw new ClientPageRedirectException(AirSentConstants.XML_PAGE);
82         }
83         else if ( header != null && header.indexOf(AirSentConstants.WAP_CONTENT) != -1) {
84         throw new ClientPageRedirectException(AirSentConstants.WAP_PAGE);
85         }
86         else if (header != null && header.indexOf(AirSentConstants.XHTML_CONTENT)
87              != -1) {}
88         else if (header != null && header.indexOf(AirSentConstants.CHTML_CONTENT) != -1) {
89         //????
90
}
91         else {
92         if(userAgent == null) {
93             throw new ClientPageRedirectException(AirSentConstants.CHTML_PAGE);
94         }
95         }
96     } catch (Exception JavaDoc e) {
97         throw new AirSentPresentationException("Trouble rerouting header:"
98                            + header, e);
99     }
100     }
101     
102     /**
103      * Gets the suffix of a PO name based on the
104      * accept type.
105      * @param String PO classname
106      * @return PO new full classname with suffix.
107      */

108     public static String JavaDoc getPageName(HttpPresentationComms comms, String JavaDoc poName)
109     throws AirSentPresentationException {
110     String JavaDoc header = null;
111     String JavaDoc userAgent = null;
112     String JavaDoc flashClient = null;
113     
114     try {
115         flashClient = comms.request.getParameter("flash");
116         userAgent = comms.request.getHeader("User-Agent");
117         if (flashClient != null) {
118         comms.response.setEncoding("ISO-8859-1");
119         return poName + "XML";
120         }
121         //
122
//DoCoMo code thanks to Ray Mercer
123
//
124
else if(userAgent != null && userAgent.indexOf("DoCoMo") != -1) {
125         return poName + "CHTML";
126         } else if(userAgent != null && userAgent.indexOf("Pixo") != -1) {
127         return poName + "CHTML";
128         } else if(userAgent != null && userAgent.indexOf("RIM") != -1) {
129         return poName + "HTML";
130         } else if(userAgent != null && userAgent.indexOf("UP.") != -1) {
131         return poName + "WML";
132         } else if ((header = comms.request.getHeader(AirSentConstants.ACCEPT))
133                == null) {
134         // Can't decipher accept type.
135
return null;
136         }else if (header.indexOf(AirSentConstants.HTML_CONTENT) != -1) {
137             //Defaulting to html.
138
return poName + "HTML";
139         } else if (header.indexOf(AirSentConstants.XML_CONTENT) != -1) {
140         return poName + "XML";
141         } else if (header.indexOf(AirSentConstants.WAP_CONTENT) != -1) {
142         return poName + "WML";
143         } else if (header.indexOf(AirSentConstants.XHTML_CONTENT) != -1) {
144         return poName + "XHTML";
145         } else if (header.indexOf(AirSentConstants.CHTML_CONTENT) != -1) {
146         return poName + "CHTML";
147         }
148         else {
149         if(userAgent == null) {
150             //Defaulting to chtml.
151
return poName + "CHTML";
152         }
153         else {
154             //Defaulting to html.
155
return poName + "HTML";
156         }
157         }
158     } catch (Exception JavaDoc e) {
159         throw new AirSentPresentationException("Trouble rerouting header:"
160                            + header, e);
161     }
162     }
163     
164     /**
165      *Recursivly walk the dom and replace any url with the
166      *url + a timestamp=current time in millis.
167       **!!Hack for buggy phone caching!!
168       */

169     public static void setURLTimeStamp(org.enhydra.xml.xmlc.XMLObject dom) {
170         DeviceUtils.setNodeTimeStamp(dom.getDocumentElement());
171     }
172
173     
174     /**
175      *Recursivly walk the DOM replacing urls.
176      **!!Hack for buggy phone caching!!
177      */

178     public static void setNodeTimeStamp(Node node) {
179         if (Node.ELEMENT_NODE == node.getNodeType()){
180             String JavaDoc urlValue = null;
181             if(!(urlValue = ((Element)node).getAttribute("href")).equals("")) {
182             ((Element)node).setAttribute("href", DeviceUtils.getTimeStampParam(urlValue));
183             }
184             NodeList nodeList = node.getChildNodes();
185             for(int curNode = 0; curNode < nodeList.getLength(); curNode++){
186             DeviceUtils.setNodeTimeStamp(nodeList.item(curNode));
187             }
188         }
189     }
190     
191     /**
192      **Append timestamp to force caching
193      **!!Hack for buggy phone caching!!
194      **
195      */

196     public static String JavaDoc getTimeStampParam(String JavaDoc url) {
197         String JavaDoc delimiter = url.indexOf("?") != -1 ? "&" : "?";
198         return url + delimiter + "timestamp=" + System.currentTimeMillis();
199     }
200
201     
202     
203
204
205 }
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
Popular Tags