responseWriter;
/**
* Creates a new instance of this class given the underlying I/O session.
*
* @param session the underlying I/O session.
* @param requestFactory HTTP request factory.
* @param allocator byte buffer allocator.
* @param params HTTP parameters.
*/
public DefaultNHttpServerConnection(
final IOSession session,
final HttpRequestFactory requestFactory,
final ByteBufferAllocator allocator,
final HttpParams params) {
super(session, allocator, params);
if (requestFactory == null) {
throw new IllegalArgumentException("Request factory may not be null");
}
this.requestParser = createRequestParser(this.inbuf, requestFactory, params);
this.responseWriter = createResponseWriter(this.outbuf, params);
}
@Override
protected ContentLengthStrategy createIncomingContentStrategy() {
return new DisallowIdentityContentLengthStrategy(new LaxContentLengthStrategy(0));
}
/**
* Creates an instance of {@link NHttpMessageParser} to be used
* by this connection for parsing incoming {@link HttpRequest} 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 createRequestParser(
final SessionInputBuffer buffer,
final HttpRequestFactory requestFactory,
final HttpParams params) {
// override in derived class to specify a line parser
return new DefaultHttpRequestParser(buffer, null, requestFactory, params);
}
/**
* Creates an instance of {@link NHttpMessageWriter} to be used
* by this connection for writing out outgoing {@link HttpResponse}
* 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 createResponseWriter(
final SessionOutputBuffer buffer,
final HttpParams params) {
// override in derived class to specify a line formatter
return new DefaultHttpResponseWriter(buffer, null, params);
}
/**
* @since 4.2
*/
protected void onRequestReceived(final HttpRequest request) {
}
/**
* @since 4.2
*/
protected void onResponseSubmitted(final HttpResponse response) {
}
public void resetInput() {
this.request = null;
this.contentDecoder = null;
this.requestParser.reset();
}
public void resetOutput() {
this.response = null;
this.contentEncoder = null;
this.responseWriter.reset();
}
public void consumeInput(final NHttpServerEventHandler handler) {
if (this.status != ACTIVE) {
this.session.clearEvent(EventMask.READ);
return;
}
try {
if (this.request == null) {
int bytesRead;
do {
bytesRead = this.requestParser.fillBuffer(this.session.channel());
if (bytesRead > 0) {
this.inTransportMetrics.incrementBytesTransferred(bytesRead);
}
this.request = this.requestParser.parse();
} while (bytesRead > 0 && this.request == null);
if (this.request != null) {
if (this.request instanceof HttpEntityEnclosingRequest) {
// Receive incoming entity
HttpEntity entity = prepareDecoder(this.request);
((HttpEntityEnclosingRequest)this.request).setEntity(entity);
}
this.connMetrics.incrementRequestCount();
onRequestReceived(this.request);
handler.requestReceived(this);
if (this.contentDecoder == null) {
// No request entity is expected
// Ready to receive a new request
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()) {
// Request entity received
// Ready to receive a new request
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 NHttpServerEventHandler 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.responseReady(this);
}
}
}
} catch (Exception ex) {
handler.exception(this, ex);
} finally {
// Finally set the buffered output flag
this.hasBufferedOutput = this.outbuf.hasData();
}
}
public void submitResponse(final HttpResponse response) throws IOException, HttpException {
if (response == null) {
throw new IllegalArgumentException("HTTP response may not be null");
}
assertNotClosed();
if (this.response != null) {
throw new HttpException("Response already submitted");
}
onResponseSubmitted(response);
this.responseWriter.write(response);
this.hasBufferedOutput = this.outbuf.hasData();
if (response.getStatusLine().getStatusCode() >= 200) {
this.connMetrics.incrementResponseCount();
if (response.getEntity() != null) {
this.response = response;
prepareEncoder(response);
}
}
this.session.setEvent(EventMask.WRITE);
}
public boolean isResponseSubmitted() {
return this.response != null;
}
public void consumeInput(final NHttpServiceHandler handler) {
consumeInput(new NHttpServerEventHandlerAdaptor(handler));
}
public void produceOutput(NHttpServiceHandler handler) {
produceOutput(new NHttpServerEventHandlerAdaptor(handler));
}
}