提交 cffa3e1d 编写于 作者: A ascrutae

修复部分问题

上级 59c1038e
......@@ -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 {
@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));
}
/**
......
......@@ -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
* <p>
* 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 <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 String parentLevelId;
private int levelId = 0;
private long cost;
private int routeKey;
private int routeKey;
private Map<String, String> 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;
}
......
......@@ -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
* <p>
* 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 <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 String parentLevelId;
private int levelId;
private long startTime;
private int routeKey;
private Map<String, String> 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;
}
......
......@@ -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.
* <p>
......@@ -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.
*/
......
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(){
......
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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册