package com.orange.oss.cloudfoundry.cscpi;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.orange.oss.cloudfoundry.cscpi.domain.CPIResponse;


@RestController
@RequestMapping("/cpi")
public class CPIRestController {
	
	private static Logger logger=LoggerFactory.getLogger(CPIRestController.class.getName());
	
	@Autowired
	private CPIAdapter cpiAdapter;
	
	
	@RequestMapping(method=RequestMethod.POST,produces="application/json")
	public @ResponseBody JsonNode execute(@RequestBody JsonNode  request) {
		logger.info("==> received \n {}",request);
		CPIResponse response=this.cpiAdapter.execute(request);
		logger.info("cpi-core response : {}",response);
		
		
		JsonNodeFactory nodeFactory = new JsonNodeFactory() {
		};

		ObjectNode node = nodeFactory.objectNode();
		ObjectNode child = nodeFactory.objectNode(); // the child
		child.put("result", response.result.get(0));
		child.put("error", response.error);
		child.put("log", response.log);
		
		logger.info("generated json response payload {}",node.asText());
		
		return node;

	}


}

@@ -512,24 +512,76 @@
 d.JsonNode;%0A
+import com.fasterxml.jackson.databind.ObjectMapper;%0A
 import com.f
@@ -1270,101 +1270,125 @@
 %09%0A%09%09
-JsonNodeFactory nodeFactory = new JsonNodeFactory() %7B%0A%09%09%7D;%0A%0A%09%09ObjectNode node = nodeFactory.o
+%0A%09%09%0A%09%09ObjectMapper mapper = new ObjectMapper();%0A%09%09JsonNode rootNode = mapper.createObjectNode(); // will be of type O
 bjec
@@ -1392,22 +1392,23 @@
 jectNode
-();
 %0A%09%09
+//((
 ObjectNo
@@ -1413,63 +1413,66 @@
 Node
- child = nodeFactory.objectNode(); // the child%0A%09%09child
+) rootNode).put(%22name%22, %22Tatu%22);%0A%09%09((ObjectNode) rootNode)
 .put
@@ -1505,29 +1505,47 @@
 .get(0));%0A%09%09
-child
+((ObjectNode) rootNode)
 .put(%22error%22
@@ -1569,13 +1569,31 @@
 ;%0A%09%09
-child
+((ObjectNode) rootNode)
 .put
@@ -1669,17 +1669,21 @@
 oad %7B%7D%22,
-n
+rootN
 ode.asTe
@@ -1701,17 +1701,21 @@
 %09return 
-n
+rootN
 ode;%0A%0A%09%7D

fix cpi-core rest output json serialization