|
@@ -1,228 +0,0 @@
|
|
-package com.qmth.themis.backend.util;
|
|
|
|
-
|
|
|
|
-import com.qmth.themis.business.constant.SystemConstant;
|
|
|
|
-import com.qmth.themis.business.util.JacksonUtil;
|
|
|
|
-import com.qmth.themis.common.contanst.Constants;
|
|
|
|
-import com.qmth.themis.common.enums.ExceptionResultEnum;
|
|
|
|
-import com.qmth.themis.common.exception.BusinessException;
|
|
|
|
-import com.qmth.themis.common.util.Result;
|
|
|
|
-import com.qmth.themis.common.util.ResultUtil;
|
|
|
|
-import org.springframework.web.context.request.RequestContextHolder;
|
|
|
|
-import org.springframework.web.context.request.ServletRequestAttributes;
|
|
|
|
-
|
|
|
|
-import javax.servlet.http.HttpServletRequest;
|
|
|
|
-import javax.servlet.http.HttpServletResponse;
|
|
|
|
-import java.io.IOException;
|
|
|
|
-import java.util.Objects;
|
|
|
|
-
|
|
|
|
-/**
|
|
|
|
- * @Description: http工具
|
|
|
|
- * @Param:
|
|
|
|
- * @return:
|
|
|
|
- * @Author: wangliang
|
|
|
|
- * @Date: 2020/4/10
|
|
|
|
- */
|
|
|
|
-public class ServletUtil {
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 输出错误
|
|
|
|
- *
|
|
|
|
- * @param code
|
|
|
|
- * @param message
|
|
|
|
- * @throws IOException
|
|
|
|
- */
|
|
|
|
- public static void responseError(int code, String message) throws IOException {
|
|
|
|
- Result result = ResultUtil.error(code, message);
|
|
|
|
- String json = JacksonUtil.parseJson(result);
|
|
|
|
- getResponse().getWriter().print(json);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 获取请求的accessToken
|
|
|
|
- *
|
|
|
|
- * @return
|
|
|
|
- */
|
|
|
|
- public static String getRequestToken() {
|
|
|
|
- HttpServletRequest request = getRequest();
|
|
|
|
- // 从header中获取token
|
|
|
|
- String token = request.getHeader(SystemConstant.ACCESS_TOKEN);
|
|
|
|
- // 如果header中不存在token,则从参数中获取token
|
|
|
|
- if (Objects.isNull(token)) {
|
|
|
|
- token = request.getParameter(SystemConstant.ACCESS_TOKEN);
|
|
|
|
- }
|
|
|
|
- return token;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 获取请求的platform
|
|
|
|
- *
|
|
|
|
- * @return
|
|
|
|
- */
|
|
|
|
- public static String getRequestPlatform() {
|
|
|
|
- HttpServletRequest request = getRequest();
|
|
|
|
- // 从header中获取platform
|
|
|
|
- String platform = request.getHeader(Constants.HEADER_PLATFORM);
|
|
|
|
- // 如果header中不存在platform,则从参数中获取platform
|
|
|
|
- if (Objects.isNull(platform)) {
|
|
|
|
- platform = request.getParameter(Constants.HEADER_PLATFORM);
|
|
|
|
- if (Objects.isNull(platform)) {
|
|
|
|
- throw new BusinessException(ExceptionResultEnum.PLATFORM_INVALID);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- return platform;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 获取请求的deviceId
|
|
|
|
- *
|
|
|
|
- * @return
|
|
|
|
- */
|
|
|
|
- public static String getRequestDeviceId() {
|
|
|
|
- HttpServletRequest request = getRequest();
|
|
|
|
- // 从header中获取deviceId
|
|
|
|
- String deviceId = request.getHeader(Constants.HEADER_DEVICE_ID);
|
|
|
|
- // 如果header中不存在deviceId,则从参数中获取deviceId
|
|
|
|
- if (Objects.isNull(deviceId)) {
|
|
|
|
- deviceId = request.getParameter(Constants.HEADER_DEVICE_ID);
|
|
|
|
- if (Objects.isNull(deviceId)) {
|
|
|
|
- throw new BusinessException(ExceptionResultEnum.DEVICE_ID_INVALID);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- return deviceId;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 获取请求的time
|
|
|
|
- *
|
|
|
|
- * @return
|
|
|
|
- */
|
|
|
|
- public static String getRequestTime() {
|
|
|
|
- HttpServletRequest request = getRequest();
|
|
|
|
- // 从header中获取time
|
|
|
|
- String time = request.getHeader(Constants.HEADER_TIME);
|
|
|
|
- // 如果header中不存在time,则从参数中获取time
|
|
|
|
- if (Objects.isNull(time)) {
|
|
|
|
- time = request.getParameter(Constants.HEADER_TIME);
|
|
|
|
- if (Objects.isNull(time)) {
|
|
|
|
- throw new BusinessException(ExceptionResultEnum.TIME_INVALID);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- return time;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 获取请求的Authorization
|
|
|
|
- *
|
|
|
|
- * @return
|
|
|
|
- */
|
|
|
|
- public static String getRequestAuthorization() {
|
|
|
|
- HttpServletRequest request = getRequest();
|
|
|
|
- // 从header中获取authorization
|
|
|
|
- String authorization = request.getHeader(Constants.HEADER_AUTHORIZATION);
|
|
|
|
- // 如果header中不存在authorization,则从参数中获取authorization
|
|
|
|
- if (Objects.isNull(authorization)) {
|
|
|
|
- authorization = request.getParameter(Constants.HEADER_AUTHORIZATION);
|
|
|
|
- if (Objects.isNull(authorization)) {
|
|
|
|
- throw new BusinessException(ExceptionResultEnum.AUTHORIZATION_INVALID);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- return authorization;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 获取请求的Authorization
|
|
|
|
- *
|
|
|
|
- * @return
|
|
|
|
- */
|
|
|
|
- public static String getRequestAuthorizationForAspect() {
|
|
|
|
- HttpServletRequest request = getRequest();
|
|
|
|
- // 从header中获取authorization
|
|
|
|
- String authorization = request.getHeader(Constants.HEADER_AUTHORIZATION);
|
|
|
|
- // 如果header中不存在authorization,则从参数中获取authorization
|
|
|
|
- if (Objects.isNull(authorization)) {
|
|
|
|
- authorization = request.getParameter(Constants.HEADER_AUTHORIZATION);
|
|
|
|
- }
|
|
|
|
- return authorization;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 获取请求的time
|
|
|
|
- *
|
|
|
|
- * @return
|
|
|
|
- */
|
|
|
|
- public static String getRequestTimeForTime() {
|
|
|
|
- HttpServletRequest request = getRequest();
|
|
|
|
- // 从header中获取time
|
|
|
|
- String time = request.getHeader(Constants.HEADER_TIME);
|
|
|
|
- // 如果header中不存在time,则从参数中获取time
|
|
|
|
- if (Objects.isNull(time)) {
|
|
|
|
- time = request.getParameter(Constants.HEADER_TIME);
|
|
|
|
- }
|
|
|
|
- return time;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 获取请求的md5
|
|
|
|
- *
|
|
|
|
- * @return
|
|
|
|
- */
|
|
|
|
- public static String getRequestMd5() {
|
|
|
|
- return getRequest().getHeader(SystemConstant.MD5);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 获取请求的path
|
|
|
|
- *
|
|
|
|
- * @return
|
|
|
|
- */
|
|
|
|
- public static String getRequestPath() {
|
|
|
|
- return getRequest().getHeader(SystemConstant.PATH);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 获取请求的Session
|
|
|
|
- *
|
|
|
|
- * @return
|
|
|
|
- */
|
|
|
|
- public static Object getRequestSession() {
|
|
|
|
- return getRequest().getAttribute(SystemConstant.SESSION);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 获取请求的account
|
|
|
|
- *
|
|
|
|
- * @return
|
|
|
|
- */
|
|
|
|
- public static Object getRequestAccount() {
|
|
|
|
- return getRequest().getAttribute(SystemConstant.ACCOUNT);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 获取请求的org
|
|
|
|
- *
|
|
|
|
- * @return
|
|
|
|
- */
|
|
|
|
- public static Object getRequestOrg() {
|
|
|
|
- return getRequest().getAttribute(SystemConstant.ORG);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 获取HttpServletRequest
|
|
|
|
- *
|
|
|
|
- * @return
|
|
|
|
- */
|
|
|
|
- public static HttpServletRequest getRequest() {
|
|
|
|
- ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
|
|
|
- return servletRequestAttributes.getRequest();
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 获取HttpServletResponse
|
|
|
|
- *
|
|
|
|
- * @return
|
|
|
|
- */
|
|
|
|
- public static HttpServletResponse getResponse() {
|
|
|
|
- ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
|
|
|
- return servletRequestAttributes.getResponse();
|
|
|
|
- }
|
|
|
|
-}
|
|
|