diff --git a/jcommon/docean-plugin/docean-plugin-mongodb/src/main/java/run/mone/controller/MongodbController.java b/jcommon/docean-plugin/docean-plugin-mongodb/src/main/java/run/mone/controller/MongodbController.java index e0240bd55..de9b240c7 100644 --- a/jcommon/docean-plugin/docean-plugin-mongodb/src/main/java/run/mone/controller/MongodbController.java +++ b/jcommon/docean-plugin/docean-plugin-mongodb/src/main/java/run/mone/controller/MongodbController.java @@ -1,5 +1,8 @@ package run.mone.controller; + +import com.xiaomi.youpin.docean.anno.ModelAttribute; + import com.xiaomi.youpin.docean.anno.RequestMapping; import com.xiaomi.youpin.docean.anno.RequestParam; import com.xiaomi.youpin.docean.mvc.ContextHolder; @@ -46,6 +49,14 @@ public T getById(@RequestParam("id") String id) { return datastore.find(this.clazz).filter(Filters.eq("id", id)).first(); } + + + @RequestMapping(path = "/getByIdAndUid", method = "get") + public T getByIdAndUid(@ModelAttribute("user") User user, @RequestParam("id") String id) { + return datastore.find(this.clazz).filter(Filters.and(Filters.eq("id", id), Filters.eq("uid", user.getId()))).first(); + } + + //按id删除(class) @Auth @RequestMapping(path = "/deleteById", method = "get") @@ -54,10 +65,9 @@ public boolean deleteById(@RequestParam("id") String id) { return true; } - @Auth(role = "user") - @RequestMapping(path = "/deleteByIdAndUid", method = "get") - public boolean deleteByIdAndUid(@RequestParam("id") String id) { - User user = getCurrentUser(); + + @RequestMapping(path = "/deleteByIdAndUid") + public boolean deleteByIdAndUid(@ModelAttribute("user") User user, String id) { this.datastore.find(this.clazz).filter(Filters.and(Filters.eq("id", id), Filters.eq("uid", user.getId()))).delete(); return true; } diff --git a/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/Mvc.java b/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/Mvc.java index 8d5437d5b..f220225d0 100644 --- a/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/Mvc.java +++ b/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/Mvc.java @@ -271,8 +271,16 @@ public void callMethod(MvcContext context, MvcRequest request, MvcResponse respo } Throwable unwrapThrowable = ExceptionUtil.unwrapThrowable(ex); result.setCode(500); + int httpCode = 200; + if (ex instanceof DoceanException de) { + int code = de.getCode(); + if (0 != code) { + result.setCode(code); + httpCode = code; + } + } result.setMessage(unwrapThrowable.getMessage()); - response.writeAndFlush(context, gson.toJson(result)); + response.writeAndFlush(context, gson.toJson(result), httpCode); }); } @@ -293,7 +301,7 @@ private Object[] getMethodParams(MvcContext context, MvcRequest request, HttpReq Object obj = context.session().getAttribute(name); //提取失败,用户需要登录 if (null == obj) { - throw new DoceanException("You are required to log in first."); + throw new DoceanException("You are required to log in first.", 401); } return obj; }); diff --git a/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/mvc/Get.java b/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/mvc/Get.java index f63fb77c9..89767f658 100644 --- a/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/mvc/Get.java +++ b/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/mvc/Get.java @@ -19,10 +19,13 @@ import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; +import com.xiaomi.youpin.docean.anno.ModelAttribute; import com.xiaomi.youpin.docean.anno.RequestParam; +import com.xiaomi.youpin.docean.mvc.util.RequestUtils; import io.netty.handler.codec.http.QueryStringDecoder; import java.lang.annotation.Annotation; +import java.lang.reflect.Parameter; import java.util.Arrays; import java.util.Map; import java.util.stream.Collectors; @@ -38,21 +41,28 @@ public static JsonElement getParams(HttpRequestMethod method, String uri, MvcCon JsonObject res = new JsonObject(); params.entrySet().forEach(it -> res.addProperty(it.getKey(), it.getValue())); context.setParams(res); + JsonArray resArray = new JsonArray(); + + //get 只允许第一个参数是session user + Parameter[] methodParameters = method.getMethod().getParameters(); + if (methodParameters.length > 0 && (methodParameters[0].getAnnotation(ModelAttribute.class) != null)) { + resArray.add(RequestUtils.createSessionJsonObject(methodParameters[0].getAnnotation(ModelAttribute.class).value())); + } - JsonArray array = new JsonArray(); Annotation[][] anns = method.getMethod().getParameterAnnotations(); Arrays.stream(anns).forEach(it -> { if (it.length > 0) { RequestParam param = getRequestParam(it); String name = param.value(); if (!params.containsKey(name)) { - array.add(""); + resArray.add(""); } else { - array.add(params.get(name)); + resArray.add(params.get(name)); } } }); - return array; + + return resArray; } private static RequestParam getRequestParam(Annotation[] annos) { diff --git a/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/mvc/MvcResponse.java b/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/mvc/MvcResponse.java index 1b7409746..ff9ac2bc5 100644 --- a/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/mvc/MvcResponse.java +++ b/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/mvc/MvcResponse.java @@ -70,6 +70,17 @@ public void writeAndFlush(MvcContext context, String message) { responseStatus = HttpResponseStatus.valueOf(Integer.valueOf(status)); } writeAndFlush(context, responseStatus, message); + + writeAndFlush(context, message, HttpResponseStatus.OK.code()); + } + + public void writeAndFlush(MvcContext context, String message, int code) { + HttpResponseStatus responseStatus = HttpResponseStatus.valueOf(code); + String status = context.getResHeaders().get("x-status"); + if (null != status) { + responseStatus = HttpResponseStatus.valueOf(Integer.valueOf(status)); + } + writeAndFlush(context, responseStatus, message); } public void clear() { diff --git a/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/mvc/Post.java b/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/mvc/Post.java index ac3f0e43c..e9b1cb2ab 100644 --- a/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/mvc/Post.java +++ b/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/mvc/Post.java @@ -20,9 +20,16 @@ import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; + import com.xiaomi.youpin.docean.anno.ModelAttribute; import com.xiaomi.youpin.docean.mvc.httpmethod.HttpMethodUtils; import com.xiaomi.youpin.docean.mvc.util.GsonUtils; +import com.xiaomi.youpin.docean.mvc.util.RequestUtils; + +import java.lang.reflect.Parameter; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.concurrent.atomic.AtomicInteger; import java.lang.reflect.Parameter; import java.util.ArrayList; @@ -49,7 +56,7 @@ public static JsonArray getParams(HttpRequestMethod method, byte[] data, MvcCont if (null == arguments) { Arrays.stream(methodParameters).forEach(it -> { if (it.getAnnotation(ModelAttribute.class) != null) { - arrayRes.add(obj(it.getAnnotation(ModelAttribute.class).value())); + arrayRes.add(RequestUtils.createSessionJsonObject(it.getAnnotation(ModelAttribute.class).value())); } }); return arrayRes; @@ -60,7 +67,7 @@ public static JsonArray getParams(HttpRequestMethod method, byte[] data, MvcCont AtomicInteger i = new AtomicInteger(0); Arrays.stream(methodParameters).forEach(it -> { if (it.getAnnotation(ModelAttribute.class) != null) { - arrayRes.add(obj(it.getAnnotation(ModelAttribute.class).value())); + arrayRes.add(RequestUtils.createSessionJsonObject(it.getAnnotation(ModelAttribute.class).value())); } else { arrayRes.add(list.get(i.get())); i.incrementAndGet(); @@ -72,7 +79,7 @@ public static JsonArray getParams(HttpRequestMethod method, byte[] data, MvcCont if (arguments.isJsonObject()) { Arrays.stream(methodParameters).forEach(it -> { if (it.getAnnotation(ModelAttribute.class) != null) { - arrayRes.add(obj(it.getAnnotation(ModelAttribute.class).value())); + arrayRes.add(RequestUtils.createSessionJsonObject(it.getAnnotation(ModelAttribute.class).value())); } else { arrayRes.add(arguments.getAsJsonObject()); } @@ -83,7 +90,7 @@ public static JsonArray getParams(HttpRequestMethod method, byte[] data, MvcCont if (arguments.isJsonPrimitive()) { Arrays.stream(methodParameters).forEach(it -> { if (it.getAnnotation(ModelAttribute.class) != null) { - arrayRes.add(obj(it.getAnnotation(ModelAttribute.class).value())); + arrayRes.add(RequestUtils.createSessionJsonObject(it.getAnnotation(ModelAttribute.class).value())); } else { arrayRes.add(arguments.getAsJsonPrimitive()); } @@ -112,6 +119,7 @@ public static JsonArray getParams(HttpRequestMethod method, byte[] data, MvcCont } return arrayRes; + } private static JsonObject obj(String name) { @@ -121,4 +129,6 @@ private static JsonObject obj(String name) { return obj; } + + } diff --git a/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/mvc/util/RequestUtils.java b/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/mvc/util/RequestUtils.java index 512880f30..53200edef 100644 --- a/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/mvc/util/RequestUtils.java +++ b/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/mvc/util/RequestUtils.java @@ -1,6 +1,7 @@ package com.xiaomi.youpin.docean.mvc.util; import com.google.gson.Gson; +import com.google.gson.JsonObject; import com.xiaomi.youpin.docean.config.HttpServerConfig; import com.xiaomi.youpin.docean.mvc.HttpRequestUtils; import com.xiaomi.youpin.docean.mvc.upload.Upload; @@ -18,8 +19,6 @@ */ public abstract class RequestUtils { - private static Gson gson = new Gson(); - public static byte[] getData(HttpServerConfig config, String uri, FullHttpRequest request) { return getData(config, uri, request, null); } @@ -49,4 +48,11 @@ public static Map headers(FullHttpRequest request) { return request.headers().entries().stream().collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e, n) -> n)); } + public static JsonObject createSessionJsonObject(String name) { + JsonObject obj = new JsonObject(); + obj.addProperty("__type__", "session"); + obj.addProperty("__name__", name); + return obj; + } + } diff --git a/jcommon/easy/src/main/java/com/xiaomi/youpin/docean/exception/DoceanException.java b/jcommon/easy/src/main/java/com/xiaomi/youpin/docean/exception/DoceanException.java index b54921225..28cbaa23c 100644 --- a/jcommon/easy/src/main/java/com/xiaomi/youpin/docean/exception/DoceanException.java +++ b/jcommon/easy/src/main/java/com/xiaomi/youpin/docean/exception/DoceanException.java @@ -16,12 +16,17 @@ package com.xiaomi.youpin.docean.exception; +import lombok.Getter; + /** * @author goodjava@qq.com * @date 2020/6/20 */ public class DoceanException extends RuntimeException { + @Getter + private int code; + public DoceanException(Throwable ex) { super(ex); } @@ -33,4 +38,9 @@ public DoceanException() { public DoceanException(String m) { super(m); } + + public DoceanException(String message, int code) { + super(message); + this.code = code; + } }