Skip to content

Commit

Permalink
[pinpoint-apm#8806] Refactor HashMap for HttpResponse to Response object
Browse files Browse the repository at this point in the history
  • Loading branch information
emeroad committed Apr 25, 2022
1 parent a751cd0 commit df7b3e2
Show file tree
Hide file tree
Showing 21 changed files with 287 additions and 226 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import com.navercorp.pinpoint.web.vo.AgentActiveThreadDumpFactory;
import com.navercorp.pinpoint.web.vo.AgentActiveThreadDumpList;
import com.navercorp.pinpoint.web.vo.AgentInfo;
import com.navercorp.pinpoint.web.vo.CodeResult;
import com.navercorp.pinpoint.web.response.CodeResult;
import org.apache.thrift.TBase;
import org.apache.thrift.TException;
import org.springframework.web.bind.annotation.GetMapping;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import com.navercorp.pinpoint.web.vo.AgentInstallationInfo;
import com.navercorp.pinpoint.web.vo.AgentStatus;
import com.navercorp.pinpoint.web.vo.ApplicationAgentsList;
import com.navercorp.pinpoint.web.vo.CodeResult;
import com.navercorp.pinpoint.web.response.CodeResult;
import com.navercorp.pinpoint.web.vo.DefaultAgentInfoFilter;
import com.navercorp.pinpoint.common.server.util.time.Range;
import com.navercorp.pinpoint.web.vo.timeline.inspector.InspectorTimeline;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,16 @@
import com.navercorp.pinpoint.common.util.StringUtils;
import com.navercorp.pinpoint.web.alarm.CheckerCategory;
import com.navercorp.pinpoint.web.alarm.vo.Rule;
import com.navercorp.pinpoint.web.response.SuccessResponse;
import com.navercorp.pinpoint.web.service.AlarmService;
import com.navercorp.pinpoint.web.service.WebhookSendInfoService;
import com.navercorp.pinpoint.web.response.AlarmResponse;
import com.navercorp.pinpoint.web.response.ErrorResponse;
import com.navercorp.pinpoint.web.response.Response;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.GetMapping;
Expand All @@ -34,9 +39,7 @@
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

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

/**
Expand All @@ -61,24 +64,8 @@ public AlarmController(AlarmService alarmService, WebhookSendInfoService webhook
this.webhookSendInfoService = Objects.requireNonNull(webhookSendInfoService, "webhookSendInfoService");
}

private Map<String, String> getErrorStringMap(String errorCode, String errorMessage) {
Map<String, String> returnMap = new HashMap<>();
returnMap.put("errorCode", errorCode);
returnMap.put("errorMessage", errorMessage);
return returnMap;
}

private Map<String, String> getResultStringMap(String result, String ruleId) {
Map<String, String> returnMap = new HashMap<>();
returnMap.put("result", result);
returnMap.put("ruleId", ruleId);
return returnMap;
}

private Map<String, String> getResultStringMap(String result) {
Map<String, String> returnMap = new HashMap<>();
returnMap.put("result", result);
return returnMap;
private Response getAlarmResponse(String result, String ruleId) {
return new AlarmResponse(result, ruleId);
}

private boolean isRuleDataValidForPost(Rule rule) {
Expand All @@ -89,27 +76,27 @@ private boolean isRuleDataValidForPost(Rule rule) {
}

@PostMapping()
public Map<String, String> insertRule(@RequestBody Rule rule) {
public ResponseEntity<Response> insertRule(@RequestBody Rule rule) {
if (!isRuleDataValidForPost(rule)) {
return getErrorStringMap("500", "there is not applicationId/checkerName/userGroupId/threashold to insert alarm rule");
return ErrorResponse.badRequest("there is not applicationId/checkerName/userGroupId/threashold to insert alarm rule");
}
String ruleId = alarmService.insertRule(rule);
return getResultStringMap("SUCCESS", ruleId);
return ResponseEntity.ok(getAlarmResponse("SUCCESS", ruleId));
}

@DeleteMapping()
public Map<String, String> deleteRule(@RequestBody Rule rule) {
public ResponseEntity<Response> deleteRule(@RequestBody Rule rule) {
if (StringUtils.isEmpty(rule.getRuleId())) {
return getErrorStringMap("500", "there is not ruleId to delete alarm rule");
return ErrorResponse.badRequest("there is not ruleId to delete alarm rule");
}
alarmService.deleteRule(rule);
return getResultStringMap("SUCCESS");
return SuccessResponse.ok();
}

@GetMapping()
public Object getRule(@RequestParam(value=USER_GROUP_ID, required=false) String userGroupId, @RequestParam(value=APPLICATION_ID, required=false) String applicationId) {
if (StringUtils.isEmpty(userGroupId) && StringUtils.isEmpty(applicationId)) {
return getErrorStringMap("500", "there is not userGroupId or applicationID to get alarm rule");
return ErrorResponse.badRequest("there is not userGroupId or applicationID to get alarm rule");
}

if (StringUtils.hasLength(userGroupId)) {
Expand All @@ -120,43 +107,43 @@ public Object getRule(@RequestParam(value=USER_GROUP_ID, required=false) String
}

@PutMapping()
public Map<String, String> updateRule(@RequestBody Rule rule) {
public ResponseEntity<Response> updateRule(@RequestBody Rule rule) {
if (StringUtils.isEmpty(rule.getRuleId()) || StringUtils.isEmpty(rule.getApplicationId()) || StringUtils.isEmpty(rule.getCheckerName()) || StringUtils.isEmpty(rule.getUserGroupId())) {
return getErrorStringMap("500", "there is not ruleId/userGroupId/applicationid/checkerName to update alarm rule");
return ErrorResponse.badRequest("there is not ruleId/userGroupId/applicationid/checkerName to update alarm rule");
}
alarmService.updateRule(rule);
return getResultStringMap("SUCCESS");
return SuccessResponse.ok();
}

@PostMapping(value = "/includeWebhooks")
public Map<String, String> insertRuleWithWebhooks(@RequestBody RuleWithWebhooks ruleWithWebhooks) {
public ResponseEntity<Response> insertRuleWithWebhooks(@RequestBody RuleWithWebhooks ruleWithWebhooks) {
Rule rule = ruleWithWebhooks.getRule();
if (!isRuleDataValidForPost(rule)) {
return getErrorStringMap("500", "there is not applicationId/checkerName/userGroupId/threashold to insert alarm rule");
return ErrorResponse.badRequest("there is not applicationId/checkerName/userGroupId/threashold to insert alarm rule");
}

if (!webhookEnable) {
return getErrorStringMap("500", "webhook should be enabled to bind webhook to an alarm");
return ErrorResponse.serverError("webhook should be enabled to bind webhook to an alarm");
}

String ruleId = alarmService.insertRuleWithWebhooks(rule, ruleWithWebhooks.getWebhookIds());

return getResultStringMap("SUCCESS", ruleId);
return ResponseEntity.ok(getAlarmResponse("SUCCESS", ruleId));
}

@PutMapping(value = "/includeWebhooks")
public Map<String, String> updateRuleWithWebhooks(@RequestBody RuleWithWebhooks ruleWithWebhooks) {
public ResponseEntity<Response> updateRuleWithWebhooks(@RequestBody RuleWithWebhooks ruleWithWebhooks) {
Rule rule = ruleWithWebhooks.getRule();
if (StringUtils.isEmpty(rule.getRuleId()) || StringUtils.isEmpty(rule.getApplicationId()) || StringUtils.isEmpty(rule.getCheckerName()) || StringUtils.isEmpty(rule.getUserGroupId())) {
return getErrorStringMap("500", "there is not ruleId/userGroupId/applicationid/checkerName to update alarm rule");
return ErrorResponse.badRequest("there is not ruleId/userGroupId/applicationid/checkerName to update alarm rule");
}

if (!webhookEnable) {
return getErrorStringMap("500", "webhook should be enabled to bind webhook to an alarm");
return ErrorResponse.serverError("webhook should be enabled to bind webhook to an alarm");
}

alarmService.updateRuleWithWebhooks(rule, ruleWithWebhooks.getWebhookIds());
return getResultStringMap("SUCCESS");
return SuccessResponse.ok();
}

@GetMapping(value = "/checker")
Expand All @@ -165,9 +152,9 @@ public List<String> getCheckerName() {
}

@ExceptionHandler(Exception.class)
public Map<String, String> handleException(Exception e) {
public ResponseEntity<Response> handleException(Exception e) {
logger.warn(" Exception occurred while trying to CRUD Alarm Rule information", e);
return getErrorStringMap("500", String.format("Exception occurred while trying to Alarm Rule information: %s", e.getMessage()));
return ErrorResponse.serverError(String.format("Exception occurred while trying to Alarm Rule information: %s", e.getMessage()));
}

static public class RuleWithWebhooks {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@
import com.navercorp.pinpoint.web.util.ValueValidator;
import com.navercorp.pinpoint.web.vo.User;

import com.navercorp.pinpoint.web.response.ErrorResponse;
import com.navercorp.pinpoint.web.response.Response;
import com.navercorp.pinpoint.web.response.SuccessResponse;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.GetMapping;
Expand All @@ -33,9 +37,7 @@
import org.springframework.web.bind.annotation.RestController;

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

/**
Expand All @@ -55,35 +57,25 @@ public UserController(UserService userService) {
}

@PostMapping()
public Map<String, String> insertUser(@RequestBody User user) {
if (ValueValidator.validateUser(user) == false) {
Map<String, String> result = new HashMap<>();
result.put("errorCode", "500");
result.put("errorMessage", "User information validation failed to creating user infomation.");
return result;
public ResponseEntity<Response> insertUser(@RequestBody User user) {
if (!ValueValidator.validateUser(user)) {
return ErrorResponse.badRequest("User information validation failed to creating user information.");
}

userService.insertUser(user);

Map<String, String> result = new HashMap<>();
result.put("result", "SUCCESS");
return result;
return SuccessResponse.ok();
}

@DeleteMapping()
public Map<String, String> deletetUser(@RequestBody User user) {
public ResponseEntity<Response> deletetUser(@RequestBody User user) {
if (StringUtils.isEmpty(user.getUserId())) {
Map<String, String> result = new HashMap<>();
result.put("errorCode", "500");
result.put("errorMessage", "there is not userId in params to delete user");
return result;
return ErrorResponse.badRequest("there is not userId in params to delete user");
}

userService.deleteUser(user.getUserId());

Map<String, String> result = new HashMap<>();
result.put("result", "SUCCESS");
return result;
return SuccessResponse.ok();
}

@GetMapping()
Expand All @@ -102,36 +94,25 @@ public Object getUser(@RequestParam(value = "userId", required = false) String u
} catch (Exception e) {
logger.error("can't select user", e);

Map<String, String> result = new HashMap<>();
result.put("errorCode", "500");
result.put("errorMessage", "This api need to collect condition for search.");
return result;
return ErrorResponse.serverError("This api need to collect condition for search.");
}
}

@PutMapping()
public Map<String, String> updateUser(@RequestBody User user) {
if (ValueValidator.validateUser(user) == false) {
Map<String, String> result = new HashMap<>();
result.put("errorCode", "500");
result.put("errorMessage", "User information validation failed to creating user infomation.");
return result;
public ResponseEntity<Response> updateUser(@RequestBody User user) {
if (!ValueValidator.validateUser(user)) {
return ErrorResponse.badRequest("User information validation failed to creating user infomation.");
}

userService.updateUser(user);

Map<String, String> result = new HashMap<>();
result.put("result", "SUCCESS");
return result;
return SuccessResponse.ok();
}

@ExceptionHandler(Exception.class)
public Map<String, String> handleException(Exception e) {
logger.error(" Exception occurred while trying to CRUD user information", e);
public ResponseEntity<Response> handleException(Exception e) {
logger.error("Exception occurred while trying to CRUD user information", e);

Map<String, String> result = new HashMap<>();
result.put("errorCode", "500");
result.put("errorMessage", "Exception occurred while trying to CRUD user information");
return result;
return ErrorResponse.serverError("Exception occurred while trying to CRUD user information");
}
}
Loading

0 comments on commit df7b3e2

Please sign in to comment.