/*
 * 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.InputStream;
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 {
            URL url = new URL("http://www.itcuties.com");
            InputStream is = url.openStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
             
            String line;
            while ( (line = br.readLine()) != null)
                System.out.println(line);
             
            br.close();
            is.close();
             
        } catch (Exception e) {
            e.printStackTrace();
        }   
         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);
    }
   

}

@@ -1140,110 +1140,32 @@
 ava.
-io.BufferedReader;%0Aimport java.io.InputStream;%0Aimport java.io.InputStreamReader;%0Aimport java.net.URL;%0A
+net.*;%0Aimport java.io.*;
 %0A%0A@S
@@ -1593,472 +1593,138 @@
     
-   try %7B%0A            URL url = new URL(%22http://www.itcuties.com%22);%0A            InputStream is = url.openStream();%0A            BufferedReader br = new BufferedReader(new InputStreamReader(is));%0A             %0A            String line;%0A            while ( (line = br.readLine()) != null)%0A                System.out.println(line);%0A             %0A            br.close();%0A            is.close();%0A             %0A        %7D catch (Exception e) %7B%0A            e.printStackTrace(
+String output  = getUrlContents(%22http://cdn.crunchify.com/wp-content/uploads/code/json.sample.txt%22);%0A    System.out.println(output
 );%0A 
@@ -1734,12 +1734,8 @@
     
-%7D   
 %0A   
@@ -2107,11 +2107,896 @@
   %7D%0A   %0A
+  private static String getUrlContents(String theUrl)%0A  %7B%0A    StringBuilder content = new StringBuilder();%0A%0A    // many of these calls can throw exceptions, so i've just%0A    // wrapped them all in one try/catch statement.%0A    try%0A    %7B%0A      // create a url object%0A      URL url = new URL(theUrl);%0A%0A      // create a urlconnection object%0A      URLConnection urlConnection = url.openConnection();%0A%0A      // wrap the urlconnection in a bufferedreader%0A      BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));%0A%0A      String line;%0A%0A      // read from the urlconnection via the bufferedreader%0A      while ((line = bufferedReader.readLine()) != null)%0A      %7B%0A        content.append(line + %22%5Cn%22);%0A      %7D%0A      bufferedReader.close();%0A    %7D%0A    catch(Exception e)%0A    %7B%0A      e.printStackTrace();%0A    %7D%0A    return content.toString();%0A  %7D
 %0A%7D%0A

Update EchoApplication.java