View Javadoc

1   /**
2    * Copyright 2005-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Handles setting the request browser type in the EDL XML.
30   *
31   * @author Kuali Rice Team (kuali-rice@googlegroups.com)
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  	 * get user-agent header
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  }