<commit_before>/**
 * Copyright 2011-2014 eBusiness Information, Groupe Excilys (www.ebusinessinformation.fr)
 *
 * 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 io.gatling.http.ahc

import com.ning.http.client.{ AsyncHandlerExtensions, HttpResponseBodyPart, HttpResponseHeaders, HttpResponseStatus, ProgressAsyncHandler }
import com.ning.http.client.AsyncHandler.STATE.CONTINUE
import com.typesafe.scalalogging.slf4j.StrictLogging

/**
 * This class is the AsyncHandler that AsyncHttpClient needs to process a request's response
 *
 * It is part of the HttpRequestAction
 *
 * @constructor constructs a GatlingAsyncHandler
 * @param tx the data about the request to be sent and processed
 * @param responseBuilder the builder for the response
 */
class AsyncHandler(tx: HttpTx) extends ProgressAsyncHandler[Unit] with AsyncHandlerExtensions with StrictLogging {

	val responseBuilder = tx.responseBuilderFactory(tx.request)
	private var done = false

	def onRequestSent {
		if (!done) responseBuilder.updateFirstByteSent
	}

	def onRetry {
		if (!done) responseBuilder.reset
		else logger.error("onRetry is not supposed to be called once done")
	}

	def onHeaderWriteCompleted = {
		if (!done) responseBuilder.updateLastByteSent
		CONTINUE
	}

	def onContentWriteCompleted = {
		if (!done) responseBuilder.updateLastByteSent
		CONTINUE
	}

	def onContentWriteProgress(amount: Long, current: Long, total: Long) = CONTINUE

	def onStatusReceived(status: HttpResponseStatus) = {
		if (!done) responseBuilder.accumulate(status)
		CONTINUE
	}

	def onHeadersReceived(headers: HttpResponseHeaders) = {
		if (!done) responseBuilder.accumulate(headers)
		CONTINUE
	}

	def onBodyPartReceived(bodyPart: HttpResponseBodyPart) = {
		if (!done) responseBuilder.accumulate(bodyPart)
		CONTINUE
	}

	def onCompleted {
		if (!done) {
			done = true
			try {
				val response = responseBuilder.build
				AsyncHandlerActor.instance ! OnCompleted(tx, response)
			} catch {
				case e: Exception => sendOnThrowable(e)
			}
		}
	}

	def onThrowable(throwable: Throwable) {
		if (!done) {
			done = true
			responseBuilder.updateLastByteReceived
			sendOnThrowable(throwable)
		}
	}

	def sendOnThrowable(throwable: Throwable) {
		val errorMessage = throwable.getClass.getName + Option(throwable.getMessage).map(": " + _).getOrElse("")

		if (logger.underlying.isInfoEnabled)<commit_msg>Better use pattern-matching than Option().map().getOrElse()<commit_after>/**
 * Copyright 2011-2014 eBusiness Information, Groupe Excilys (www.ebusinessinformation.fr)
 *
 * 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 io.gatling.http.ahc

import com.ning.http.client.{ AsyncHandlerExtensions, HttpResponseBodyPart, HttpResponseHeaders, HttpResponseStatus, ProgressAsyncHandler }
import com.ning.http.client.AsyncHandler.STATE.CONTINUE
import com.typesafe.scalalogging.slf4j.StrictLogging

/**
 * This class is the AsyncHandler that AsyncHttpClient needs to process a request's response
 *
 * It is part of the HttpRequestAction
 *
 * @constructor constructs a GatlingAsyncHandler
 * @param tx the data about the request to be sent and processed
 * @param responseBuilder the builder for the response
 */
class AsyncHandler(tx: HttpTx) extends ProgressAsyncHandler[Unit] with AsyncHandlerExtensions with StrictLogging {

	val responseBuilder = tx.responseBuilderFactory(tx.request)
	private var done = false

	def onRequestSent {
		if (!done) responseBuilder.updateFirstByteSent
	}

	def onRetry {
		if (!done) responseBuilder.reset
		else logger.error("onRetry is not supposed to be called once done")
	}

	def onHeaderWriteCompleted = {
		if (!done) responseBuilder.updateLastByteSent
		CONTINUE
	}

	def onContentWriteCompleted = {
		if (!done) responseBuilder.updateLastByteSent
		CONTINUE
	}

	def onContentWriteProgress(amount: Long, current: Long, total: Long) = CONTINUE

	def onStatusReceived(status: HttpResponseStatus) = {
		if (!done) responseBuilder.accumulate(status)
		CONTINUE
	}

	def onHeadersReceived(headers: HttpResponseHeaders) = {
		if (!done) responseBuilder.accumulate(headers)
		CONTINUE
	}

	def onBodyPartReceived(bodyPart: HttpResponseBodyPart) = {
		if (!done) responseBuilder.accumulate(bodyPart)
		CONTINUE
	}

	def onCompleted {
		if (!done) {
			done = true
			try {
				val response = responseBuilder.build
				AsyncHandlerActor.instance ! OnCompleted(tx, response)
			} catch {
				case e: Exception => sendOnThrowable(e)
			}
		}
	}

	def onThrowable(throwable: Throwable) {
		if (!done) {
			done = true
			responseBuilder.updateLastByteReceived
			sendOnThrowable(throwable)
		}
	}

	def sendOnThrowable(throwable: Throwable) {
		val className = throwable.getClass.getName
		val errorMessage = throwable.getMessage match {
			case null => className
			case m => s"$className: $m"
		}

		if (logger.underlying.isInfoEnabled)
{'fraction_non_alphanumeric': 0.08357496162374212, 'fraction_numerical': 0.004434589800443459, 'mean_word_length': 4.5900857959961865, 'pattern_counts': {'":': 1, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '3228023', 'n_tokens_mistral': 1823, 'n_tokens_neox': 1673, 'n_words': 522}