/*
 * 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.net.*;
import java.io.*;

@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());

     String output  = getUrlContents("http://cdn.crunchify.com/wp-content/uploads/code/json.sample.txt");
    System.out.println(output);
        
         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);
    }
   
  private static String getUrlContents(String theUrl)
  {
    StringBuilder content = new StringBuilder();

    // many of these calls can throw exceptions, so i've just
    // wrapped them all in one try/catch statement.
    try
    {
      // create a url object
      URL url = new URL(theUrl);

      // create a urlconnection object
      URLConnection urlConnection = url.openConnection();

      // wrap the urlconnection in a bufferedreader
      BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));

      String line;

      // read from the urlconnection via the bufferedreader
      while ((line = bufferedReader.readLine()) != null)
      {
        content.append(line + "\n");
      }
      bufferedReader.close();
    }
    catch(Exception e)
    {
      e.printStackTrace();
    }
    return content.toString();
  }
}

@@ -1593,243 +1593,31 @@
     
-String output  = getUrlContents(%22http://cdn.crunchify.com/wp-content/uploads/code/json.sample.txt%22);%0A    System.out.println(output);%0A        %0A         return new TextMessage(%22TEST: %22+event.getMessage().getText());%0A        //URL website
+ try %7B%0A         URL url
  = n
@@ -1632,724 +1632,377 @@
 http
+s
 ://
-cdn.crunchify.com/wp-content/uploads/code/json.sample.txt%22);%0A        %0A//        return new TextMessage(response.toString()
+www.amrood.com%22);%0A         URLConnection urlConnection = url.openConnection(
 );%0A    
-%7D%0A%0A
     
-@EventMapping%0A    public void handleDefaultMessageEvent(Event event) %7B%0A        System.out.println(%22event: %22 + event);%0A    %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
+ HttpURLConnection connection = null;%0A         if(urlConnection instanceof HttpURLConnection) %7B%0A            connection = (HttpURLConnection) urlConnection;%0A         %7Delse %7B
 %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
+  System.out.println(%22Please enter an HTTP URL.%22);%0A            return;%0A         %7D%0A         %0A   
     
@@ -2018,30 +2018,18 @@
 dReader 
-bufferedReader
+in
  = new B
@@ -2042,16 +2042,29 @@
 dReader(
+%0A            
 new Inpu
@@ -2077,20 +2077,17 @@
 mReader(
-urlC
+c
 onnectio
@@ -2104,25 +2104,27 @@
 Stream()));%0A
-%0A
+   
       String
@@ -2128,75 +2128,62 @@
 ing 
-line
+urlString = %22%22
 ;%0A
-%0A
       
-// read from the urlconnection via the bufferedreader%0A
+   String current;%0A         %0A   
     
@@ -2193,32 +2193,22 @@
 hile
- 
 ((
-line = bufferedReader
+current = in
 .rea
@@ -2228,60 +2228,51 @@
 ull)
+ %7B
 %0A     
- %7B%0A
        
- content.append(line + %22%5Cn%22)
+urlString += current
 ;%0A      
 %7D%0A  
@@ -2267,24 +2267,27 @@
 ;%0A      
+   
 %7D%0A      
 buffered
@@ -2282,48 +2282,57 @@
     
-bufferedReader.close(
+   System.out.println(urlString
 );%0A    
-%7D%0A  
   
+%7D
 catch(
+IO
 Exce
@@ -2343,21 +2343,20 @@
 n e)
+ %7B
 %0A    
-%7B%0A
      
- 
 e.pr
@@ -2380,28 +2380,244 @@
     
+  
 %7D%0A    
-return content
+    %0A         return new TextMessage(%22TEST: %22+event.getMessage().getText());%0A        //URL website = new URL(%22http://cdn.crunchify.com/wp-content/uploads/code/json.sample.txt%22);%0A        %0A//        return new TextMessage(response
 .toS
@@ -2627,12 +2627,151 @@
 ng()
+)
 ;%0A  
-%7D
+  %7D%0A%0A    @EventMapping%0A    public void handleDefaultMessageEvent(Event event) %7B%0A        System.out.println(%22event: %22 + event);%0A    %7D%0A   %0A  
 %0A%7D%0A

Update EchoApplication.java