diff --git a/skywalking-storage-center/skywalking-routing/src/main/java/com/a/eye/skywalking/routing/http/RestfulAPIService.java b/skywalking-storage-center/skywalking-routing/src/main/java/com/a/eye/skywalking/routing/http/RestfulAPIService.java index 8dfa6092de7799bae19d69c66c461a95a00e0b04..95ce78e7cb5671d4605e47d6239c1d2846838830 100644 --- a/skywalking-storage-center/skywalking-routing/src/main/java/com/a/eye/skywalking/routing/http/RestfulAPIService.java +++ b/skywalking-storage-center/skywalking-routing/src/main/java/com/a/eye/skywalking/routing/http/RestfulAPIService.java @@ -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)); } /** diff --git a/skywalking-storage-center/skywalking-routing/src/main/java/com/a/eye/skywalking/routing/http/module/AckSpanModule.java b/skywalking-storage-center/skywalking-routing/src/main/java/com/a/eye/skywalking/routing/http/module/AckSpanModule.java index 471f5d12b5845087e7c1960a4dc8285f2a5436f0..1dfd75aa697b0c561bb672c4e07417fd43479473 100644 --- a/skywalking-storage-center/skywalking-routing/src/main/java/com/a/eye/skywalking/routing/http/module/AckSpanModule.java +++ b/skywalking-storage-center/skywalking-routing/src/main/java/com/a/eye/skywalking/routing/http/module/AckSpanModule.java @@ -9,13 +9,16 @@ import java.util.Map; /** * Ack span module + *

+ * 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 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; diff --git a/skywalking-storage-center/skywalking-routing/src/main/java/com/a/eye/skywalking/routing/http/module/RequestSpanModule.java b/skywalking-storage-center/skywalking-routing/src/main/java/com/a/eye/skywalking/routing/http/module/RequestSpanModule.java index 7cbd20b73dfae475a8e73056cbe0d3ddde909333..8442bbefc59813bb0984ccce6dbd8e248a3e5fa3 100644 --- a/skywalking-storage-center/skywalking-routing/src/main/java/com/a/eye/skywalking/routing/http/module/RequestSpanModule.java +++ b/skywalking-storage-center/skywalking-routing/src/main/java/com/a/eye/skywalking/routing/http/module/RequestSpanModule.java @@ -9,22 +9,24 @@ import java.util.Map; /** * request span module + *

+ * 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 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; diff --git a/skywalking-storage-center/skywalking-routing/src/main/java/com/a/eye/skywalking/routing/http/module/ResponseMessage.java b/skywalking-storage-center/skywalking-routing/src/main/java/com/a/eye/skywalking/routing/http/module/ResponseMessage.java index 38fad22e5935780434179d8ec5bfe1130c9a9aa3..d90e27d5c03df11f23502b32e6fb0297484ecb32 100644 --- a/skywalking-storage-center/skywalking-routing/src/main/java/com/a/eye/skywalking/routing/http/module/ResponseMessage.java +++ b/skywalking-storage-center/skywalking-routing/src/main/java/com/a/eye/skywalking/routing/http/module/ResponseMessage.java @@ -1,31 +1,48 @@ 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. + *

+ * 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; } }