提交 59c1038e 编写于 作者: wu-sheng's avatar wu-sheng

Refactor codes, and add comments.

上级 45a5d125
......@@ -2,7 +2,6 @@ package com.a.eye.skywalking.routing.http;
import com.a.eye.skywalking.logging.api.ILog;
import com.a.eye.skywalking.logging.api.LogManager;
import com.a.eye.skywalking.network.dependencies.com.google.gson.Gson;
import com.a.eye.skywalking.routing.http.module.ResponseMessage;
import java.io.IOException;
......@@ -11,20 +10,19 @@ import java.util.Map;
import fi.iki.elonen.NanoHTTPD;
import static com.a.eye.skywalking.routing.http.module.ResponseMessage.REQUEST_METHOD_NOT_SUPPORT;
import static com.a.eye.skywalking.routing.http.module.ResponseMessage.GET_NOT_SUPPORT;
import static com.a.eye.skywalking.routing.http.module.ResponseMessage.SERVER_ERROR;
public class RestfulAPIService extends NanoHTTPD {
public static final String JSON_MIME_TYPE = "application/json";
private static ILog logger = LogManager.getLogger(RestfulAPIService.class);
private static final SpanStorageController spanController = new SpanStorageController();
public static final String JSON_MIME_TYPE = "application/json";
private ILog logger = LogManager.getLogger(RestfulAPIService.class);
public RestfulAPIService(String host, int port) {
super(host, port);
}
public void doStart() throws IOException {
try {
start(NanoHTTPD.SOCKET_READ_TIMEOUT, false);
......@@ -39,10 +37,10 @@ public class RestfulAPIService extends NanoHTTPD {
public Response serve(IHTTPSession session) {
if (session.getMethod() != Method.POST) {
return newFixedLengthResponse(Response.Status.OK, JSON_MIME_TYPE,
new Gson().toJson(REQUEST_METHOD_NOT_SUPPORT));
String.valueOf(GET_NOT_SUPPORT));
}
ResponseMessage responseMessage = ResponseMessage.URL_NOT_FOUND;
ResponseMessage responseMessage = ResponseMessage.NOT_FOUND;
try {
String postData = getPostData(session);
if (spanController.isAddAckSpanURI(session.getUri())) {
......@@ -58,8 +56,7 @@ public class RestfulAPIService extends NanoHTTPD {
responseMessage = SERVER_ERROR;
}
return newFixedLengthResponse(Response.Status.OK, JSON_MIME_TYPE, new Gson().toJson
(responseMessage));
return newFixedLengthResponse(Response.Status.OK, JSON_MIME_TYPE, String.valueOf(responseMessage));
}
/**
......
......@@ -9,13 +9,16 @@ import java.util.Map;
/**
* Ack span module
* <p>
* All fields in this class will be initialized by {@link com.google.gson.Gson#fromJson(String, Class)},
* ignore the un-assign values warning.
*/
public class AckSpanModule {
private String traceId;
private String parentLevelId = "";
private int levelId = 0;
private int levelId = 0;
private long cost;
private int routeKey;
private int routeKey;
private Map<String, String> tags;
......@@ -24,16 +27,15 @@ public class AckSpanModule {
return null;
}
return AckSpan.newBuilder().putAllTags(tags).setLevelId(levelId).setParentLevel
(parentLevelId).setRouteKey(routeKey).setCost(cost)
.setTraceId(TraceIdUtil.toTraceId(traceId)).build();
return AckSpan.newBuilder().putAllTags(tags).setLevelId(levelId).setParentLevel(parentLevelId)
.setRouteKey(routeKey).setCost(cost).setTraceId(TraceIdUtil.toTraceId(traceId)).build();
}
private boolean illegalAckSpan() {
if (StringUtil.isEmpty(traceId)) {
return true;
}
if (tags.isEmpty()){
if (tags.isEmpty()) {
return true;
}
return false;
......
......@@ -9,22 +9,24 @@ import java.util.Map;
/**
* request span module
* <p>
* All fields in this class will be initialized by {@link com.google.gson.Gson#fromJson(String, Class)},
* ignore the un-assign values warning.
*/
public class RequestSpanModule {
private String traceId;
private String parentLevelId = "";
private int levelId;
private long startTime;
private int routeKey;
private int levelId;
private long startTime;
private int routeKey;
private Map<String, String> tags;
public RequestSpan convertToGRPCModule() {
if (illegalRequestSpan()) {
return null;
}
return RequestSpan.newBuilder().putAllTags(tags).setLevelId(levelId).setParentLevel
(parentLevelId).setRouteKey(routeKey).setStartDate(startTime)
.setTraceId(TraceIdUtil.toTraceId(traceId)).build();
return RequestSpan.newBuilder().putAllTags(tags).setLevelId(levelId).setParentLevel(parentLevelId)
.setRouteKey(routeKey).setStartDate(startTime).setTraceId(TraceIdUtil.toTraceId(traceId)).build();
}
......@@ -32,7 +34,7 @@ public class RequestSpanModule {
if (StringUtil.isEmpty(traceId)) {
return true;
}
if (tags.isEmpty()){
if (tags.isEmpty()) {
return true;
}
return false;
......
package com.a.eye.skywalking.routing.http.module;
import com.google.gson.JsonObject;
import com.a.eye.skywalking.network.dependencies.com.google.gson.Gson;
public class ResponseMessage {
public static final ResponseMessage OK = new ResponseMessage(200, "Store success");
public static final ResponseMessage REQUEST_METHOD_NOT_SUPPORT = new ResponseMessage(403, "Request method " +
"not support");
public static final ResponseMessage SERVER_ERROR = new ResponseMessage(500, "Server error");
public static final ResponseMessage URL_NOT_FOUND = new ResponseMessage(404, "Not found");
/**
* A {@link ResponseMessage} represent a status code and response messages for http-service.
* <p>
* Created by wusheng on 2017/1/13.
*/
public enum ResponseMessage {
/**
* Request span or Ack Span are received and parsed without any errors.
*/
OK(200, "Store success"),
/**
* Request a no-supported service.
*/
GET_NOT_SUPPORT(405, "Get method not support"),
/**
* An internal error occurs.
*/
SERVER_ERROR(500, "Server error"),
/**
* No service found. Also mean not provide this service.
*/
NOT_FOUND(404, "Not found");
/**
* Response code:
* 200 -- store success
* 403 -- request method not support
* 500 -- server error
* 404 -- not found
* The {@link String} represents the return message of the http services.
* It is in the JSON format, and formatted by {@link com.google.gson.Gson}.
*/
private int code;
private String message;
ResponseMessage(int code, String message) {
this.code = code;
this.message = message;
JsonObject messageFormatter = new JsonObject();
messageFormatter.addProperty("code", code);
messageFormatter.addProperty("message", message);
this.message = messageFormatter.toString();
}
public int getCode() {
return code;
/**
* @return the return message of the http services.
*/
@Override
public String toString() {
return message;
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册