get Bot Name from user agent - Java Network

Java examples for Network:User Agent

Description

get Bot Name from user agent

Demo Code


import javax.servlet.http.HttpServletRequest;

public class Main{

    /*from  w w w  . j  ava 2s. c o  m*/
    public static String[] getBotName(String userAgent) {
        if (userAgent == null) {
            return null;
        }
        userAgent = userAgent.toLowerCase();
        int pos = 0;
        String res = null;
        if ((pos = userAgent.indexOf("baiduspider")) > -1) {
            res = "Baidu";
            pos += -1;
        } else if ((pos = userAgent.indexOf("sosospider")) > -1) {
            res = "Soso";
            pos += -1;
        } else if ((pos = userAgent.indexOf("sogou web spider/")) > -1) {
            res = "Sogou";
            pos += 17;
        } else if ((pos = userAgent.indexOf("yodaobot")) > -1) {
            res = "Yodao";
            pos += -1;
        } else if ((pos = userAgent.indexOf("yahoo!+slurp")) > -1) {
            res = "Yahoo";
            pos += -1;
        } else if ((pos = userAgent.indexOf("msnbot/")) > -1) {
            res = "MSNBot";
            pos += 7;
        } else if ((pos = userAgent.indexOf("googlebot/")) > -1) {
            res = "Google";
            pos += 10;
        } else if ((pos = userAgent.indexOf("webcrawler/")) > -1) {
            res = "WebCrawler";
            pos += 11;
        } else if ((pos = userAgent.indexOf("inktomi")) > -1) {
            res = "Inktomi";
            pos = -1;
        } else if ((pos = userAgent.indexOf("teoma")) > -1) {
            res = "Teoma";
            pos = -1;
        } else if ((pos = userAgent.indexOf("alexa.com")) > -1) {
            res = "Alexa";
            pos = -1;
        } else if ((pos = userAgent.indexOf("crawler")) > -1) {
            res = "Crawler";
            pos = -1;
        } else if ((pos = userAgent.indexOf("bot")) > -1) {
            res = "Bot";
            pos = -1;
        }
        if (res == null) {
            return null;
        }
        return getArray(res, res, res + getVersionNumber(userAgent, pos));
    }
    private static String[] getArray(String a, String b, String c) {
        String[] res = new String[3];
        res[0] = a;
        res[1] = b;
        res[2] = c;
        return res;
    }
    private static String getVersionNumber(String userAgent, int position) {
        if (position < 0) {
            return "";
        }
        StringBuffer res = new StringBuffer();
        int status = 0;

        while (position < userAgent.length()) {
            char c = userAgent.charAt(position);
            switch (status) {
            case 0:
                // <SPAN class="codecomment"> No valid digits encountered
                // yet</span>
                if (c == ' ' || c == '/') {
                    break;
                }
                if (c == ';' || c == ')') {
                    return "";
                }
                status = 1;
                // fall through
            case 1:
                // <SPAN class="codecomment"> Version number in progress</span>
                if (c == ';' || c == '/' || c == ')' || c == '('
                        || c == '[') {
                    return res.toString().trim();
                }
                if (c == ' ') {
                    status = 2;
                }
                res.append(c);
                break;
            case 2:
                // <SPAN class="codecomment"> Space encountered - Might need to
                // end the parsing</span>
                if ((Character.isLetter(c) && Character.isLowerCase(c))
                        || Character.isDigit(c)) {
                    res.append(c);
                    status = 1;
                } else {
                    return res.toString().trim();
                }
                break;
            }
            position++;
        }
        return res.toString().trim();
    }
}

Related Tutorials