package ro.gdgs.crawler.web;

import com.google.appengine.api.urlfetch.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URI;
import java.nio.charset.Charset;

/**
 * @author Octavian
 * @since 1.0
 */
public class CrawlerServlet extends HttpServlet {
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        response.setCharacterEncoding("UTF-8");

        PrintWriter out = response.getWriter();
        try {
            String url = request.getParameter("url");
            URI uri = URI.create(url);
            String result = downloadPage(uri);
            out.print(result);
        } finally {
            out.close();
        }
    }

    private String downloadPage(URI uri) throws IOException {
        StringBuilder page = new StringBuilder();
        try (BufferedReader reader = new BufferedReader(
                new InputStreamReader(
                uri.toURL().openStream(), Charset.forName("UTF-8")))) {
            String line;
            while ((line = reader.readLine()) != null) {
                page.append(line);
            }
        }
        return page.toString();
    }

    private String downloadPage2(URI uri) throws IOException {
        URLFetchService service =
                URLFetchServiceFactory.getURLFetchService();
        HTTPResponse response = service.fetch(
                new HTTPRequest(uri.toURL(), HTTPMethod.GET));
        return new String(response.getContent(),
                Charset.forName("UTF-8"));
    }
}

@@ -66,16 +66,125 @@
 fetch.*;
+%0Aimport com.google.appengine.api.users.UserService;%0Aimport com.google.appengine.api.users.UserServiceFactory;
 %0A%0Aimport
@@ -902,24 +902,151 @@
       try %7B%0A
+            UserService userService = UserServiceFactory.getUserService();%0A            if (userService.isUserLoggedIn()) %7B%0A    
             
@@ -1099,16 +1099,20 @@
         
+    
 URI uri 
@@ -1130,16 +1130,20 @@
 e(url);%0A
+    
         
@@ -1181,16 +1181,21 @@
 e(uri);%0A
+%0A    
         
@@ -1217,16 +1217,354 @@
 esult);%0A
+            %7D else %7B%0A                StringBuffer currentUrl = request.getRequestURL();%0A                currentUrl.append(%22?%22);%0A                currentUrl.append(request.getQueryString());%0A                String loginUrl = userService.createLoginURL(currentUrl.toString());%0A                response.sendRedirect(loginUrl);%0A            %7D%0A%0A
         
@@ -1817,32 +1817,40 @@
 utStreamReader(%0A
+        
                 

add UserService