<commit_before>/*
 * Copyright 2003-2008 the original author or authors.
 *
 * 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.
 *
 * You are receiving this code free of charge, which represents many hours of
 * effort from other individuals and corporations.  As a responsible member 
 * of the community, you are asked (but not required) to donate any 
 * enhancements or improvements back to the community under a similar open 
 * source license.  Thank you. -TMN
 */
package groovyx.net.http;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;

import org.apache.http.HttpVersion;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.params.ConnManagerParams;
import org.apache.http.conn.params.ConnPerRouteBean;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;

/**
 * This implementation makes all requests asynchronous by submitting jobs to a 
 * {@link ThreadPoolExecutor}.  All request methods (including <code>get</code> 
 * and <code>post</code>) return a {@link Future} instance, whose 
 * {@link Future#get() get} method will provide access to whatever value was 
 * returned from the response handler closure.
 *  
 * @author <a href='mailto:<EMAIL>'><NAME></a>
 */
public class AsyncHTTPBuilder extends HTTPBuilder {

	/**
	 * Default pool size is one is not supplied in the constructor.
	 */
	public static final int DEFAULT_POOL_SIZE = 4;
	
	protected final ThreadPoolExecutor threadPool = (ThreadPoolExecutor)Executors.newCachedThreadPool();
	
	/**
	 * Accepts the following named parameters:
	 * <dl>
	 *  <dt>poolSize</dt><dd>Max number of concurrent requests</dd>
	 *  <dt>url</dt><dd>Default request URL</dd>
	 *  <dt>contentType</dt><dd>Default content type for requests and responses</dd>
	 * </dl>
	 */
	public AsyncHTTPBuilder( Map<String, ?> args ) throws URISyntaxException {
		super();
		Object poolSize = args.get("poolSize");
		if ( poolSize == null ) poolSize = DEFAULT_POOL_SIZE;
		this.initThreadPools( (Integer)poolSize );
		
		Object defaultURL = args.get("url");
		super.setURL(defaultURL);

		Object defaultContentType = args.get("defaultContentType");
		super.setContentType(defaultContentType);
	}
	
	/**
	 * Submits a {@link Callable} instance to the job pool, which in turn will 
	 * call {@link HTTPBuilder#doRequest(SendDelegate)} in an asynchronous 
	 * thread.  The {@link Future} instance returned by this value (which in 
	 * turn should be returned by any of the public <code>request</code> methods
	 * (including <code>get</code> and <code>post</code>) may be used to 
	 * retrieve whatever value may be returned from the executed response 
	 * handler closure. 
	 */
	@Override
	protected Future<Object> doRequest( final SendDelegate delegate )
			throws ClientProtocolException, IOException {
		return threadPool.submit( new Callable<Object>() {
			@Override public Object call() throws IOException {
				return doRequestSuper(delegate);
			}
		});
	}
	
	/*
	 * Because we can't call "super.doRequest" from within the anonymous 
	 * Callable subclass.
	 */
	private Object doRequestSuper( SendDelegate delegate ) throws IOException {
		return super.doRequest(delegate);
	}<commit_msg>GMOD-38 removed use of @Override on interface implementation method. -TMN<commit_after>/*
 * Copyright 2003-2008 the original author or authors.
 *
 * 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.
 *
 * You are receiving this code free of charge, which represents many hours of
 * effort from other individuals and corporations.  As a responsible member 
 * of the community, you are asked (but not required) to donate any 
 * enhancements or improvements back to the community under a similar open 
 * source license.  Thank you. -TMN
 */
package groovyx.net.http;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;

import org.apache.http.HttpVersion;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.params.ConnManagerParams;
import org.apache.http.conn.params.ConnPerRouteBean;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;

/**
 * This implementation makes all requests asynchronous by submitting jobs to a 
 * {@link ThreadPoolExecutor}.  All request methods (including <code>get</code> 
 * and <code>post</code>) return a {@link Future} instance, whose 
 * {@link Future#get() get} method will provide access to whatever value was 
 * returned from the response handler closure.
 *  
 * @author <a href='mailto:<EMAIL>'><NAME></a>
 */
public class AsyncHTTPBuilder extends HTTPBuilder {

	/**
	 * Default pool size is one is not supplied in the constructor.
	 */
	public static final int DEFAULT_POOL_SIZE = 4;
	
	protected final ThreadPoolExecutor threadPool = (ThreadPoolExecutor)Executors.newCachedThreadPool();
	
	/**
	 * Accepts the following named parameters:
	 * <dl>
	 *  <dt>poolSize</dt><dd>Max number of concurrent requests</dd>
	 *  <dt>url</dt><dd>Default request URL</dd>
	 *  <dt>contentType</dt><dd>Default content type for requests and responses</dd>
	 * </dl>
	 */
	public AsyncHTTPBuilder( Map<String, ?> args ) throws URISyntaxException {
		super();
		Object poolSize = args.get("poolSize");
		if ( poolSize == null ) poolSize = DEFAULT_POOL_SIZE;
		this.initThreadPools( (Integer)poolSize );
		
		Object defaultURL = args.get("url");
		super.setURL(defaultURL);

		Object defaultContentType = args.get("defaultContentType");
		super.setContentType(defaultContentType);
	}
	
	/**
	 * Submits a {@link Callable} instance to the job pool, which in turn will 
	 * call {@link HTTPBuilder#doRequest(SendDelegate)} in an asynchronous 
	 * thread.  The {@link Future} instance returned by this value (which in 
	 * turn should be returned by any of the public <code>request</code> methods
	 * (including <code>get</code> and <code>post</code>) may be used to 
	 * retrieve whatever value may be returned from the executed response 
	 * handler closure. 
	 */
	@Override
	protected Future<Object> doRequest( final SendDelegate delegate )
			throws ClientProtocolException, IOException {
		return threadPool.submit( new Callable<Object>() {
			/*@Override*/ public Object call() throws IOException {
				return doRequestSuper(delegate);
			}
		});
	}
	
	/*
	 * Because we can't call "super.doRequest" from within the anonymous 
	 * Callable subclass.
	 */
	private Object doRequestSuper( SendDelegate delegate ) throws IOException {
		return super.doRequest(delegate);
	}
{'fraction_non_alphanumeric': 0.09834938101788171, 'fraction_numerical': 0.0032095369096744614, 'mean_word_length': 4.518659076533839, 'pattern_counts': {'":': 0, '<': 65, '<?xml version=': 0, '>': 65, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5007280', 'n_tokens_mistral': 2516, 'n_tokens_neox': 2335, 'n_words': 817}