Ver código fonte

fix response object type

superfree 5 anos atrás
pai
commit
5681c564b9

+ 3 - 0
lambda-service/src/main/java/com/example/lambda/service/interpreter/type/Atom.java

@@ -6,6 +6,9 @@ public class Atom implements Ast {
 
     private String name;
 
+    public Atom() {
+    }
+
     public Atom(Integer id, String name) {
         this.id = id;
         this.name = name;

+ 6 - 7
lambda-service/src/main/java/com/example/lambda/service/web/LambdaController.java

@@ -3,6 +3,7 @@ package com.example.lambda.service.web;
 import com.example.lambda.service.web.entity.*;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
+import org.springframework.http.ResponseEntity;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestBody;
@@ -29,17 +30,15 @@ public class LambdaController {
     }
 
     @PostMapping("/pattern")
-    public ServiceResponse pattern(@RequestBody PatternParam param) {
-        System.out.println("pattern: " + param.getData().toString());
-        ServiceResponse response = lambdaService.pattern(param);
-        return response;
+    public ResponseEntity<String> pattern(@RequestBody PatternParam param) {
+        System.out.println("pattern: " + param.getData());
+        return lambdaService.pattern(param);
     }
 
     @PostMapping("/render")
-    public ServiceResponse render(@RequestBody RenderParam param) {
+    public ResponseEntity<String> render(@RequestBody RenderParam param) {
         System.out.println("render: " + param.getData().toString());
-        ServiceResponse response = lambdaService.render(param);
-        return response;
+        return lambdaService.render(param);
     }
 
 }

+ 61 - 26
lambda-service/src/main/java/com/example/lambda/service/web/LambdaService.java

@@ -9,14 +9,13 @@ import com.example.lambda.service.interpreter.Result;
 import com.example.lambda.service.interpreter.type.Atom;
 import com.example.lambda.service.utils.SVGRenderer;
 import com.example.lambda.service.web.entity.*;
-import com.example.lambda.service.web.type.IRType;
-import com.example.lambda.service.web.type.IType;
-import com.example.lambda.service.web.type.SourceType;
-import com.example.lambda.service.web.type.TargetType;
-import com.example.lambda.service.web.typedata.AtomInfo;
+import com.example.lambda.service.web.type.*;
 import com.example.lambda.service.web.typedata.IRAtom;
+import com.example.lambda.service.web.utils.ResponseUtils;
+import com.fasterxml.jackson.core.JsonProcessingException;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.ResponseEntity;
 import org.springframework.stereotype.Service;
 
 import java.util.*;
@@ -36,14 +35,11 @@ public class LambdaService {
         return interpreter != null ? "Interpreter is available(" + interpreter + ")" : "Interpreter is unavailable";
     }
 
-    private List<TypeInfo> createTypeInfos(IType[] types) {
-        List<TypeInfo> infos = new ArrayList<>();
-        for (IType type : types) {
-            infos.add(new TypeInfo(type));
-        }
-        return infos;
-    }
-
+    /**
+     * 获取服务列表
+     *
+     * @return
+     */
     public ServiceInfo info() {
         ServiceInfo info = new ServiceInfo();
         // add types
@@ -66,10 +62,24 @@ public class LambdaService {
         return info;
     }
 
-    public ServiceResponse pattern(PatternParam param) {
+    private List<TypeInfo> createTypeInfos(IType[] types) {
+        List<TypeInfo> infos = new ArrayList<>();
+        for (IType type : types) {
+            infos.add(new TypeInfo(type));
+        }
+        return infos;
+    }
+
+    /**
+     * 解析服务
+     *
+     * @param param
+     * @return
+     */
+    public ResponseEntity<String> pattern(PatternParam param) {
         if (param.getType() != SourceType.PATTERN) {
             // 参数类型不存在
-            return ServiceResponse1.error(PATTERN_SOURCE_TYPE_UNKNOWN);
+            return ResponseUtils.error(PATTERN_SOURCE_TYPE_UNKNOWN);
         }
         String pattern = param.getData();
         try {
@@ -77,22 +87,24 @@ public class LambdaService {
 //            Result res = interpreter.solveErrorStub(pattern);
             if (res.getState() == InterpreterState.ERROR) {
                 // 解析失败
-                return ServiceResponse1.error(res.getMsg());
+                return ResponseUtils.error(res.getMsg());
             }
+            System.out.println(res.getAst());
             switch (param.getTarget()) {
                 case ATOM:
-                    List<Object> atoms = extractAtoms(res.getAst());
-                    return ServiceResponse1.success(atoms);
+                    IRAtom irAtom = new IRAtom("\\1.\\2.2", extractAtoms(res.getAst()));
+                    return ResponseUtils.build(irAtom);
                 case AST:
                 default:
-                    return ServiceResponse1.success(res.getAst());
+                    return ResponseUtils.build(res.getAst());
             }
         } catch (Exception e) {
-            return ServiceResponse1.error("interpreter error");
+            e.printStackTrace();
+            return ResponseUtils.error("interpreter error");
         }
     }
 
-    private List<Object> extractAtoms(Ast ast) {
+    private List<Atom> extractAtoms(Ast ast) {
         Set<Atom> atoms = new HashSet<>();
         Queue<Ast> Q = new LinkedList<>();
         Q.add(ast);
@@ -111,12 +123,35 @@ public class LambdaService {
         return new ArrayList<>(atoms);
     }
 
-    public ServiceResponse render(RenderParam param) {
-        IRAtom data = new ObjectMapper().convertValue(param.getData(), IRAtom.class);
+    /**
+     * 渲染服务
+     *
+     * @param param
+     * @return
+     */
+    public ResponseEntity<String> render(RenderParam param) {
+        String el = "";
+        switch (param.getType()) {
+            case ATOM:
+                el = renderAtom(new ObjectMapper().convertValue(param.getData(), IRAtom.class));
+                break;
+            case AST:
+            default:
+                break;
+        }
+        try {
+            return ResponseUtils.build(el);
+        } catch (JsonProcessingException e) {
+            e.printStackTrace();
+            return ResponseUtils.error("json serialize error");
+        }
+    }
+
+    private String renderAtom(IRAtom data) {
         // build atom mapper
         Map<Integer, String> atoms = new HashMap<>();
-        for (AtomInfo info : data.getAtoms()) {
-            atoms.put(info.getId(), info.getName());
+        for (Atom atom : data.getAtoms()) {
+            atoms.put(atom.getId(), atom.getName());
         }
 //        System.out.println(atoms);
         // reformat pattern
@@ -133,6 +168,6 @@ public class LambdaService {
             renderer.addText(text, x, y);
             x += 8;
         }
-        return ServiceResponse1.success(renderer.build());
+        return renderer.build();
     }
 }

+ 0 - 4
lambda-service/src/main/java/com/example/lambda/service/web/entity/ServiceResponse.java

@@ -1,4 +0,0 @@
-package com.example.lambda.service.web.entity;
-
-public interface ServiceResponse {
-}

+ 0 - 72
lambda-service/src/main/java/com/example/lambda/service/web/entity/ServiceResponse1.java

@@ -1,72 +0,0 @@
-package com.example.lambda.service.web.entity;
-
-import com.example.lambda.service.web.type.ServiceResponseType;
-
-import java.util.List;
-
-public class ServiceResponse1 implements ServiceResponse {
-
-    private ServiceResponseType status;
-    private String msg;
-    private Object data;   // { list: data }
-    private List<Object> list; // []
-
-    public ServiceResponse1(ServiceResponseType status, String msg, Object data, List<Object> list) {
-        this.status = status;
-        this.msg = msg;
-        this.data = data;
-        this.list = list;
-    }
-
-    public static ServiceResponse1 success() {
-        return new ServiceResponse1(ServiceResponseType.SUCCESS, "success", null, null);
-    }
-
-    public static ServiceResponse1 success(Object data) {
-        return new ServiceResponse1(ServiceResponseType.SUCCESS, "success", data, null);
-    }
-
-    public static ServiceResponse1 success(List<Object> list) {
-        return new ServiceResponse1(ServiceResponseType.SUCCESS, "success", null, list);
-    }
-
-    public static ServiceResponse1 error() {
-        return new ServiceResponse1(ServiceResponseType.ERROR, "unknown error", null, null);
-    }
-
-    public static ServiceResponse1 error(String msg) {
-        return new ServiceResponse1(ServiceResponseType.ERROR, msg, null, null);
-    }
-
-    public ServiceResponseType getStatus() {
-        return status;
-    }
-
-    public void setStatus(ServiceResponseType status) {
-        this.status = status;
-    }
-
-    public String getMsg() {
-        return msg;
-    }
-
-    public void setMsg(String msg) {
-        this.msg = msg;
-    }
-
-    public Object getData() {
-        return data;
-    }
-
-    public void setData(Object data) {
-        this.data = data;
-    }
-
-    public List<Object> getList() {
-        return list;
-    }
-
-    public void setList(List<Object> list) {
-        this.list = list;
-    }
-}

+ 0 - 35
lambda-service/src/main/java/com/example/lambda/service/web/entity/ServiceResponse2.java

@@ -1,35 +0,0 @@
-package com.example.lambda.service.web.entity;
-
-import com.example.lambda.service.web.type.ServiceResponseType;
-
-import java.util.List;
-
-public class ServiceResponse2 implements ServiceResponse {
-    private ServiceResponseType status;
-    private String msg;
-    private List<Object> data;   // { list: data }
-
-    public ServiceResponseType getStatus() {
-        return status;
-    }
-
-    public void setStatus(ServiceResponseType status) {
-        this.status = status;
-    }
-
-    public String getMsg() {
-        return msg;
-    }
-
-    public void setMsg(String msg) {
-        this.msg = msg;
-    }
-
-    public List<Object> getData() {
-        return data;
-    }
-
-    public void setData(List<Object> data) {
-        this.data = data;
-    }
-}

+ 0 - 5
lambda-service/src/main/java/com/example/lambda/service/web/type/ServiceResponseType.java

@@ -1,5 +0,0 @@
-package com.example.lambda.service.web.type;
-
-public enum ServiceResponseType {
-    SUCCESS, ERROR
-}

+ 0 - 22
lambda-service/src/main/java/com/example/lambda/service/web/typedata/AtomInfo.java

@@ -1,22 +0,0 @@
-package com.example.lambda.service.web.typedata;
-
-public class AtomInfo {
-    private Integer id;
-    private String name;
-
-    public Integer getId() {
-        return id;
-    }
-
-    public void setId(Integer id) {
-        this.id = id;
-    }
-
-    public String getName() {
-        return name;
-    }
-
-    public void setName(String name) {
-        this.name = name;
-    }
-}

+ 13 - 3
lambda-service/src/main/java/com/example/lambda/service/web/typedata/IRAtom.java

@@ -1,10 +1,20 @@
 package com.example.lambda.service.web.typedata;
 
+import com.example.lambda.service.interpreter.type.Atom;
+
 import java.util.List;
 
 public class IRAtom {
     private String pattern;
-    private List<AtomInfo> atoms;
+    private List<Atom> atoms;
+
+    public IRAtom() {
+    }
+
+    public IRAtom(String pattern, List<Atom> atoms) {
+        this.pattern = pattern;
+        this.atoms = atoms;
+    }
 
     public String getPattern() {
         return pattern;
@@ -14,11 +24,11 @@ public class IRAtom {
         this.pattern = pattern;
     }
 
-    public List<AtomInfo> getAtoms() {
+    public List<Atom> getAtoms() {
         return atoms;
     }
 
-    public void setAtoms(List<AtomInfo> atoms) {
+    public void setAtoms(List<Atom> atoms) {
         this.atoms = atoms;
     }
 }

+ 37 - 0
lambda-service/src/main/java/com/example/lambda/service/web/utils/ResponseUtils.java

@@ -0,0 +1,37 @@
+package com.example.lambda.service.web.utils;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.MediaType;
+import org.springframework.http.ResponseEntity;
+import org.springframework.stereotype.Component;
+
+@Component
+public class ResponseUtils {
+
+    private static ObjectMapper objectMapper;
+
+    private static HttpHeaders headersJson;
+
+    {
+        objectMapper = new ObjectMapper();
+        headersJson = new HttpHeaders();
+        headersJson.setContentType(MediaType.APPLICATION_JSON);
+    }
+
+    public static ResponseEntity<String> build(Object data) throws JsonProcessingException {
+        String res;
+        if (!(data instanceof String)) {
+            res = objectMapper.writeValueAsString(data);
+        } else {
+            res = (String) data;
+        }
+        return new ResponseEntity<>(res, headersJson, HttpStatus.OK);
+    }
+
+    public static ResponseEntity<String> error(String msg) {
+        return new ResponseEntity<>(msg, headersJson, HttpStatus.INTERNAL_SERVER_ERROR);
+    }
+}

+ 5 - 20
subject-center/src/main/java/com/example/subject/center/SubjectCenterController.java

@@ -2,9 +2,9 @@ package com.example.subject.center;
 
 import com.example.subject.center.entity.*;
 import com.example.subject.center.utils.RequestUtils;
-import com.fasterxml.jackson.databind.ObjectMapper;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
+import org.springframework.http.ResponseEntity;
 import org.springframework.web.bind.annotation.*;
 
 import javax.servlet.http.HttpServletRequest;
@@ -34,32 +34,17 @@ public class SubjectCenterController {
     }
 
     @PostMapping("/{serviceName}/pattern")
-    public Response pattern(HttpServletRequest request, @PathVariable String serviceName) {
+    public ResponseEntity<Object> pattern(HttpServletRequest request, @PathVariable String serviceName) {
         String body = RequestUtils.getBody(request);
-//        System.out.printf("[render proxy]: service=%s\n", serviceName);
         System.out.printf("[pattern proxy]: service=%s , body=%s\n", serviceName, body);
-        ServiceResponse response = subjectCenterService.patternProxy(getServiceId(serviceName), body);
-        return solveServiceResponse(response);
+        return subjectCenterService.patternProxy(getServiceId(serviceName), body);
     }
 
     @PostMapping("/{serviceName}/render")
-    public Response render(HttpServletRequest request, @PathVariable String serviceName) {
+    public ResponseEntity<Object> render(HttpServletRequest request, @PathVariable String serviceName) {
         String body = RequestUtils.getBody(request);
-//        System.out.printf("[render proxy]: service=%s\n", serviceName);
         System.out.printf("[render proxy]: service=%s , body=%s\n", serviceName, body);
-        ServiceResponse response = subjectCenterService.renderProxy(getServiceId(serviceName), body);
-        return solveServiceResponse(response);
+        return subjectCenterService.renderProxy(getServiceId(serviceName), body);
     }
 
-    private Response solveServiceResponse(ServiceResponse serviceResponse) {
-        switch (serviceResponse.getStatus()) {
-            case SUCCESS:
-                Object data = serviceResponse.getData();
-                if (data == null) data = serviceResponse.getList();
-                return Response.ok(data);
-            case ERROR:
-                return Response.error(serviceResponse.getMsg());
-        }
-        return Response.error("unknown controller error");
-    }
 }

+ 3 - 5
subject-center/src/main/java/com/example/subject/center/SubjectCenterService.java

@@ -1,9 +1,7 @@
 package com.example.subject.center;
 
-import com.example.subject.center.entity.PatternParam;
-import com.example.subject.center.entity.RenderParam;
 import com.example.subject.center.entity.ServiceInfo;
-import com.example.subject.center.entity.ServiceResponse;
+import org.springframework.http.ResponseEntity;
 
 import java.util.List;
 
@@ -11,7 +9,7 @@ public interface SubjectCenterService {
 
     List<ServiceInfo> getServiceList();
 
-    ServiceResponse patternProxy(String serviceId, String body);
+    ResponseEntity<Object> patternProxy(String serviceId, String body);
 
-    ServiceResponse renderProxy(String serviceId, String body);
+    ResponseEntity<Object> renderProxy(String serviceId, String body);
 }

+ 38 - 19
subject-center/src/main/java/com/example/subject/center/SubjectCenterServiceImpl.java

@@ -3,9 +3,7 @@ package com.example.subject.center;
 import com.example.subject.center.entity.*;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.cloud.client.discovery.DiscoveryClient;
-import org.springframework.http.HttpEntity;
-import org.springframework.http.HttpHeaders;
-import org.springframework.http.MediaType;
+import org.springframework.http.*;
 import org.springframework.stereotype.Service;
 import org.springframework.web.client.RestTemplate;
 
@@ -37,36 +35,57 @@ public class SubjectCenterServiceImpl implements SubjectCenterService {
         return serviceInfos;
     }
 
-    private ServiceResponse serviceProxy(String serviceId, String method, String body) {
-        // build url
-        String url = "http://" + serviceId + "/" + method;
-        System.out.println("[" + method + " proxy]: " + url);
-        // build entity
-        HttpHeaders headers = new HttpHeaders();
-        headers.setContentType(MediaType.APPLICATION_JSON);
-        HttpEntity<String> httpEntity = new HttpEntity<String>(body, headers);
-        return restTemplate.postForObject(url, httpEntity, ServiceResponse.class);
-    }
+//    private ResponseEntity<Object> serviceProxy(String serviceId, String method, String body) {
+//        // build url
+//        String url = "http://" + serviceId + "/" + method;
+//        System.out.println("[" + method + " proxy]: " + url);
+//        // build entity
+//        HttpHeaders headers = new HttpHeaders();
+//        headers.setContentType(MediaType.APPLICATION_JSON);
+//        HttpEntity<String> httpEntity = new HttpEntity<String>(body, headers);
+//        ResponseEntity<Object> res = restTemplate.postForEntity(url, httpEntity, Object.class);
+//        return res;
+//    }
 
     private static final String PROXY_METHOD_PATTERN = "pattern";
     private static final String PROXY_METHOD_RENDER = "render";
+    private static final String SERIVE_REQUEST_ERROR = "service request error";
 
     @Override
-    public ServiceResponse patternProxy(String serviceId, String body) {
+    public ResponseEntity<Object> patternProxy(String serviceId, String body) {
+        String url = createServiceURL(serviceId, PROXY_METHOD_PATTERN);
+        HttpEntity<String> httpEntity = createHttpEntity(body);
+        System.out.println("[" + PROXY_METHOD_PATTERN + " proxy]: " + url);
         try {
-            return serviceProxy(serviceId, PROXY_METHOD_PATTERN, body);
+            return restTemplate.postForEntity(url, httpEntity, Object.class);
         } catch (Exception e) {
             e.printStackTrace();
-            return ServiceResponse.error();
+            return new ResponseEntity<Object>(SERIVE_REQUEST_ERROR, HttpStatus.INTERNAL_SERVER_ERROR);
         }
     }
 
     @Override
-    public ServiceResponse renderProxy(String serviceId, String body) {
+    public ResponseEntity<Object> renderProxy(String serviceId, String body) {
+        String url = createServiceURL(serviceId, PROXY_METHOD_RENDER);
+        HttpEntity<String> httpEntity = createHttpEntity(body);
+        System.out.println("[" + PROXY_METHOD_RENDER + " proxy]: " + url);
         try {
-            return serviceProxy(serviceId, PROXY_METHOD_RENDER, body);
+            ResponseEntity<String> res = restTemplate.postForEntity(url, httpEntity, String.class);
+            return new ResponseEntity<>(res.getBody(), res.getStatusCode());
         } catch (Exception e) {
-            return ServiceResponse.error();
+            e.printStackTrace();
+            return new ResponseEntity<Object>(SERIVE_REQUEST_ERROR, HttpStatus.INTERNAL_SERVER_ERROR);
         }
     }
+
+    private String createServiceURL(String serviceId, String method) {
+        return String.format("http://%s/%s", serviceId, method);
+    }
+
+    private HttpEntity<String> createHttpEntity(String body) {
+        HttpHeaders headers = new HttpHeaders();
+        headers.setContentType(MediaType.APPLICATION_JSON);
+        HttpEntity<String> httpEntity = new HttpEntity<String>(body, headers);
+        return httpEntity;
+    }
 }

+ 0 - 37
subject-center/src/main/java/com/example/subject/center/entity/Response.java

@@ -1,37 +0,0 @@
-package com.example.subject.center.entity;
-
-public class Response {
-
-    private Integer status;
-
-    private Object data;
-
-    private Response(Integer status, Object data) {
-        this.status = status;
-        this.data = data;
-    }
-
-    public static Response ok(Object data) {
-        return new Response(200, data);
-    }
-
-    public static Response error(String msg) {
-        return new Response(403, msg);
-    }
-
-    public Integer getStatus() {
-        return status;
-    }
-
-    public void setStatus(Integer status) {
-        this.status = status;
-    }
-
-    public Object getData() {
-        return data;
-    }
-
-    public void setData(Object data) {
-        this.data = data;
-    }
-}

+ 0 - 59
subject-center/src/main/java/com/example/subject/center/entity/ServiceResponse.java

@@ -1,59 +0,0 @@
-package com.example.subject.center.entity;
-
-import java.util.List;
-
-public class ServiceResponse {
-
-    private static final String SERIVE_REQUEST_ERROR = "service request error";
-
-    private ServiceResponseType status; // 服务状态 SUCCESS | ERROR
-    private String msg; // 错误信息
-    private Object data; // 返回数据
-    private List<Object> list; // 列表类型返回数据
-
-    public ServiceResponse() {
-    }
-
-    public ServiceResponse(ServiceResponseType status, String msg, Object data, List<Object> list) {
-        this.status = status;
-        this.msg = msg;
-        this.data = data;
-        this.list = list;
-    }
-
-    public static ServiceResponse error() {
-        return new ServiceResponse(ServiceResponseType.ERROR, SERIVE_REQUEST_ERROR, null, null);
-    }
-
-    public ServiceResponseType getStatus() {
-        return status;
-    }
-
-    public void setStatus(ServiceResponseType status) {
-        this.status = status;
-    }
-
-    public String getMsg() {
-        return msg;
-    }
-
-    public void setMsg(String msg) {
-        this.msg = msg;
-    }
-
-    public Object getData() {
-        return data;
-    }
-
-    public void setData(Object data) {
-        this.data = data;
-    }
-
-    public List<Object> getList() {
-        return list;
-    }
-
-    public void setList(List<Object> list) {
-        this.list = list;
-    }
-}