¶Ô±ÈÐÂÎļþ |
| | |
| | | /* |
| | | * Copyright (c) 2018-2028, DreamLu All rights reserved. |
| | | * |
| | | * Redistribution and use in source and binary forms, with or without |
| | | * modification, are permitted provided that the following conditions are met: |
| | | * |
| | | * Redistributions of source code must retain the above copyright notice, |
| | | * this list of conditions and the following disclaimer. |
| | | * Redistributions in binary form must reproduce the above copyright |
| | | * notice, this list of conditions and the following disclaimer in the |
| | | * documentation and/or other materials provided with the distribution. |
| | | * Neither the name of the dreamlu.net developer nor the names of its |
| | | * contributors may be used to endorse or promote products derived from |
| | | * this software without specific prior written permission. |
| | | * Author: DreamLu 墿¥æ¢¦ (596392912@qq.com) |
| | | */ |
| | | package org.springblade.core.tool.utils; |
| | | |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springblade.core.tool.jackson.JsonUtil; |
| | | import org.springframework.http.MediaType; |
| | | import org.springframework.lang.Nullable; |
| | | import org.springframework.util.Assert; |
| | | import org.springframework.web.bind.annotation.ResponseBody; |
| | | import org.springframework.web.context.request.RequestAttributes; |
| | | import org.springframework.web.context.request.RequestContextHolder; |
| | | import org.springframework.web.context.request.ServletRequestAttributes; |
| | | import org.springframework.web.method.HandlerMethod; |
| | | |
| | | import javax.servlet.ServletInputStream; |
| | | import javax.servlet.http.Cookie; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.io.BufferedReader; |
| | | import java.io.IOException; |
| | | import java.io.InputStreamReader; |
| | | import java.io.PrintWriter; |
| | | import java.nio.charset.StandardCharsets; |
| | | import java.util.Enumeration; |
| | | import java.util.Objects; |
| | | import java.util.function.Predicate; |
| | | |
| | | |
| | | /** |
| | | * Miscellaneous utilities for web applications. |
| | | * |
| | | * @author L.cm |
| | | */ |
| | | @Slf4j |
| | | public class WebUtil extends org.springframework.web.util.WebUtils { |
| | | |
| | | public static final String USER_AGENT_HEADER = "user-agent"; |
| | | |
| | | /** |
| | | * 夿æ¯å¦ajaxè¯·æ± |
| | | * spring ajax è¿å嫿 ResponseBody æè
RestController注解 |
| | | * |
| | | * @param handlerMethod HandlerMethod |
| | | * @return æ¯å¦ajaxè¯·æ± |
| | | */ |
| | | public static boolean isBody(HandlerMethod handlerMethod) { |
| | | ResponseBody responseBody = ClassUtil.getAnnotation(handlerMethod, ResponseBody.class); |
| | | return responseBody != null; |
| | | } |
| | | |
| | | /** |
| | | * 读åcookie |
| | | * |
| | | * @param name cookie name |
| | | * @return cookie value |
| | | */ |
| | | @Nullable |
| | | public static String getCookieVal(String name) { |
| | | HttpServletRequest request = WebUtil.getRequest(); |
| | | Assert.notNull(request, "request from RequestContextHolder is null"); |
| | | return getCookieVal(request, name); |
| | | } |
| | | |
| | | /** |
| | | * 读åcookie |
| | | * |
| | | * @param request HttpServletRequest |
| | | * @param name cookie name |
| | | * @return cookie value |
| | | */ |
| | | @Nullable |
| | | public static String getCookieVal(HttpServletRequest request, String name) { |
| | | Cookie cookie = getCookie(request, name); |
| | | return cookie != null ? cookie.getValue() : null; |
| | | } |
| | | |
| | | /** |
| | | * æ¸
é¤ æä¸ªæå®çcookie |
| | | * |
| | | * @param response HttpServletResponse |
| | | * @param key cookie key |
| | | */ |
| | | public static void removeCookie(HttpServletResponse response, String key) { |
| | | setCookie(response, key, null, 0); |
| | | } |
| | | |
| | | /** |
| | | * 设置cookie |
| | | * |
| | | * @param response HttpServletResponse |
| | | * @param name cookie name |
| | | * @param value cookie value |
| | | * @param maxAgeInSeconds maxage |
| | | */ |
| | | public static void setCookie(HttpServletResponse response, String name, @Nullable String value, int maxAgeInSeconds) { |
| | | Cookie cookie = new Cookie(name, value); |
| | | cookie.setPath(StringPool.SLASH); |
| | | cookie.setMaxAge(maxAgeInSeconds); |
| | | cookie.setHttpOnly(true); |
| | | response.addCookie(cookie); |
| | | } |
| | | |
| | | /** |
| | | * è·å HttpServletRequest |
| | | * |
| | | * @return {HttpServletRequest} |
| | | */ |
| | | public static HttpServletRequest getRequest() { |
| | | RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes(); |
| | | return (requestAttributes == null) ? null : ((ServletRequestAttributes) requestAttributes).getRequest(); |
| | | } |
| | | |
| | | /** |
| | | * è¿åjson |
| | | * |
| | | * @param response HttpServletResponse |
| | | * @param result ç»æå¯¹è±¡ |
| | | */ |
| | | public static void renderJson(HttpServletResponse response, Object result) { |
| | | renderJson(response, result, MediaType.APPLICATION_JSON_VALUE); |
| | | } |
| | | |
| | | /** |
| | | * è¿åjson |
| | | * |
| | | * @param response HttpServletResponse |
| | | * @param result ç»æå¯¹è±¡ |
| | | * @param contentType contentType |
| | | */ |
| | | public static void renderJson(HttpServletResponse response, Object result, String contentType) { |
| | | response.setCharacterEncoding("UTF-8"); |
| | | response.setContentType(contentType); |
| | | try (PrintWriter out = response.getWriter()) { |
| | | out.append(JsonUtil.toJson(result)); |
| | | } catch (IOException e) { |
| | | log.error(e.getMessage(), e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * è·åip |
| | | * |
| | | * @return {String} |
| | | */ |
| | | public static String getIP() { |
| | | return getIP(WebUtil.getRequest()); |
| | | } |
| | | |
| | | private static final String[] IP_HEADER_NAMES = new String[]{ |
| | | "x-forwarded-for", |
| | | "Proxy-Client-IP", |
| | | "WL-Proxy-Client-IP", |
| | | "HTTP_CLIENT_IP", |
| | | "HTTP_X_FORWARDED_FOR" |
| | | }; |
| | | |
| | | private static final Predicate<String> IP_PREDICATE = (ip) -> StringUtil.isBlank(ip) || StringPool.UNKNOWN.equalsIgnoreCase(ip); |
| | | |
| | | /** |
| | | * è·åip |
| | | * |
| | | * @param request HttpServletRequest |
| | | * @return {String} |
| | | */ |
| | | @Nullable |
| | | public static String getIP(@Nullable HttpServletRequest request) { |
| | | if (request == null) { |
| | | return StringPool.EMPTY; |
| | | } |
| | | String ip = null; |
| | | for (String ipHeader : IP_HEADER_NAMES) { |
| | | ip = request.getHeader(ipHeader); |
| | | if (!IP_PREDICATE.test(ip)) { |
| | | break; |
| | | } |
| | | } |
| | | if (IP_PREDICATE.test(ip)) { |
| | | ip = request.getRemoteAddr(); |
| | | } |
| | | return StringUtil.isBlank(ip) ? null : StringUtil.splitTrim(ip, StringPool.COMMA)[0]; |
| | | } |
| | | |
| | | /** |
| | | * è·å请æ±å¤´çå¼ |
| | | * |
| | | * @param name 请æ±å¤´åç§° |
| | | * @return 请æ±å¤´ |
| | | */ |
| | | public static String getHeader(String name) { |
| | | HttpServletRequest request = getRequest(); |
| | | return Objects.requireNonNull(request).getHeader(name); |
| | | } |
| | | |
| | | /** |
| | | * è·å请æ±å¤´çå¼ |
| | | * |
| | | * @param name 请æ±å¤´åç§° |
| | | * @return 请æ±å¤´ |
| | | */ |
| | | public static Enumeration<String> getHeaders(String name) { |
| | | HttpServletRequest request = getRequest(); |
| | | return Objects.requireNonNull(request).getHeaders(name); |
| | | } |
| | | |
| | | /** |
| | | * è·åææç请æ±å¤´ |
| | | * |
| | | * @return 请æ±å¤´éå |
| | | */ |
| | | public static Enumeration<String> getHeaderNames() { |
| | | HttpServletRequest request = getRequest(); |
| | | return Objects.requireNonNull(request).getHeaderNames(); |
| | | } |
| | | |
| | | /** |
| | | * è·å请æ±åæ° |
| | | * |
| | | * @param name 请æ±åæ°å |
| | | * @return 请æ±åæ° |
| | | */ |
| | | public static String getParameter(String name) { |
| | | HttpServletRequest request = getRequest(); |
| | | return Objects.requireNonNull(request).getParameter(name); |
| | | } |
| | | |
| | | /** |
| | | * è·å request 请æ±ä½ |
| | | * |
| | | * @param servletInputStream servletInputStream |
| | | * @return body |
| | | */ |
| | | public static String getRequestBody(ServletInputStream servletInputStream) { |
| | | StringBuilder sb = new StringBuilder(); |
| | | BufferedReader reader = null; |
| | | try { |
| | | reader = new BufferedReader(new InputStreamReader(servletInputStream, StandardCharsets.UTF_8)); |
| | | String line; |
| | | while ((line = reader.readLine()) != null) { |
| | | sb.append(line); |
| | | } |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } finally { |
| | | if (servletInputStream != null) { |
| | | try { |
| | | servletInputStream.close(); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | if (reader != null) { |
| | | try { |
| | | reader.close(); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | } |
| | | return sb.toString(); |
| | | } |
| | | |
| | | /** |
| | | * è·å request 请æ±å
容 |
| | | * |
| | | * @param request request |
| | | * @return {String} |
| | | */ |
| | | public static String getRequestContent(HttpServletRequest request) { |
| | | try { |
| | | String queryString = request.getQueryString(); |
| | | if (StringUtil.isNotBlank(queryString)) { |
| | | return new String(queryString.getBytes(Charsets.ISO_8859_1), Charsets.UTF_8).replaceAll("&", "&").replaceAll("%22", "\""); |
| | | } |
| | | String charEncoding = request.getCharacterEncoding(); |
| | | if (charEncoding == null) { |
| | | charEncoding = StringPool.UTF_8; |
| | | } |
| | | byte[] buffer = getRequestBody(request.getInputStream()).getBytes(); |
| | | String str = new String(buffer, charEncoding).trim(); |
| | | if (StringUtil.isBlank(str)) { |
| | | StringBuilder sb = new StringBuilder(); |
| | | Enumeration<String> parameterNames = request.getParameterNames(); |
| | | while (parameterNames.hasMoreElements()) { |
| | | String key = parameterNames.nextElement(); |
| | | String value = request.getParameter(key); |
| | | StringUtil.appendBuilder(sb, key, "=", value, "&"); |
| | | } |
| | | str = StringUtil.removeSuffix(sb.toString(), "&"); |
| | | } |
| | | return str.replaceAll("&", "&"); |
| | | } catch (Exception ex) { |
| | | ex.printStackTrace(); |
| | | return StringPool.EMPTY; |
| | | } |
| | | } |
| | | |
| | | |
| | | } |
| | | |