<commit_before>/*   
 * Copyright [2013] [<NAME>]
 *
 * 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.
 */
package com.opens.asyncokhttpclient;

import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.util.Map;

public class AsyncHttpRequest implements Runnable {
	
	private AsyncHttpResponse mResponse;
	private HttpURLConnection mClient;
	private RequestParams mRequestParams;
	private RequestModel mRequest;
	
	public AsyncHttpRequest(HttpURLConnection client, AsyncHttpResponse responseHandler, 
			RequestParams params, RequestModel request) {
		this.mResponse = responseHandler;
		this.mClient = client;
		this.mRequest = request;
		this.mRequestParams = params;
	}
	
	@Override
	public void run() {
		try {
			if(this.mResponse != null) this.mResponse.sendStartMessage();
			this.makeRequest();
			if(this.mResponse != null) this.mResponse.sendEndMessage();
		} catch(Exception e) {
			if(this.mResponse != null) this.mResponse.sendEndMessage();
		}
	}
	
	private void makeRequest() throws Exception {
		if(!Thread.currentThread().isInterrupted()) {
			try {
				if(!Thread.currentThread().isInterrupted()) {
					if(this.mResponse != null) { //TODO - Possible resource leak, close the inputstream
						this.mClient.setRequestMethod(this.mRequest.getRequestMethod());
						if(this.mRequestParams != null && !this.mRequest.getRequestMethod().equals("GET")) {
							this.mClient.setDoOutput(true);
							OutputStream params = this.mClient.getOutputStream();
							params.write(this.mRequestParams.toString().getBytes());
							params.close();
						}
						for(Map.Entry<String, String> entry : this.mRequest.getHeaders().entrySet()) {
							this.mClient.setRequestProperty(entry.getKey(), entry.getValue());
						}
						this.mResponse.sendResponseMessage(this.mClient);
					}
				}
			} catch(Exception e) {
				if(!Thread.currentThread().isInterrupted()) throw e;
			}
		}
	}
	
}<commit_msg>Set the header fields before getOutputStream<commit_after>/*   
 * Copyright [2013] [<NAME>]
 *
 * 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.
 */
package com.opens.asyncokhttpclient;

import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.util.Map;

public class AsyncHttpRequest implements Runnable {
	
	private AsyncHttpResponse mResponse;
	private HttpURLConnection mClient;
	private RequestParams mRequestParams;
	private RequestModel mRequest;
	
	public AsyncHttpRequest(HttpURLConnection client, AsyncHttpResponse responseHandler, 
			RequestParams params, RequestModel request) {
		this.mResponse = responseHandler;
		this.mClient = client;
		this.mRequest = request;
		this.mRequestParams = params;
	}
	
	@Override
	public void run() {
		try {
			if(this.mResponse != null) this.mResponse.sendStartMessage();
			this.makeRequest();
			if(this.mResponse != null) this.mResponse.sendEndMessage();
		} catch(Exception e) {
			if(this.mResponse != null) this.mResponse.sendEndMessage();
		}
	}
	
	private void makeRequest() throws Exception {
		if(!Thread.currentThread().isInterrupted()) {
			try {
				if(!Thread.currentThread().isInterrupted()) {
					if(this.mResponse != null) { //TODO - Possible resource leak, close the inputstream
						this.mClient.setRequestMethod(this.mRequest.getRequestMethod());
						for(Map.Entry<String, String> entry : this.mRequest.getHeaders().entrySet()) {
							this.mClient.setRequestProperty(entry.getKey(), entry.getValue());
						}
						if(this.mRequestParams != null && !this.mRequest.getRequestMethod().equals("GET")) {
							OutputStream params = this.mClient.getOutputStream();
							params.write(this.mRequestParams.toString().getBytes());
							params.close();
						}
						this.mResponse.sendResponseMessage(this.mClient);
					}
				}
			} catch(Exception e) {
				if(!Thread.currentThread().isInterrupted()) throw e;
			}
		}
	}
	
}
{'fraction_non_alphanumeric': 0.11140856672158155, 'fraction_numerical': 0.0032948929159802307, 'mean_word_length': 4.027950310559007, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '5328529', 'n_tokens_mistral': 1578, 'n_tokens_neox': 1352, 'n_words': 364}