Coverage Report - org.kuali.rice.edl.impl.components.BrowserHandler
 
Classes in this File Line Coverage Branch Coverage Complexity
BrowserHandler
0%
0/27
0%
0/16
3
 
 1  
 /**
 2  
  * Copyright 2005-2011 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  0
 public class BrowserHandler implements EDLModelComponent {
 35  
 
 36  0
         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  0
             String userAgent = browserString(edlContext.getRequestParser());
 42  0
             setBrowser(dom, uaConvert(userAgent));
 43  0
         }
 44  
         
 45  
         /**
 46  
          * get user-agent header
 47  
          */
 48  
         private String browserString(RequestParser requestParser) {
 49  0
             return requestParser.getRequest().getHeader("User-Agent");
 50  
         }
 51  
 
 52  
         private void setBrowser(Document dom, String bStr) {
 53  0
             if (!StringUtils.isBlank(bStr)) {
 54  0
                     Element currentPageElement = EDLXmlUtils.getOrCreateChildElement(dom.getDocumentElement(), BROWSER_EL, true);
 55  0
                     currentPageElement.appendChild(dom.createTextNode(bStr));
 56  0
                     if (LOG.isDebugEnabled()) {
 57  0
                             LOG.debug("Appended" + bStr + " to XML field " + currentPageElement.getNodeName());
 58  
                     }
 59  
             }
 60  0
         }
 61  
         
 62  
         private String uaConvert(String userAgent){
 63  0
             String res = userAgent;
 64  0
             int ie = userAgent.indexOf("MSIE");
 65  0
             int ff = userAgent.indexOf("Firefox/");
 66  0
             int saf = userAgent.indexOf("Safari/");
 67  0
             int chr = userAgent.indexOf("Chrome/");
 68  0
             if(ie>0){
 69  0
                 res = "InternetExplorer";
 70  0
             }else if(ff > 0){
 71  0
                 res = "Firefox";
 72  0
             }else if(saf > 0 && chr < 0){
 73  0
                 res = "Safari";
 74  0
             }else if(saf > 0 && chr > 0){
 75  0
             res = "Chrome";
 76  
             }else{
 77  0
                 res = "Other";
 78  
             }            
 79  0
             return res;
 80  
         }
 81  
         
 82  
 
 83  
             
 84  
 
 85  
 }