提交 75d79fd4 编写于 作者: 浅梦2013's avatar 浅梦2013

统一响应模型

上级 62700c14
......@@ -48,7 +48,10 @@ public enum ResultCode {
* 未知错误
*/
E105(HttpResponseStatus.C500, 105),
/**
* 请求方法错误
*/
E404(HttpResponseStatus.C404, 404),
;
private final HttpResponseStatus statusCode;
......
......@@ -48,19 +48,15 @@ public class MqttHttpRequestHandler implements HttpRequestHandler {
return resp500(request, requestLine, e);
}
// 2. 路由处理
HandlerInfo handler = MqttHttpRoutes.getHandler(requestLine);
HttpHandler handler = MqttHttpRoutes.getHandler(requestLine);
if (handler == null) {
return resp404(request, requestLine);
}
Method method = requestLine.getMethod();
if (handler.getMethod() != method) {
return Result.fail(new HttpResponse(request), ResultCode.E104);
}
if (logger.isInfoEnabled()) {
logger.info("mqtt http api {} path:{}", method.name(), requestLine.getPathAndQuery());
logger.info("mqtt http api {} path:{}", requestLine.method, requestLine.getPathAndQuery());
}
try {
return handler.getHandler().apply(request);
return handler.apply(request);
} catch (Exception e) {
return resp500(request, requestLine, e);
}
......@@ -71,9 +67,7 @@ public class MqttHttpRequestHandler implements HttpRequestHandler {
if (logger.isErrorEnabled()) {
logger.error("mqtt http {} path:{} 404", requestLine.getMethod().name(), requestLine.getPathAndQuery());
}
HttpResponse response = new HttpResponse(request);
response.setStatus(HttpResponseStatus.C404);
return response;
return Result.fail(request, ResultCode.E404);
}
@Override
......@@ -81,8 +75,7 @@ public class MqttHttpRequestHandler implements HttpRequestHandler {
if (logger.isErrorEnabled()) {
logger.error("mqtt http {} path:{} error", requestLine.getMethod().name(), requestLine.getPathAndQuery(), throwable);
}
HttpResponse response = new HttpResponse(request);
return Result.fail(response, ResultCode.E105);
return Result.fail(request, ResultCode.E105);
}
@Override
......
......@@ -28,7 +28,7 @@ import java.util.*;
*/
public final class MqttHttpRoutes {
private static final LinkedList<HttpFilter> FILTERS = new LinkedList<>();
private static final Map<String, HandlerInfo> ROUTS = new HashMap<>();
private static final Map<RouteInfo, HttpHandler> ROUTS = new HashMap<>();
/**
* 注册 filter 到 first
......@@ -75,17 +75,7 @@ public final class MqttHttpRoutes {
* @param handler HttpHandler
*/
public static void register(Method method, String path, HttpHandler handler) {
ROUTS.put(path, new HandlerInfo(method, handler));
}
/**
* 读取路由
*
* @param path 路径
* @return HandlerInfo
*/
public static HandlerInfo getHandler(String path) {
return ROUTS.get(path);
ROUTS.put(new RouteInfo(path, method), handler);
}
/**
......@@ -94,8 +84,10 @@ public final class MqttHttpRoutes {
* @param requestLine RequestLine
* @return HttpHandler
*/
public static HandlerInfo getHandler(RequestLine requestLine) {
return getHandler(requestLine.getPath());
public static HttpHandler getHandler(RequestLine requestLine) {
String path = requestLine.getPath();
Method method = requestLine.getMethod();
return ROUTS.get(new RouteInfo(path, method));
}
}
......@@ -25,21 +25,13 @@ import java.util.Objects;
*
* @author L.cm
*/
public class HandlerInfo {
public class RouteInfo {
private final String path;
private final Method method;
private final HttpHandler handler;
public HandlerInfo(Method method, HttpHandler handler) {
public RouteInfo(String path, Method method) {
this.path = path;
this.method = method;
this.handler = handler;
}
public Method getMethod() {
return method;
}
public HttpHandler getHandler() {
return handler;
}
@Override
......@@ -47,16 +39,23 @@ public class HandlerInfo {
if (this == o) {
return true;
}
if (!(o instanceof HandlerInfo)) {
if (o == null || getClass() != o.getClass()) {
return false;
}
HandlerInfo that = (HandlerInfo) o;
return method == that.method &&
Objects.equals(handler, that.handler);
RouteInfo that = (RouteInfo) o;
return Objects.equals(path, that.path) && method == that.method;
}
@Override
public int hashCode() {
return Objects.hash(method, handler);
return Objects.hash(path, method);
}
@Override
public String toString() {
return "HandlerInfo{" +
"path='" + path + '\'' +
", method=" + method +
'}';
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册