001/*
002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0
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 *
016 * Contributors:
017 *     Thomas Roger <troger@nuxeo.com>
018 */
019
020package org.nuxeo.common.utils;
021
022import java.util.regex.Pattern;
023
024/**
025 * Helper class to detect Html5 Dnd compliant browsers based on the User Agent string
026 *
027 * @author Tiry (tdelprat@nuxeo.com)
028 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a>
029 */
030public class UserAgentMatcher {
031
032    private static final Pattern UA_FIREFOX_3 = Pattern.compile("^[Mm]ozilla.*[Ff]irefox(/|\\s)?(3\\.[6789].*)");
033
034    private static final Pattern UA_FIREFOX_FROM_4 = Pattern.compile("^[Mm]ozilla.*[Ff]irefox(/|\\s)?(([456789].*)|([1-9][0123456789].*))");
035
036    private static final Pattern UA_SAFARI_FROM_5 = Pattern.compile("^Mozilla.*AppleWebKit.*Version/[5-9].*");
037
038    private static final Pattern UA_CHROME = Pattern.compile("^Mozilla.*AppleWebKit.*Chrom(e|ium)/([1-9][0123456789].([0-9.])*)(?: Safari/([0-9.])*)?");
039
040    private static final Pattern UA_MSIE_67 = Pattern.compile("^Mozilla/4.0 \\(compatible; MSIE [67].[0-9]((?!Trident).)*$");
041
042    private static final Pattern UA_MSIE_FROM_10 = Pattern.compile("^Mozilla.*[Tt]rident/[6-9]\\..*");
043
044    private static final Pattern UA_MSEDGE = Pattern.compile("^Mozilla.*Edge/.*");
045
046    private UserAgentMatcher() {
047        // Helper class
048    }
049
050    public static boolean isFirefox3(String UA) {
051        return UA_FIREFOX_3.matcher(UA).matches();
052    }
053
054    public static boolean isFirefox4OrMore(String UA) {
055        return UA_FIREFOX_FROM_4.matcher(UA).matches();
056    }
057
058    public static boolean isSafari5(String UA) {
059        return UA_SAFARI_FROM_5.matcher(UA).matches();
060    }
061
062    public static boolean isChrome(String UA) {
063        return UA_CHROME.matcher(UA).matches();
064    }
065
066    public static boolean html5DndIsSupported(String UA) {
067        return isFirefox3(UA) || isFirefox4OrMore(UA) || isSafari5(UA) || isChrome(UA) || isMSIE10OrMore(UA) || isMSEdge(UA);
068    }
069
070    public static boolean isMSIE6or7(String UA) {
071        return UA_MSIE_67.matcher(UA).matches();
072    }
073
074    /**
075     * @since 5.9.5
076     */
077    public static boolean isMSIE10OrMore(String UA) {
078        return UA_MSIE_FROM_10.matcher(UA).matches();
079    }
080
081    /**
082     * @since 7.4
083     */
084    public static boolean isMSEdge(String UA) {
085        return UA_MSEDGE.matcher(UA).matches();
086    }
087
088    public static boolean isHistoryPushStateSupported(String UA) {
089        return isFirefox4OrMore(UA) || isSafari5(UA) || isChrome(UA) || isMSIE10OrMore(UA);
090    }
091}