1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.kuali.rice.edl.impl.components; |
18 | |
|
19 | |
import org.apache.commons.lang.StringUtils; |
20 | |
import org.apache.commons.logging.Log; |
21 | |
import org.apache.commons.logging.LogFactory; |
22 | |
import org.kuali.rice.edl.impl.EDLContext; |
23 | |
import org.kuali.rice.edl.impl.EDLModelComponent; |
24 | |
import org.kuali.rice.edl.impl.EDLXmlUtils; |
25 | |
import org.kuali.rice.edl.impl.RequestParser; |
26 | |
import org.w3c.dom.Document; |
27 | |
import org.w3c.dom.Element; |
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | 0 | public class BrowserHandler implements EDLModelComponent { |
36 | |
|
37 | 0 | private static final Log LOG = LogFactory.getLog(BrowserHandler.class); |
38 | |
|
39 | |
public static final String BROWSER_EL = "requestBrowser"; |
40 | |
|
41 | |
public void updateDOM(Document dom, Element configElement, EDLContext edlContext) { |
42 | 0 | String userAgent = browserString(edlContext.getRequestParser()); |
43 | 0 | setBrowser(dom, uaConvert(userAgent)); |
44 | 0 | } |
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
private String browserString(RequestParser requestParser) { |
50 | 0 | return requestParser.getRequest().getHeader("User-Agent"); |
51 | |
} |
52 | |
|
53 | |
private void setBrowser(Document dom, String bStr) { |
54 | 0 | if (!StringUtils.isBlank(bStr)) { |
55 | 0 | Element currentPageElement = EDLXmlUtils.getOrCreateChildElement(dom.getDocumentElement(), BROWSER_EL, true); |
56 | 0 | currentPageElement.appendChild(dom.createTextNode(bStr)); |
57 | 0 | if (LOG.isDebugEnabled()) { |
58 | 0 | LOG.debug("Appended" + bStr + " to XML field " + currentPageElement.getNodeName()); |
59 | |
} |
60 | |
} |
61 | 0 | } |
62 | |
|
63 | |
private String uaConvert(String userAgent){ |
64 | 0 | String res = userAgent; |
65 | 0 | int ie = userAgent.indexOf("MSIE"); |
66 | 0 | int ff = userAgent.indexOf("Firefox/"); |
67 | 0 | int saf = userAgent.indexOf("Safari/"); |
68 | 0 | int chr = userAgent.indexOf("Chrome/"); |
69 | 0 | if(ie>0){ |
70 | 0 | res = "InternetExplorer"; |
71 | 0 | }else if(ff > 0){ |
72 | 0 | res = "Firefox"; |
73 | 0 | }else if(saf > 0 && chr < 0){ |
74 | 0 | res = "Safari"; |
75 | 0 | }else if(saf > 0 && chr > 0){ |
76 | 0 | res = "Chrome"; |
77 | |
}else{ |
78 | 0 | res = "Other"; |
79 | |
} |
80 | 0 | return res; |
81 | |
} |
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
} |