提交 cffa3e1d 编写于 作者: A ascrutae

修复部分问题

上级 59c1038e
...@@ -27,4 +27,13 @@ public class TraceIdUtil { ...@@ -27,4 +27,13 @@ public class TraceIdUtil {
} }
public static boolean isIllegalTraceId(String traceId){
if (StringUtil.isEmpty(traceId)){
return true;
}
return traceId.split("\\.").length != 6;
}
} }
...@@ -36,7 +36,7 @@ public class RestfulAPIService extends NanoHTTPD { ...@@ -36,7 +36,7 @@ public class RestfulAPIService extends NanoHTTPD {
@Override @Override
public Response serve(IHTTPSession session) { public Response serve(IHTTPSession session) {
if (session.getMethod() != Method.POST) { 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)); String.valueOf(GET_NOT_SUPPORT));
} }
...@@ -56,7 +56,8 @@ public class RestfulAPIService extends NanoHTTPD { ...@@ -56,7 +56,8 @@ public class RestfulAPIService extends NanoHTTPD {
responseMessage = SERVER_ERROR; responseMessage = SERVER_ERROR;
} }
return newFixedLengthResponse(Response.Status.OK, JSON_MIME_TYPE, String.valueOf(responseMessage)); return newFixedLengthResponse(responseMessage.getStatus(), JSON_MIME_TYPE, String.valueOf
(responseMessage));
} }
/** /**
......
...@@ -4,21 +4,20 @@ import com.a.eye.skywalking.network.grpc.AckSpan; ...@@ -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.StringUtil;
import com.a.eye.skywalking.util.TraceIdUtil; import com.a.eye.skywalking.util.TraceIdUtil;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import static com.a.eye.skywalking.util.TraceIdUtil.isIllegalTraceId;
/** /**
* Ack span module * Ack span module <p> All fields in this class will be initialized by {@link
* <p> * com.google.gson.Gson#fromJson(String, Class)}, ignore the un-assign values warning.
* 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 { public class AckSpanModule {
private String traceId; private String traceId;
private String parentLevelId = ""; private String parentLevelId;
private int levelId = 0; private int levelId = 0;
private long cost; private long cost;
private int routeKey; private int routeKey;
private Map<String, String> tags; private Map<String, String> tags;
...@@ -32,9 +31,10 @@ public class AckSpanModule { ...@@ -32,9 +31,10 @@ public class AckSpanModule {
} }
private boolean illegalAckSpan() { private boolean illegalAckSpan() {
if (StringUtil.isEmpty(traceId)) { if (isIllegalTraceId(traceId)) {
return true; return true;
} }
if (tags.isEmpty()) { if (tags.isEmpty()) {
return true; return true;
} }
......
...@@ -4,21 +4,20 @@ import com.a.eye.skywalking.network.grpc.RequestSpan; ...@@ -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.StringUtil;
import com.a.eye.skywalking.util.TraceIdUtil; import com.a.eye.skywalking.util.TraceIdUtil;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import static com.a.eye.skywalking.util.TraceIdUtil.isIllegalTraceId;
/** /**
* request span module * request span module <p> All fields in this class will be initialized by {@link
* <p> * com.google.gson.Gson#fromJson(String, Class)}, ignore the un-assign values warning.
* 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 { public class RequestSpanModule {
private String traceId; private String traceId;
private String parentLevelId = ""; private String parentLevelId;
private int levelId; private int levelId;
private long startTime; private long startTime;
private int routeKey; private int routeKey;
private Map<String, String> tags; private Map<String, String> tags;
public RequestSpan convertToGRPCModule() { public RequestSpan convertToGRPCModule() {
...@@ -31,9 +30,10 @@ public class RequestSpanModule { ...@@ -31,9 +30,10 @@ public class RequestSpanModule {
} }
private boolean illegalRequestSpan() { private boolean illegalRequestSpan() {
if (StringUtil.isEmpty(traceId)) { if (isIllegalTraceId(traceId)) {
return true; return true;
} }
if (tags.isEmpty()) { if (tags.isEmpty()) {
return true; return true;
} }
......
...@@ -2,6 +2,9 @@ package com.a.eye.skywalking.routing.http.module; ...@@ -2,6 +2,9 @@ package com.a.eye.skywalking.routing.http.module;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import fi.iki.elonen.NanoHTTPD;
/** /**
* A {@link ResponseMessage} represent a status code and response messages for http-service. * A {@link ResponseMessage} represent a status code and response messages for http-service.
* <p> * <p>
...@@ -11,19 +14,19 @@ public enum ResponseMessage { ...@@ -11,19 +14,19 @@ public enum ResponseMessage {
/** /**
* Request span or Ack Span are received and parsed without any errors. * 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. * 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. * 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. * 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. * The {@link String} represents the return message of the http services.
...@@ -31,13 +34,24 @@ public enum ResponseMessage { ...@@ -31,13 +34,24 @@ public enum ResponseMessage {
*/ */
private String message; 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(); JsonObject messageFormatter = new JsonObject();
messageFormatter.addProperty("code", code); messageFormatter.addProperty("code", status.getRequestStatus());
messageFormatter.addProperty("message", message); messageFormatter.addProperty("message", message);
this.message = messageFormatter.toString(); this.message = messageFormatter.toString();
} }
public NanoHTTPD.Response.IStatus getStatus() {
return status;
}
/** /**
* @return the return message of the http services. * @return the return message of the http services.
*/ */
......
package com.a.eye.skywalking.routing.http; 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. * Created by xin on 2017/1/10.
...@@ -16,8 +17,9 @@ public class ResponseResult { ...@@ -16,8 +17,9 @@ public class ResponseResult {
this.responseBody = responseBody; this.responseBody = responseBody;
} }
public ResponseMessage getResponseMessage() { public JsonObject getResponseMessage() {
return new Gson().fromJson(responseBody, ResponseMessage.class); JsonParser jsonParser = new JsonParser();
return (JsonObject) jsonParser.parse(responseBody);
} }
public int getStatusCode(){ public int getStatusCode(){
......
package com.a.eye.skywalking.routing.http; 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 com.a.eye.skywalking.routing.http.module.ResponseMessage;
import org.junit.After; import org.junit.After;
...@@ -28,17 +31,15 @@ public class RestfulAPIServiceTest { ...@@ -28,17 +31,15 @@ public class RestfulAPIServiceTest {
@Test @Test
public void testRequestMethodWithGet() throws IOException { public void testRequestMethodWithGet() throws IOException {
ResponseResult responseResult = HttpClientUtil.doGet(REST_URL_PREFIX); ResponseResult responseResult = HttpClientUtil.doGet(REST_URL_PREFIX);
assertEquals(200, responseResult.getStatusCode()); assertEquals(405, responseResult.getStatusCode());
ResponseMessage responseMessage = responseResult.getResponseMessage(); validateResponseCode(405, responseResult);
assertEquals(403, responseMessage.getCode());
} }
@Test @Test
public void testRequestMethodWithWrongURL() throws IOException { public void testRequestMethodWithWrongURL() throws IOException {
ResponseResult responseResult = HttpClientUtil.doPost(REST_URL_PREFIX, "{}"); ResponseResult responseResult = HttpClientUtil.doPost(REST_URL_PREFIX, "{}");
assertEquals(200, responseResult.getStatusCode()); assertEquals(404, responseResult.getStatusCode());
ResponseMessage responseMessage = responseResult.getResponseMessage(); validateResponseCode(404, responseResult);
assertEquals(404, responseMessage.getCode());
} }
@Test @Test
...@@ -58,8 +59,7 @@ public class RestfulAPIServiceTest { ...@@ -58,8 +59,7 @@ public class RestfulAPIServiceTest {
" }\n" + " }\n" +
"}]"); "}]");
assertEquals(200, responseResult.getStatusCode()); assertEquals(200, responseResult.getStatusCode());
ResponseMessage responseMessage = responseResult.getResponseMessage(); validateResponseCode(200, responseResult);
assertEquals(200, responseMessage.getCode());
} }
@Test @Test
...@@ -84,11 +84,9 @@ public class RestfulAPIServiceTest { ...@@ -84,11 +84,9 @@ public class RestfulAPIServiceTest {
"}]"); "}]");
assertEquals(200, responseResult.getStatusCode()); assertEquals(200, responseResult.getStatusCode());
ResponseMessage responseMessage = responseResult.getResponseMessage(); validateResponseCode(200, responseResult);
assertEquals(200, responseMessage.getCode());
} }
@Test @Test
public void testAddWithErrorRequestSpanJson() throws IOException { public void testAddWithErrorRequestSpanJson() throws IOException {
ResponseResult responseResult = HttpClientUtil.doPost ResponseResult responseResult = HttpClientUtil.doPost
...@@ -110,9 +108,14 @@ public class RestfulAPIServiceTest { ...@@ -110,9 +108,14 @@ public class RestfulAPIServiceTest {
" }\n" + " }\n" +
"}"); "}");
assertEquals(200, responseResult.getStatusCode()); assertEquals(500, responseResult.getStatusCode());
ResponseMessage responseMessage = responseResult.getResponseMessage(); validateResponseCode(500, responseResult);
assertEquals(500, responseMessage.getCode()); }
private void validateResponseCode(int expectedCode, ResponseResult responseResult) {
JsonObject responseJson = responseResult.getResponseMessage();
assertEquals(expectedCode, responseJson.get("code").getAsInt());
} }
@After @After
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册