yzhao244 commented on a change in pull request #434:
URL: 
https://github.com/apache/incubator-eventmesh/pull/434#discussion_r670975624



##########
File path: 
eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/CreateSubjectHandler.java
##########
@@ -0,0 +1,94 @@
+package org.apache.eventmesh.runtime.admin.handler;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.util.ServiceLoader;
+
+import org.apache.commons.lang3.StringUtils;
+import org.apache.eventmesh.runtime.util.NetUtils;
+import org.apache.eventmesh.store.api.openschema.common.ServiceException;
+import org.apache.eventmesh.store.api.openschema.request.SubjectCreateRequest;
+import org.apache.eventmesh.store.api.openschema.response.SubjectResponse;
+import org.apache.eventmesh.store.api.openschema.service.SchemaService;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.sun.net.httpserver.HttpExchange;
+import com.sun.net.httpserver.HttpHandler;
+
+import com.fasterxml.jackson.databind.DeserializationFeature;
+import com.fasterxml.jackson.databind.SerializationFeature;
+import com.fasterxml.jackson.annotation.JsonInclude;
+
+public class CreateSubjectHandler implements HttpHandler {
+       
+       private Logger logger = 
LoggerFactory.getLogger(CreateSubjectHandler.class);
+       
+       private static ObjectMapper jsonMapper;
+       
+       static {
+        jsonMapper = new ObjectMapper();        
+        jsonMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
+        jsonMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
+        jsonMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);          
+    }
+       
+    @Override
+    public void handle(HttpExchange httpExchange) throws IOException {
+        String result = "";
+        OutputStream out = httpExchange.getResponseBody();
+        try {
+            String params = NetUtils.parsePostBody(httpExchange);              
                  
+            SubjectCreateRequest subjectCreateRequest = 
jsonMapper.readValue(params, SubjectCreateRequest.class);                
+            String subject = subjectCreateRequest.getSubject();
+            
+            if (StringUtils.isBlank(subject)) {
+                result = "Create subject failed. Parameter subject not found.";
+                logger.error(result);
+                out.write(result.getBytes());
+                return;
+            }
+            SchemaService schemaService = getSchemaService();
+            SubjectResponse subjectResponse = 
schemaService.createSubject(subjectCreateRequest);
+            if (subjectResponse != null) {
+                logger.info("createTopic subject: {}", subject);               
       
+                httpExchange.getResponseHeaders().add("Content-Type", 
"appication/json");
+                httpExchange.sendResponseHeaders(200, 0);
+                result = jsonMapper.writeValueAsString(subjectResponse);       
         
+                logger.info(result);
+                out.write(result.getBytes());
+                return;
+            } else {
+                httpExchange.sendResponseHeaders(500, 0);
+                result = String.format("create subject failed! Server side 
error");
+                logger.error(result);
+                out.write(result.getBytes());
+                return;
+            }
+        } catch (ServiceException e) {                 
+               httpExchange.getResponseHeaders().add("Content-Type", 
"appication/json");
+            httpExchange.sendResponseHeaders(500, 0);                          
  
+            result = jsonMapper.writeValueAsString(e.getErrorResponse());
+            logger.error(result);
+            out.write(result.getBytes());
+            return;
+        } finally {
+            if (out != null) {
+                try {
+                    out.close();
+                } catch (IOException e) {
+                    logger.warn("out close failed...", e);
+                }
+            }
+        }
+    }
+    
+    private SchemaService getSchemaService() {

Review comment:
       fixed




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to