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