Skip to content

Commit

Permalink
[#8806] Apply ResponseEntity to CodeResult
Browse files Browse the repository at this point in the history
  • Loading branch information
emeroad committed Apr 26, 2022
1 parent 89b4570 commit 13763b7
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,13 @@
import com.navercorp.pinpoint.web.response.CodeResult;
import org.apache.thrift.TBase;
import org.apache.thrift.TException;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

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

/**
Expand All @@ -50,10 +48,6 @@
@RestController
@RequestMapping("/agent")
public class AgentCommandController {

private static final int CODE_SUCCESS = 0;
private static final int CODE_FAIL = -1;

private final ConfigProperties webProperties;
private final AgentService agentService;

Expand All @@ -63,18 +57,18 @@ public AgentCommandController(ConfigProperties webProperties, AgentService agent
}

@GetMapping(value = "/activeThreadDump")
public CodeResult getActiveThreadDump(@RequestParam(value = "applicationName") String applicationName,
public ResponseEntity<CodeResult> getActiveThreadDump(@RequestParam(value = "applicationName") String applicationName,
@RequestParam(value = "agentId") String agentId,
@RequestParam(value = "limit", required = false, defaultValue = "-1") int limit,
@RequestParam(value = "threadName", required = false) String[] threadNameList,
@RequestParam(value = "localTraceId", required = false) Long[] localTraceIdList) throws TException {
@RequestParam(value = "threadName", required = false) List<String> threadNameList,
@RequestParam(value = "localTraceId", required = false) List<Long> localTraceIdList) {
if (!webProperties.isEnableActiveThreadDump()) {
return new CodeResult(CODE_FAIL, "Disable activeThreadDump option. 'config.enable.activeThreadDump=false'");
return CodeResult.serverError("Disable activeThreadDump option. 'config.enable.activeThreadDump=false'");
}

AgentInfo agentInfo = agentService.getAgentInfo(applicationName, agentId);
if (agentInfo == null) {
return new CodeResult(CODE_FAIL, String.format("Can't find suitable Agent(%s/%s)", applicationName, agentId));
return CodeResult.serverError(String.format("Can't find suitable Agent(%s/%s)", applicationName, agentId));
}

TCmdActiveThreadDump threadDump = new TCmdActiveThreadDump();
Expand All @@ -83,10 +77,10 @@ public CodeResult getActiveThreadDump(@RequestParam(value = "applicationName") S
}

if (threadNameList != null) {
threadDump.setThreadNameList(Arrays.asList(threadNameList));
threadDump.setThreadNameList(threadNameList);
}
if (localTraceIdList != null) {
threadDump.setLocalTraceIdList(Arrays.asList(localTraceIdList));
threadDump.setLocalTraceIdList(localTraceIdList);
}

try {
Expand All @@ -100,13 +94,13 @@ public CodeResult getActiveThreadDump(@RequestParam(value = "applicationName") S
AgentActiveThreadDumpFactory factory = new AgentActiveThreadDumpFactory();
AgentActiveThreadDumpList activeThreadDumpList = factory.create1(activeThreadDumps);

Map<String, Object> responseData = createResponseData(activeThreadDumpList, activeThreadDumpResponse.getType(), activeThreadDumpResponse.getSubType(), activeThreadDumpResponse.getVersion());
return new CodeResult(CODE_SUCCESS, responseData);
ThreadDumpResult responseData = createResponseData(activeThreadDumpList, activeThreadDumpResponse.getType(), activeThreadDumpResponse.getSubType(), activeThreadDumpResponse.getVersion());
return CodeResult.ok(responseData);
}
}
return handleFailedResponse(pinpointRouteResponse);
} catch (TException e) {
return new CodeResult(CODE_FAIL, e.getMessage());
return CodeResult.serverError(e.getMessage());
}
}

Expand All @@ -123,40 +117,64 @@ private boolean isSuccessResponse(PinpointRouteResponse pinpointRouteResponse) {
return true;
}

private Map<String, Object> createResponseData(AgentActiveThreadDumpList activeThreadDumpList, String type, String subType, String version) {
Map<String, Object> response = new HashMap<>(4);
response.put("threadDumpData", activeThreadDumpList);
response.put("type", type);
response.put("subType", subType);
response.put("version", version);
private ThreadDumpResult createResponseData(AgentActiveThreadDumpList activeThreadDumpList, String type, String subType, String version) {
return new ThreadDumpResult(activeThreadDumpList, type, subType, version);
}

public static class ThreadDumpResult {
private final AgentActiveThreadDumpList threadDumpData;
private final String type;
private final String subType;
private final String version;

return response;
public ThreadDumpResult(AgentActiveThreadDumpList threadDumpData, String type, String subType, String version) {
this.threadDumpData = threadDumpData;
this.type = type;
this.subType = subType;
this.version = version;
}

public AgentActiveThreadDumpList getThreadDumpData() {
return threadDumpData;
}

public String getType() {
return type;
}

public String getSubType() {
return subType;
}

public String getVersion() {
return version;
}
}

@GetMapping(value = "/activeThreadLightDump")
public CodeResult getActiveThreadLightDump(@RequestParam(value = "applicationName") String applicationName,
@RequestParam(value = "agentId") String agentId,
@RequestParam(value = "limit", required = false, defaultValue = "-1") int limit,
@RequestParam(value = "threadName", required = false) String[] threadNameList,
@RequestParam(value = "localTraceId", required = false) Long[] localTraceIdList) throws TException {
public ResponseEntity<CodeResult> getActiveThreadLightDump(@RequestParam(value = "applicationName") String applicationName,
@RequestParam(value = "agentId") String agentId,
@RequestParam(value = "limit", required = false, defaultValue = "-1") int limit,
@RequestParam(value = "threadName", required = false) List<String> threadNameList,
@RequestParam(value = "localTraceId", required = false) List<Long> localTraceIdList) {
if (!webProperties.isEnableActiveThreadDump()) {
return new CodeResult(CODE_FAIL, "Disable activeThreadDump option. 'config.enable.activeThreadDump=false'");
return CodeResult.serverError("Disable activeThreadDump option. 'config.enable.activeThreadDump=false'");
}

AgentInfo agentInfo = agentService.getAgentInfo(applicationName, agentId);
if (agentInfo == null) {
return new CodeResult(CODE_FAIL, String.format("Can't find suitable Agent(%s/%s)", applicationName, agentId));
return CodeResult.serverError(String.format("Can't find suitable Agent(%s/%s)", applicationName, agentId));
}

TCmdActiveThreadLightDump threadDump = new TCmdActiveThreadLightDump();
if (limit > 0) {
threadDump.setLimit(limit);
}
if (threadNameList != null) {
threadDump.setThreadNameList(Arrays.asList(threadNameList));
threadDump.setThreadNameList(threadNameList);
}
if (localTraceIdList != null) {
threadDump.setLocalTraceIdList(Arrays.asList(localTraceIdList));
threadDump.setLocalTraceIdList(localTraceIdList);
}

try {
Expand All @@ -170,30 +188,30 @@ public CodeResult getActiveThreadLightDump(@RequestParam(value = "applicationNam
AgentActiveThreadDumpFactory factory = new AgentActiveThreadDumpFactory();
AgentActiveThreadDumpList activeThreadDumpList = factory.create2(activeThreadDumps);

Map<String, Object> responseData = createResponseData(activeThreadDumpList, activeThreadDumpResponse.getType(), activeThreadDumpResponse.getSubType(), activeThreadDumpResponse.getVersion());
return new CodeResult(CODE_SUCCESS, responseData);
ThreadDumpResult responseData = createResponseData(activeThreadDumpList, activeThreadDumpResponse.getType(), activeThreadDumpResponse.getSubType(), activeThreadDumpResponse.getVersion());
return CodeResult.ok( responseData);
}
}
return handleFailedResponse(pinpointRouteResponse);
} catch (TException e) {
return new CodeResult(CODE_FAIL, e.getMessage());
return CodeResult.serverError(e.getMessage());
}
}

private CodeResult handleFailedResponse(PinpointRouteResponse response) {
private ResponseEntity<CodeResult> handleFailedResponse(PinpointRouteResponse response) {
if (response == null) {
return new CodeResult(CODE_FAIL, "response is null");
return CodeResult.serverError("response is null");
}

TRouteResult routeResult = response.getRouteResult();
if (routeResult != TRouteResult.OK) {
return new CodeResult(CODE_FAIL, routeResult.name());
return CodeResult.serverError(routeResult.name());
} else {
TBase<?, ?> tBase = response.getResponse();
if (tBase instanceof TResult) {
return new CodeResult(CODE_FAIL, ((TResult) tBase).getMessage());
return CodeResult.serverError(((TResult) tBase).getMessage());
} else {
return new CodeResult(CODE_FAIL, "unknown");
return CodeResult.serverError("unknown");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
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;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
Expand All @@ -48,10 +49,6 @@
*/
@RestController
public class AgentInfoController {

private static final int CODE_SUCCESS = 0;
private static final int CODE_FAIL = -1;

private final AgentInfoService agentInfoService;

private final AgentEventService agentEventService;
Expand Down Expand Up @@ -182,30 +179,30 @@ public InspectorTimeline getAgentStatusTimeline(
}

@RequestMapping(value = "/isAvailableAgentId")
public CodeResult isAvailableAgentId(@RequestParam("agentId") String agentId) {
public ResponseEntity<CodeResult> isAvailableAgentId(@RequestParam("agentId") String agentId) {
final IdValidateUtils.CheckResult result = IdValidateUtils.checkId(agentId, PinpointConstants.AGENT_ID_MAX_LEN);
if (result == IdValidateUtils.CheckResult.FAIL_LENGTH) {
return new CodeResult(CODE_FAIL, "length range is 1 ~ 24");
return CodeResult.badRequest("length range is 1 ~ 24");
}
if (result == IdValidateUtils.CheckResult.FAIL_PATTERN) {
return new CodeResult(CODE_FAIL, "invalid pattern(" + IdValidateUtils.ID_PATTERN_VALUE + ")");
return CodeResult.badRequest("invalid pattern(" + IdValidateUtils.ID_PATTERN_VALUE + ")");
}

if (agentInfoService.isExistAgentId(agentId)) {
return new CodeResult(CODE_FAIL, "already exist agentId");
return CodeResult.serverError("already exist agentId");
}

return new CodeResult(CODE_SUCCESS, "OK");
return CodeResult.ok("OK");
}

@RequestMapping(value = "/getAgentInstallationInfo")
public CodeResult getAgentDownloadUrl() {
public ResponseEntity<CodeResult> getAgentDownloadUrl() {
AgentDownloadInfo latestStableAgentDownloadInfo = agentInfoService.getLatestStableAgentDownloadInfo();
if (latestStableAgentDownloadInfo != null) {
return new CodeResult(0, new AgentInstallationInfo(latestStableAgentDownloadInfo));
return CodeResult.ok(new AgentInstallationInfo(latestStableAgentDownloadInfo));
}

return new CodeResult(-1, "can't find suitable download url");
return CodeResult.serverError("can't find suitable download url");
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.navercorp.pinpoint.web.service.ApplicationService;
import com.navercorp.pinpoint.web.vo.ApplicationAgentHostList;
import com.navercorp.pinpoint.web.response.CodeResult;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
Expand All @@ -35,9 +36,6 @@
@RestController
public class ApplicationController {

private static final int CODE_SUCCESS = 0;
private static final int CODE_FAIL = -1;

private final AgentInfoService agentInfoService;

private final ApplicationService applicationService;
Expand All @@ -50,33 +48,33 @@ public ApplicationController(AgentInfoService agentInfoService, ApplicationServi
@GetMapping(value = "/getApplicationHostInfo", params = {"!durationDays"})
public ApplicationAgentHostList getApplicationHostInfo (
@RequestParam(value = "offset", required = false, defaultValue = "1") int offset,
@RequestParam(value = "limit", required = false, defaultValue = "100") int limit) throws Exception {
@RequestParam(value = "limit", required = false, defaultValue = "100") int limit) {
return agentInfoService.getApplicationAgentHostList(offset, limit);
}

@GetMapping(value = "/getApplicationHostInfo", params = {"durationDays"})
public ApplicationAgentHostList getApplicationHostInfo (
@RequestParam(value = "offset", required = false, defaultValue = "1") int offset,
@RequestParam(value = "limit", required = false, defaultValue = "100") int limit,
@RequestParam(value = "durationDays") int durationDays) throws Exception {
@RequestParam(value = "durationDays") int durationDays) {
return agentInfoService.getApplicationAgentHostList(offset, limit, durationDays);
}

@RequestMapping(value = "/isAvailableApplicationName")
public CodeResult isAvailableApplicationName(@RequestParam("applicationName") String applicationName) {
public ResponseEntity<CodeResult> isAvailableApplicationName(@RequestParam("applicationName") String applicationName) {
final IdValidateUtils.CheckResult result = IdValidateUtils.checkId(applicationName, PinpointConstants.APPLICATION_NAME_MAX_LEN);
if (result == IdValidateUtils.CheckResult.FAIL_LENGTH) {
return new CodeResult(CODE_FAIL, "length range is 1 ~ 24");
return CodeResult.badRequest("length range is 1 ~ 24");
}
if (result == IdValidateUtils.CheckResult.FAIL_PATTERN) {
return new CodeResult(CODE_FAIL, "invalid pattern(" + IdValidateUtils.ID_PATTERN_VALUE + ")");
return CodeResult.badRequest("invalid pattern(" + IdValidateUtils.ID_PATTERN_VALUE + ")");
}

if (applicationService.isExistApplicationName(applicationName)) {
return new CodeResult(CODE_FAIL, "already exist applicationName");
return CodeResult.serverError("already exist applicationName");
}

return new CodeResult(CODE_SUCCESS, "OK");
return CodeResult.ok("OK");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.apache.thrift.TException;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
Expand All @@ -38,9 +39,6 @@
@RequestMapping("/command")
public class CommandController {

private static final int CODE_SUCCESS = 0;
private static final int CODE_FAIL = -1;

// FIX ME: created for a simple ping/pong test for now
// need a formal set of APIs and proper code

Expand All @@ -53,12 +51,12 @@ public CommandController(AgentService agentService) {
}

@GetMapping(value = "/echo")
public CodeResult echo(@RequestParam("applicationName") String applicationName, @RequestParam("agentId") String agentId,
@RequestParam("startTimeStamp") long startTimeStamp, @RequestParam("message") String message) throws TException {
public ResponseEntity<CodeResult> echo(@RequestParam("applicationName") String applicationName, @RequestParam("agentId") String agentId,
@RequestParam("startTimeStamp") long startTimeStamp, @RequestParam("message") String message) throws TException {

AgentInfo agentInfo = agentService.getAgentInfo(applicationName, agentId, startTimeStamp);
if (agentInfo == null) {
return new CodeResult(CODE_FAIL, String.format("Can't find suitable PinpointServer(%s/%s/%d).", applicationName, agentId, startTimeStamp));
return CodeResult.serverError(String.format("Can't find suitable PinpointServer(%s/%s/%d).", applicationName, agentId, startTimeStamp));
}

TCommandEcho echo = new TCommandEcho();
Expand All @@ -69,19 +67,19 @@ public CodeResult echo(@RequestParam("applicationName") String applicationName,
if (pinpointRouteResponse != null && pinpointRouteResponse.getRouteResult() == TRouteResult.OK) {
TBase<?, ?> result = pinpointRouteResponse.getResponse();
if (result == null) {
return new CodeResult(CODE_FAIL, "result null.");
return CodeResult.serverError("result null.");
} else if (result instanceof TCommandEcho) {
return new CodeResult(CODE_SUCCESS, ((TCommandEcho) result).getMessage());
return CodeResult.ok(((TCommandEcho) result).getMessage());
} else if (result instanceof TResult) {
return new CodeResult(CODE_FAIL, ((TResult) result).getMessage());
return CodeResult.serverError(((TResult) result).getMessage());
} else {
return new CodeResult(CODE_FAIL, result.toString());
return CodeResult.serverError(result.toString());
}
} else {
return new CodeResult(CODE_FAIL, "unknown");
return CodeResult.serverError("unknown");
}
} catch (TException e) {
return new CodeResult(CODE_FAIL, e.getMessage());
return CodeResult.serverError(e.getMessage());
}
}

Expand Down
Loading

0 comments on commit 13763b7

Please sign in to comment.