/*
 * Copyright 2016 LINE Corporation
 *
 * LINE Corporation licenses this file to you under the Apache License,
 * version 2.0 (the "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at:
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

package com.example.bot.spring.echo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import com.linecorp.bot.model.event.Event;
import com.linecorp.bot.model.event.MessageEvent;
import com.linecorp.bot.model.event.message.TextMessageContent;
import com.linecorp.bot.model.message.TextMessage;
import com.linecorp.bot.spring.boot.annotation.EventMapping;
import com.linecorp.bot.spring.boot.annotation.LineMessageHandler;


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;

@SpringBootApplication
@LineMessageHandler
public class EchoApplication {
    public static void main(String[] args) {
        SpringApplication.run(EchoApplication.class, args);
    }
        
    @EventMapping
    public TextMessage handleTextMessageEvent(MessageEvent<TextMessageContent> event)  {
        System.out.println("event: " + event);
        //return new TextMessage("TEST: "+event.getMessage().getText());
try{  
//code that may throw exception  
     URL url = new URL("http://cdn.crunchify.com/wp-content/uploads/code/json.sample.txt");

    BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
    String str;
    while ((str = in.readLine()) != null) {
      System.out.println(str); 
    }
    in.close();
}
  catch(IOException ex) {
    System.out.println("Couldn't open URL. Try again.");
  }
       return new TextMessage("TEST: "+event.getMessage().getText());
 
        //URL website = new URL("http://cdn.crunchify.com/wp-content/uploads/code/json.sample.txt");
        
//        return new TextMessage(response.toString());
    }

    @EventMapping
    public void handleDefaultMessageEvent(Event event) {
        System.out.println("event: " + event);
    }
   
}

@@ -1124,16 +1124,68 @@
 dler;%0A%0A%0A
+import java.net.URL;%0Aimport java.net.URLConnection;%0A
 import j
@@ -1241,36 +1241,16 @@
 Reader;%0A
-import java.net.URL;
 %0A%0A@Sprin
@@ -1668,61 +1668,196 @@
 ));%0A
+S
 tr
-y%7B  %0A//code that may thro
+ing result = getUrlAsString(%22https://www.google.com%22);%0ASystem.out.println(result);%0A        %0A       return ne
 w 
+T
 ex
-ception  %0A     URL url
+tMessage(%22TEST: %22+event.getMessage().getText());%0A %0A        //URL website
  = n
@@ -1936,154 +1936,156 @@
 %22);%0A
-%0A    BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));%0A    String str;%0A    while ((str = in.readLine()) != null
+        %0A//        return new TextMessage(response.toString());%0A    %7D%0A%0A    @EventMapping%0A    public void handleDefaultMessageEvent(Event event
 ) %7B%0A
+  
     
@@ -2109,14 +2109,27 @@
 tln(
-str
+%22event: %22 + event
 );
- 
 %0A   
@@ -2138,181 +2138,397 @@
 %0A   
- in.close();%0A%7D%0A  catch(IOException ex) %7B%0A    System.out.println(%22Couldn't open URL. Try again.%22);%0A  %7D%0A       return new TextMessage(%22TEST: %22+event.getMessage().getText
+%0Apublic static String getUrlAsString(String url)%0A%7B%0A    try%0A    %7B%0A        URL urlObj = new URL(url);%0A        URLConnection con = urlObj.openConnection();%0A%0A        con.setDoOutput(true); // we want the response %0A        con.setRequestProperty(%22Cookie%22, %22myCookie=test123%22);%0A        con.connect();%0A%0A        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream
 ())
+)
 ;%0A
- 
 %0A   
@@ -2536,291 +2536,396 @@
     
-//URL website = new URL(%22http://cdn.crunchify.com/wp-content/uploads/code/json.sample.txt%22);%0A        %0A//        return new TextMessage(response.toString());%0A    %7D%0A%0A    @EventMapping%0A    public void handleDefaultMessageEvent(Event event) %7B%0A        System.out.println(%22event: %22 + event
+StringBuilder response = new StringBuilder();%0A        String inputLine;%0A%0A        String newLine = System.getProperty(%22line.separator%22);%0A        while ((inputLine = in.readLine()) != null)%0A        %7B%0A            response.append(inputLine + newLine);%0A        %7D%0A%0A        in.close();%0A%0A        return response.toString();%0A    %7D%0A    catch (Exception e)%0A    %7B%0A        throw new RuntimeException(e
 );%0A 
@@ -2929,14 +2929,12 @@
 ;%0A    %7D%0A
-   
+%7D
 %0A%7D%0A

Update EchoApplication.java