diff --git a/skywalking-commons/skywalking-util/src/main/java/com/a/eye/skywalking/util/TraceIdUtil.java b/skywalking-commons/skywalking-util/src/main/java/com/a/eye/skywalking/util/TraceIdUtil.java index e431512940901704f9e5e64cd56343c0c74bbe04..cc7d90d878a9e3ef597c2b773a2d6ff5f9bfd8af 100644 --- a/skywalking-commons/skywalking-util/src/main/java/com/a/eye/skywalking/util/TraceIdUtil.java +++ b/skywalking-commons/skywalking-util/src/main/java/com/a/eye/skywalking/util/TraceIdUtil.java @@ -27,4 +27,13 @@ public class TraceIdUtil { } + public static boolean isIllegalTraceId(String traceId){ + if (StringUtil.isEmpty(traceId)){ + return true; + } + + return traceId.split("\\.").length != 6; + } + + } 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 95ce78e7cb5671d4605e47d6239c1d2846838830..54c28960d079cfb9592cd8737f2287fc033e70cb 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 @@ -36,7 +36,7 @@ public class RestfulAPIService extends NanoHTTPD { @Override public Response serve(IHTTPSession session) { if (session.getMethod() != Method.POST) { - return newFixedLengthResponse(Response.Status.OK, JSON_MIME_TYPE, + return newFixedLengthResponse(GET_NOT_SUPPORT.getStatus(), JSON_MIME_TYPE, String.valueOf(GET_NOT_SUPPORT)); } @@ -56,7 +56,8 @@ public class RestfulAPIService extends NanoHTTPD { responseMessage = SERVER_ERROR; } - return newFixedLengthResponse(Response.Status.OK, JSON_MIME_TYPE, String.valueOf(responseMessage)); + return newFixedLengthResponse(responseMessage.getStatus(), 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 1dfd75aa697b0c561bb672c4e07417fd43479473..6f62352b1f3fe3ee2edba6a63425e9fd2574717c 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 @@ -4,21 +4,20 @@ import com.a.eye.skywalking.network.grpc.AckSpan; import com.a.eye.skywalking.util.StringUtil; import com.a.eye.skywalking.util.TraceIdUtil; -import java.util.HashMap; import java.util.Map; +import static com.a.eye.skywalking.util.TraceIdUtil.isIllegalTraceId; + /** - * 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. + * 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 String parentLevelId; + private int levelId = 0; private long cost; - private int routeKey; + private int routeKey; private Map tags; @@ -32,9 +31,10 @@ public class AckSpanModule { } private boolean illegalAckSpan() { - if (StringUtil.isEmpty(traceId)) { + if (isIllegalTraceId(traceId)) { return true; } + if (tags.isEmpty()) { return true; } 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 8442bbefc59813bb0984ccce6dbd8e248a3e5fa3..7a3a3293a169a5c5532ec57c1e805e7cdca127a0 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 @@ -4,21 +4,20 @@ import com.a.eye.skywalking.network.grpc.RequestSpan; import com.a.eye.skywalking.util.StringUtil; import com.a.eye.skywalking.util.TraceIdUtil; -import java.util.HashMap; import java.util.Map; +import static com.a.eye.skywalking.util.TraceIdUtil.isIllegalTraceId; + /** - * 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. + * 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 String parentLevelId; + private int levelId; + private long startTime; + private int routeKey; private Map tags; public RequestSpan convertToGRPCModule() { @@ -31,9 +30,10 @@ public class RequestSpanModule { } private boolean illegalRequestSpan() { - if (StringUtil.isEmpty(traceId)) { + if (isIllegalTraceId(traceId)) { return true; } + if (tags.isEmpty()) { return true; } 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 d90e27d5c03df11f23502b32e6fb0297484ecb32..c93ebf4c50221b0647d5afc7365ca950ac52268f 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 @@ -2,6 +2,9 @@ package com.a.eye.skywalking.routing.http.module; import com.google.gson.JsonObject; +import fi.iki.elonen.NanoHTTPD; + + /** * A {@link ResponseMessage} represent a status code and response messages for http-service. *

@@ -11,19 +14,19 @@ public enum ResponseMessage { /** * Request span or Ack Span are received and parsed without any errors. */ - OK(200, "Store success"), + OK(NanoHTTPD.Response.Status.OK, "Store success"), /** * Request a no-supported service. */ - GET_NOT_SUPPORT(405, "Get method not support"), + GET_NOT_SUPPORT(NanoHTTPD.Response.Status.METHOD_NOT_ALLOWED, "Get method not support"), /** * An internal error occurs. */ - SERVER_ERROR(500, "Server error"), + SERVER_ERROR(NanoHTTPD.Response.Status.INTERNAL_ERROR, "Server error"), /** * No service found. Also mean not provide this service. */ - NOT_FOUND(404, "Not found"); + NOT_FOUND(NanoHTTPD.Response.Status.NOT_FOUND, "Not found"); /** * The {@link String} represents the return message of the http services. @@ -31,13 +34,24 @@ public enum ResponseMessage { */ private String message; - ResponseMessage(int code, String message) { + + /** + * + */ + private NanoHTTPD.Response.IStatus status; + + ResponseMessage(NanoHTTPD.Response.Status status, String message) { + this.status = status; JsonObject messageFormatter = new JsonObject(); - messageFormatter.addProperty("code", code); + messageFormatter.addProperty("code", status.getRequestStatus()); messageFormatter.addProperty("message", message); this.message = messageFormatter.toString(); } + public NanoHTTPD.Response.IStatus getStatus() { + return status; + } + /** * @return the return message of the http services. */ diff --git a/skywalking-storage-center/skywalking-routing/src/test/java/com/a/eye/skywalking/routing/http/ResponseResult.java b/skywalking-storage-center/skywalking-routing/src/test/java/com/a/eye/skywalking/routing/http/ResponseResult.java index 4a7a026e8d41057168f9ef91a2061c766a30128f..70e4a8b30c017d5f15b77adb8378db1ee0f00e8c 100644 --- a/skywalking-storage-center/skywalking-routing/src/test/java/com/a/eye/skywalking/routing/http/ResponseResult.java +++ b/skywalking-storage-center/skywalking-routing/src/test/java/com/a/eye/skywalking/routing/http/ResponseResult.java @@ -1,8 +1,9 @@ package com.a.eye.skywalking.routing.http; -import com.google.gson.Gson; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; -import com.a.eye.skywalking.routing.http.module.ResponseMessage; /** * Created by xin on 2017/1/10. @@ -16,8 +17,9 @@ public class ResponseResult { this.responseBody = responseBody; } - public ResponseMessage getResponseMessage() { - return new Gson().fromJson(responseBody, ResponseMessage.class); + public JsonObject getResponseMessage() { + JsonParser jsonParser = new JsonParser(); + return (JsonObject) jsonParser.parse(responseBody); } public int getStatusCode(){ diff --git a/skywalking-storage-center/skywalking-routing/src/test/java/com/a/eye/skywalking/routing/http/RestfulAPIServiceTest.java b/skywalking-storage-center/skywalking-routing/src/test/java/com/a/eye/skywalking/routing/http/RestfulAPIServiceTest.java index 13515b5114c7763f068295e93a8c9120633161c8..2f7905242f6d99749370ebedc3d3573757998853 100644 --- a/skywalking-storage-center/skywalking-routing/src/test/java/com/a/eye/skywalking/routing/http/RestfulAPIServiceTest.java +++ b/skywalking-storage-center/skywalking-routing/src/test/java/com/a/eye/skywalking/routing/http/RestfulAPIServiceTest.java @@ -1,6 +1,9 @@ package com.a.eye.skywalking.routing.http; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; + import com.a.eye.skywalking.routing.http.module.ResponseMessage; import org.junit.After; @@ -28,17 +31,15 @@ public class RestfulAPIServiceTest { @Test public void testRequestMethodWithGet() throws IOException { ResponseResult responseResult = HttpClientUtil.doGet(REST_URL_PREFIX); - assertEquals(200, responseResult.getStatusCode()); - ResponseMessage responseMessage = responseResult.getResponseMessage(); - assertEquals(403, responseMessage.getCode()); + assertEquals(405, responseResult.getStatusCode()); + validateResponseCode(405, responseResult); } @Test public void testRequestMethodWithWrongURL() throws IOException { ResponseResult responseResult = HttpClientUtil.doPost(REST_URL_PREFIX, "{}"); - assertEquals(200, responseResult.getStatusCode()); - ResponseMessage responseMessage = responseResult.getResponseMessage(); - assertEquals(404, responseMessage.getCode()); + assertEquals(404, responseResult.getStatusCode()); + validateResponseCode(404, responseResult); } @Test @@ -58,8 +59,7 @@ public class RestfulAPIServiceTest { " }\n" + "}]"); assertEquals(200, responseResult.getStatusCode()); - ResponseMessage responseMessage = responseResult.getResponseMessage(); - assertEquals(200, responseMessage.getCode()); + validateResponseCode(200, responseResult); } @Test @@ -84,11 +84,9 @@ public class RestfulAPIServiceTest { "}]"); assertEquals(200, responseResult.getStatusCode()); - ResponseMessage responseMessage = responseResult.getResponseMessage(); - assertEquals(200, responseMessage.getCode()); + validateResponseCode(200, responseResult); } - @Test public void testAddWithErrorRequestSpanJson() throws IOException { ResponseResult responseResult = HttpClientUtil.doPost @@ -110,9 +108,14 @@ public class RestfulAPIServiceTest { " }\n" + "}"); - assertEquals(200, responseResult.getStatusCode()); - ResponseMessage responseMessage = responseResult.getResponseMessage(); - assertEquals(500, responseMessage.getCode()); + assertEquals(500, responseResult.getStatusCode()); + validateResponseCode(500, responseResult); + } + + + private void validateResponseCode(int expectedCode, ResponseResult responseResult) { + JsonObject responseJson = responseResult.getResponseMessage(); + assertEquals(expectedCode, responseJson.get("code").getAsInt()); } @After