Spring Mobile是如何判断访问设备的类型的
Spring最近换域名了,去转转,发现了一个有意思的项目:spring mobile。
http://projects.spring.io/spring-mobile/
这个项目有很多实用的功能,如识别访问我们网站的设备是什么类型的(手机,平板,PC),据域名或者url来切换不同访问内容,据不同的访问设备转到不同的view中。
识别访问设备类型的核心代码在org.springframework.mobile.device.LiteDeviceResolver 类中,这个类实际上根据http请求头部的User-Agent的内容来判断设备到底是哪种类型的。
可以看到,这个类实际上是很简单的。不过字符串的匹配算法可能可以优化下。
- public class LiteDeviceResolver implements DeviceResolver {
- private final List<String> mobileUserAgentPrefixes = new ArrayList<String>();
- private final List<String> mobileUserAgentKeywords = new ArrayList<String>();
- private final List<String> tabletUserAgentKeywords = new ArrayList<String>();
- private final List<String> normalUserAgentKeywords = new ArrayList<String>();
- public LiteDeviceResolver() {
- init();
- }
- public LiteDeviceResolver(List<String> normalUserAgentKeywords) {
- init();
- this.normalUserAgentKeywords.addAll(normalUserAgentKeywords);
- }
- public Device resolveDevice(HttpServletRequest request) {
- String userAgent = request.getHeader("User-Agent");
- // UserAgent keyword detection of Normal devices
- if (userAgent != null) {
- userAgent = userAgent.toLowerCase();
- for (String keyword : normalUserAgentKeywords) {
- if (userAgent.contains(keyword)) {
- return resolveFallback(request);
- }
- }
- }
- // UAProf detection
- if (request.getHeader("x-wap-profile") != null || request.getHeader("Profile") != null) {
- return LiteDevice.MOBILE_INSTANCE;
- }
- // User-Agent prefix detection
- if (userAgent != null && userAgent.length() >= 4) {
- String prefix = userAgent.substring(0, 4).toLowerCase();
- if (mobileUserAgentPrefixes.contains(prefix)) {
- return LiteDevice.MOBILE_INSTANCE;
- }
- }
- // Accept-header based detection
- String accept = request.getHeader("Accept");
- if (accept != null && accept.contains("wap")) {
- return LiteDevice.MOBILE_INSTANCE;
- }
- // UserAgent keyword detection for Mobile and Tablet devices
- if (userAgent != null) {
- userAgent = userAgent.toLowerCase();
- // Android special case
- if (userAgent.contains("android") && !userAgent.contains("mobile")) {
- return LiteDevice.TABLET_INSTANCE;
- }
- // Kindle Fire special case
- if (userAgent.contains("silk") && !userAgent.contains("mobile")) {
- return LiteDevice.TABLET_INSTANCE;
- }
- for (String keyword : tabletUserAgentKeywords) {
- if (userAgent.contains(keyword)) {
- return LiteDevice.TABLET_INSTANCE;
- }
- }
- for (String keyword : mobileUserAgentKeywords) {
- if (userAgent.contains(keyword)) {
- return LiteDevice.MOBILE_INSTANCE;
- }
- }
- }
- // OperaMini special case
- @SuppressWarnings("rawtypes")
- Enumeration headers = request.getHeaderNames();
- while (headers.hasMoreElements()) {
- String header = (String) headers.nextElement();
- if (header.contains("OperaMini")) {
- return LiteDevice.MOBILE_INSTANCE;
- }
- }
- return resolveFallback(request);
- }
- // subclassing hooks
- /**
- * List of user agent prefixes that identify mobile devices.
- * Used primarily to match by operator or handset manufacturer.
- */
- protected List<String> getMobileUserAgentPrefixes() {
- return mobileUserAgentPrefixes;
- }
- /**
- * List of user agent keywords that identify mobile devices.
- * Used primarily to match by mobile platform or operating system.
- */
- protected List<String> getMobileUserAgentKeywords() {
- return mobileUserAgentKeywords;
- }
- /**
- * List of user agent keywords that identify tablet devices.
- * Used primarily to match by tablet platform or operating system.
- */
- protected List<String> getTabletUserAgentKeywords() {
- return tabletUserAgentKeywords;
- }
- /**
- * List of user agent keywords that identify normal devices.
- * Any items in this list take precedence over the mobile and
- * tablet user agent keywords, effectively overriding those.
- */
- protected List<String> getNormalUserAgentKeywords() {
- return normalUserAgentKeywords;
- }
- /**
- * Initialize this device resolver implementation.
- * Registers the known set of device signature strings.
- * Subclasses may override to register additional strings.
- */
- protected void init() {
- getMobileUserAgentPrefixes().addAll(Arrays.asList(KNOWN_MOBILE_USER_AGENT_PREFIXES));
- getMobileUserAgentKeywords().addAll(Arrays.asList(KNOWN_MOBILE_USER_AGENT_KEYWORDS));
- getTabletUserAgentKeywords().addAll(Arrays.asList(KNOWN_TABLET_USER_AGENT_KEYWORDS));
- }
- /**
- * Fallback called if no mobile device is matched by this resolver.
- * The default implementation of this method returns a "normal" {@link Device} that is neither mobile or a tablet.
- * Subclasses may override to try additional mobile or tablet device matching before falling back to a "normal" device.
- */
- protected Device resolveFallback(HttpServletRequest request) {
- return LiteDevice.NORMAL_INSTANCE;
- }
- // internal helpers
- private static final String[] KNOWN_MOBILE_USER_AGENT_PREFIXES = new String[] { "w3c ", "w3c-", "acs-", "alav",
- "alca", "amoi", "audi", "avan", "benq", "bird", "blac", "blaz", "brew", "cell", "cldc", "cmd-", "dang",
- "doco", "eric", "hipt", "htc_", "inno", "ipaq", "ipod", "jigs", "kddi", "keji", "leno", "lg-c", "lg-d",
- "lg-g", "lge-", "lg/u", "maui", "maxo", "midp", "mits", "mmef", "mobi", "mot-", "moto", "mwbp", "nec-",
- "newt", "noki", "palm", "pana", "pant", "phil", "play", "port", "prox", "qwap", "sage", "sams", "sany",
- "sch-", "sec-", "send", "seri", "sgh-", "shar", "sie-", "siem", "smal", "smar", "sony", "sph-", "symb",
- "t-mo", "teli", "tim-", "tosh", "tsm-", "upg1", "upsi", "vk-v", "voda", "wap-", "wapa", "wapi", "wapp",
- "wapr", "webc", "winw", "winw", "xda ", "xda-" };
- private static final String[] KNOWN_MOBILE_USER_AGENT_KEYWORDS = new String[] { "blackberry", "webos", "ipod",
- "lge vx", "midp", "maemo", "mmp", "mobile", "netfront", "hiptop", "nintendo DS", "novarra", "openweb",
- "opera mobi", "opera mini", "palm", "psp", "phone", "smartphone", "symbian", "up.browser", "up.link",
- "wap", "windows ce" };
- private static final String[] KNOWN_TABLET_USER_AGENT_KEYWORDS = new String[] { "ipad", "playbook", "hp-tablet",
- "kindle" };
- }
Spring Mobile是如何判断访问设备的类型的的更多相关文章
- Java判断访问设备为手机、微信、PC工具类
package com.lwj.util; import javax.servlet.http.HttpServletRequest; /** * 判断访问设备为PC或者手机--工具类 * * @de ...
- JS判断访问设备、客户端操作系统类型
先给出一个实例:判断windows.linux.android 复制以下代码另存为html文件即可. <html> <head> <title>判断操作系统< ...
- [转]JS判断访问设备、客户端操作系统类型
本文转自:http://www.cnblogs.com/duanguyuan/p/3534470.html 先给出一个实例:判断windows.linux.android 复制以下代码另存为html文 ...
- JS判断访问设备(userAgent)加载不同页面 JS判断客户端操作系统类型(platform)
//平台.设备和操作系统 var system ={ win : false, mac : false, xll : false }; //检测平台 var p = navigator.platfor ...
- PHP和js判断访问设备是否是微信浏览器实例
PHP和js判断访问设备是否是微信浏览器实例,代码非常精简,适合新手学习. js判断是否是微信浏览器: 1 function is_weixin() { 2 var ua = window.navig ...
- 判断访问浏览器客户端类型(pc,mac,ipad,iphone,android)
<script type="text/javascript"> //平台.设备和操作系统 var system = { win: false, mac: false, ...
- JS判断访问设备是移动设备还是pc
<scripttype="text/javascript"> function browserRedirect() { var sUserAgent= navigato ...
- 如何判断 ios设备的类型(iphone,ipod,ipad)
功能函数: -(bool)checkDevice:(NSString*)name { NSString* deviceType = [UIDevice currentDevice].model; NS ...
- Spring Mobile——探测客户端设备和系统
Spring Mobile--探测客户端设备和系统 今天闲来无事,浏览Spring的官方网站,发现了Spring Mobile项目,之前也看到过,还以为是针对手机端的项目,并没有细看.今天仔细看了一下 ...
随机推荐
- POJ 1155 - TELE 树型DP(泛化背包转移)..
dp[x][y]代表以x为根的子树..连接了y个终端用户(叶子)..所能获得的最大收益... dp[x][ ]可以看成当根为x时..有个背包空间为0~m...每个空间上记录了到到达这个空间的最大收益. ...
- for循环语句之棋盘放粮食、百鸡百钱、纸张的折叠问题
1.棋盘放粮食 ; ; i < ; i++) { ; ; j <= i; j++) { x = x * ; } lszl = lszl + x; } double zl = lszl * ...
- [HDU 2553]--N皇后问题(回溯)/N皇后问题的分析
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2553 N皇后问题 Time Limit: 2000/1000 MS (Java/Others) ...
- [Swust OJ 1091]--土豪我们做朋友吧(并查集,最值维护)
题目链接:http://acm.swust.edu.cn/problem/1091/ Time limit(ms): 1000 Memory limit(kb): 32768 人都有缺钱的时候,缺 ...
- 10994 - Simple Addition(规律)
Problem E Simple Addition Input: Standard Input Output: Standard Output Let’s define a simple recurs ...
- Ural 1319 - Hotel
You programmers are lucky! You don't have to deal with these terrible people – designers… This st ...
- PHP urlencode()和rawurlencode()使用和区别
string urlencode ( string $str ) 此函数便于将字符串编码并将其用于 URL 的请求部分,同时它还便于将变量传递给下一页. 返回值 返回字符串,此字符串中除了 -_. 之 ...
- svn笔记2
Examining History Your Subversion repository is like a time machine. It keeps a record of every chan ...
- 基于visual Studio2013解决C语言竞赛题之0413同构数
题目 解决代码及点评 该题目与水仙花数类似,只是条件不同,循环还是一样的 /***************************************************** ...
- 查看死锁原因 /data/anr/traces.txt
Android ANR这个错误大家并不陌生,但是从Android 2.2开始出错的ANR信息会自动上传给Google进行系统分析改进,当然了你的应用ANR错误其实保存在一个文件中,在/data/anr ...