Skip to content

Commit

Permalink
[Optimization] optimization RestResponse (#2921)
Browse files Browse the repository at this point in the history
  • Loading branch information
VampireAchao authored Aug 6, 2023
1 parent 279e2d6 commit 2565cdd
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,49 +17,56 @@

package org.apache.streampark.console.base.domain;

import javax.validation.constraints.NotNull;

import java.util.HashMap;

public class RestResponse extends HashMap<String, Object> {

public static final String STATUS_SUCCESS = "success";
public static final String STATUS_FAIL = "error";
public static final String STATUS_KEY = "status";
public static final String CODE_KEY = "code";
public static final String MESSAGE_KEY = "message";
public static final String DATA_KEY = "data";

private static final long serialVersionUID = 1L;

public static RestResponse success(Object data) {
RestResponse resp = new RestResponse();
resp.put("status", STATUS_SUCCESS);
resp.put("code", ResponseCode.CODE_SUCCESS);
resp.put("data", data);
resp.put(STATUS_KEY, STATUS_SUCCESS);
resp.put(CODE_KEY, ResponseCode.CODE_SUCCESS);
resp.put(DATA_KEY, data);
return resp;
}

public static RestResponse success() {
RestResponse resp = new RestResponse();
resp.put("status", STATUS_SUCCESS);
resp.put("code", ResponseCode.CODE_SUCCESS);
resp.put(STATUS_KEY, STATUS_SUCCESS);
resp.put(CODE_KEY, ResponseCode.CODE_SUCCESS);
return resp;
}

public static RestResponse fail(String message, Long code) {
RestResponse resp = new RestResponse();
resp.put("status", STATUS_FAIL);
resp.put("message", message);
resp.put("code", code);
resp.put("data", null);
resp.put(STATUS_KEY, STATUS_FAIL);
resp.put(MESSAGE_KEY, message);
resp.put(CODE_KEY, code);
resp.put(DATA_KEY, null);
return resp;
}

public RestResponse message(String message) {
this.put("message", message);
this.put(MESSAGE_KEY, message);
return this;
}

public RestResponse data(Object data) {
this.put("data", data);
this.put(DATA_KEY, data);
return this;
}

@NotNull
@Override
public RestResponse put(String key, Object value) {
super.put(key, value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public RestResponse signin(
throws Exception {

if (StringUtils.isEmpty(username)) {
return RestResponse.success().put("code", 0);
return RestResponse.success().put(RestResponse.CODE_KEY, 0);
}
User user = authenticator.authenticate(username, password, loginType);
return userService.getLoginUserInfo(user);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public class AccessTokenServiceImpl extends ServiceImpl<AccessTokenMapper, Acces
public RestResponse generateToken(Long userId, String expireTime, String description) {
User user = userService.getById(userId);
if (Objects.isNull(user)) {
return RestResponse.success().put("code", 0).message("user not available");
return RestResponse.success().put(RestResponse.CODE_KEY, 0).message("user not available");
}

if (StringUtils.isEmpty(expireTime)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,18 +282,20 @@ public void transferResource(Long userId, Long targetUserId) {
@Override
public RestResponse getLoginUserInfo(User user) {
if (user == null) {
return RestResponse.success().put("code", 0);
return RestResponse.success().put(RestResponse.CODE_KEY, 0);
}

if (User.STATUS_LOCK.equals(user.getStatus())) {
return RestResponse.success().put("code", 1);
return RestResponse.success().put(RestResponse.CODE_KEY, 1);
}
// set team
fillInTeam(user);

// no team.
if (user.getLastTeamId() == null) {
return RestResponse.success().data(user.getUserId()).put("code", ResponseCode.CODE_FORBIDDEN);
return RestResponse.success()
.data(user.getUserId())
.put(RestResponse.CODE_KEY, ResponseCode.CODE_FORBIDDEN);
}

updateLoginTime(user.getUsername());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ void testCrudToken() throws Exception {
String expireTime = "9999-01-01 00:00:00";
RestResponse restResponse = accessTokenService.generateToken(mockUserId, expireTime, "");
Assertions.assertNotNull(restResponse);
Assertions.assertInstanceOf(AccessToken.class, restResponse.get("data"));
Assertions.assertInstanceOf(AccessToken.class, restResponse.get(RestResponse.DATA_KEY));

// verify
AccessToken accessToken = (AccessToken) restResponse.get("data");
AccessToken accessToken = (AccessToken) restResponse.get(RestResponse.DATA_KEY);
LOG.info(accessToken.getToken());
JWTToken jwtToken = new JWTToken(WebUtils.decryptToken(accessToken.getToken()));
LOG.info(jwtToken.getToken());
Expand All @@ -73,7 +73,7 @@ void testCrudToken() throws Exception {
Long tokenId = accessToken.getId();
RestResponse toggleTokenResp = accessTokenService.toggleToken(tokenId);
Assertions.assertNotNull(toggleTokenResp);
Assertions.assertTrue((Boolean) toggleTokenResp.get("data"));
Assertions.assertTrue((Boolean) toggleTokenResp.get(RestResponse.DATA_KEY));

// get
AccessToken afterToggle = accessTokenService.getByUserId(mockUserId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.streampark.console.core.service;

import org.apache.streampark.console.SpringTestBase;
import org.apache.streampark.console.base.domain.RestResponse;
import org.apache.streampark.console.core.entity.Application;
import org.apache.streampark.console.core.entity.Resource;
import org.apache.streampark.console.core.enums.EngineType;
Expand Down Expand Up @@ -54,13 +55,17 @@ void testLockUser() throws Exception {
user.setStatus(User.STATUS_LOCK);
Map<String, Object> data =
(Map<String, Object>)
userService.updateUser(user).getOrDefault("data", Collections.emptyMap());
userService
.updateUser(user)
.getOrDefault(RestResponse.DATA_KEY, Collections.emptyMap());
Assertions.assertNotEquals(true, data.get("needTransferResource"));
// unlock user
user.setStatus(User.STATUS_VALID);
Map<String, Object> data1 =
(Map<String, Object>)
userService.updateUser(user).getOrDefault("data", Collections.emptyMap());
userService
.updateUser(user)
.getOrDefault(RestResponse.DATA_KEY, Collections.emptyMap());
Assertions.assertNotEquals(true, data1.get("needTransferResource"));

Resource resource = new Resource();
Expand All @@ -74,7 +79,9 @@ void testLockUser() throws Exception {
user.setStatus(User.STATUS_LOCK);
Map<String, Object> data2 =
(Map<String, Object>)
userService.updateUser(user).getOrDefault("data", Collections.emptyMap());
userService
.updateUser(user)
.getOrDefault(RestResponse.DATA_KEY, Collections.emptyMap());
Assertions.assertEquals(true, data2.get("needTransferResource"));
}

Expand Down

0 comments on commit 2565cdd

Please sign in to comment.