package org.jenkinsci.plugins.codesonar.services;

import hudson.AbortException;
import java.io.InputStream;
import java.io.Serializable;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.io.IOUtils;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.client.fluent.Request;
import org.apache.http.client.fluent.Response;

/**
 *
 * @author Andrius
 */
public class HttpService implements Serializable {
    
    private static final Logger logger = Logger.getLogger(HttpService.class.getName());
    
    public String getContentFromUrlAsString(String url) throws AbortException { 
        System.out.println("------------sending request----------------");
        
        System.out.println("------------url----------------");
        System.out.println(url);
        
        logger.fine(String.format("Request sent to %s", url));        
        String output;
        try {
            Response response = Request.Get(url).execute();
            
            HttpResponse httpResponse = response.returnResponse();

            System.out.println("------------reading headers----------------");
            Header[] allHeaders = httpResponse.getAllHeaders();
            for (Header header : allHeaders) {
                System.out.println(header.getName());
                System.out.println(header.getValue());
                System.out.println("----------------------------");
            }
            
            InputStream contentStream = httpResponse.getEntity().getContent();
            output = IOUtils.toString(contentStream); 
            
//                output = response.returnContent().asString();
//            output = Request.Get(url).execute().returnContent().asString();
        } catch (Exception e) {
            logger.log(Level.SEVERE, String.format("[CodeSonar] Error on url: %s", url), e);
            throw new AbortException(String.format("[CodeSonar] Error on url: %s%n[CodeSonar] Message is: %s", url, e.getMessage()));
        } 
        
        return output;
    }
}

@@ -131,16 +131,39 @@
 izable;%0A
+import java.util.List;%0A
 import j
@@ -325,16 +325,106 @@
 sponse;%0A
+import org.apache.http.client.CookieStore;%0Aimport org.apache.http.client.fluent.Executor;%0A
 import o
@@ -512,89 +512,367 @@
 se;%0A
-%0A/**%0A *%0A * @author Andrius%0A */%0Apublic class HttpService implements Serializable %7B
+import org.apache.http.cookie.Cookie;%0Aimport org.apache.http.impl.client.BasicCookieStore;%0A%0A/**%0A *%0A * @author Andrius%0A */%0Apublic class HttpService implements Serializable %7B%0A    CookieStore httpCookieStore;%0A    Executor executor;%0A    %0A    public HttpService() %7B%0A        httpCookieStore = new BasicCookieStore();%0A        executor = Executor.newInstance();%0A    %7D
 %0A   
@@ -1301,25 +1301,25 @@
 rl));       
- 
+%0A
 %0A        Str
@@ -1376,16 +1376,83 @@
 ponse = 
+executor.cookieStore(httpCookieStore)%0A                    .execute(
 Request.
@@ -1459,25 +1459,16 @@
 Get(url)
-.execute(
 );%0A     
@@ -1929,32 +1929,467 @@
  %7D%0A            %0A
+            System.out.println(%22------------reading cookie store----------------%22);%0A            List%3CCookie%3E cookies = httpCookieStore.getCookies();%0A            for (Cookie cooky : cookies) %7B%0A                String str = String.format(%22cookie name %25s; value %25s%22, cooky.getName(), cooky.getValue());%0A                System.out.println(str);%0A                System.out.println(%22----------------------------%22);%0A            %7D%0A            %0A
             Inpu

add the use of cookie store