001: /*
002: * Copyright (c) 1999-2001 Lutris Technologies, Inc. All Rights
003: * Reserved.
004: *
005: * This source code file is distributed by Lutris Technologies, Inc. for
006: * use only by licensed users of product(s) that include this source
007: * file. Use of this source file or the software that uses it is covered
008: * by the terms and conditions of the Lutris Enhydra Development License
009: * Agreement included with this product.
010: *
011: * This Software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF
012: * ANY KIND, either express or implied. See the License for the specific terms
013: * governing rights and limitations under the License.
014: *
015: * Contributor(s):
016: *
017: * $Id: DeviceUtils.java,v 1.1 2006-09-11 12:29:11 sinisa Exp $
018: */
019:
020: package com.lutris.airsent.presentation;
021:
022: import com.lutris.airsent.presentation.*;
023: import com.lutris.appserver.server.httpPresentation.*;
024: import javax.servlet.http.HttpServletRequest;
025: import org.enhydra.xml.xmlc.*;
026: import org.w3c.dom.*;
027:
028: /**
029: * Provides utilities for outputing and forewarding to devices.
030: *
031: *
032: *
033: */
034: public class DeviceUtils {
035: /**
036: * Method that determines which content to output based on
037: * the accept type.
038: * Current types:
039: * text/html
040: * text/xml
041: * text/vnd.wap.wml
042: *
043: *
044: */
045: public static void rerouteForContent(HttpPresentationComms comms)
046: throws AirSentPresentationException {
047: String header = null;
048: String userAgent = null;
049: String flashClient = null;
050: try {
051: flashClient = comms.request.getParameter("flash");
052: userAgent = comms.request.getHeader("User-Agent");
053: if (flashClient != null) {
054: comms.response.setEncoding("ISO8859_1");
055: throw new ClientPageRedirectException(
056: AirSentConstants.XML_PAGE);
057: } else if (userAgent != null
058: && userAgent.indexOf("DoCoMo") != -1) {
059: throw new ClientPageRedirectException(
060: AirSentConstants.CHTML_PAGE);
061: } else if (userAgent != null
062: && userAgent.indexOf("Pixo") != -1) {
063: throw new ClientPageRedirectException(
064: AirSentConstants.CHTML_PAGE);
065: } else if (userAgent != null
066: && userAgent.indexOf("RIM") != -1) {
067: throw new ClientPageRedirectException(
068: AirSentConstants.CHTML_PAGE);
069: } else if (userAgent != null
070: && userAgent.indexOf("Digital Paths") != -1) {
071: throw new ClientPageRedirectException(
072: AirSentConstants.CHTML_PAGE);
073: } else if (userAgent != null
074: && userAgent.indexOf("UP.") != -1) {
075: throw new ClientPageRedirectException(
076: AirSentConstants.WAP_PAGE);
077: } else if ((header = comms.request
078: .getHeader(AirSentConstants.ACCEPT)) == null) {
079: // Just display HTML
080: return;
081: } else if (header != null
082: && header.indexOf(AirSentConstants.HTML_CONTENT) != -1
083: || flashClient != null) {
084: return;
085: } else if (header != null
086: && header.indexOf(AirSentConstants.XML_CONTENT) != -1
087: || flashClient != null) {
088: throw new ClientPageRedirectException(
089: AirSentConstants.XML_PAGE);
090: } else if (header != null
091: && header.indexOf(AirSentConstants.WAP_CONTENT) != -1) {
092: throw new ClientPageRedirectException(
093: AirSentConstants.WAP_PAGE);
094: } else if (header != null
095: && header.indexOf(AirSentConstants.XHTML_CONTENT) != -1) {
096: } else if (header != null
097: && header.indexOf(AirSentConstants.CHTML_CONTENT) != -1) {
098: //????
099: } else {
100: if (userAgent == null) {
101: throw new ClientPageRedirectException(
102: AirSentConstants.CHTML_PAGE);
103: }
104: }
105: } catch (Exception e) {
106: throw new AirSentPresentationException(
107: "Trouble rerouting header:" + header, e);
108: }
109: }
110:
111: /**
112: * Gets the suffix of a PO name based on the
113: * accept type.
114: * @param String PO classname
115: * @return PO new full classname with suffix.
116: */
117: public static String getPageName(HttpPresentationComms comms,
118: String poName) throws AirSentPresentationException {
119: String header = null;
120: String userAgent = null;
121: String flashClient = null;
122:
123: try {
124: flashClient = comms.request.getParameter("flash");
125: userAgent = comms.request.getHeader("User-Agent");
126: if (flashClient != null) {
127: comms.response.setEncoding("ISO-8859-1");
128: return poName + "XML";
129: }
130: //
131: //DoCoMo code thanks to Ray Mercer
132: //
133: else if (userAgent != null
134: && userAgent.indexOf("DoCoMo") != -1) {
135: return poName + "CHTML";
136: } else if (userAgent != null
137: && userAgent.indexOf("Pixo") != -1) {
138: return poName + "CHTML";
139: } else if (userAgent != null
140: && userAgent.indexOf("RIM") != -1) {
141: return poName + "HTML";
142: } else if (userAgent != null
143: && userAgent.indexOf("UP.") != -1) {
144: return poName + "WML";
145: } else if ((header = comms.request
146: .getHeader(AirSentConstants.ACCEPT)) == null) {
147: // Can't decipher accept type.
148: return null;
149: } else if (header.indexOf(AirSentConstants.HTML_CONTENT) != -1) {
150: //Defaulting to html.
151: return poName + "HTML";
152: } else if (header.indexOf(AirSentConstants.XML_CONTENT) != -1) {
153: return poName + "XML";
154: } else if (header.indexOf(AirSentConstants.WAP_CONTENT) != -1) {
155: return poName + "WML";
156: } else if (header.indexOf(AirSentConstants.XHTML_CONTENT) != -1) {
157: return poName + "XHTML";
158: } else if (header.indexOf(AirSentConstants.CHTML_CONTENT) != -1) {
159: return poName + "CHTML";
160: } else {
161: if (userAgent == null) {
162: //Defaulting to chtml.
163: return poName + "CHTML";
164: } else {
165: //Defaulting to html.
166: return poName + "HTML";
167: }
168: }
169: } catch (Exception e) {
170: throw new AirSentPresentationException(
171: "Trouble rerouting header:" + header, e);
172: }
173: }
174:
175: /**
176: *Recursivly walk the dom and replace any url with the
177: *url + a timestamp=current time in millis.
178: **!!Hack for buggy phone caching!!
179: */
180: public static void setURLTimeStamp(
181: org.enhydra.xml.xmlc.XMLObject dom) {
182: DeviceUtils.setNodeTimeStamp(dom.getDocumentElement());
183: }
184:
185: /**
186: *Recursivly walk the DOM replacing urls.
187: **!!Hack for buggy phone caching!!
188: */
189: public static void setNodeTimeStamp(Node node) {
190: if (Node.ELEMENT_NODE == node.getNodeType()) {
191: String urlValue = null;
192: if (!(urlValue = ((Element) node).getAttribute("href"))
193: .equals("")) {
194: ((Element) node).setAttribute("href", DeviceUtils
195: .getTimeStampParam(urlValue));
196: }
197: NodeList nodeList = node.getChildNodes();
198: for (int curNode = 0; curNode < nodeList.getLength(); curNode++) {
199: DeviceUtils.setNodeTimeStamp(nodeList.item(curNode));
200: }
201: }
202: }
203:
204: /**
205: **Append timestamp to force caching
206: **!!Hack for buggy phone caching!!
207: **
208: */
209: public static String getTimeStampParam(String url) {
210: String delimiter = url.indexOf("?") != -1 ? "&" : "?";
211: return url + delimiter + "timestamp="
212: + System.currentTimeMillis();
213: }
214:
215: }
|