From d41fd2190382d0f45d6e484ee76e304c8549f524 Mon Sep 17 00:00:00 2001 From: Shiwen Cheng Date: Fri, 5 Mar 2021 21:08:11 +0800 Subject: [PATCH] [1.3.6-prepare][Improvement][Api] Use WebUtils.getCookie instead of BaseService.getCookie #4559 (#4968) --- .../interceptor/LocaleChangeInterceptor.java | 4 ++-- .../api/service/BaseService.java | 23 ------------------ .../api/service/SessionService.java | 18 ++++++++------ .../api/service/BaseServiceTest.java | 24 ++++--------------- 4 files changed, 17 insertions(+), 52 deletions(-) diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/interceptor/LocaleChangeInterceptor.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/interceptor/LocaleChangeInterceptor.java index 3fe236e065..2ed4e6f24b 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/interceptor/LocaleChangeInterceptor.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/interceptor/LocaleChangeInterceptor.java @@ -17,7 +17,6 @@ package org.apache.dolphinscheduler.api.interceptor; -import org.apache.dolphinscheduler.api.service.BaseService; import org.apache.dolphinscheduler.common.Constants; import java.util.Locale; @@ -30,12 +29,13 @@ import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.lang.Nullable; import org.springframework.util.StringUtils; import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; +import org.springframework.web.util.WebUtils; public class LocaleChangeInterceptor extends HandlerInterceptorAdapter { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) { - Cookie cookie = BaseService.getCookie(request, Constants.LOCALE_LANGUAGE); + Cookie cookie = WebUtils.getCookie(request, Constants.LOCALE_LANGUAGE); if (cookie != null) { // Proceed in cookie return true; diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/BaseService.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/BaseService.java index e0617f36c1..3b81e2c313 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/BaseService.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/BaseService.java @@ -21,11 +21,8 @@ import org.apache.dolphinscheduler.api.utils.Result; import org.apache.dolphinscheduler.common.Constants; import org.apache.dolphinscheduler.common.enums.UserType; import org.apache.dolphinscheduler.common.utils.HadoopUtils; -import org.apache.dolphinscheduler.common.utils.StringUtils; import org.apache.dolphinscheduler.dao.entity.User; -import javax.servlet.http.Cookie; -import javax.servlet.http.HttpServletRequest; import java.text.MessageFormat; import java.util.Map; @@ -112,26 +109,6 @@ public class BaseService { return false; } - - /** - * get cookie info by name - * @param request request - * @param name 'sessionId' - * @return get cookie info - */ - public static Cookie getCookie(HttpServletRequest request, String name) { - Cookie[] cookies = request.getCookies(); - if (cookies != null && cookies.length > 0) { - for (Cookie cookie : cookies) { - if (StringUtils.isNotEmpty(name) && name.equalsIgnoreCase(cookie.getName())) { - return cookie; - } - } - } - - return null; - } - /** * create tenant dir if not exists * @param tenantCode tenant code diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/SessionService.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/SessionService.java index 2588dd0e65..2e8d4bf376 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/SessionService.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/SessionService.java @@ -23,18 +23,22 @@ import org.apache.dolphinscheduler.common.utils.CollectionUtils; import org.apache.dolphinscheduler.dao.entity.Session; import org.apache.dolphinscheduler.dao.entity.User; import org.apache.dolphinscheduler.dao.mapper.SessionMapper; + import org.apache.commons.lang.StringUtils; + +import java.util.Date; +import java.util.List; +import java.util.UUID; + +import javax.servlet.http.Cookie; +import javax.servlet.http.HttpServletRequest; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; - -import javax.servlet.http.Cookie; -import javax.servlet.http.HttpServletRequest; -import java.util.Date; -import java.util.List; -import java.util.UUID; +import org.springframework.web.util.WebUtils; /** * session service @@ -57,7 +61,7 @@ public class SessionService extends BaseService{ String sessionId = request.getHeader(Constants.SESSION_ID); if(StringUtils.isBlank(sessionId)) { - Cookie cookie = getCookie(request, Constants.SESSION_ID); + Cookie cookie = WebUtils.getCookie(request, Constants.SESSION_ID); if (cookie != null) { sessionId = cookie.getValue(); diff --git a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/BaseServiceTest.java b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/BaseServiceTest.java index 02086a8259..773ec805f7 100644 --- a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/BaseServiceTest.java +++ b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/BaseServiceTest.java @@ -22,6 +22,10 @@ import org.apache.dolphinscheduler.common.Constants; import org.apache.dolphinscheduler.common.enums.UserType; import org.apache.dolphinscheduler.common.utils.HadoopUtils; import org.apache.dolphinscheduler.dao.entity.User; + +import java.util.HashMap; +import java.util.Map; + import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -33,12 +37,6 @@ import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.mock.web.MockCookie; -import org.springframework.mock.web.MockHttpServletRequest; - -import javax.servlet.http.Cookie; -import java.util.HashMap; -import java.util.Map; @RunWith(PowerMockRunner.class) @PowerMockIgnore({"sun.security.*", "javax.net.*"}) @@ -92,20 +90,6 @@ public class BaseServiceTest { baseService.putMsg(result,Status.PROJECT_NOT_FOUNT,"test"); } @Test - public void testGetCookie(){ - - MockHttpServletRequest request = new MockHttpServletRequest(); - MockCookie mockCookie = new MockCookie("userId","1"); - request.setCookies(mockCookie); - //cookie is not null - Cookie cookie = BaseService.getCookie(request,"userId"); - Assert.assertNotNull(cookie); - //cookie is null - cookie = BaseService.getCookie(request,"userName"); - Assert.assertNull(cookie); - - } - @Test public void testCreateTenantDirIfNotExists(){ PowerMockito.mockStatic(HadoopUtils.class);