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