<commit_before>/**
 *                    
 * @author greg (at) myrobotlab.org
 *  
 * This file is part of MyRobotLab (http://myrobotlab.org).
 *
 * MyRobotLab is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version (subject to the "Classpath" exception
 * as provided in the LICENSE.txt file that accompanied this code).
 *
 * MyRobotLab is distributed in the hope that it will be useful or fun,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * All libraries in thirdParty bundle are subject to their own license
 * requirements - please refer to http://myrobotlab.org/libraries for 
 * details.
 * 
 * Enjoy !
 * 
 * */

package org.myrobotlab.service;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.httpclient.HttpException;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.myrobotlab.framework.Service;
import org.myrobotlab.framework.ServiceType;
import org.myrobotlab.logging.Level;
import org.myrobotlab.logging.LoggerFactory;
import org.myrobotlab.logging.Logging;
import org.myrobotlab.logging.LoggingFactory;
import org.myrobotlab.service.data.HttpData;
import org.myrobotlab.service.interfaces.HttpDataListener;
import org.myrobotlab.service.interfaces.HttpResponseListener;
import org.myrobotlab.service.interfaces.ServiceInterface;
import org.slf4j.Logger;

/**
 * HttpClient - wrapper for Apache HttpClient
 * 
 * @author GroG
 * 
 *         TODO - asynchronous call back similar to AngularJS promise - or at
 *         least a callback method is call .. onHttpResponse
 * 
 *         Synchronous or Asynchrounous - Synchronous by default, Asynchronous
 *         if a callback method is supplied or Non-Blocking method is called
 */
public class HttpClient extends Service implements HttpDataListener, HttpResponseListener {

  private static final long serialVersionUID = 1L;

  // this httpclient should be thread safe -
  // transient org.apache.commons.httpclient.De client = null;
  // DefaultHttpClient client = new DefaultHttpClient();

  public final static Logger log = LoggerFactory.getLogger(HttpClient.class);

  /**
   * This static method returns all the details of the class without it having
   * to be constructed. It has description, categories, dependencies, and peer
   * definitions.
   * 
   * @return ServiceType - returns all the data
   * 
   */
  static public ServiceType getMetaData() {

    ServiceType meta = new ServiceType(HttpClient.class.getCanonicalName());
    meta.addDescription("an HTTP client, used to fetch information on the web");
    meta.addCategory("network");
    meta.addDependency("org.apache.commons.httpclient", "4.5.2");
    // TODO: this depends on an apache http 3x jar also!
    meta.addDependency("commons-httpclient", "3.1");
    
    return meta;
  }

  transient DefaultHttpClient client;

  transient HashMap<String, String> formFields = new HashMap<String, String>();

  public HttpClient(String n) {
    super(n);
  }

  public void addFormField(String name, String value) {
    formFields.put(name, value);
  }

  public void addHttpDataListener(ServiceInterface listener) {
    /*
     * TODO - finish this thought out .. it would mean a Map of method
     * signatures to interface methods .. and direct callbacks Pro - is an
     * optimization Con - is potentially blocking the callback thread for "too"<commit_msg>finished fix for httpclient - don't use DefaultHttpClient !!!<commit_after>/**
 *                    
 * @author greg (at) myrobotlab.org
 *  
 * This file is part of MyRobotLab (http://myrobotlab.org).
 *
 * MyRobotLab is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version (subject to the "Classpath" exception
 * as provided in the LICENSE.txt file that accompanied this code).
 *
 * MyRobotLab is distributed in the hope that it will be useful or fun,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * All libraries in thirdParty bundle are subject to their own license
 * requirements - please refer to http://myrobotlab.org/libraries for 
 * details.
 * 
 * Enjoy !
 * 
 * */

package org.myrobotlab.service;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.httpclient.HttpException;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.myrobotlab.framework.Service;
import org.myrobotlab.framework.ServiceType;
import org.myrobotlab.logging.Level;
import org.myrobotlab.logging.LoggerFactory;
import org.myrobotlab.logging.Logging;
import org.myrobotlab.logging.LoggingFactory;
import org.myrobotlab.service.data.HttpData;
import org.myrobotlab.service.interfaces.HttpDataListener;
import org.myrobotlab.service.interfaces.HttpResponseListener;
import org.myrobotlab.service.interfaces.ServiceInterface;
import org.slf4j.Logger;

/**
 * HttpClient - wrapper for Apache HttpClient
 * 
 * @author GroG
 * 
 *         TODO - asynchronous call back similar to AngularJS promise - or at
 *         least a callback method is call .. onHttpResponse
 * 
 *         Synchronous or Asynchrounous - Synchronous by default, Asynchronous
 *         if a callback method is supplied or Non-Blocking method is called
 */
public class HttpClient extends Service implements HttpDataListener, HttpResponseListener {

  private static final long serialVersionUID = 1L;

  // this httpclient should be thread safe -
  // transient org.apache.commons.httpclient.De client = null;
  // DefaultHttpClient client = new DefaultHttpClient();

  public final static Logger log = LoggerFactory.getLogger(HttpClient.class);

  /**
   * This static method returns all the details of the class without it having
   * to be constructed. It has description, categories, dependencies, and peer
   * definitions.
   * 
   * @return ServiceType - returns all the data
   * 
   */
  static public ServiceType getMetaData() {

    ServiceType meta = new ServiceType(HttpClient.class.getCanonicalName());
    meta.addDescription("an HTTP client, used to fetch information on the web");
    meta.addCategory("network");
    meta.addDependency("org.apache.commons.httpclient", "4.5.2");
    // TODO: this depends on an apache http 3x jar also!
    meta.addDependency("commons-httpclient", "3.1");
    
    return meta;
  }

  transient CloseableHttpClient client;

  transient HashMap<String, String> formFields = new HashMap<String, String>();

  public HttpClient(String n) {
    super(n);
  }

  public void addFormField(String name, String value) {
    formFields.put(name, value);
  }

  public void addHttpDataListener(ServiceInterface listener) {
    /*
     * TODO - finish this thought out .. it would mean a Map of method
     * signatures to interface methods .. and direct callbacks Pro - is an
     * optimization Con - is potentially blocking the callback thread for "too"
{'fraction_non_alphanumeric': 0.0860302677532014, 'fraction_numerical': 0.0020954598370197903, 'mean_word_length': 3.9918651946542707, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3821575', 'n_tokens_mistral': 2405, 'n_tokens_neox': 2343, 'n_words': 776}