/*
 * #%L
 * wcm.io
 * %%
 * Copyright (C) 2019 wcm.io
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */
package io.wcm.qa.glnm.sampling.jsoup.base;

import java.util.HashMap;
import java.util.Map;

import org.jsoup.Connection;
import org.jsoup.Jsoup;

import io.wcm.qa.glnm.sampling.Sampler;
import io.wcm.qa.glnm.sampling.base.CachingBasedSampler;

/**
 * Functionality that helps building Jsoup based samplers.
 *
 * @param <T> type of sample returned by sampler
 * @since 3.0.0
 */
public abstract class JsoupBasedSampler<T> extends CachingBasedSampler<T> {

  private String url;
  private Map<String, String> requestCookies = new HashMap<String, String>();
  private Sampler<Map<String, String>> cookieSampler;

  /**
   * <p>Constructor for JsoupBasedSampler.</p>
   *
   * @param url to connect to
   * @since 3.0.0
   */
  public JsoupBasedSampler(String url) {
    setUrl(url);
  }

  /**
   * <p>Getter for the field <code>cookieSampler</code>.</p>
   *
   * @return a {@link io.wcm.qa.glnm.sampling.Sampler} object.
   * @since 3.0.0
   */
  public Sampler<Map<String, String>> getCookieSampler() {
    return cookieSampler;
  }

  /**
   * <p>Getter for the field <code>requestCookies</code>.</p>
   *
   * @return a {@link java.util.Map} object.
   * @since 3.0.0
   */
  public Map<String, String> getRequestCookies() {
    return requestCookies;
  }

  /**
   * <p>Getter for the field <code>url</code>.</p>
   *
   * @return a {@link java.lang.String} object.
   * @since 3.0.0
   */
  public String getUrl() {
    return url;
  }

  /**
   * <p>Setter for the field <code>cookieSampler</code>.</p>
   *
   * @param cookieSampler a {@link io.wcm.qa.glnm.sampling.Sampler} object.
   * @since 3.0.0
   */
  public void setCookieSampler(Sampler<Map<String, String>> cookieSampler) {
    this.cookieSampler = cookieSampler;
  }

  /**
   * <p>Setter for the field <code>requestCookies</code>.</p>
   *
   * @param cookies a {@link java.util.Map} object.
   * @since 3.0.0
   */
  public void setRequestCookies(Map<String, String> cookies) {
    this.requestCookies = cookies;
  }

  /**
   * @return {@link JsoupConnectionProvider} to use for connection
   */
  protected JsoupConnectionProvider getConnectionProvider() {
    return new JsoupConnectionProvider() {
      @Override
      public Connection getConnection() {
        return Jsoup.connect(getUrl());
      }
    };
  }

  /**
   * @return connection used to fetch document
   */
  protected Connection getJsoupConnection() {
    Connection connection = getConnectionProvider().getConnection();
    if (useRequestCookies()) {
      connection.cookies(getRequestCookies());
    }
    if (useCookieSampler()) {
      connection.cookies(getCookieSampler().sampleValue());
    }
    connection.ignoreContentType(true);
    return connection;
  }

  protected void setUrl(String newUrl) {
    this.url = newUrl;
  }

  protected boolean useCookieSampler() {
    return getCookieSampler() != null;
  }

  protected boolean useRequestCookies() {
    return getRequestCookies() != null;
  }

}

@@ -1089,26 +1089,112 @@
 private 
-String url
+JsoupConnectionProvider connectionProvider;%0A  private Sampler%3CMap%3CString, String%3E%3E cookieSampler
 ;%0A  priv
@@ -1280,49 +1280,17 @@
 te S
-ampler%3CMap%3CString, String%3E%3E cookieSampler
+tring url
 ;%0A%0A 
@@ -2115,24 +2115,164 @@
 n url;%0A  %7D%0A%0A
+  protected void setConnectionProvider(JsoupConnectionProvider connectionProvider) %7B%0A    this.connectionProvider = connectionProvider;%0A  %7D%0A%0A
   /**%0A   * %3C
@@ -2950,22 +2950,76 @@
 ) %7B%0A    
-return
+if (connectionProvider == null) %7B%0A      connectionProvider =
  new Jso
@@ -3039,24 +3039,27 @@
 rovider() %7B%0A
+%0A  
       @Overr
@@ -3068,16 +3068,18 @@
 e%0A      
+  
 public C
@@ -3102,24 +3102,26 @@
 nection() %7B%0A
+  
         retu
@@ -3158,15 +3158,57 @@
     
-%7D
+  %7D%0A
 %0A    
-%7D
+  %7D;%0A    %7D%0A    return connectionProvider
 ;%0A  

add setter for connection provider (#39)