Skip to content

Commit

Permalink
fea: add config for mvc origin_response path (#788)
Browse files Browse the repository at this point in the history
* fea:mvc add origin response path config

* opt code for mvc origin_response config
  • Loading branch information
TreasureJade authored Feb 27, 2024
1 parent 4dbc169 commit 97e2c07
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
16 changes: 10 additions & 6 deletions jcommon/docean/src/main/java/com/xiaomi/youpin/docean/Mvc.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.xiaomi.youpin.docean.common.MethodInvoker;
import com.xiaomi.youpin.docean.common.NamedThreadFactory;
import com.xiaomi.youpin.docean.common.Safe;
import com.xiaomi.youpin.docean.common.StringUtils;
import com.xiaomi.youpin.docean.config.HttpServerConfig;
import com.xiaomi.youpin.docean.exception.DoceanException;
import com.xiaomi.youpin.docean.listener.event.Event;
Expand Down Expand Up @@ -81,6 +82,7 @@ private void setConfig(Ioc ioc) {
this.mvcConfig.setResponseOriginalValue(Boolean.valueOf(ioc.getBean(MvcConst.RESPONSE_ORIGINAL_VALUE, MvcConst.FALSE)));
this.mvcConfig.setPoolSize(Integer.valueOf(ioc.getBean(MvcConst.MVC_POOL_SIZE, String.valueOf(MvcConst.DEFAULT_MVC_POOL_SIZE))));
this.mvcConfig.setVirtualThread(Boolean.valueOf(ioc.getBean(MvcConst.VIRTUAL_THREAD, MvcConst.TRUE)));
this.mvcConfig.setResponseOriginalPath(ioc.getBean(MvcConst.RESPONSE_ORIGINAL_PATH, ""));
ioc.publishEvent(new Event(EventType.mvcBegin, this.mvcConfig));
}

Expand Down Expand Up @@ -208,12 +210,14 @@ public void callMethod(MvcContext context, MvcRequest request, MvcResponse respo
return;
}
// get whether the configuration returns an unwrapped value
if (this.mvcConfig.isResponseOriginalValue()) {
if (data instanceof String) {
response.writeAndFlush(context, (String) data);
} else {
response.writeAndFlush(context, gson.toJson(data));
}
boolean needOriginalValue = this.mvcConfig.isResponseOriginalValue();
if (!needOriginalValue && StringUtils.isNotBlank(this.mvcConfig.getResponseOriginalPath())) {
needOriginalValue = Arrays.stream(mvcConfig.getResponseOriginalPath().split(","))
.anyMatch(i -> i.equals(method.getPath()));
}
if (needOriginalValue) {
String responseData = data instanceof String ? (String) data : gson.toJson(data);
response.writeAndFlush(context, responseData);
} else {
result.setData(data);
response.writeAndFlush(context, gson.toJson(result));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,9 @@ public class MvcConfig implements Serializable {
*/
private boolean virtualThread;

/**
* url that need to return original data like"/test1,/test2"
*/
private String responseOriginalPath;

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public abstract class MvcConst {

public static final String VIRTUAL_THREAD = "$virtual-threaad";

public static final String RESPONSE_ORIGINAL_PATH = "$response-original-path";

public static final int DEFAULT_MVC_POOL_SIZE = 200;

public static ScopedValue<MvcContext> MVC_CONTEXT = ScopedValue.newInstance();
Expand Down

0 comments on commit 97e2c07

Please sign in to comment.