in core/src/main/java/com/taobao/arthas/core/shell/term/impl/http/api/HttpApiHandler.java [116:182]
private ApiResponse processRequest(ChannelHandlerContext ctx, ApiRequest apiRequest) {
String actionStr = apiRequest.getAction();
try {
if (StringUtils.isBlank(actionStr)) {
throw new ApiException("'action' is required");
}
ApiAction action;
try {
action = ApiAction.valueOf(actionStr.trim().toUpperCase());
} catch (IllegalArgumentException e) {
throw new ApiException("unknown action: " + actionStr);
}
//no session required
if (ApiAction.INIT_SESSION.equals(action)) {
return processInitSessionRequest(apiRequest);
}
//required session
Session session = null;
boolean allowNullSession = ApiAction.EXEC.equals(action);
String sessionId = apiRequest.getSessionId();
if (StringUtils.isBlank(sessionId)) {
if (!allowNullSession) {
throw new ApiException("'sessionId' is required");
}
} else {
session = sessionManager.getSession(sessionId);
if (session == null) {
throw new ApiException("session not found: " + sessionId);
}
sessionManager.updateAccessTime(session);
}
// 标记所谓的一次性session
if (session == null) {
session = sessionManager.createSession();
session.put(ONETIME_SESSION_KEY, new Object());
}
// 请求到达这里,如果有需要鉴权,则已经在前面的handler里处理过了
// 如果有鉴权取到的 Subject,则传递到 arthas的session里
HttpSession httpSession = HttpSessionManager.getHttpSessionFromContext(ctx);
if (httpSession != null) {
Object subject = httpSession.getAttribute(ArthasConstants.SUBJECT_KEY);
if (subject != null) {
session.put(ArthasConstants.SUBJECT_KEY, subject);
}
}
//dispatch requests
ApiResponse response = dispatchRequest(action, apiRequest, session);
if (response != null) {
return response;
}
} catch (ApiException e) {
logger.info("process http api request failed: {}", e.getMessage());
return createResponse(ApiState.FAILED, e.getMessage());
} catch (Throwable e) {
logger.error("process http api request failed: " + e.getMessage(), e);
return createResponse(ApiState.FAILED, "process http api request failed: " + e.getMessage());
}
return createResponse(ApiState.REFUSED, "Unsupported action: " + actionStr);
}