Skip to content

Commit

Permalink
[#noissue] Cleanup Response
Browse files Browse the repository at this point in the history
  • Loading branch information
emeroad committed Jul 10, 2024
1 parent 5157e23 commit 330fa1b
Show file tree
Hide file tree
Showing 22 changed files with 112 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.navercorp.pinpoint.common.server.response.MapResponse;
import com.navercorp.pinpoint.common.server.response.Result;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -79,7 +81,7 @@ public boolean preHandle(HttpServletRequest request, HttpServletResponse respons
}

private String jsonError(String errorMessage) throws JsonProcessingException {
SimpleResponse response = new SimpleResponse(false, errorMessage);
MapResponse response = new MapResponse(Result.FAIL, errorMessage);
return mapper.writeValueAsString(response);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
package com.navercorp.pinpoint.collector.manage.controller;

import com.navercorp.pinpoint.collector.manage.HandlerManager;
import com.navercorp.pinpoint.common.server.response.MapResponse;
import com.navercorp.pinpoint.common.server.response.Response;
import com.navercorp.pinpoint.common.server.response.Result;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
Expand All @@ -39,10 +42,10 @@ public HandlerManagerController(HandlerManager handlerManager) {
}

@GetMapping(value = "/enableAccess")
public ResponseEntity<SimpleResponse> enableAccess() {
public ResponseEntity<Response> enableAccess() {
try {
handlerManager.enableAccess();
return ResponseEntity.ok(SimpleResponse.success());
return ResponseEntity.ok(MapResponse.ok());
} catch (Exception e) {
return unauthorizedResponse(e.getMessage());
}
Expand All @@ -51,27 +54,27 @@ public ResponseEntity<SimpleResponse> enableAccess() {


@GetMapping(value = "/disableAccess")
public ResponseEntity<SimpleResponse> disableAccess() {
public ResponseEntity<Response> disableAccess() {
try {
handlerManager.disableAccess();
return ResponseEntity.ok(SimpleResponse.success());
return ResponseEntity.ok(MapResponse.ok());
} catch (Exception e) {
return unauthorizedResponse(e.getMessage());
}
}

@GetMapping(value = "/isEnable")
public ResponseEntity<SimpleResponse> isEnable() {
public ResponseEntity<Response> isEnable() {
boolean isEnable = handlerManager.isEnable();

SimpleResponse simpleResponse = SimpleResponse.success();
simpleResponse.addAttribute("isEnable", isEnable);
MapResponse response = new MapResponse(Result.SUCCESS);
response.addAttribute("isEnable", isEnable);

return ResponseEntity.ok(simpleResponse);
return ResponseEntity.ok(response);
}

private ResponseEntity<SimpleResponse> unauthorizedResponse(String errorMessage) {
SimpleResponse body = new SimpleResponse(false, errorMessage);
private ResponseEntity<Response> unauthorizedResponse(String errorMessage) {
MapResponse body = new MapResponse(Result.FAIL, errorMessage);
return ResponseEntity.status(HttpStatus.FORBIDDEN).body(body);
}
}
Original file line number Diff line number Diff line change
@@ -1,42 +1,26 @@
package com.navercorp.pinpoint.collector.manage.controller;
package com.navercorp.pinpoint.common.server.response;

import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.navercorp.pinpoint.common.server.response.Response;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@JsonInclude(JsonInclude.Include.NON_NULL)
public class SimpleResponse implements Response {
private final boolean result;
private final String message;
public class MapResponse extends SimpleResponse {
private Map<String, Object> attributeMap;

public static SimpleResponse success() {
return new SimpleResponse(true);
public static Response ok() {
return new MapResponse(Result.SUCCESS);
}

public SimpleResponse(boolean result, String message) {
this.result = result;
this.message = message;
public MapResponse(Result result, String message) {
super(result, message);
}

public SimpleResponse(boolean result) {
this.result = result;
this.message = null;
}

public String getResult() {
if (result) {
return "success";
}
return "fail";
}

public String getMessage() {
return message;
public MapResponse(Result result) {
super(result);
}

public void addAttribute(String attributeName, Object attributeValue) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package com.navercorp.pinpoint.common.server.response;

public interface Response {
}
Result getResult();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.navercorp.pinpoint.common.server.response;

public enum Result {
SUCCESS, FAIL;

public static Result of(boolean success) {
if (success) {
return SUCCESS;
}
return FAIL;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,29 @@

import java.util.Objects;

public class SuccessResponse implements Response {
private final String result;
public class SimpleResponse implements Response {
private final Result result;
private final String message;

public static Response ok() {
return new SuccessResponse("SUCCESS");
return new SimpleResponse(Result.SUCCESS);
}

public static Response ok(String message) {
return new SuccessResponse("SUCCESS", message);
return new SimpleResponse(Result.SUCCESS, message);
}

public SuccessResponse(String result) {
public SimpleResponse(Result result) {
this(result, null);
}

public SuccessResponse(String result, String message) {
public SimpleResponse(Result result, String message) {
this.result = Objects.requireNonNull(result, "result");
this.message = message;

}

public String getResult() {
@Override
public Result getResult() {
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
package com.navercorp.pinpoint.web.authorization.controller;

import com.navercorp.pinpoint.common.server.response.Response;
import com.navercorp.pinpoint.common.server.response.SuccessResponse;
import com.navercorp.pinpoint.common.server.response.Result;
import com.navercorp.pinpoint.common.server.response.SimpleResponse;
import com.navercorp.pinpoint.common.util.StringUtils;
import com.navercorp.pinpoint.web.alarm.CheckerCategory;
import com.navercorp.pinpoint.web.alarm.vo.Rule;
Expand Down Expand Up @@ -62,7 +63,7 @@ public AlarmResponse insertRule(@RequestBody Rule rule) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "there is not applicationId/checkerName/userGroupId/threashold to insert alarm rule");
}
final String ruleId = alarmService.insertRule(rule);
return new AlarmResponse("SUCCESS", ruleId);
return new AlarmResponse(Result.SUCCESS, ruleId);
}

@DeleteMapping
Expand All @@ -71,7 +72,7 @@ public Response deleteRule(@RequestBody Rule rule) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "there is not ruleId to delete alarm rule");
}
alarmService.deleteRule(rule);
return SuccessResponse.ok();
return SimpleResponse.ok();
}

@GetMapping(params = USER_GROUP_ID)
Expand All @@ -93,7 +94,7 @@ public Response updateRule(@RequestBody Rule rule) {
);
}
alarmService.updateRule(rule);
return SuccessResponse.ok();
return SimpleResponse.ok();
}

@GetMapping(value = "/checker")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
package com.navercorp.pinpoint.web.authorization.controller;

import com.navercorp.pinpoint.common.server.response.Response;
import com.navercorp.pinpoint.common.server.response.SuccessResponse;
import com.navercorp.pinpoint.common.server.response.SimpleResponse;
import com.navercorp.pinpoint.common.util.StringUtils;
import com.navercorp.pinpoint.web.service.UserService;
import com.navercorp.pinpoint.web.util.ValueValidator;
Expand Down Expand Up @@ -65,7 +65,7 @@ public Response insertUser(@RequestBody User user) {
);
}
userService.insertUser(user);
return SuccessResponse.ok();
return SimpleResponse.ok();
}

@DeleteMapping
Expand All @@ -77,7 +77,7 @@ public Response deleteUser(@RequestBody User user) {
);
}
userService.deleteUser(user.getUserId());
return SuccessResponse.ok();
return SimpleResponse.ok();
}

@GetMapping(params = "userId")
Expand Down Expand Up @@ -129,7 +129,7 @@ public Response updateUser(@RequestBody User user) {
);
}
userService.updateUser(user);
return SuccessResponse.ok();
return SimpleResponse.ok();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
package com.navercorp.pinpoint.web.authorization.controller;

import com.navercorp.pinpoint.common.server.response.Response;
import com.navercorp.pinpoint.common.server.response.SuccessResponse;
import com.navercorp.pinpoint.common.server.response.Result;
import com.navercorp.pinpoint.common.server.response.SimpleResponse;
import com.navercorp.pinpoint.common.util.StringUtils;
import com.navercorp.pinpoint.web.response.CreateUserGroupResponse;
import com.navercorp.pinpoint.web.service.UserGroupService;
Expand Down Expand Up @@ -71,7 +72,7 @@ public CreateUserGroupResponse createUserGroup(@RequestBody UserGroup userGroup)

try {
final String userGroupNumber = userGroupService.createUserGroup(userGroup);
return new CreateUserGroupResponse("SUCCESS", userGroupNumber);
return new CreateUserGroupResponse(Result.SUCCESS, userGroupNumber);
} catch (PinpointUserGroupException e) {
logger.error(e.getMessage(), e);
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, e.getMessage());
Expand All @@ -89,7 +90,7 @@ public Response deleteUserGroup(@RequestBody UserGroup userGroup) {

try {
userGroupService.deleteUserGroup(userGroup);
return SuccessResponse.ok();
return SimpleResponse.ok();
} catch (PinpointUserGroupException e) {
logger.error(e.getMessage(), e);
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, e.getMessage());
Expand Down Expand Up @@ -122,7 +123,7 @@ public Response insertUserGroupMember(@RequestBody UserGroupMemberParam userGrou
);
}
userGroupService.insertMember(userGroupMember);
return SuccessResponse.ok();
return SimpleResponse.ok();
}

@DeleteMapping(value = "/member")
Expand All @@ -135,7 +136,7 @@ public Response deleteUserGroupMember(@RequestBody UserGroupMemberParam userGrou
);
}
userGroupService.deleteMember(userGroupMember);
return SuccessResponse.ok();
return SimpleResponse.ok();
}

@GetMapping(value = "/member")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
package com.navercorp.pinpoint.web.authorization.controller;

import com.navercorp.pinpoint.common.server.response.Response;
import com.navercorp.pinpoint.common.server.response.SuccessResponse;
import com.navercorp.pinpoint.common.server.response.Result;
import com.navercorp.pinpoint.common.server.response.SimpleResponse;
import com.navercorp.pinpoint.web.alarm.vo.Rule;
import com.navercorp.pinpoint.web.response.AlarmResponse;
import com.navercorp.pinpoint.web.webhook.WebhookModule;
Expand Down Expand Up @@ -57,7 +58,7 @@ public AlarmResponse insertRuleWithWebhooks(@RequestBody RuleWithWebhooks ruleWi
}

final String ruleId = webhookAlarmServiceFacade.insertRuleWithWebhooks(rule, ruleWithWebhooks.getWebhookIds());
return new AlarmResponse("SUCCESS", ruleId);
return new AlarmResponse(Result.SUCCESS, ruleId);
}

@PutMapping(value = "/includeWebhooks")
Expand All @@ -67,7 +68,7 @@ public Response updateRuleWithWebhooks(@RequestBody RuleWithWebhooks ruleWithWeb
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "there is not ruleId/userGroupId/applicationid/checkerName to update alarm rule");
}
webhookAlarmServiceFacade.updateRuleWithWebhooks(rule, ruleWithWebhooks.getWebhookIds());
return SuccessResponse.ok();
return SimpleResponse.ok();
}

static public class RuleWithWebhooks {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package com.navercorp.pinpoint.web.controller;

import com.navercorp.pinpoint.common.server.response.Response;
import com.navercorp.pinpoint.common.server.response.SuccessResponse;
import com.navercorp.pinpoint.common.server.response.SimpleResponse;
import com.navercorp.pinpoint.common.server.util.time.Range;
import com.navercorp.pinpoint.web.service.AgentStatisticsService;
import com.navercorp.pinpoint.web.util.DateTimeUtils;
Expand Down Expand Up @@ -65,7 +65,7 @@ public Response insertAgentCount(
boolean success = agentStatisticsService.insertAgentCount(agentCountStatistics);

if (success) {
return SuccessResponse.ok();
return SimpleResponse.ok();
} else {
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "insert DAO error.");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package com.navercorp.pinpoint.web.response;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.navercorp.pinpoint.common.server.response.SuccessResponse;
import com.navercorp.pinpoint.common.server.response.Result;
import com.navercorp.pinpoint.common.server.response.SimpleResponse;

public class AlarmResponse extends SuccessResponse {
public class AlarmResponse extends SimpleResponse {
private final String ruleId;

public AlarmResponse(String result, String ruleId) {
public AlarmResponse(Result result, String ruleId) {
super(result);
this.ruleId = ruleId;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package com.navercorp.pinpoint.web.response;

import com.navercorp.pinpoint.common.server.response.SuccessResponse;
import com.navercorp.pinpoint.common.server.response.Result;
import com.navercorp.pinpoint.common.server.response.SimpleResponse;

import java.util.Objects;

public class CreateUserGroupResponse extends SuccessResponse {
public class CreateUserGroupResponse extends SimpleResponse {
private final String number;

public CreateUserGroupResponse(String result, String number) {
public CreateUserGroupResponse(Result result, String number) {
super(result);
this.number = Objects.requireNonNull(number, "number");
}
Expand Down
Loading

0 comments on commit 330fa1b

Please sign in to comment.