package focusedCrawler.crawler.async;

import java.net.URL;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import crawlercommons.fetcher.AbortedFetchException;
import crawlercommons.fetcher.FetchedResult;
import focusedCrawler.util.LinkRelevance;
import focusedCrawler.util.Page;
import focusedCrawler.util.parser.PaginaURL;
import focusedCrawler.util.storage.Storage;

public class FetchedResultHandler implements HttpDownloader.Callback {
    
    private static final Logger logger = LoggerFactory.getLogger(FetchedResultHandler.class);
    
    private Storage targetStorage;

    public FetchedResultHandler(Storage targetStorage) {
        this.targetStorage = targetStorage;
    }
    
    @Override
    public void completed(final FetchedResult response) {

        int statusCode = response.getStatusCode();
        if(statusCode >= 200 && statusCode < 300) {
            logger.info("Successfully downloaded URL=["+response.getBaseUrl()+"] HTTP-Response-Code="+statusCode);
            processData(response);
        } else {
            // TODO: Update metadata about page visits in link storage
            logger.info("Server returned bad code for URL=["+response.getBaseUrl()+"] HTTP-Response-Code="+statusCode);
        }
    }
    
    @Override
    public void failed(String url, final Exception e) {
        if(e instanceof AbortedFetchException) {
            AbortedFetchException afe = (AbortedFetchException) e;
            logger.info("Download aborted: \n>URL: {}\n>Reason: {}", url, afe.getAbortReason());
        } else {
            logger.info("Failed to download URL: "+url, e.getMessage());
        }
    }
    
    private void processData(FetchedResult response) {
        try {
            String body = new String(response.getContent());
            
            Page page = new Page(new URL(response.getFetchedUrl()), body);
            PaginaURL pageParser = new PaginaURL(page.getURL(), page.getContent());
            page.setPageURL(pageParser);
            
            LinkRelevance link = (LinkRelevance) response.getPayload().get(HttpDownloader.PAYLOAD_KEY);
            
            final double relevance = link.getRelevance();
            if(relevance > LinkRelevance.DEFAULT_HUB_RELEVANCE &&
               relevance < LinkRelevance.DEFAULT_AUTH_RELEVANCE){
                page.setHub(true);
            }
            
            page.setRelevance(relevance);
            
            logger.info(relevance + " Sending page to TargetStorage: "+ response.getFetchedUrl());
            targetStorage.insert(page);
            
        } catch (Exception e) {
            logger.error("Problem while processing data.", e);
        }
    }
    
}

@@ -53,16 +53,154 @@
 et.URL;%0A
+import java.util.Arrays;%0Aimport java.util.HashMap;%0Aimport java.util.List;%0Aimport java.util.Map;%0A%0Aimport org.apache.tika.metadata.Metadata;
 %0Aimport 
@@ -1870,21 +1870,93 @@
     
-String body =
+Page page = new Page(%0A                new URL(response.getBaseUrl()),%0A               
  new
@@ -1977,33 +1977,33 @@
 se.getContent())
-;
+,
 %0A            %0A  
@@ -2003,42 +2003,73 @@
     
-%0A            Page page = new Page(
+    parseResponseHeaders(response.getHeaders()),%0A                
 new 
@@ -2101,14 +2101,21 @@
 l())
-, body
+%0A            
 );%0A 
@@ -2933,20 +2933,470 @@
      %7D%0A    %7D
+%0A%0A    private Map%3CString, List%3CString%3E%3E parseResponseHeaders(Metadata headerAsMetadata) %7B%0A        Map%3CString, List%3CString%3E%3E responseHeaders = new HashMap%3C%3E();%0A        String%5B%5D names = headerAsMetadata.names();%0A        if(names != null && names.length %3E 0) %7B%0A            for(String name : names) %7B%0A                responseHeaders.put(name, Arrays.asList(headerAsMetadata.getValues(name)));%0A            %7D%0A        %7D%0A        return responseHeaders;%0A    %7D
 %0A    %0A%7D%0A

Store response headers in Page object