001/** 002 * Copyright (c) 2015-2022, Michael Yang 杨福海 (fuhai999@gmail.com). 003 * <p> 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 * <p> 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * <p> 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 io.jboot.utils; 017 018import com.jfinal.core.Controller; 019import io.jboot.web.controller.JbootControllerContext; 020 021import javax.servlet.http.HttpServletRequest; 022 023public class RequestUtil { 024 025 static String[] mobileAgents = {"iphone", "android", "phone", "mobile", "wap", "netfront", "java", "opera mobi", 026 "opera mini", "ucweb", "windows ce", "symbian", "series", "webos", "sony", "blackberry", "dopod", "nokia", 027 "samsung", "palmsource", "xda", "pieplus", "meizu", "midp", "cldc", "motorola", "foma", "docomo", 028 "up.browser", "up.link", "blazer", "helio", "hosin", "huawei", "novarra", "coolpad", "webos", "techfaith", 029 "palmsource", "alcatel", "amoi", "ktouch", "nexian", "ericsson", "philips", "sagem", "wellcom", "bunjalloo", 030 "maui", "smartphone", "iemobile", "spice", "bird", "zte-", "longcos", "pantech", "gionee", "portalmmm", 031 "jig browser", "hiptop", "benq", "haier", "^lct", "320x320", "240x320", "176x220", "w3c ", "acs-", "alav", 032 "alca", "amoi", "audi", "avan", "benq", "bird", "blac", "blaz", "brew", "cell", "cldc", "cmd-", "dang", 033 "doco", "eric", "hipt", "inno", "ipaq", "java", "jigs", "kddi", "keji", "leno", "lg-c", "lg-d", "lg-g", 034 "lge-", "maui", "maxo", "midp", "mits", "mmef", "mobi", "mot-", "moto", "mwbp", "nec-", "newt", "noki", 035 "oper", "palm", "pana", "pant", "phil", "play", "port", "prox", "qwap", "sage", "sams", "sany", "sch-", 036 "sec-", "send", "seri", "sgh-", "shar", "sie-", "siem", "smal", "smar", "sony", "sph-", "symb", "t-mo", 037 "teli", "tim-", "tsm-", "upg1", "upsi", "vk-v", "voda", "wap-", "wapa", "wapi", "wapp", "wapr", "webc", 038 "winw", "winw", "xda", "xda-", "googlebot-mobile"}; 039 040 public static boolean isAjaxRequest(HttpServletRequest request) { 041 String header = request.getHeader("X-Requested-With"); 042 return "XMLHttpRequest".equalsIgnoreCase(header); 043 } 044 045 public static boolean isJsonContentType(HttpServletRequest request) { 046 String contentType = request.getContentType(); 047 return contentType != null && contentType.toLowerCase().contains("application/json"); 048 } 049 050 051 public static boolean isMultipartRequest(HttpServletRequest request) { 052 String contentType = request.getContentType(); 053 return contentType != null && contentType.toLowerCase().contains("multipart"); 054 } 055 056 /** 057 * 是否是手机浏览器 058 * 059 * @return 060 */ 061 public static boolean isMobileBrowser(HttpServletRequest request) { 062 String ua = request.getHeader("User-Agent"); 063 if (StrUtil.isNotBlank(ua)) { 064 ua = ua.toLowerCase(); 065 for (String mobileAgent : mobileAgents) { 066 if (ua.indexOf(mobileAgent) > -1) { 067 return true; 068 } 069 } 070 } 071 return false; 072 } 073 074 /** 075 * 是否是微信浏览器 076 * 077 * @return 078 */ 079 public static boolean isWechatBrowser(HttpServletRequest request) { 080 String ua = request.getHeader("User-Agent"); 081 return StrUtil.isNotBlank(ua) && ua.toLowerCase().indexOf("micromessenger") > -1; 082 } 083 084 085 /** 086 * 是否是PC版的微信浏览器 087 * 088 * @param request 089 * @return 090 */ 091 public static boolean isWechatPcBrowser(HttpServletRequest request) { 092 String ua = request.getHeader("User-Agent"); 093 return StrUtil.isNotBlank(ua) && ua.toLowerCase().indexOf("windowswechat") > -1; 094 } 095 096 /** 097 * 是否是IE浏览器 098 * 099 * @return 100 */ 101 public static boolean isIEBrowser(HttpServletRequest request) { 102 String ua = request.getHeader("User-Agent"); 103 if (StrUtil.isBlank(ua)) { 104 return false; 105 } 106 107 ua = ua.toLowerCase(); 108 if (ua.indexOf("msie") > -1) { 109 return true; 110 } 111 112 if (ua.indexOf("gecko") > -1 && ua.indexOf("rv:11") > -1) { 113 return true; 114 } 115 116 return false; 117 } 118 119 public static String getIpAddress(HttpServletRequest request) { 120 String ip = request.getHeader("X-Forwarded-For"); 121 if (StrUtil.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) { 122 ip = request.getHeader("X-Real-IP"); 123 } 124 if (StrUtil.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) { 125 ip = request.getHeader("Proxy-Client-IP"); 126 } 127 if (StrUtil.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) { 128 ip = request.getHeader("WL-Proxy-Client-IP"); 129 } 130 if (StrUtil.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) { 131 ip = request.getHeader("HTTP_CLIENT_IP"); 132 } 133 if (StrUtil.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) { 134 ip = request.getHeader("HTTP_X_FORWARDED_FOR"); 135 } 136 if (StrUtil.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) { 137 ip = request.getRemoteAddr(); 138 } 139 140 if (ip != null && ip.contains(",")) { 141 String[] ips = ip.split(","); 142 for (String strIp : ips) { 143 if (!("unknown".equalsIgnoreCase(strIp))) { 144 ip = strIp; 145 break; 146 } 147 } 148 } 149 150 if ("0:0:0:0:0:0:0:1".equals(ip)) { 151 ip = "127.0.0.1"; 152 } 153 154 return ip; 155 } 156 157 public static String getUserAgent(HttpServletRequest request) { 158 return request.getHeader("User-Agent"); 159 } 160 161 162 public static String getReferer(HttpServletRequest request) { 163 return request.getHeader("Referer"); 164 } 165 166 167 public static String getBaseUrl(HttpServletRequest request) { 168 int port = request.getServerPort(); 169 return request.getScheme() + "://" + 170 request.getServerName() + 171 (port == 80 || port == 443 ? "" : ":" + port) + 172 request.getContextPath(); 173 } 174 175 176 public static String getBaseUrl() { 177 Controller controller = JbootControllerContext.get(); 178 return controller == null ? null : getBaseUrl(controller.getRequest()); 179 } 180 181 182 public static String getCurrentUrl() { 183 Controller controller = JbootControllerContext.get(); 184 return controller == null ? null : getCurrentUrl(controller.getRequest()); 185 } 186 187 188 public static String getCurrentUrl(HttpServletRequest request) { 189 String queryString = request.getQueryString(); 190 String url = getBaseUrl(request) + request.getServletPath(); 191 if (StrUtil.isNotBlank(queryString)) { 192 url = url.concat("?").concat(queryString); 193 } 194 return url; 195 } 196 197 198 public static String getCurrentEncodeUrl() { 199 Controller controller = JbootControllerContext.get(); 200 return controller == null ? null : getCurrentEncodeUrl(controller.getRequest()); 201 } 202 203 204 public static String getCurrentEncodeUrl(HttpServletRequest request) { 205 return StrUtil.urlEncode(getCurrentUrl(request)); 206 } 207 208 209}