requestWriter;
/**
* Creates a new instance of this class given the underlying I/O session.
*
* @param session the underlying I/O session.
* @param responseFactory HTTP response factory.
* @param allocator byte buffer allocator.
* @param params HTTP parameters.
*/
public DefaultNHttpClientConnection(
final IOSession session,
final HttpResponseFactory responseFactory,
final ByteBufferAllocator allocator,
final HttpParams params) {
super(session, allocator, params);
if (responseFactory == null) {
throw new IllegalArgumentException("Response factory may not be null");
}
this.responseParser = createResponseParser(this.inbuf, responseFactory, params);
this.requestWriter = createRequestWriter(this.outbuf, params);
this.hasBufferedInput = false;
this.hasBufferedOutput = false;
this.session.setBufferStatus(this);
}
/**
* Creates an instance of {@link NHttpMessageParser} to be used
* by this connection for parsing incoming {@link HttpResponse} messages.
*
* This method can be overridden in a super class in order to provide
* a different implementation of the {@link NHttpMessageParser} interface.
*
* @return HTTP response parser.
*/
protected NHttpMessageParser createResponseParser(
final SessionInputBuffer buffer,
final HttpResponseFactory responseFactory,
final HttpParams params) {
// override in derived class to specify a line parser
return new DefaultHttpResponseParser(buffer, null, responseFactory, params);
}
/**
* Creates an instance of {@link NHttpMessageWriter} to be used
* by this connection for writing out outgoing {@link HttpRequest} messages.
*
* This method can be overridden by a super class in order to provide
* a different implementation of the {@link NHttpMessageWriter} interface.
*
* @return HTTP response parser.
*/
protected NHttpMessageWriter createRequestWriter(
final SessionOutputBuffer buffer,
final HttpParams params) {
// override in derived class to specify a line formatter
return new DefaultHttpRequestWriter(buffer, null, params);
}
/**
* @since 4.2
*/
protected void onResponseReceived(final HttpResponse response) {
}
/**
* @since 4.2
*/
protected void onRequestSubmitted(final HttpRequest request) {
}
public void resetInput() {
this.response = null;
this.contentDecoder = null;
this.responseParser.reset();
}
public void resetOutput() {
this.request = null;
this.contentEncoder = null;
this.requestWriter.reset();
}
public void consumeInput(final NHttpClientEventHandler handler) {
if (this.status != ACTIVE) {
this.session.clearEvent(EventMask.READ);
return;
}
try {
if (this.response == null) {
int bytesRead;
do {
bytesRead = this.responseParser.fillBuffer(this.session.channel());
if (bytesRead > 0) {
this.inTransportMetrics.incrementBytesTransferred(bytesRead);
}
this.response = this.responseParser.parse();
} while (bytesRead > 0 && this.response == null);
if (this.response != null) {
if (this.response.getStatusLine().getStatusCode() >= 200) {
HttpEntity entity = prepareDecoder(this.response);
this.response.setEntity(entity);
this.connMetrics.incrementResponseCount();
}
onResponseReceived(this.response);
handler.responseReceived(this);
if (this.contentDecoder == null) {
resetInput();
}
}
if (bytesRead == -1) {
handler.endOfInput(this);
}
}
if (this.contentDecoder != null && (this.session.getEventMask() & SelectionKey.OP_READ) > 0) {
handler.inputReady(this, this.contentDecoder);
if (this.contentDecoder.isCompleted()) {
// Response entity received
// Ready to receive a new response
resetInput();
}
}
} catch (HttpException ex) {
resetInput();
handler.exception(this, ex);
} catch (Exception ex) {
handler.exception(this, ex);
} finally {
// Finally set buffered input flag
this.hasBufferedInput = this.inbuf.hasData();
}
}
public void produceOutput(final NHttpClientEventHandler handler) {
try {
if (this.outbuf.hasData()) {
int bytesWritten = this.outbuf.flush(this.session.channel());
if (bytesWritten > 0) {
this.outTransportMetrics.incrementBytesTransferred(bytesWritten);
}
}
if (!this.outbuf.hasData()) {
if (this.status == CLOSING) {
this.session.close();
this.status = CLOSED;
resetOutput();
return;
} else {
if (this.contentEncoder != null) {
handler.outputReady(this, this.contentEncoder);
if (this.contentEncoder.isCompleted()) {
resetOutput();
}
}
}
if (this.contentEncoder == null && !this.outbuf.hasData()) {
if (this.status == CLOSING) {
this.session.close();
this.status = CLOSED;
}
if (this.status != CLOSED) {
this.session.clearEvent(EventMask.WRITE);
handler.requestReady(this);
}
}
}
} catch (Exception ex) {
handler.exception(this, ex);
} finally {
// Finally set buffered output flag
this.hasBufferedOutput = this.outbuf.hasData();
}
}
public void submitRequest(final HttpRequest request) throws IOException, HttpException {
if (request == null) {
throw new IllegalArgumentException("HTTP request may not be null");
}
assertNotClosed();
if (this.request != null) {
throw new HttpException("Request already submitted");
}
onRequestSubmitted(request);
this.requestWriter.write(request);
this.hasBufferedOutput = this.outbuf.hasData();
if (request instanceof HttpEntityEnclosingRequest
&& ((HttpEntityEnclosingRequest) request).getEntity() != null) {
prepareEncoder(request);
this.request = request;
}
this.connMetrics.incrementRequestCount();
this.session.setEvent(EventMask.WRITE);
}
public boolean isRequestSubmitted() {
return this.request != null;
}
public void consumeInput(final NHttpClientHandler handler) {
consumeInput(new NHttpClientEventHandlerAdaptor(handler));
}
public void produceOutput(final NHttpClientHandler handler) {
produceOutput(new NHttpClientEventHandlerAdaptor(handler));
}
}