001/**
002 * Copyright 2005-2016 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.edl.impl.components;
017
018import org.apache.commons.lang.StringUtils;
019import org.apache.commons.logging.Log;
020import org.apache.commons.logging.LogFactory;
021import org.kuali.rice.edl.impl.EDLContext;
022import org.kuali.rice.edl.impl.EDLModelComponent;
023import org.kuali.rice.edl.impl.EDLXmlUtils;
024import org.kuali.rice.edl.impl.RequestParser;
025import org.w3c.dom.Document;
026import org.w3c.dom.Element;
027
028/**
029 * Handles setting the request browser type in the EDL XML.
030 *
031 * @author Kuali Rice Team (kuali-rice@googlegroups.com)
032 *
033 */
034public class BrowserHandler implements EDLModelComponent {
035
036        private static final Log LOG = LogFactory.getLog(BrowserHandler.class);
037        
038        public static final String BROWSER_EL = "requestBrowser";
039        
040        public void updateDOM(Document dom, Element configElement, EDLContext edlContext) {
041            String userAgent = browserString(edlContext.getRequestParser());
042            setBrowser(dom, uaConvert(userAgent));
043        }
044        
045        /**
046         * get user-agent header
047         */
048        private String browserString(RequestParser requestParser) {
049            return requestParser.getRequest().getHeader("User-Agent");
050        }
051
052        private void setBrowser(Document dom, String bStr) {
053            if (!StringUtils.isBlank(bStr)) {
054                Element currentPageElement = EDLXmlUtils.getOrCreateChildElement(dom.getDocumentElement(), BROWSER_EL, true);
055                currentPageElement.appendChild(dom.createTextNode(bStr));
056                if (LOG.isDebugEnabled()) {
057                        LOG.debug("Appended" + bStr + " to XML field " + currentPageElement.getNodeName());
058                }
059            }
060        }
061        
062        private String uaConvert(String userAgent){
063            String res = userAgent;
064            int ie = userAgent.indexOf("MSIE");
065            int ff = userAgent.indexOf("Firefox/");
066            int saf = userAgent.indexOf("Safari/");
067            int chr = userAgent.indexOf("Chrome/");
068            if(ie>0){
069                res = "InternetExplorer";
070            }else if(ff > 0){
071                res = "Firefox";
072            }else if(saf > 0 && chr < 0){
073                res = "Safari";
074            }else if(saf > 0 && chr > 0){
075            res = "Chrome";
076            }else{
077                res = "Other";
078            }       
079            return res;
080        }
081        
082
083            
084
085}