package org.zstack.core.cloudbus;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.zstack.core.Platform;
import org.zstack.core.thread.AsyncThread;
import org.zstack.header.exception.CloudRuntimeException;
import org.zstack.utils.Utils;
import org.zstack.utils.logging.CLogger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Enumeration;

@Controller
public class CloudBusController {
    private static final CLogger logger = Utils.getLogger(CloudBusController.class);

    @Autowired
    private CloudBusImpl3 cloudbus;

    private HttpEntity<String> toHttpEntity(HttpServletRequest req) {
        try {
            StringBuilder sb = new StringBuilder();
            String line;
            while ((line = req.getReader().readLine()) != null) {
                sb.append(line);
            }
            req.getReader().close();

            HttpHeaders header = new HttpHeaders();
            for (Enumeration e = req.getHeaderNames(); e.hasMoreElements() ;) {
                String name = e.nextElement().toString();
                header.add(name, req.getHeader(name));
            }

            return new HttpEntity<>(sb.toString(), header);
        } catch (Exception e) {
            logger.warn(e.getMessage(), e);
            throw new CloudRuntimeException(e);
        }
    }

    @RequestMapping(value = CloudBusImpl3.HTTP_BASE_URL, method = RequestMethod.POST)
    public void handle(HttpServletRequest request, HttpServletResponse response) {
        HttpEntity<String> entity = toHttpEntity(request);
        cloudbus.handleHttpRequest(entity, response);
    }
}

@@ -32,71 +32,8 @@
 s;%0A%0A
-import org.springframework.beans.factory.annotation.Autowired;%0A
 impo
@@ -329,51 +329,8 @@
 rm;%0A
-import org.zstack.core.thread.AsyncThread;%0A
 impo
@@ -553,88 +553,8 @@
 se;%0A
-import java.io.IOException;%0Aimport java.lang.reflect.InvocationTargetException;%0A
 impo
@@ -716,60 +716,8 @@
 );%0A%0A
-    @Autowired%0A    private CloudBusImpl3 cloudbus;%0A%0A
     
@@ -1721,24 +1721,79 @@
         
-c
+Platform.getComponentLoader().getComponent(C
 loud
-b
+B
 us
+Impl3.class)
 .handleH

implement cloudbus using http instead of rabbitmq