package hello;

import org.springframework.stereotype.Controller;
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.json.JSONObject;

@Controller
@RequestMapping("/webhook")
public class HelloWorldController {

    @RequestMapping(method = RequestMethod.POST)
    public @ResponseBody String webhook(@RequestBody String obj){
       String text = null;
       String source = null;
       String name=null;
       String respostring=null;
       String text1=null;
       String rescode_str=null;
try{
	    	JSONObject objS = new JSONObject(obj);
//          System.out.println(objS.get("id"));
          JSONObject result = objS.getJSONObject("result");
	        text = result.getString("resolvedQuery");
	  JSONObject originalRequest = objS.getJSONObject("originalRequest");
	        source = originalRequest.getString("source");
	JSONObject data = originalRequest.getJSONObject("data");
	JSONObject user = data.getJSONObject("user");
	        name=user.getString("name");

	        text1="I love India";
	      //  int rescode=authorize();
	    //    rescode_str=String.valueOf(rescode);//Now it will return "10"  
	HelloWorldController hwc=new HelloWorldController();
	String res=hwc.authorize(obj);
           
            
	    
          
}catch(Exception e){
	text = "last ctach"+e.getMessage();
}

	       return  "{speech: "+res+",displayText: "+res+", source: biz-webhook-sample}";
	       //return  "{speech: "+text+" source "+source+",displayText: "+text+", source: biz-webhook-sample}";
	    }
	  public String authorize(String obj){
  
		 StringBuilder result=null;
                 int statusCode = 0;
		 try {
            int BUFFER_SIZE = 4096;
          URL url = new URL("http://34.196.246.23:8101/MessagingToolController?webhookrespo=hi");
            HttpURLConnection http = (HttpURLConnection) url.openConnection();

            http.setRequestMethod("POST");
            http.setRequestProperty("Content-Type", "application/json");
      
             http.setUseCaches(false);
        
            http.setDoOutput(true);
   
            JSONObject json = new JSONObject();
            json.put("userName",  obj.toString());
          
           DataOutputStream wr = new DataOutputStream(http.getOutputStream());
           wr.writeBytes(json.toString());
           wr.flush();
           wr.close();
            
            /******************/
             /*System.out.println(" nlp_response step 8 : ");
             InputStream inputStream1= new ByteArrayInputStream(obj.toString().getBytes());
 System.out.println(" nlp_response step 9 : ");
            OutputStream outputStream = http.getOutputStream();
 System.out.println(" nlp_response step 10 : ");
            byte[] buffer = new byte[BUFFER_SIZE];
            int bytesRead = -1;
            while ((bytesRead = inputStream1.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }
             System.out.println(" nlp_response step 11 : ");
            outputStream.flush();
            outputStream.close();
            inputStream1.close();*/
            /******************/
         
            statusCode = http.getResponseCode();
            System.out.println("code=======" + statusCode);
            String newLine = System.getProperty("line.separator");
            BufferedReader reader = new BufferedReader(new InputStreamReader(http.getInputStream()));
            result = new StringBuilder();
            String line;
            boolean flag = false;
            while ((line = reader.readLine()) != null) {
                result.append(flag ? newLine : "").append(line);
                flag = true;
            }
            System.out.println(result);
        } catch (Exception e) {
            System.out.println("error  " + e.getMessage());
        }
               //  return result;
                 return result;
	}
	
}

@@ -1715,2373 +1715,8 @@
 %0A%09  
-public String authorize(String obj)%7B%0A  %0A%09%09 StringBuilder result=null;%0A                 int statusCode = 0;%0A%09%09 try %7B%0A            int BUFFER_SIZE = 4096;%0A          URL url = new URL(%22http://34.196.246.23:8101/MessagingToolController?webhookrespo=hi%22);%0A            HttpURLConnection http = (HttpURLConnection) url.openConnection();%0A%0A            http.setRequestMethod(%22POST%22);%0A            http.setRequestProperty(%22Content-Type%22, %22application/json%22);%0A      %0A             http.setUseCaches(false);%0A        %0A            http.setDoOutput(true);%0A   %0A            JSONObject json = new JSONObject();%0A            json.put(%22userName%22,  obj.toString());%0A          %0A           DataOutputStream wr = new DataOutputStream(http.getOutputStream());%0A           wr.writeBytes(json.toString());%0A           wr.flush();%0A           wr.close();%0A            %0A            /******************/%0A             /*System.out.println(%22 nlp_response step 8 : %22);%0A             InputStream inputStream1= new ByteArrayInputStream(obj.toString().getBytes());%0A System.out.println(%22 nlp_response step 9 : %22);%0A            OutputStream outputStream = http.getOutputStream();%0A System.out.println(%22 nlp_response step 10 : %22);%0A            byte%5B%5D buffer = new byte%5BBUFFER_SIZE%5D;%0A            int bytesRead = -1;%0A            while ((bytesRead = inputStream1.read(buffer)) != -1) %7B%0A                outputStream.write(buffer, 0, bytesRead);%0A            %7D%0A             System.out.println(%22 nlp_response step 11 : %22);%0A            outputStream.flush();%0A            outputStream.close();%0A            inputStream1.close();*/%0A            /******************/%0A         %0A            statusCode = http.getResponseCode();%0A            System.out.println(%22code=======%22 + statusCode);%0A            String newLine = System.getProperty(%22line.separator%22);%0A            BufferedReader reader = new BufferedReader(new InputStreamReader(http.getInputStream()));%0A            result = new StringBuilder();%0A            String line;%0A            boolean flag = false;%0A            while ((line = reader.readLine()) != null) %7B%0A                result.append(flag ? newLine : %22%22).append(line);%0A                flag = true;%0A            %7D%0A            System.out.println(result);%0A        %7D catch (Exception e) %7B%0A            System.out.println(%22error  %22 + e.getMessage());%0A        %7D%0A               //  return result;%0A                 return result;%0A%09%7D
 %0A%09%0A%7D

Update HelloWorldController.java