package edu.oakland.uPortalTesterWebapp.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

@Controller
public class Application {

    private static final Log log = LogFactory.getLog(Application.class);

    private Document getDocument(String url){
        try{
            return Jsoup.connect(url).get();
        }catch(Exception e){
            throw new IllegalArgumentException("Invalid url.");
        }
    }

    @RequestMapping(value="/", method=RequestMethod.GET)
    public ModelAndView home(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView home = new ModelAndView("home");
        home.addObject("running", getRunning(request));
        return home;
    }

    private boolean getRunning(HttpServletRequest request) {

		String ipAddress = request.getRemoteAddr();
		if(log.isDebugEnabled()) {
			log.debug("TEST");
		}

	    Document document = getDocument("http://localhost:8080/uPortal");
	    if ( ( document.getElementById("portal").html() ).length() > 0 ) {
		    return true;
	    } else {
		    return false;
	    }
    }
}

@@ -815,56 +815,152 @@
     
-throw new IllegalArgumentException(%22Invalid url.
+log.error(%22Exception: getDocument: %22, e);%0A            log.error(%22Returning new Document to fail gracefully.%22);%0A            return new Document(%22
 %22);%0A
@@ -1324,16 +1324,47 @@
 t) %7B%0A%0A%09%09
+if(log.isDebugEnabled()) %7B%0A%09%09  
 String i
@@ -1405,54 +1405,55 @@
 ;%0A%09%09
-if(
+%09
 log.
-isD
+d
 ebug
-Enabled()) %7B%0A%09%09%09log.debug(%22TEST%22
+(%22Request generated by: %22 + ipAddress
 );%0A%09
@@ -1531,20 +1531,36 @@
 %22);%0A
-%09
     
-if (
+  try %7B%0A        return
  (
- 
 docu
@@ -1615,47 +1615,80 @@
 %3E 0 
-) %7B%0A%09%09    return true;%0A%09    %7D else %7B%0A%09%09
+? true : false;%0A      %7D catch (Exception e) %7B%0A        log.error(e);%0A    
     
@@ -1701,17 +1701,18 @@
  false;%0A
-%09
+  
     %7D%0A  

Add proper logging #1