package org.bee.webBee.download;

import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.util.EntityUtils;
import org.bee.webBee.HttpClient.HttpClientPool;
import org.bee.webBee.html.Html;
import org.bee.webBee.linker.Page;
import org.bee.webBee.linker.Request;
import org.bee.webBee.processor.Setting;
import org.bee.webBee.processor.Task;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;

import java.io.IOException;
import java.util.logging.Logger;

/**
 * data 2017-03-23   23:43
 * E-mail   sis.nonacosa@gmail.com
 * @author sis.nonacosa
 */
public class HttpClientDownloader implements DownLoader {

    private Logger logger = Logger.getLogger(String.valueOf(getClass()));
//    private Page page = new Page();
    @Override
    public Page download(Request request, Task task) {
        Setting setting = task.getSetting();

        HttpRequestBase httpMethod =  null;
        httpMethod = HttpClientPool.getInstance().generateHttpMethod(request,task,httpMethod);

        HttpClientBuilder httpClientBuilder = HttpClientPool.getInstance().generateClient(setting,httpMethod);
        CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
        Page page = new Page();
        try {
            CloseableHttpResponse closeableHttpResponse = closeableHttpClient.execute(httpMethod);
            System.out.println(closeableHttpResponse.getStatusLine().getStatusCode());
            System.out.println(request.getUrl());
            page.setHtml(closeableHttpResponse);
            page.setRequest(request);
            page.setUrl(request.getUrl());
            page.setStatusCode(closeableHttpResponse.getStatusLine().getStatusCode());
            task.setHtml(page.getHtml()); //自传递api到Bee处理器
            closeableHttpResponse.close();
            //todo  do while策略处理异常

            return page;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return page;
    }

//    public Html getHtml(){
//        return page.getHtml();
//    }
}

@@ -172,16 +172,63 @@
 ntPool;%0A
+import org.bee.webBee.HttpClient.HttpResponse;%0A
 import o
@@ -591,27 +591,109 @@
 java.io.
-IOException
+File;%0Aimport java.io.FileOutputStream;%0Aimport java.io.IOException;%0Aimport java.io.InputStream
 ;%0Aimport
@@ -1066,518 +1066,154 @@
     
-Setting setting = task.getSetting();%0A%0A        HttpRequestBase httpMethod =  null;%0A        httpMethod = HttpClientPool.getInstance().generateHttpMethod(request,task,httpMethod);%0A%0A        HttpClientBuilder httpClientBuilder = HttpClientPool.getInstance().generateClient(setting,httpMethod);%0A        CloseableHttpClient closeableHttpClient = httpClientBuilder.build();%0A        Page page = new Page();%0A        try %7B%0A            CloseableHttpResponse closeableHttpResponse = closeableHttpClient.execute(httpMethod);
+CloseableHttpResponse closeableHttpResponse  = HttpResponse.getInstance(request,task).getResponse();%0A        Page page = new Page();%0A        try %7B
 %0A   
@@ -1338,32 +1338,33 @@
 uest.getUrl());%0A
+%0A
             page
@@ -1434,16 +1434,48 @@
 quest);%0A
+            page.setTast(task);%0A
         
@@ -1867,76 +1867,7 @@
  %7D%0A%0A
-//    public Html getHtml()%7B%0A//        return page.getHtml();%0A//    %7D
 %0A%7D%0A

add task queue to Page