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

Refactor codes, and add comments.

上级 45a5d125
...@@ -2,7 +2,6 @@ package com.a.eye.skywalking.routing.http; ...@@ -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.ILog;
import com.a.eye.skywalking.logging.api.LogManager; 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 com.a.eye.skywalking.routing.http.module.ResponseMessage;
import java.io.IOException; import java.io.IOException;
...@@ -11,20 +10,19 @@ import java.util.Map; ...@@ -11,20 +10,19 @@ import java.util.Map;
import fi.iki.elonen.NanoHTTPD; 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; import static com.a.eye.skywalking.routing.http.module.ResponseMessage.SERVER_ERROR;
public class RestfulAPIService extends NanoHTTPD { 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(); 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) { public RestfulAPIService(String host, int port) {
super(host, port); super(host, port);
} }
public void doStart() throws IOException { public void doStart() throws IOException {
try { try {
start(NanoHTTPD.SOCKET_READ_TIMEOUT, false); start(NanoHTTPD.SOCKET_READ_TIMEOUT, false);
...@@ -39,10 +37,10 @@ public class RestfulAPIService extends NanoHTTPD { ...@@ -39,10 +37,10 @@ public class RestfulAPIService extends NanoHTTPD {
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(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 { try {
String postData = getPostData(session); String postData = getPostData(session);
if (spanController.isAddAckSpanURI(session.getUri())) { if (spanController.isAddAckSpanURI(session.getUri())) {
...@@ -58,8 +56,7 @@ public class RestfulAPIService extends NanoHTTPD { ...@@ -58,8 +56,7 @@ public class RestfulAPIService extends NanoHTTPD {
responseMessage = SERVER_ERROR; responseMessage = SERVER_ERROR;
} }
return newFixedLengthResponse(Response.Status.OK, JSON_MIME_TYPE, new Gson().toJson return newFixedLengthResponse(Response.Status.OK, JSON_MIME_TYPE, String.valueOf(responseMessage));
(responseMessage));
} }
/** /**
......
...@@ -9,6 +9,9 @@ import java.util.Map; ...@@ -9,6 +9,9 @@ import java.util.Map;
/** /**
* Ack span module * 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 { public class AckSpanModule {
private String traceId; private String traceId;
...@@ -24,16 +27,15 @@ public class AckSpanModule { ...@@ -24,16 +27,15 @@ public class AckSpanModule {
return null; return null;
} }
return AckSpan.newBuilder().putAllTags(tags).setLevelId(levelId).setParentLevel return AckSpan.newBuilder().putAllTags(tags).setLevelId(levelId).setParentLevel(parentLevelId)
(parentLevelId).setRouteKey(routeKey).setCost(cost) .setRouteKey(routeKey).setCost(cost).setTraceId(TraceIdUtil.toTraceId(traceId)).build();
.setTraceId(TraceIdUtil.toTraceId(traceId)).build();
} }
private boolean illegalAckSpan() { private boolean illegalAckSpan() {
if (StringUtil.isEmpty(traceId)) { if (StringUtil.isEmpty(traceId)) {
return true; return true;
} }
if (tags.isEmpty()){ if (tags.isEmpty()) {
return true; return true;
} }
return false; return false;
......
...@@ -9,6 +9,9 @@ import java.util.Map; ...@@ -9,6 +9,9 @@ import java.util.Map;
/** /**
* request span module * 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 { public class RequestSpanModule {
private String traceId; private String traceId;
...@@ -22,9 +25,8 @@ public class RequestSpanModule { ...@@ -22,9 +25,8 @@ public class RequestSpanModule {
if (illegalRequestSpan()) { if (illegalRequestSpan()) {
return null; return null;
} }
return RequestSpan.newBuilder().putAllTags(tags).setLevelId(levelId).setParentLevel return RequestSpan.newBuilder().putAllTags(tags).setLevelId(levelId).setParentLevel(parentLevelId)
(parentLevelId).setRouteKey(routeKey).setStartDate(startTime) .setRouteKey(routeKey).setStartDate(startTime).setTraceId(TraceIdUtil.toTraceId(traceId)).build();
.setTraceId(TraceIdUtil.toTraceId(traceId)).build();
} }
...@@ -32,7 +34,7 @@ public class RequestSpanModule { ...@@ -32,7 +34,7 @@ public class RequestSpanModule {
if (StringUtil.isEmpty(traceId)) { if (StringUtil.isEmpty(traceId)) {
return true; return true;
} }
if (tags.isEmpty()){ if (tags.isEmpty()) {
return true; return true;
} }
return false; return false;
......
package com.a.eye.skywalking.routing.http.module; package com.a.eye.skywalking.routing.http.module;
import com.google.gson.JsonObject;
import com.a.eye.skywalking.network.dependencies.com.google.gson.Gson; /**
* A {@link ResponseMessage} represent a status code and response messages for http-service.
public class ResponseMessage { * <p>
public static final ResponseMessage OK = new ResponseMessage(200, "Store success"); * Created by wusheng on 2017/1/13.
public static final ResponseMessage REQUEST_METHOD_NOT_SUPPORT = new ResponseMessage(403, "Request method " + */
"not support"); public enum ResponseMessage {
public static final ResponseMessage SERVER_ERROR = new ResponseMessage(500, "Server error"); /**
public static final ResponseMessage URL_NOT_FOUND = new ResponseMessage(404, "Not found"); * 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: * The {@link String} represents the return message of the http services.
* 200 -- store success * It is in the JSON format, and formatted by {@link com.google.gson.Gson}.
* 403 -- request method not support
* 500 -- server error
* 404 -- not found
*/ */
private int code;
private String message; private String message;
ResponseMessage(int code, String message) { ResponseMessage(int code, String message) {
this.code = code; JsonObject messageFormatter = new JsonObject();
this.message = message; 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.
先完成此消息的编辑!
想要评论请 注册