Skip to content

Commit

Permalink
修复DEBUG时二进制结果输出不正确的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
javamxd committed Jul 26, 2020
1 parent e7e692d commit 4c71e61
Showing 1 changed file with 26 additions and 22 deletions.
48 changes: 26 additions & 22 deletions src/main/java/org/ssssssss/magicapi/config/WebUIController.java
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ public JsonBean<List<ApiInfo>> list() {
*/
@RequestMapping("/continue")
@ResponseBody
public JsonBean<Object> debugContinue(String id) {
public Object debugContinue(String id, HttpServletResponse response) throws IOException {
MagicScriptDebugContext context = MagicScriptDebugContext.getDebugContext(id);
if (context == null) {
return new JsonBean<>(0, "debug session not found!", resultProvider.buildResult(0, "debug session not found!"));
Expand All @@ -222,7 +222,7 @@ public JsonBean<Object> debugContinue(String id) {
} else if (context.isException()) {
return resolveThrowable((Throwable) context.getReturnValue());
}
return new JsonBodyBean<>(resultProvider.buildResult(context.getReturnValue()), context.getReturnValue());
return convertResult(context.getReturnValue(), response);
}

/**
Expand Down Expand Up @@ -262,7 +262,7 @@ public JsonBean<List<ScriptClass>> clazz(String className) {
/**
* 测试运行
*
* @param request 请求参数
* @param request 请求参数
*/
@RequestMapping("/test")
@ResponseBody
Expand Down Expand Up @@ -307,38 +307,42 @@ public Object test(HttpServletRequest servletRequest, @RequestBody(required = fa
} else if (context.isException()) { //判断是否出现异常
return resolveThrowable((Throwable) context.getReturnValue());
}
if (result instanceof ResponseEntity) {
ResponseEntity entity = (ResponseEntity) result;
for (Map.Entry<String, List<String>> entry : entity.getHeaders().entrySet()) {
String key = entry.getKey();
for (String value : entry.getValue()) {
response.addHeader("MA-" + key, value);
}
}
if(entity.getHeaders().isEmpty()){
return ResponseEntity.ok(new JsonBean<>(entity.getBody()));
}
return ResponseEntity.ok(new JsonBean<>(convertToBase64(entity.getBody())));
}
return new JsonBean<>(resultProvider.buildResult(result));
return convertResult(result, response);
} catch (Exception e) {
return resolveThrowable(e);
}
}
return new JsonBean<>(resultProvider.buildResult(0, "脚本不能为空"));
}

private Object convertResult(Object result, HttpServletResponse response) throws IOException {
if (result instanceof ResponseEntity) {
ResponseEntity entity = (ResponseEntity) result;
for (Map.Entry<String, List<String>> entry : entity.getHeaders().entrySet()) {
String key = entry.getKey();
for (String value : entry.getValue()) {
response.addHeader("MA-" + key, value);
}
}
if (entity.getHeaders().isEmpty()) {
return ResponseEntity.ok(new JsonBean<>(entity.getBody()));
}
return ResponseEntity.ok(new JsonBean<>(convertToBase64(entity.getBody())));
}
return new JsonBean<>(resultProvider.buildResult(result));
}

private String convertToBase64(Object value) throws IOException {
if(value instanceof String || value instanceof Number){
if (value instanceof String || value instanceof Number) {
return convertToBase64(value.toString().getBytes());
}else if(value instanceof byte[]){
} else if (value instanceof byte[]) {
return Base64.getEncoder().encodeToString((byte[]) value);
}else if(value instanceof InputStream){
} else if (value instanceof InputStream) {
return convertToBase64(IOUtils.toByteArray((InputStream) value));
}else if(value instanceof InputStreamSource){
} else if (value instanceof InputStreamSource) {
InputStreamSource iss = (InputStreamSource) value;
return convertToBase64(iss.getInputStream());
}else {
} else {
return convertToBase64(new ObjectMapper().writeValueAsString(value));
}
}
Expand Down

0 comments on commit 4c71e61

Please sign in to comment.